+function cLog(e){configs.log&&$.writeln(e)}function cDebug(e){configs.debug&&$.writeln(e)}function listMatchNames(e){for(var r=1;r<=e.numProperties;r++){var o=e.property(r);consLog(o.matchName+"("+o.name+")")}}function ExecutionTime(){var e,r,o;this.constructor=function(){},this.start=function(){e=(new Date).getTime()},this.stop=function(){r=(new Date).getTime(),o=r-e},this.time=function(){return"Execution time : "+Math.floor(o/1e3)+"s "+o%1e3+"ms"}}function ProgressBar(e,r,o){function t(){s&&n.update()}var n,p,c,i,a,s;return this.testInfos="Processing element :current on :max",this.constructor=function(e,r,o){return _this=this,s=!1,i={min:e,max:r,current:o},a={min:0,max:100,current:0},a.max=i.max-i.min+1,n=new Window("palette",configs.title,void 0,{resizeable:!1,borderless:"not quite true"}),n.preferredSize=[420,40],p=n.add("progressbar",void 0,a.min,a.max),p.preferredSize.width=400,p.show(),c=n.add("statictext",void 0,"Loading, please wait",{justify:"center"}),c.preferredSize=[400,17],this.update(o),this},this.start=function(){s=!0,this.update(i.current),n.show()},this.end=function(){n.hide()},this.update=function(e){i.current=e,a.current=i.current+1-i.min;var r=this.testInfos.replace(":current",a.current).replace(":max",a.max);p.value=a.current,c.text=r,cDebug(r),t()},this.constructor(e,r,o)}function explodeLayer(e){cLog("Exploding layer : "+e.name);var r=e.property("Contents"),o=[];if(!(r.numProperties>configs.itemAmountWarning)||confirm("You have more than "+configs.itemAmountWarning+" elements. Execution time might be long, are you sure you want to continue ?")){var t=new ProgressBar(1,r.numProperties,1);t.start();for(c=r.numProperties;c>0;c--){var n=r.property(c);if(t.update(r.numProperties-c),n.enabled){var p=emptyDuplicateLayer(e);p.name=e.name+" - "+n.name,p.enabled=!1,p.shy=!0,o.push(p),p.property("Contents").canAddProperty(n.matchName)&©Properties(n,p.property("Contents").addProperty(n.matchName),"")}}t.end();for(var c=0;c<o.length;c++)o[c].enabled=!0,o[c].shy=!1,configs.dryRun&&o[c].remove();return o}}function explode(){if(app.project.activeItem.selectedLayers.length>1)alert("Select a single shape layer");else{var e=app.project.activeItem.selectedLayers[0];if(void 0!=e&&"ADBE Vector Layer"===e.matchName){cLog("=================="),cLog("Configs :");for(config in configs)configs.hasOwnProperty(config)&&cLog(" "+config+" : "+configs[config]);cLog("");var r=new ExecutionTime;r.start();var o=e.containingComp.hideShyLayers;e.containingComp.hideShyLayers=!0;explodeLayer(e);e.moveToBeginning(),e.containingComp.hideShyLayers=o,r.stop(),cLog(r.time())}else alert("Select a shape layer")}}function emptyDuplicateLayer(e){var r=e.containingComp.layers.addShape();return copyProperty("anchorPoint",e,r),copyProperty("position",e,r),copyProperty("scale",e,r),copyProperty("rotation",e,r),copyProperty("opacity",e,r),r}function copyProperties(e,r,o){for(var t=1;t<=e.numProperties;t++){var n=e.property(t);if(!n.enabled||!r.canAddProperty(n.matchName))return;cDebug(o+n.matchName);var p=r.addProperty(n.matchName);switch(n.matchName){case"ADBE Vector Filter - Merge":copyProperty("mode",n,p);break;case"ADBE Vector Materials Group":cDebug(o+"-- skipped");break;case"ADBE Vector Graphic - Stroke":copyPropertyStroke(n,p);break;case"ADBE Vector Graphic - Fill":copyPropertyFill(n,p);break;case"ADBE Vector Transform Group":copyPropertyTransform(n,p);break;case"ADBE Vector Shape - Rect":copyPropertyRect(n,p);break;case"ADBE Vector Shape - Ellipse":copyPropertyEllipse(n,p);break;case"ADBE Vector Shape - Star":copyPropertyStar(n,p);break;case"ADBE Root Vectors Group":case"ADBE Vectors Group":case"ADBE Vector Group":copyProperties(n,p,o+=" ");break;case"ADBE Vector Shape - Group":copyPropertyShape(n,p);break;case"ADBE Vector Blend Mode":p.setValue(n.value)}}}function copyProperty(e,r,o){o[e].setValue(r[e].value)}function copyPropertyShape(e,r){r.property("ADBE Vector Shape").setValue(e.property("ADBE Vector Shape").value)}function copyPropertyStroke(e,r){copyProperty("composite",e,r),copyProperty("color",e,r),copyProperty("strokeWidth",e,r),copyProperty("lineCap",e,r),copyProperty("lineJoin",e,r),copyProperty("miterLimit",e,r)}function copyPropertyFill(e,r){copyProperty("composite",e,r),copyProperty("fillRule",e,r),copyProperty("color",e,r)}function copyPropertyTransform(e,r){copyProperty("anchorPoint",e,r),copyProperty("position",e,r),copyProperty("scale",e,r),copyProperty("skew",e,r),copyProperty("skewAxis",e,r),copyProperty("rotation",e,r),copyProperty("opacity",e,r)}function copyPropertyRect(e,r){copyProperty("shapeDirection",e,r),copyProperty("size",e,r),copyProperty("position",e,r),copyProperty("roundness",e,r)}function copyPropertyEllipse(e,r){copyProperty("shapeDirection",e,r),copyProperty("size",e,r),copyProperty("position",e,r)}function copyPropertyStar(e,r){copyProperty("shapeDirection",e,r),copyProperty("type",e,r),copyProperty("points",e,r),copyProperty("position",e,r),copyProperty("rotation",e,r),copyProperty("innerRadius",e,r),copyProperty("outerRadius",e,r),copyProperty("innerRoundness",e,r),copyProperty("outerRoundness",e,r)}function createUI(e){if(e instanceof Panel)var r=e;else(r=new Window("palette",configs.title,void 0,{resizeable:!0})).show();var o=r.add("button",[10,10,100,30],"Explode layer");return r.bounds.width=120,r.bounds.height=40,o.onClick=function(){explode()},r}var configs={title:"Explode layer tool",debug:!1,log:!0,itemAmountWarning:50,dryRun:!1},_panel=createUI(this);
0 commit comments