-
Notifications
You must be signed in to change notification settings - Fork 0
/
aspacer.mel
201 lines (187 loc) · 6.54 KB
/
aspacer.mel
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// a tool to space out bulk-imported assets across a grid
// author: dimelo waterson (@boner-cmd)
// from help.autodesk.com sample MEL code
// https://help.autodesk.com/view/MAYAUL/2020/ENU/index.html?contextId=COMMANDS-INDEX
// and publicly-accessible content from Professor Alan Price's Autumn 2005
// "Experimental Scripting for Animation in Maya" course at Ohio State University
// https://www.asc.ohio-state.edu/price.566/courses/694/
// TODO if script is loaded, does procedure need to be global? (no, so... ?)
global proc aSpacer()
{
if (`window -exists aspaceWindowB`) deleteUI aspaceWindowB;
window
-title "Asset Spacer"
-maximizeButton false
-resizeToFitChildren true
-sizeable false
"aspaceWindowB";
formLayout "formLayB";
text
-label "Matrix object spacing"
"spacingLabel";
floatField
-minValue 0
-value 0
-precision 2
-w 75
"spacingField";
radioButtonGrp
-numberOfRadioButtons 4
-label "Matrix dimensions (approximate)"
-labelArray4 "1x1" "4x3" "3x2" "2x1"
-annotation1 "Ratio of 1.00"
-annotation2 "Ratio of 1.33"
-annotation3 "Ratio of 1.50"
-annotation4 "Ratio of 2.00"
-columnAttach 1 "left" 0
-columnWidth 1 166
-columnWidth 2 40
-columnWidth 3 40
-columnWidth 4 40
-columnWidth 5 40
"ratioRadioGrp";
button
-label "Go"
-command "if ( vfySelection() == 1 ) spaceObjs(updateSpacerVars());"
"goButton";
checkBoxGrp
-numberOfCheckBoxes 2
-label1 "Attempt to center the matrix"
-label2 "Skip non-visible objects"
"chkFeatures";
checkBox
-label "Arrange on Y-Axis"
"chkY";
formLayout -edit
-attachForm "spacingLabel" "left" 5
-attachForm "spacingLabel" "top" 10
-attachControl "spacingField" "left" 5 "spacingLabel"
-attachForm "spacingField" "top" 6
-attachForm "chkY" "top" 8
-attachControl "chkY" "left" 8 "spacingField"
-attachControl "ratioRadioGrp" "top" 10 "spacingLabel"
-attachForm "ratioRadioGrp" "left" 5
-attachControl "goButton" "top" 10 "ratioRadioGrp"
-attachForm "goButton" "left" 5
-attachControl "chkFeatures" "left" 5 "goButton"
-attachControl "chkFeatures" "top" 12 "ratioRadioGrp"
"formLayB";
showWindow aspaceWindowB;
}
// command for accessing private procedures without using the GUI
global proc aSpacerCmd(float[] $spacerVars) {
// where $spacerVars is: {
// matrix spacing (a float),
// matrix ratio (a float),
// if centered (a boolean int),
// whether to skip hidden objs (a boolean int),
// whether to lay out objs up y axis (a boolean int)
// }
checkVarArray($spacerVars);
vfySelection();
spaceObjs($spacerVars);
}
proc checkVarArray(float[] $spacerVars) {
if ( size($spacerVars) < 5 ){
warning "Asset Spacer only takes 5 parameters.\n";
error "Too few parameters provided to aSpacerCmd.\n";
}
if ( size($spacerVars) > 5 ) {
warning "Asset Spacer only takes 5 parameters.\n";
error "Too many parameters provided to aSpacerCmd.\n";
}
}
proc float[] updateSpacerVars() {
// assign default values
float $spaceInput = 0;
float $matrixRatio = 1;
int $centerChk = 0;
int $skipHidden = 0;
int $layupY = 0;
// query form elements to get values
$spaceInput = `floatField -query -value spacingField`;
$matrixRatio = `radioButtonGrp -query -select ratioRadioGrp`;
if ( $matrixRatio == 0) $matrixRatio = 1;
$centerChk = `checkBoxGrp -query -value1 chkFeatures`;
$skipHidden = `checkBoxGrp -query -value2 chkFeatures`;
$layupY = `checkBox -query -value chkY`;
float $updateArray[] = {$spaceInput, $matrixRatio, $centerChk, $skipHidden,$layupY};
return $updateArray;
}
// TODO consider converting to use error instead of print?
// TODO still need a better way to detect if a group is selected
proc int vfySelection() {
$selection = `ls -sl`;
$selectionSize = size($selection);
if ( $selectionSize > 1 ) {
print "More than one object selected.\n";
} else if ( $selectionSize == 0 ) {
print "No object selected.\n";
} else {
$shapeCheck = `listRelatives -s $selection`;
if ( size($shapeCheck) != 0 ) {
print "Selection has Shape children. Are you sure a group is selected?\n";
} else {
return 1;
}
}
return 0;
}
// TODO review to see if any procedures could be broken out
// TODO look for opportunities to comment, if required
proc spaceObjs( float $inputArray[] ) {
$spacing = $inputArray[0];
$ratio = $inputArray[1];
$center = $inputArray[2];
$skiphide = $inputArray[3];
$layY = $inputArray[4];
$selection = `ls -sl`;
$givenObjects = `listRelatives $selection`;
int $xcount = 0;
int $ycount = 0;
int $zcount = 0;
$totalObjs = size($givenObjects);
$algWidth = ($totalObjs * sqrt($ratio)) / sqrt($totalObjs);
$algHeight = sqrt($totalObjs) / sqrt($ratio);
float $spacingCorrection = ($spacing * .5);
for ( $obj in $givenObjects ) {
string $visobj = $obj + ".visibility";
int $visobjval = `getAttr $visobj`;
if ( (($visobjval == 1) && ($skiphide == 1)) || ($skiphide == 0) ) {
string $transobj_x = $obj + ".translateX";
string $transobj_z = $obj + ".translateZ";
string $transobj_y = $obj + ".translateY";
if ( $layY == 0 ) {
if ( $xcount >= $algWidth ) {
$xcount = 0;
$zcount += 1;
}
setAttr $transobj_y 0;
if ( $center == 1 ) {
float $xOffset = -(($algWidth/2) * $spacing) + $spacingCorrection;
float $zOffset = -(($algHeight/2) * $spacing) + $spacingCorrection;
setAttr $transobj_x (($xcount * $spacing) + $xOffset);
setAttr $transobj_z (($zcount * $spacing) + $zOffset);
} else {
setAttr $transobj_x ($xcount * $spacing);
setAttr $transobj_z ($zcount * $spacing);
}
} else {
if ( $xcount >= $algWidth ) {
$xcount = 0;
$ycount += 1;
}
setAttr $transobj_z 0;
if ( $center == 1 ) {
// don't center on Y axis
float $xOffset = -(($algWidth/2) * $spacing) + $spacingCorrection;
setAttr $transobj_x (($xcount * $spacing) + $xOffset);
} else {
setAttr $transobj_x ($xcount * $spacing);
}
setAttr $transobj_y ($ycount * $spacing);
}
$xcount += 1;
}
}
}