XM Magdalena 3D print, GGeorgy (3D)
LC #42 Pipers Alley

View Full Version : Initial value that will be kept when rerun?

09-19-2010, 02:02 AM
'm very new to MEL and I need help. I tried googling but I really don't know what to look for.

To illustrate what I want to achieve:
1st run of the script: It stores the value in a variable. Say.. int $X = getAttr pCube1.translateX; or something like that. From that $X is 6.

2nd run and nth run: Now for the second time the script is executed, I want it to skip assigning the translateX so that the X would keep the value of 6.

Is that possible with a single script file?

09-19-2010, 02:03 AM
First run will be calculated every time the software starts.

Actually I only want to hold on an initial randomized number and use that same number everytime the the script is being ran after the first run.

global proc $dumper() {

int $randMode;
int $initialPosition;

if ( $randMode == 0 ) {

$initialPosition = ` rand -5 5 `;
$randMode = 1

print $initialPosition;

Unfortunately, that doesn't work because everytime I run the script it initializes all the values making the $randMode to hold the value of 0 again.

I'm now looking for a solution that would allow me to access values outside the script or file. Do you know if Maya keeps track of the number of times a script has been run in the current session?

Thanks a lot.

09-19-2010, 03:16 AM
use an optionVar

09-19-2010, 04:05 AM
of course Maya can't keep track of the number of times .
but you can do with a global int variable

global int $time = 1;

global proc myProcedure()
global int $time;
int $x;
$x = rand(-3,3);
eval("global int $time"+(string)($time) +";");
print ("this is the " + (string)($time) + " time(s) you run the script,the variable x is "+(string)($x));

global proc getOldValue(int $myTime)
int $myTime;
eval("global int $time"+(string)($myTime)+";");
eval("print (\"the " +(string)($myTime)+" time(s) value is \" + (string)($time"+(string)($myTime)+"));");

this is the 1 time(s) you run the script,the variable x is 0
this is the 2 time(s) you run the script,the variable x is -2
this is the 3 time(s) you run the script,the variable x is 1
this is the 4 time(s) you run the script,the variable x is -1
this is the 5 time(s) you run the script,the variable x is 1
this is the 6 time(s) you run the script,the variable x is 1
this is the 7 time(s) you run the script,the variable x is 2

the 5 time(s) value is 1
the 3 time(s) value is 1

hope can help you !

09-19-2010, 10:47 AM
You can achieve what you want using global variables mj. Using globals is generally not recommended, but this is one instance where it is probably the simplest solution as one side-effect of global variables is that once initialised with a value, reinitialisation with a different value is ignored, which is the behaviour you want here.

global proc dumper()
global int $gInitialPosition = -98765; // HAS to be initialised with a fixed value
if ($gInitialPosition == -98765)
$gInitialPosition = `rand -5 5`;
print $gInitialPosition;

However, you need to understand the difference between sourcing (or re-defining) a proc definition, and simply calling the procedure. Every time this proc is sourced the global initialPosition will be recalculated, and every time the procedure is called, the global initialPosition will remain the same.

There are other ways to impliment this, and this isn't necessarily the best way, depending on how you want to use it, but as you are newish to MEL it's the simplest.


09-19-2010, 03:43 PM
@mduvekot JasonJune2009 NaughtyNathan - Thank you!

Done! After minutes of testing, I believe the script is working as I intended. Using global values was the solution I was looking for.

I haven't explored global values' initialization value. Right now, I simply use
[global type $name;] that reads as 0. Thanks for bringing it up, I will explore that further.

09-23-2010, 11:16 AM
actually if you create your global int you cannot determine if its accidentially 0 or if it was set to the value.

But there is an env (http://download.autodesk.com/us/maya/2009help/Commands/env.html) command.
This line would return 1 if $myGlobalVar would exist globally:
stringArrayCount "$myGlobalVar" (env());
// Result: 0 //

CGTalk Moderation
09-23-2010, 11:16 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.