Skip to content

Commit

Permalink
some new snippets. Needs TOC
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiantheblind committed Jul 19, 2013
1 parent ae14d94 commit 30ab28b
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions automagically-sort-selectedlayers-by-name.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/** * This script tries to sort layers by name * more a proof of concept thingy * using this technique can hold some problems * - in this case the "et" gets found in "consectetur" "et" "amet" * - the last matching pattern will be the one that gets executed * */// Copyright (c) 2013// Fabian "fabiantheblind" Morón Zirfas// Permission is hereby granted, free of charge, to any// person obtaining a copy of this software and associated// documentation files (the "Software"), to deal in the Software// without restriction, including without limitation the rights// to use, copy, modify, merge, publish, distribute, sublicense,// and/or sell copies of the Software, and to permit persons to// whom the Software is furnished to do so, subject to// the following conditions:// The above copyright notice and this permission notice// shall be included in all copies or substantial portions of the Software.// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIO// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.// see also http://www.opensource.org/licenses/mit-license.php/** * get unique Array content * does not work on mixed arrays * http://stackoverflow.com/questions/1960473/unique-values-in-an-array * @return {Array} [description] */Array.prototype.getUnique = function(){ var u = {}, a = []; for(var i = 0, l = this.length; i < l; ++i){ if(u.hasOwnProperty(this[i])) { continue; } a.push(this[i]); u[this[i]] = 1; } return a;};/** * LayerSet Class * @param {String} _name The name of the class corresponds to the pattern */function LayerSet(_name){ this.name = _name; this.collection = [];// will hold layers this.add = function(layer){ this.collection.push(layer); };}/** * A Pattern Class * @param {a regex string} _reg String */function Pattern(_reg){ this.reg = escapeRegExp(_reg);}function automagically_sort(){app.beginUndoGroup("automagically sort by name");/** * prerequisites check if thete is a comp * @type {[type]} */var curComp = app.project.activeItem; if (!curComp || !(curComp instanceof CompItem)){ // alert("noComp"); // return; curComp = app.project.items.addComp("automagically sort", 500, 500, 1, 10, 25); }var names = ["Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipisicing", "elit", "sed", "do", "eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua","Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipisicing", "elit", "sed", "do", "eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua"];// some random namesvar layers = [];// holds the layersfor(var l = 0; l < names.length;l++){ var lay = curComp.layers.addNull();// add a null lay.name = makeid(4) + " " + names[l] + " " + makeid(5); // give it a name layers.push(lay);// push to array} // close l create layers loop. // this shoudl be a space seprated field or something like that var unique_names = names.getUnique(); // use the prototype functiionvar patterns = []; // this will hold the patternsfor (var n = 0; n < unique_names.length; n++) { patterns.push( new Pattern( unique_names[n]));} // close ncreate patterns from names loopvar sets = []; // will hold all the layer setsfor(var r =0;r < patterns.length;r++){ var set = new LayerSet(patterns[r].reg);// make a new set per pattern for (var i = 0; i < layers.length; i++) { var layer = layers[i];//isolate var reg = new RegExp( patterns[r].reg ,'g');// make a regex if(reg.test(layer.name) === true){ set.add(layer);// Yes found something push to LayerSet } // close regex check } // close i layers loop if(set.collection.length > 0){ sets.push(set); }}// close r pattern loop/** * Now the sorting * We need to get every item from the set and move it to the top */// alert(sets.toSource());for (var s = 0; s < sets.length; s++) { var curSet = sets[s];// isolate if(curSet.collection.length > 0){ // continue; // we have something in the set loop the collection var collabel = (s)%16; for (var cl = 0; cl < curSet.collection.length; cl++) { var curLayer = curSet.collection[cl]; curLayer.moveToBeginning(); if(parseInt (app.version, 10) > 10){ // CS6+ feature curLayer.label = collabel; } } // end cl collection loop }// if there are no layers move to the next set} // end s sets loopapp.endUndoGroup();}/** * [escapeRegExp description] * @param {[type]} str [description] * @return {[type]} [description] * http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex */function escapeRegExp(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");}/** * Make pseudo random text found here * http://stackoverflow.com/questions/1349404/generate-a-string-of-5-random-characters-in-javascript * @return {String} [Pseudo random string] */function makeid(num){ var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for( var i=0; i < num; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text;} // _ _ ______ _______ _ _ _ _ _____ _______ _______ _____ _ _ _____ // | \ | |/ __ \ \ / / __ \| | | | \ | | __ \ /\|__ __|__ __|_ _| \ | |/ ____| // | \| | | | \ \ /\ / /| |__) | | | | \| | | | | / \ | | | | | | | \| | | __ // | . ` | | | |\ \/ \/ / | _ /| | | | . ` | | | |/ /\ \ | | | | | | | . ` | | |_ | // | |\ | |__| | \ /\ / | | \ \| |__| | |\ | |__| / ____ \| | | | _| |_| |\ | |__| | // |_| \_|\____/ \/ \/ |_| \_\\____/|_| \_|_____/_/ \_\_| |_| |_____|_| \_|\_____|automagically_sort();
Expand Down
2 changes: 1 addition & 1 deletion connect-with-path.jsx
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(function(thisObj){// basic panelmpo_import(thisObj); function mpo_import(thisObj){// this is globalimport_data = { 'x':10, 'y':10};/// THIS WILL CHECK IF PANEL IS DOCKABLE OR FLAOTING WINDOW var win = buildUI(thisObj );if ((win !== null) && (win instanceof Window)) { win.center(); win.show();} // end if win null and not a instance of window function buildUI (thisObj ) { var H = 25; // the height var W = 30; // the width var G = 5; // the gutter var x = G; var y = G; var rownum = 1; var columnnum = 3; var gutternum = 2; var win = (thisObj instanceof Panel) ? thisObj : new Window('palette', 'Connect With Path',[0,0,gutternum*G + W*columnnum,gutternum*G + H*rownum],{resizeable: true}); if (win !== null) { // win.check_box = win.add('checkbox',[x,y,x+W*2,y + H],'check'); // win.check_box.value = metaObject.setting1; win.do_it_button = win.add('button', [x ,y,x+W*3,y + H], 'connect them'); // win.up_button = win.add('button', [x + W*5+ G,y,x + W*6,y + H], 'Up'); // win.check_box.onClick = function (){ // alert("check"); // }; win.do_it_button.onClick = function () { connect_all_layers(); }; } return win;}function connect_all_layers(){// "in function main. From here on it is a straight run"// var curComp = app.project.activeItem; if (!curComp || !(curComp instanceof CompItem)){ alert('please select a comp'); return; } if(curComp.selectedLayers.length < 2){ alert('Please select at least an even number of layer'); return; } app.beginUndoGroup('connect-layers'); var layerlist = []; for(var i = 0; i < curComp.selectedLayers.length;i+=2){ var lays = [ curComp.selectedLayers[i] , curComp.selectedLayers[i+1]]; layerlist.push(lays); } var shapes = []; for(var j = 0; j < layerlist.length; j++){ var p1 = layerlist[j][0].transform.position.value; var p2 = layerlist[j][1].transform.position.value; var n = layerlist[j][0].name + " <-> " +layerlist[j][1].name; shapes.push(connect(p1 ,p2 , curComp, n)); } for(var k = 0; k < shapes.length;k++){ shapes[k].selected = true; } app.endUndoGroup(); } function connect(pos1, pos2, comp, name){ var curshape = comp.layers.addShape(); curshape.name = name; make_path([ [ pos1[0]- comp.width / 2,pos1[1]- comp.width / 2 ],[ pos2[0] - comp.width / 2,pos2[1] - comp.width / 2 ] ],curshape,true,null); return curshape; }/** * Path creation. As always the basis is taken from * http://www.redefinery.com/ae/fundamentals/ * * http://www.redefinery.com/ae/fundamentals/masks/ * with some additions to fit my needs * Still... Thanks 2 Jeff Almasol aka redefinery * * * * this builds a path using ternary operators * I like it that why a lot. Pretty slick * http://stackoverflow.com/questions/1771786/question-mark-in-javascript * * @param {Array of Arrays} path holds the coordinates for the path * @param {AVLayer} layer The layer to draw on * @param {String} rowname This is to sort ot rows * @param {String} pathname The name of the path * @param {Array of 3 Values 0 -1 } maskcolor The color of the masks always per Glyph * @param {ADBE Vectors Group} shapegroup The group that contains the path * @return {ADBE Vectors Group} for reuse. * * @todo Get the hang of the diffrent paths in a Character */function make_path(path,layer, use_shapes, shapegroup){var masksGroup = null; masksGroup = use_shapes ? layer("ADBE Root Vectors Group") : layer("ADBE Mask Parade");// Get// PropertyGroup for the shape// or// the PropertyGroup for the masks // masksGroup = layer("ADBE Mask Parade");if (masksGroup !== null){ var mask = null;// Create a new maskif(shapegroup === null && use_shapes === true){ var pregroup = masksGroup.addProperty("ADBE Vector Group"); pregroup.name = "group";//charname + ' ' + rowname; shapegroup = pregroup.addProperty("ADBE Vectors Group"); mask = shapegroup;} mask = use_shapes ? shapegroup.addProperty("ADBE Vector Shape - Group") : masksGroup.addProperty("ADBE Mask Atom"); // mask = masksGroup.addProperty("ADBE Mask Atom"); if (mask !== null){ mask.name = "path"; // use_shapes ? pathname : charname + ' ' + rowname + ' ' + pathname; // mask.color = maskcolor; var s = new Shape();// new shape object if (s !== null){ s.vertices = path; // The close attribute defaults to true s.closed = false; // put the path verticies into the shape or mask maskShape = use_shapes ? maskShape = mask.property("ADBE Vector Shape") : mask.property("ADBE Mask Shape"); // Change the mask shape (not keyframed) maskShape.setValue(s); } } }// return shapegroup;} }// close mpo_import})(this);
(function(thisObj){// basic panelmpo_import(thisObj); function mpo_import(thisObj){// this is globalimport_data = { 'x':10, 'y':10};/// THIS WILL CHECK IF PANEL IS DOCKABLE OR FLAOTING WINDOW var win = buildUI(thisObj );if ((win !== null) && (win instanceof Window)) { win.center(); win.show();} // end if win null and not a instance of window function buildUI (thisObj ) { var H = 25; // the height var W = 30; // the width var G = 5; // the gutter var x = G; var y = G; var rownum = 1; var columnnum = 3; var gutternum = 2; var win = (thisObj instanceof Panel) ? thisObj : new Window('palette', 'Connect With Path',[0,0,gutternum*G + W*columnnum,gutternum*G + H*rownum],{resizeable: true}); if (win !== null) { // win.check_box = win.add('checkbox',[x,y,x+W*2,y + H],'check'); // win.check_box.value = metaObject.setting1; win.do_it_button = win.add('button', [x ,y,x+W*3,y + H], 'connect them'); // win.up_button = win.add('button', [x + W*5+ G,y,x + W*6,y + H], 'Up'); // win.check_box.onClick = function (){ // alert("check"); // }; win.do_it_button.onClick = function () { connect_all_layers(); }; } return win;}function connect_all_layers(){// "in function main. From here on it is a straight run"// var curComp = app.project.activeItem; if (!curComp || !(curComp instanceof CompItem)){ alert('please select a comp'); return; } if(curComp.selectedLayers.length < 2){ alert('Please select at least an even number of layer'); return; } app.beginUndoGroup('connect-layers'); var buffer = curComp.layers.addNull(); buffer.name = "buffer"; var layerlist = []; for(var i = 0; i < curComp.selectedLayers.length;i+=2){ var lays = [ curComp.selectedLayers[i] , curComp.selectedLayers[i+1]]; layerlist.push(lays); } var shapes = []; for(var j = 0; j < layerlist.length; j++){ var global_curr_pos = [0, 0, 0]; var p1 = get_position(layerlist[j][0], curComp.time, buffer);;//layerlist[j][0].transform.position.value; // var p2 = get_position(layerlist[j][1], curComp.time, buffer); //layerlist[j][1].transform.position.value; var n = layerlist[j][0].name + " <-> " +layerlist[j][1].name; shapes.push(connect(p1 ,p2 , curComp, n)); } for(var k = 0; k < shapes.length;k++){ shapes[k].selected = true; } app.endUndoGroup(); } function connect(pos1, pos2, comp, name){ var curshape = comp.layers.addShape(); curshape.name = name; make_path([ [ pos1[0]- comp.width / 2,pos1[1]- comp.width / 2 ],[ pos2[0] - comp.width / 2,pos2[1] - comp.width / 2 ] ],curshape,true,null); return curshape; }/** * Path creation. As always the basis is taken from * http://www.redefinery.com/ae/fundamentals/ * * http://www.redefinery.com/ae/fundamentals/masks/ * with some additions to fit my needs * Still... Thanks 2 Jeff Almasol aka redefinery * * * * this builds a path using ternary operators * I like it that why a lot. Pretty slick * http://stackoverflow.com/questions/1771786/question-mark-in-javascript * * @param {Array of Arrays} path holds the coordinates for the path * @param {AVLayer} layer The layer to draw on * @param {String} rowname This is to sort ot rows * @param {String} pathname The name of the path * @param {Array of 3 Values 0 -1 } maskcolor The color of the masks always per Glyph * @param {ADBE Vectors Group} shapegroup The group that contains the path * @return {ADBE Vectors Group} for reuse. * * @todo Get the hang of the diffrent paths in a Character */function make_path(path,layer, use_shapes, shapegroup){var masksGroup = null; masksGroup = use_shapes ? layer("ADBE Root Vectors Group") : layer("ADBE Mask Parade");// Get// PropertyGroup for the shape// or// the PropertyGroup for the masks // masksGroup = layer("ADBE Mask Parade");if (masksGroup !== null){ var mask = null;// Create a new maskif(shapegroup === null && use_shapes === true){ var pregroup = masksGroup.addProperty("ADBE Vector Group"); pregroup.name = "group";//charname + ' ' + rowname; shapegroup = pregroup.addProperty("ADBE Vectors Group"); mask = shapegroup;} mask = use_shapes ? shapegroup.addProperty("ADBE Vector Shape - Group") : masksGroup.addProperty("ADBE Mask Atom"); // mask = masksGroup.addProperty("ADBE Mask Atom"); if (mask !== null){ mask.name = "path"; // use_shapes ? pathname : charname + ' ' + rowname + ' ' + pathname; // mask.color = maskcolor; var s = new Shape();// new shape object if (s !== null){ s.vertices = path; // The close attribute defaults to true s.closed = false; // put the path verticies into the shape or mask maskShape = use_shapes ? maskShape = mask.property("ADBE Vector Shape") : mask.property("ADBE Mask Shape"); // Change the mask shape (not keyframed) maskShape.setValue(s); } } }// return shapegroup;} /** * Gets the position value at a specific time * It gets shortend to 3 digits float * @param {Layer Object} layer the current layer the get the value from * @param {Number Float} time The curent time * @return {String} builds a string that looks like this: "" + x +" " + y + " "+ z */ function get_pos_values_at_time(layer, time) { // var str = ""; var x = 0.0; var y = 0.0; var z = 0.0; if(layer.parent == null) { x = layer.transform.position.valueAtTime(time, false)[0] - (layer.containingComp.width / 2); y = layer.transform.position.valueAtTime(time, false)[1] - (layer.containingComp.height / 2); } else { x = layer.transform.position.valueAtTime(time, false)[0]; y = layer.transform.position.valueAtTime(time, false)[1]; } if(layer.threeDLayer) { z = layer.transform.position.valueAtTime(time, false)[2]; } else { z = 0.0; }; return [x, y, z]; }; /** * Recursive fetching layer posiitons is cool but does not solve the parenting problem * Wee need to calc the positions a diffrent way * Add a buffer layer with an expression that calcs a parented position toWorld * thnx to the marvelous Paul Tuersley * http://aenhancers.com/viewtopic.php?p=4647 * also saw this @ http://forums.creativecow.net/thread/227/13960 * by the inginious Dan Ebberts * apply to position of buffer layer to get the world position * works 2D and 3D * a = thisComp.layer("parented layer"); * a.toWorld(a.anchorPoint); * * Gets the position of layers * @param {Layer Object} layer The Layer to analys * @param {Number Comp Current Time} time The moment to capture * @param {Layer Object} buffer A NullObject added to the Comp for buffering the positions * @return {NOTHING} Sets a global value. Should return result. Thats better * @todo Remove Global object */ function get_position(layer, time, buffer) { var x = 0; var y = 0; var z = 0; var result = [0, 0, 0]; if(layer.parent != null) { var expr = new Array(); expr.push("// obj-vertex-export parent bake expression thnx 2 Paul T. & Dan E."); expr.push("var sourceLayer = thisComp.layer(" + layer.index + ");"); expr.push("sourceLayer.toWorld(sourceLayer.anchorPoint)"); if(layer.threeDLayer == true) { buffer.threeDLayer = true; } else { buffer.threeDLayer = false; } // buffer.transform.position.expression = ""; buffer.transform.position.expression = expr.join("\n"); result = get_pos_values_at_time(buffer, time); } else { result = get_pos_values_at_time(layer, time); } x = result[0]; y = result[1]; z = result[2]; return [x,y]; // global_curr_pos = [x, y, z]; // }; }// close mpo_import})(this);
Expand Down
Loading

0 comments on commit 30ab28b

Please sign in to comment.