-
Notifications
You must be signed in to change notification settings - Fork 6
/
anchorTransform.mel
355 lines (276 loc) · 10.9 KB
/
anchorTransform.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// ***************************************************************************
// INSTALL MODULE
// ***************************************************************************
//
// DESCRIPTION:
// Add the module which is located in the same directory as the .mel file
// into the first available MAYA_MODULE_PATH directory. It doesn't matter
// where on disk the module lives as the script will make sure the path in
// the .mod file links to the correct place. There are some basic sanity
// checks in place to make sure the script doesn't error out. This includes
// Maya version compatibility with the module. Permissions of the directories
// files have to be written to and incorrectly formatted module files.
//
// REQUIRES:
// - Template .mod file ( replace regular file path with <PATH> )
// - Maya module ( scripts/icons/plug-ins etc directories )
//
// USAGE:
// source <MEL_FILE>;
//
// AUTHORS:
// Robert Joosten - [email protected]
//
// LICENSE:
// MIT
//
// VERSIONS:
// 1.0.0 - Aug 23, 2018 - Initial Release.
// 1.0.1 - Sep 17, 2018 - Allow for use in paths that contain a space.
// 1.0.2 - Oct 03, 2018 - Fix path error installing on UNIX.
//
// ***************************************************************************
proc string[] getModulePathsFromEnvironment()
{
// get all of the paths in the MAYA_MODULE_PATH variable and split them
// into a string array.
string $paths[];
string $separator = (`about -nt`) ? ";" : ":";
tokenize(getenv("MAYA_MODULE_PATH"), $separator, $paths);
return $paths;
}
// ***************************************************************************
proc string findModuleFileInPath(string $path)
{
// get the first module file found in the directory.
string $files[] = `getFileList -folder $path -filespec "*.mod"`;
return $files[0];
}
proc string findModuleFileInModulePath(string $moduleFile, string $modulePaths[])
{
// loop module paths
for( $path in $modulePaths )
{
// find moduleFile in path
string $files[] = `getFileList -folder $path -filespec $moduleFile`;
if ($files[0] != "")
return $path;
}
// declare empty output
string $empty;
return $empty;
}
// ***************************************************************************
proc string[] readModuleFile(string $path)
{
// read the module file and return each line as a string part of a string
// array.
string $lines[];
$fileId = `fopen $path "r"`;
string $data = `fread $fileId $data`;
tokenize($data, "\n", $lines);
return $lines;
}
// ***************************************************************************
proc string getConditionValue(string $line)
{
// get the condition value by splitting the string on the : character and
// returning the second section of the partition.
string $partitions[];
tokenize($line, ":", $partitions);
return $partitions[1];
}
// ***************************************************************************
proc string[] parseModuleLine(string $line)
{
// declare output variable
string $output[];
// declare default output
string $mayaVersion;
string $language;
string $platform;
// make sure its a module line by checking if the line starts with a +
// or -.
if (startsWith($line, "+") == 0 && startsWith($line, "-") == 0)
return $output;
// get partitions
string $partitions[];
tokenize($line, " ", $partitions);
// get partitions length
$length = size($partitions);
// loop partitions in reverse
for( $i=1; $i<$length+1; ++$i )
{
// get reverse index
int $index = $length - $i;
string $part = $partitions[$index];
// extract conditions
if (startsWith($part, "MAYAVERSION") == 1)
{
$mayaVersion = getConditionValue($part);
stringArrayRemoveAtIndex($index, $partitions);
}
else if (startsWith($part, "LANGUAGE") == 1)
{
$language = getConditionValue($part);
stringArrayRemoveAtIndex($index, $partitions);
}
else if (startsWith($part, "PLATFORM") == 1)
{
$platform = getConditionValue($part);
stringArrayRemoveAtIndex($index, $partitions);
}
}
// set output
$output[0] = $partitions[0];
$output[1] = $partitions[1];
$output[2] = $partitions[2];
$output[3] = $partitions[3];
$output[4] = $mayaVersion;
$output[5] = $language;
$output[6] = $platform;
return $output;
}
// ***************************************************************************
proc string[] findModuleMatch(string $lines[])
{
// get maya data
string $mayaVersion = `about -version`;
string $language = `about -uiLanguage`;
string $platform = `about -operatingSystem`;
// process module to see if its suitable for the current version of Maya.
for($line in $lines)
{
// get line data
string $lineData[] = parseModuleLine($line);
// validate line data
if ($lineData[0] == "")
continue;
// validate maya version
if ($lineData[4] != "" && $lineData[4] != $mayaVersion)
continue;
// validate language
if ($lineData[5] != "" && $lineData[5] != $language)
continue;
// validate platform
if ($lineData[6] != "" && $lineData[6] != $platform)
continue;
// return match
return $lineData;
}
// declare empty output
string $empty[];
return $empty;
}
// ***************************************************************************
proc loadModuleExtended(string $moduleFile, string $path)
{
// get separator
string $separator = "/";
// load module
loadModule -load $moduleFile;
// add scripts path to system path, for some reason after loading the
// module we are still not able to import the scripts even though the
// path is available in the MAYA_SCRIPT_PATH.
string $scriptPath = $path + $separator + "scripts";
python("import sys; sys.path.append('" + $scriptPath + "')");
// see if a userSetup.py is present in the script folder. If this is the
// case execute that file.
string $userSetup = $scriptPath + $separator + "userSetup.py";
if (`filetest -e $userSetup` == 1)
python("import __main__; execfile('" + $userSetup + "', __main__.__dict__ )");
}
// ***************************************************************************
global proc installModule()
{
// get path from mel file
string $melWhatIs = `whatIs "installModule"`;
string $melWhatIsPath = `match ": (.*)" $melWhatIs`;
int $melSize = `size $melWhatIsPath`;
string $melPath = `substring $melWhatIsPath 3 ($melSize-3)`;
string $path = dirname($melPath);
// get separator
string $separator = "/";
// find installed module
string $modules[] = `moduleInfo -listModules`;
// get module paths from the environment and choose the first one if this
// variable is not overwritten the .mod file will be written to this path.
string $modulePaths[] = getModulePathsFromEnvironment();
string $modulePathDefault;
// find module in current directory
string $moduleFileBase = findModuleFileInPath($path);
// validate module
if ($moduleFileBase == "")
error("\nNo template module file found in " + $path);
// find full module path
string $moduleFile = $path + $separator + $moduleFileBase;
// read module content
string $moduleContent[] = readModuleFile($moduleFile);
// find module match
string $moduleMatch[] = findModuleMatch($moduleContent);
string $moduleName = $moduleMatch[1];
string $moduleVersion = $moduleMatch[2];
// validate module content
if ($moduleName == "")
error("\nNo valid module information found matching the current version/language/platform of Maya in " + $moduleFile);
// check if module already exists
if (stringArrayContains($moduleName, $modules) == 1)
{
string $moduleVersionExisting = `moduleInfo -moduleName $moduleName -version`;
string $existingPath = `moduleInfo -moduleName $moduleName -path`;
if ($moduleVersion == $moduleVersionExisting && $path ==$existingPath)
{
print("The " + $moduleName + " module, version " + $moduleVersion + " at " + $path + " is already installed!");
return;
}
// find existing module path
$modulePathDefault = findModuleFileInModulePath($moduleFileBase, $modulePaths);
}
// get existing module path to the front of the list
if ($modulePathDefault != "")
{
int $index = stringArrayFind($modulePathDefault, 0, $modulePaths);
stringArrayRemoveAtIndex($index, $modulePaths);
stringArrayInsertAtIndex(0, $modulePaths, $modulePathDefault);
}
// loop module paths
for($modulePath in $modulePaths)
{
// only allow default path to be written if it exists
if ($modulePathDefault != "" && $modulePathDefault != $modulePath)
continue;
// create dir in case it doesn't exist
sysFile -makeDir $modulePath;
// construct module path
string $outputPath = $modulePath + $separator + $moduleFileBase;
// write module file
$fileId = `fopen $outputPath "w"`;
// if the default path exists error if the file cannot be written
// this means the module has been installed by hand in a directory
// that is not writable without administrator permissions. The error
// will ask the user to copy the script by hand.
if ($fileId == 0 && $modulePathDefault == $modulePath)
error("\nThe " + $moduleName + " module, version " + $moduleVersion + " could not be updated as the current path it is installed in is not writable.\nPlease overwrite the " + $moduleFileBase + " file in " + $modulePath + ".");
else if ($fileId == 0)
continue;
// write lines
print("Content:\n");
for($line in $moduleContent){
string $processedLine = `substitute "<PATH>" $line $path`;
print(" " + $processedLine + "\n");
fprint $fileId ($processedLine + "\n");
}
// print output
print("Output:\n");
print(" " + $outputPath + "\n");
// close output path
fclose $fileId;
// load module
loadModuleExtended($outputPath, $path);
return;
}
// throw error as module has not been installed
error("\nThe " + $moduleName + " module, version " + $moduleVersion + " could not be installed as non of the module paths were writable.");
}
// process
installModule();