PDA

View Full Version : popupMenu with dynamic menuItems


triptych79
01-16-2009, 12:57 PM
Hi,

I'm a Mel beginner and am trying to figure out how I can add a list of lights within my scene as menuItems in a popupMenu. The main issue I'm having is how to pass the list of lights back to my window procedure. I have a separate procedure for the light listing too (for other reasons).

Here's the code (throw a couple of lights in a scene before running it):


global proc myWin(){

if ((`window -ex exampleWindow`)==1)
deleteUI exampleWindow;

window -title "bf" exampleWindow ;

columnLayout;
button;
string $tkLightPop = `popupMenu -button 1 -pmc bf_lightLister test`;
print $tkLightPop;
showWindow exampleWindow;
}

myWin;

global proc bf_lightLister(){

popupMenu -e -dai test;
string $tkLightShapes[] = `ls -type "light"`;
int $numLights = `size($tkLightShapes)`;
int $i;


for ($i=0;$i<$numLights;$i++){

string $tkLightTrans[] = `listRelatives -p $tkLightShapes[$i]`;
menuItem -parent test -label $tkLightTrans[$i];


}
}



I'm also unsure why I'm getting unusual names for lights 2 and on. Any assistance would be appreciated.

Thanks.

goleafsgo
01-16-2009, 01:15 PM
I think all you need to change is this:
menuItem -parent test -label $tkLightTrans[$i];
to this:
menuItem -parent test -label $tkLightTrans[0];
That should fix your problem with the menu names.

Not sure what you meant by "...how to pass the list of lights back to my window procedure."?

triptych79
01-16-2009, 11:06 PM
Hi goleafsgo,


thanks for your reply. I guess the reason why I initially had ...

menuItem -parent test -label $tkLightTrans[$i];

...was to increment $i through the loop and therefore add multiple lights if there were any. If I were to keep it at $tkLightTrans[0], then it would only add the first light in the array. I'm trying to add all of the lights in the scene to the popupMenu.

And what I meant by ..."...how to pass the list of lights back to my window procedure."...is that I have myWin procedure and also bf_lightLister procedure. I wasn't sure of the best working method of getting a list of lights from my bf_lightLister procedure and passing them back as menuItems to the popupMenu in myWin procedure.

Sorry if it's still as clear as mud. :)

wrend
01-17-2009, 02:49 AM
...was to increment $i through the loop and therefore add multiple lights if there were any. If I were to keep it at $tkLightTrans[0], then it would only add the first light in the array. I'm trying to add all of the lights in the scene to the popupMenu.

i think you are confusing $tkLightTrans and $tkLightShapes...

And what I meant by ..."...how to pass the list of lights back to my window procedure."...is that I have myWin procedure and also bf_lightLister procedure. I wasn't sure of the best working method of getting a list of lights from my bf_lightLister procedure and passing them back as menuItems to the popupMenu in myWin procedure.

your myWin proc builds your UI, within which you build and instruct a popupMenu named 'test' to call bf_lightLister each time it is posted... it is this hard-baked name that allows bf_lightLister to know about the popupMenu and repopulate it with the light list each post (myWin need never be called again after construction...).

triptych79
01-17-2009, 11:27 PM
Hi Wrend,

thanks for the comments.


I think I have it working. Here is the code for any other beginners looking to learn. Any comments are welcome.

global proc myWin(){

if ((`window -ex exampleWindow`)==1)
deleteUI exampleWindow;

window -title "bf" exampleWindow ;

columnLayout;
button;
string $tkLightPop = `popupMenu -button 1 -pmc bf_lightLister test`;
print $tkLightPop;
showWindow exampleWindow;
}

myWin;

global proc bf_lightLister(){

popupMenu -e -dai test;
string $tkLightShapes[] = `ls -type "light"`;
int $numLights = `size($tkLightShapes)`;
int $i;
string $tkLightTrans[];

for ($i=0;$i<$numLights;$i++){

string $tkLightTrans[] = `listRelatives -p $tkLightShapes[$i]`;
menuItem -parent test -label $tkLightTrans -c ("select "+$tkLightTrans[0]) ;
print $tkLightTrans;

}
}

CGTalk Moderation
01-17-2009, 11:27 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.