PDA

View Full Version : noob help with declaring variable once


BPorter
04-14-2007, 03:41 AM
Hi,

global proc createNewCamMenu ()
{
string $unknown = "unknown";
string $test = `whatIs "$a"`;
if (`size($test)` == `size($unknown)`)
{
global int $a = 2;
}

optionMenu
-parent intCol1
-label ("Camera" + $a)
("camDropMenu" + $a);
$a++;
string $cams[] = `listCameras`;
for ($each in $cams)
menuItem -label $each ($each + " MenuItem");

}

I am trying to create the variable $a once so every time the proc is called, it doesn't recreate it and make it 0. The point of the proc is to create a new option menu with all the cameras in it. The name of the first one would be "camDropMenu", the 2nd "camDropMenu1" , 3rd "camDropMenu2" and so on and so forth. I am aware that I can not specify a name for the option menu's and just use the default one that Maya gives, but that is poor scripting and not disciplined. My syntax works until I try to store the proc into memory. It says $a is not declared, but if you run the conditonal by itself, it works fine. Any help would be appreciated as this is my first script. Thanks

BPorter
04-14-2007, 07:28 AM
i figured out a solution.. but if someone could tell me why this doesnt work, it'd save me so many lines of code.

int $a[] = {`exists polyCube1`,`exists polyCube2`,`exists polyCube3`}

ever_desperate
04-14-2007, 10:38 AM
If youve made $a a global variable then why shud a procedure change it unless you are willing to change it by assigning some value. In case you are not aware of this, a global variable has to be defined once in the global block and again defined in a local block of a procedure that intends to use that variable. For example:

global int $a = 9;

global proc changeA()
{
global int $a;
print ("\na= " + $a + "\n");
$a = 4;
print ("\na= " + $a + "\n");
}
changeA();

//Result:

a= 9

a= 4

BPorter
04-14-2007, 06:33 PM
the only reason I made it global is because it wouldn't exist outside of the conditional. I just want it to query if there is a variable $a, if there isn't then create it, otherwise continue with the creation of the optionMenu's.

JackSMillenium
04-14-2007, 11:10 PM
Could you explain why this wouldn't work for you:

global int $a = 2;

global proc createNewCamMenu ()
{
global int $a;

optionMenu
-parent intCol1
-label ("Camera" + $a)
("camDropMenu" + $a);
$a++;
string $cams[] = `listCameras`;
for ($each in $cams)
menuItem -label $each ($each + " MenuItem");

}


The first time you source your mel file, the global var $a will be initialized to "2", and then the createNewCamMenu will keep incrementing that every time it's called. This solution only fails if you source your mel file multiple times (which you shouldn't be doing, unless you're debugging/developping or something).

Also, one quick comment: letting maya assign default names to UI widgets is NOT "poor scripting": quite the opposite. Hardcoding names should only be used as a LAST resort if you can't get around it: it usually means you're hardcoding the name both when you create the widget, and then somewhere else when you query its value to use it, instead of passing down the widget name as a variable to whatever function may want to query it.

This makes your code more maintainable because in your case, if you decide to change the UI, you will have to remember to change all your hardcoded names in multiple places and croses your fingers you didn't forget any. But t's also a matter of preference, not gospel... :)

CGTalk Moderation
04-14-2007, 11:10 PM
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.