-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathShapeRepeaterBaker.jsx
187 lines (152 loc) · 6.37 KB
/
ShapeRepeaterBaker.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// BakeRepeat - tool similar to Cinema 4D's "curret state object"
// Creates copies of shape populated by Repeater modifier
//v0.5 - Release
//v0.51 - Bug fixes, offset is supported correctly
//by Nik Ska, 2013
//License
//Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
//You are free to use this code in your personal projects
//as long as it's open source and not commercial.
var repBaker = this;
repBaker.buildGUI = function(thisObj){
thisObj.w = (thisObj instanceof Panel) ? thisObj : new Window("palette", thisObj.scriptTitle, undefined, {resizeable:true});
thisObj.w.alignChildren = ['left', 'top']
thisObj.w.minimumSize = "width: 150, height: 110";
thisObj.w.maximumSize = "width:150, height: 110";
thisObj.w.add("staticText", undefined, "Shape Repeater Baker");
var bakeit = thisObj.w.add("button", undefined, "Bake repeater");
bakeit.size = [120,20];
var lineTwo = thisObj.w.add("group{orientation:'row'}");
lineTwo.size = "width: 125, height: 25";
var removeRepeater =lineTwo.add("checkbox", undefined, "Remove repeater")
bakeit.onClick = function(){
thisObj.doit(removeRepeater.value);
}
if (thisObj.w instanceof Window){
thisObj.w.center();
thisObj.w.show();
}
else thisObj.w.layout.layout(true);
}
repBaker.doit = function(_remove){
//Main script function
//_remove - remove or leave the repeater
var repeaterInstances = [{}]; //array for repeater(s)
function getRepeaterInstance(_arr){
//filter out selected properties
for(var j = 0 ; j < _arr.length ; j ++){
if(_arr[j].matchName == "ADBE Vector Filter - Repeater"){
return _arr[j]
}
}
return null
}
function removeRepeater(_group, _name){
var pr = _group;
for(var k = pr.numProperties; k>0 ; k--){
if(pr.property(k).name == _name){
pr.property(k).remove();
break;
}
}
}
function reverseOrder(_group, _num){
var pr = _group;
var move = _num;
for(var j = _num ; j > 0; j--){
pr.property(_num).moveTo(_num-j+1)
}
}
var activeItem = app.project.activeItem;
if(activeItem != null && activeItem instanceof CompItem){
var sel = activeItem.selectedLayers;
var rep = getRepeaterInstance(activeItem.selectedProperties);
if(rep!=null){
if(rep.propertyIndex > 2) alert("Group all layers above repeater")
else if(rep.propertyIndex == 1) alert("Put at least something above repeater")
else{
var repName = rep.name;
var numCopies = rep["ADBE Vector Repeater Copies"].value;
var order = rep["ADBE Vector Repeater Order"].value;
var repGroup = rep.parentProperty;
app.beginUndoGroup("Bake repeater");
repeaterInstances[0].repeater = rep;
this.getRepeaterAttr(rep, repeaterInstances[0]);
var _obj = repGroup.property(1);
this.makeCopies(_obj, repeaterInstances[0]);
if(_remove == true) removeRepeater(repGroup, repName);
if(order == 1) reverseOrder(repGroup, numCopies);
app.endUndoGroup();
}
}
else alert("Select repeater you want to bake")
}
}
repBaker.makeCopies =function(_obj, _repeater){
var numCopies = Number(_repeater["ADBE Vector Repeater Copies"]);
_repeater.repeater.enabled = false;
var tmp = _obj;
var offset = Number(_repeater["ADBE Vector Repeater Offset"]);
for (var i = offset; i < numCopies+offset; i++) {
this.setAttrFromRepeater([tmp], _repeater, i);
if(i<numCopies+offset-1) tmp = tmp.duplicate();
};
}
repBaker.setAttrFromRepeater = function(_shape, _repeater, _num){
//function to set shape properties based on repeater object
//_shape is a set of shapes to appy transform to
//_repeater is a repeater instance
//_num is the copy number
for(var k = 0 ; k < _shape.length ; k++){
var tr = _shape[k].property("ADBE Vector Transform Group");
var offset = Number(_repeater["ADBE Vector Repeater Offset"]);
if(_num == offset){
//moving first copy
tr.property("ADBE Vector Anchor").setValue(tr.property("ADBE Vector Anchor").value + _repeater["ADBE Vector Repeater Anchor"] - tr.property("ADBE Vector Position").value);
tr.property("ADBE Vector Position").setValue(_repeater["ADBE Vector Repeater Anchor"]+offset*_repeater["ADBE Vector Repeater Position"]);
tr.property("ADBE Vector Scale").setValue([tr.property("ADBE Vector Scale").value[0]*Math.pow(_repeater["ADBE Vector Repeater Scale"][0]/100,offset), tr.property("ADBE Vector Scale").value[1]*Math.pow(_repeater["ADBE Vector Repeater Scale"][1]/100,offset)]);
tr.property("ADBE Vector Rotation").setValue(tr.property("ADBE Vector Rotation").value+offset*_repeater["ADBE Vector Repeater Rotation"]);
}
else{
//all the others
tr.property("ADBE Vector Position").setValue(tr.property("ADBE Vector Position").value +_repeater["ADBE Vector Repeater Position"]);
tr.property("ADBE Vector Scale").setValue([tr.property("ADBE Vector Scale").value[0]*_repeater["ADBE Vector Repeater Scale"][0]/100, tr.property("ADBE Vector Scale").value[1]*_repeater["ADBE Vector Repeater Scale"][1]/100]);
tr.property("ADBE Vector Rotation").setValue(tr.property("ADBE Vector Rotation").value+_repeater["ADBE Vector Repeater Rotation"]);
}
tr.property("ADBE Vector Group Opacity").setValue(_repeater["ADBE Vector Repeater Opacity 1"] + (_num-offset)*(_repeater["ADBE Vector Repeater Opacity 2"]-_repeater["ADBE Vector Repeater Opacity 1"])/_repeater["ADBE Vector Repeater Copies"]);
}
}
/*
function setExpression(){
//placeholder for the expression setter
}
function bakeExpression(){
//placeholder for expressions baker
}
*/
repBaker.getParentLayer = function(_property){
//recursively gets the parent layer
if(_property.propertyDepth>0){
return getParentLayer(_property.parentProperty);
}
else{
return _property;
}
}
repBaker.getRepeaterAttr = function(_property, repInstance){
//recursive function to collect all repeater settings
//first - check if we walk over a property group
if(_property.propertyType == PropertyType.NAMED_GROUP){
for(var i = 1; i <= _property.numProperties; i++){
if(_property.property(i).propertyType == PropertyType.NAMED_GROUP){
this.getRepeaterAttr(_property.property(i), repInstance)
}
else{
//set repeater instance's values
repInstance[_property.property(i).matchName] = _property.property(i).value;
}
}
}
else return null
}
repBaker.buildGUI(repBaker)