PDA

View Full Version : Convert Bitmaps to Movieclips


bigevilbrain
09-18-2006, 08:48 PM
This JSFL script converts selected bitmaps to symbols (movieclips). Names the symbol the same as the bitmap minus the file extension. For instance, if your image is called "background.jpg" then the movieclip will be called "background".

1) Import a bunch of bitmaps.
2) Selected them on stage.
3) Run the script.

-- enjoy!

// ----------------
// BITMAP TO SYMBOL
// ----------------
//
// VERSION: 1.0
// DATE: 9/18/06

if (fl.getDocumentDOM() == null) {
fl.trace("Error: No document open.");
} else {
if (fl.getDocumentDOM().selection.length == 0) {
fl.trace("Error: Nothing selected.");
} else {

var convertedBitmaps = new Array();

var selArr = fl.getDocumentDOM().selection;
for (var i = 0; i < selArr.length; i++) {

var instanceRef = selArr[i]
if (instanceRef.instanceType == "bitmap") {

var libraryItemRef = instanceRef.libraryItem;
var newName = stripExtension(libraryItemRef.name);

fl.getDocumentDOM().selectNone();
fl.getDocumentDOM().selection = new Array(instanceRef);
var mc = fl.getDocumentDOM().convertToSymbol("movie clip", newName, "top left");

if (mc == null) {
// Throws an error on screen anyway...
fl.trace("Error: Couldn't convert "+newName+" to symbol.");
}
}

}

fl.getDocumentDOM().selectNone();
}
}

function stripExtension(str) {
var pos = str.lastIndexOf(".");
if (pos == -1) {
return str;
} else {
return str.slice(0,pos);
}
}

myFUD
09-18-2006, 09:25 PM
Works good. What it needs now is adding linkage names to each MC that is created. Perhaps even an option to name the instances on the stage.

bigevilbrain
09-19-2006, 02:39 AM
Hi myFUD.

// Insert after line 28 (i.e. var mc = fl.getDocumentDOM().convertToSymbol...)

// Set instance name on stage
fl.getDocumentDOM().selection[0].name = newName;

// Set linkage information
mc.linkageExportForAS = true;
mc.linkageExportInFirstFrame = true;
mc.linkageIdentifier = newName;

It appears that linkageExportForAS has to be "true" before you can set linkageIdentifier. Maybe this is a Flash bug?

Also, the linkageExportInFirstFrame line is optional... comment it out if you want.

jdolce
09-28-2006, 01:11 AM
Here is a similar script I use for converting bitmaps in the library to movieclips. You can select one or multiple bitmaps at once.

It will take a background.jpg and turn it into background_mc



var dom = fl.getDocumentDOM();
var lib = dom.library;
var newSel = [];

var selItems = lib.getSelectedItems();

for( var i=0; i<selItems.length; i++ )
{

var s = selItems[i].name.split( "." );

var nameId = s[ 0 ] + "_mc";
lib.addNewItem( "movie clip", nameId );
lib.editItem( nameId );

var tl = dom.getTimeline();
tl.layers[0].name = "L1";
lib.addItemToDocument( {x:0, y:0}, selItems[i].name );

newSel[0] = tl.layers[ 0 ].frames[ 0 ].elements[ 0 ];

dom.selectNone();
dom.selection = newSel;
var mat = dom.selection[0].matrix;
mat.tx = 0;
mat.ty = 0;
dom.selection[0].matrix = mat;
dom.selectNone();
dom.exitEditMode();

}