Skip to content

Instantly share code, notes, and snippets.

@sacrifs
Created May 24, 2012 05:48
Show Gist options
  • Save sacrifs/2779698 to your computer and use it in GitHub Desktop.
Save sacrifs/2779698 to your computer and use it in GitHub Desktop.
ステージ上にある選択中の画像をムービークリップに変換するJSFL
/**
* create movie clip from images.
*/
var _doc = fl.getDocumentDOM();
var _lib = _doc.library;
function main(){
fl.outputPanel.clear();
var selectedItems = _doc.selection;
var numItem = selectedItems.length;
if(numItem == 0){
fl.trace('nothing...');
}
else{
_doc.deleteSelection();
var targetContainer = null;
_doc.exitEditMode();
if(_doc.selection.length == 1){
targetContainer = _doc.selection[0].libraryItem;
}
var newObjList = [];
var i = 0;
for(i = 0; i < numItem; i++){
var elem = selectedItems[i];
var mc = createMC(elem);
if(mc != null){
newObjList.push(mc);
}
}
_doc.selectNone();
var numNewObj = newObjList.length;
for(i = 0; i < numNewObj; i++){
newObjList[i].selected = true;
}
if(targetContainer != null){
_doc.clipCut();
_lib.editItem(targetContainer.name);
_doc.clipPaste(true);
}
_doc.distributeToLayers();
reverseLayer();
_doc.exitEditMode();
fl.trace("complete!");
}
}
/**
* create MovieClip
* @param elem:Element
* @return Instance
*/
function createMC(elem){
if(elem.elementType == "instance"){
var item = elem.libraryItem;
var name = item.name;
if(name.search(".") != -1){
name = name.split(".")[0];
}
if(name.search("/") != -1){
var spl = name.split("/");
name = spl[spl.length - 1];
}
var nameSpl = name.split("_");
for(var i = 1, len = nameSpl.length; i < len; i++){
var txt = nameSpl[i];
var txtH = txt.charAt(0);
nameSpl[i] = txtH.toUpperCase() + txt.substr(1);
}
var mcName = nameSpl.join("");
if(!_lib.itemExists(mcName)){
_lib.addNewItem("movie clip", mcName);
_lib.selectItem(mcName);
_lib.editItem();
_lib.addItemToDocument({x:elem.width / 2, y:elem.height / 2}, item.name);
_doc.mouseClick({x:elem.width / 2, y:elem.height / 2}, false, false);
var bmp = _doc.selection[0];
adjustPos(bmp);
_doc.exitEditMode();
}
_lib.selectItem(mcName);
var targetX = elem.x + elem.width / 2;
var targetY = elem.y + elem.height / 2;
_lib.addItemToDocument({x:targetX, y:targetY});
_doc.mouseClick({x:targetX, y:targetY}, false, false);
var mc = _doc.selection[0];
adjustPos(mc);
_lib.selectNone();
return mc;
}
return null;
}
/**
* adjust object position
* @params obj:Element
*/
function adjustPos(obj){
var mat = obj.matrix;
mat.tx = Math.round(mat.tx);
mat.ty = Math.round(mat.ty);
obj.matrix = mat;
}
/**
* reverseLayer
*/
function reverseLayer(){
var tl = _doc.getTimeline();
var selectedElements = _doc.selection;
var numElem = selectedElements.length;
var i = 0;
for(i = 0; i < numElem; i++){
var layer = selectedElements[i].layer;
var indexList = tl.findLayerIndex(layer.name);
tl.setSelectedLayers(indexList[0], false);
}
var currentLayers = tl.getSelectedLayers();
var numLayer = currentLayers.length;
currentLayers.sort();
var startIndex = currentLayers[0];
var endIndex = currentLayers[numLayer - 1];
fl.trace(startIndex + " "+ endIndex + " " + numLayer);
for(i = startIndex; i < endIndex; i++ ){
_doc.getTimeline().reorderLayer(endIndex - 1, i);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment