PDA

View Full Version : Calling a local procedure as a UI command? and file-scoped local variables


decrezero
06-12-2007, 04:16 AM
I figured out you can hack together file scoped local constant by declaring them:


proc type name() { return assignment; };
// within the file ('name' == assignment)
// and assignment is of type


I figured this type of thinking might let me call a local function as a UI command, but I can't think how. I know that technically I'm abusing some of the properties of MEL, and the code won't look right.

A simple example of what I'm talking about:

Important Note: If this script, or the above script, is not loaded from a script directory then the localFunction will be declared as a global proc and the example will not be valid.

example1.mel:

proc string localFunction()
{
print "this is a local function\n";

return ""; // keeps the -c flag from sending an error
}

global proc example1()
{
window -t "test window" someWindow;

menuBarLayout;
menu -l "test";
menuItem
-l "Restore Defaults"
-c `localFunction`; // executes on window creation not when the button is clicked

showWindow someWindow;
}



I could easily just define the UI procedures as global and be done with it, but I'm concerned about namespace safety with the number of global procedures that are being defined. (and it would lower maya's general use memory footprint)

brubin
06-12-2007, 07:47 AM
example1.mel:

// if you don't need a return value and you don't want an error
// don't declare a return value!
// proc string localFunction()
proc localFunction()
// if the proc doesn't work on button-mouseclick, simply make it a global proc!
{
print "this is a local function\n";

// return "";
}

global proc example1()
{
window -t "test window" someWindow;

menuBarLayout;
menu -l "test";
menuItem
-l "Restore Defaults"
// if you don't want the function to execute when the UI is built,
// then use " instead of `
// -c `localFunction`; // executes on window creation not when the button is clicked
-c "localFunction";
showWindow someWindow;
}



I could easily just define the UI procedures as global and be done with it, but I'm concerned about namespace safety with the number of global procedures that are being defined. (and it would lower maya's general use memory footprint)
hmm, at this stage this shouldn't be of your main concern... just a thought! ;)

HIH
s.

decrezero
06-12-2007, 02:50 PM
Thanks for the quick reply Brubin but, I was looking for something a little different than the syntax correction. My post was probably unclear, but I was hoping for a way to call a local procedure through a "-c" option within something like a menu item; this normally isn't allowed. I'm interested in this more for thirty or forty menuItem scripts than for use in the example I posted.


I tried to correct my post to make things a little more clear.

goleafsgo
06-12-2007, 04:49 PM
If you are going to have lots of callbacks in the same file and you are worried about having too many of them global then you could always have just one callback for that file and maybe pass some kind of indicator.

Something like this...


global proc MySingleCallback(int $version, string $controlName)
{
if ($version == 1)
{
...
someControl -edit $controlName;
}
else if ($version == 2)
{
...
someControl -edit $controlName;
}
}

someControl -c "MySingleCallback 1 \"SomeControlName\"";
someControl -c "MySingleCallback 2 \"SomeOtherControlName\"";
someControl -c "MySingleCallback 3 \"YetAnotherControlName\"";

decrezero
06-12-2007, 07:05 PM
That seems to be exactly what I was looking for, I feel silly for not thinking of it.

Thanks.

CGTalk Moderation
06-12-2007, 07:05 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.