MEL Rendering script - global vatiables issue

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 03 March 2013   #1
MEL Rendering script - global vatiables issue

Hi MEL experts!

Im working on a little script that is -when finished- supposed to provide a quick shelf button way of saving the current image from the renderviw to the disk.

But regardless of the main purpose and for convenience purposes I want to check if the script has been executed for the first time or if it has been executed before.
And that part is causing me a problem:

For some reason the script always says that it has already been executed before even when it hasn't been.

The code below is only the functionality that checks if it has been executed before:

//Getting a list of all existing declared global variables in Maya
 string $allGlobalVariables[] = `env`;
 //Finding out how many they are exactly to be able to go through all of them in the "for()" below
 int $size=size($allGlobalVariables);
 
 //Flag to mark if the script has been executed before
 int $scriptAlreadyExecutedOnce=0;
 
 //From here on: Check if the script has been executed before by checking if one global variable ($firstLauchGlobalVariableTrue) has already been created. (Which gets created only when the script has been run the first time in this Maya session).
 
 //For debugging reasons $i starts at 1 instead of 0.
 for($i=1;$i<=$size;$i++) {
 	
    //write the name of the current `env` variable which is to be checked into the $currentlyCheckedVariable variable
 	$currentlyCheckedVariable = $allGlobalVariables[($i-1)];
 	
 	//Debugging
 	//print("$i="+($i)+" | Loop run Nr. "+($i)+" | $size: "+$size+" | scriptAlreadyExecutedOnce= "+$scriptAlreadyExecutedOnce+" | Currently checked variable name: "+ $currentlyCheckedVariable +"\n");
 		
 	if($currentlyCheckedVariable=="$firstLauchGlobalVariableTrue"){
 		
 		print( "The Variable exists so the script has been launched once already! \n" );
 		
 		$scriptAlreadyExecutedOnce=1;
 		
 		/*additional code for the actual functionality of this script will be placed here*/
 		
 		
 	}
 	
 	//Debugging
 	//print("$i="+($i)+" | Loop run Nr. "+($i)+" | $size: "+$size+" | scriptAlreadyExecutedOnce= "+$scriptAlreadyExecutedOnce+" | Currently checked variable name: "+ $currentlyCheckedVariable +"\n");
 	
 	//If we have reached the end of the "for()" and the script aparently has not been executed before then create the varaible now
 	if($i==($size)&&$scriptAlreadyExecutedOnce==0){
 		
 		print( "The Variable does not exist yet so the script has NOT been launched before! \n" );
 		
 		//Declaring the variable now so that next time the script will be executed this "if" should not be accessed
 		global int $firstLauchGlobalVariableTrue;
 				
 		/*additional code for the actual functionality of this script will be placed here*/
 		
 		}
 			
 }
 
 
 
Old 03 March 2013   #2
I'm having a hard time decoding your script, so I'll just offer an alternative:

Don't use global vars. Use optionVars.

if(!`optionVar -ex "isFirstRun"`) {
  // this is the first run
  optionVar -intValue "isFirstRun" 0;
}
else {
  // this isn't the first run
  // do something else
}  


The first time the script is run, the code above will notice that no such optionVar exists, and create it. Every following time the script is run after that, the optionVar will already exist and you can do whatever it is you want to do in that case.
Just make sure to choose a proper (that is, most probable unique) name for your optionvar.
 
Old 03 March 2013   #3
Fantastic, thanks! I'll try it right away!
 
Old 03 March 2013   #4
Thread automatically closed

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.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 09:10 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.