Skip to content

Instantly share code, notes, and snippets.

@mastergenius
Last active June 5, 2016 16:46
Show Gist options
  • Save mastergenius/4bc219d128b4cb105b8f7a558c413b17 to your computer and use it in GitHub Desktop.
Save mastergenius/4bc219d128b4cb105b8f7a558c413b17 to your computer and use it in GitHub Desktop.
fl.outputPanel.clear();
fl.trace("Magic maker 3000 v0.7");
var SPECIAL_LAYER_NAME = "[Genearated magic layer]";
function cleanupFromGeneratedLayers(timeline) {
// Delete one generated level from timeline
for (var layerIndex in timeline.layers) {
var layer = timeline.layers[layerIndex];
if (layer.name === SPECIAL_LAYER_NAME) {
fl.trace('Deleting layer with index ' + layerIndex);
//time.deleteLayer(layerIndex);
timeline.deleteLayer(0);
// Only one special layer
break;
}
}
}
function makeMagic(item) {
var dom = fl.getDocumentDOM();
// Edit item with normal editor (make item.timeline current)
dom.library.editItem(item.name);
// Get item timeline (it already current dom timeline)
var time = dom.getTimeline();
cleanupFromGeneratedLayers(time);
// Make some flash magic here
time.setSelectedLayers(0, true);
// Unlock source layer
time.setLayerProperty('locked', false);
time.duplicateLayers();
time.setLayerProperty('name', SPECIAL_LAYER_NAME);
time.setSelectedLayers(1, true);
time.setLayerProperty('locked', true);
time.setSelectedLayers(0, true);
dom.selectAll();
try {
dom.traceBitmap(1, 1, 'very smooth', 'many corners');
} catch (error) {
fl.trace("[ERROR] Trace bitmap error. Abort magic.");
cleanupFromGeneratedLayers(time);
return;
}
dom.setFillColor('#000000');
dom.setStroke('#000000', 0.5, 'solid');
try {
dom.convertLinesToFills();
} catch (error) {
fl.trace("[ERROR] convertLinesToFills error. Abort magic.");
cleanupFromGeneratedLayers(time);
return;
}
time.setLayerProperty('layerType', 'mask');
time.setSelectedLayers(1, true);
time.setLayerProperty('layerType', 'masked');
time.setSelectedLayers(0, true);
time.setLayerProperty('locked', true);
}
function analyzeLibItem(item) {
if (item.itemType != 'movie clip') {
fl.trace("> Item not a movie [" + item.itemType + "] {" + item.name + "}");
return null;
}
fl.trace("> Analyzing item {" + item.name + "}");
var timeline = item.timeline;
var itemElements = [];
for each (var layer in timeline.layers) {
fl.trace(" Layer {" + layer.name + '}' /*+ ' Frames count: ' + layer.frames.length*/);
for (var frameIndex in layer.frames) {
var frame = layer.frames[frameIndex];
fl.trace(" Frame {" + frameIndex + '}' /*+ ' Elements count: ' + frame.elements.length*/);
for each (var element in frame.elements) {
var libItem = element.libraryItem;
if (itemElements.indexOf(element) === -1) {
itemElements.push(element);
} else {
fl.trace(" [Already seen] element {" + element.name + "}" + (libItem ? " [" + libItem.itemType + "] {" + libItem.name + "}" : ""));
}
fl.trace(" Element [" + element.elementType + "]" + (element.name ? " {" + element.name + "}" : "") + (libItem ? " [" + libItem.itemType + "] {" + libItem.name + "}" : ""));
}
}
}
return itemElements;
}
function getItemElements(item) {
var result = [];
if (item.itemType != 'movie clip') {
return result;
}
var timeline = item.timeline;
for each (var layer in timeline.layers) {
for each (var frame in layer.frames) {
for each (var element in frame.elements) {
var libItem = element.libraryItem;
if (result.indexOf(element) === -1) {
result.push(element);
}
}
}
}
return result;
}
function needMagic(item) {
if (item.timeline.layers.length > 2 || item.timeline.layers.length == 0) {
//fl.trace("noMagic 1");
return false;
}
if (item.timeline.layers.length == 2) {
var badLayers = true;
for each (var layer in item.timeline.layers) {
if (layer.name === SPECIAL_LAYER_NAME) {
badLayers = false;
}
}
if (badLayers) {
//fl.trace("noMagic 2");
return false;
}
}
var allElements = getItemElements(item);
for each (var element in allElements) {
if (element.libraryItem && element.libraryItem.itemType == 'movie clip') {
// No magic for movie clips
//fl.trace("noMagic 3");
return false
}
}
return true;
}
var lib = fl.getDocumentDOM().library;
var clipsToProcess = [];
for each (var item in lib.items) {
// Render neat library structure (comment out if it so spammy)
analyzeLibItem(item);
//fl.trace(i + ' ' + item.name + ' type: ' + item.itemType);
if (item.itemType == 'movie clip' && needMagic(item)) {
fl.trace("Collect " + item.name + ' type: ' + item.itemType);
clipsToProcess.push(item);
}
}
for (var j in clipsToProcess) {
var clip = clipsToProcess[j];
fl.trace("Make magic for " + clip.name);
try {
makeMagic(clip);
} catch (error) {
fl.trace("[ERROR] When making magic for " + clip.name + "\nError text: " + error);
}
fl.trace("Done with magic for " + clip.name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment