FORUMS: list search recent posts

Passing an array through a button command with MEL

COW Forums : Autodesk Maya

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
Adam Benson
Passing an array through a button command with MEL
on Apr 21, 2013 at 1:47:04 am

Ok, I'm not going to bore anyone with the entire script, I'm just looking for a solution to the following problem, and I haven't been able to find one.
Long story short, I have a code that creates an array and then sets up a window that asks for user input and then at the very end of the window they hit a button that says:

button -label "Setup Lights" -command "`getChoice($lightsArray)`";
showWindow setupLights;
}

global proc getChoice(string $lightsArray[]){

Everything works great throughout the script, including the $lightsArray, until I send it to the procedure, then... dead. In fact, the only thing I can't get to work is passing the array to the procedure, everything else works great.

Now, I've tried several variations of the above code block (there's a whole lot more to it than this, but this was my problem child). I've tried:
-command `getChoice($lightsArray)`

-command "getChoice($lightsArray[])"

string $thisCommand[]=`getChoice($lightsArray)`;
-command $thisCommand

I've also tried adjusting my procedure:
global proc string[] getChoice($lightsArray){

global proc string getChoice($lightsArray){

global proc getChoice(string $lightsArray){

In any case, the one thing I don't seem to be able to do is to pass the array into the next procedure using the -command line in a button. Where am I going wrong?
Thanks in advance for any help.
Adam

Adam Benson
Visual Effects Artist
http://www.SleepDeprivedProductions.com
http://www.SleepDeprivedFilms.com


Return to posts index

Adam Benson
Re: Passing an array through a button command with MEL
on Apr 21, 2013 at 4:30:06 am

Well, I haven't yet been able to pass an array through the `button -command getChoice($lightsArray)`, I have found a workaround if anyone else is interested. It's not as elegant, but it does the job. Here it is. It goes within the `window` command.

string $thisChoice = stringArrayToString($lightsArray, "|");
textField -visible 0 -text $thisChoice lightArray;
button -label "Press Me" -command getChoice;
}
global proc getChoice(){
string $lightsArray[] = stringToStringArray(`textField -q -text lightArray`, "|");

By converting the array to a simple string and then putting it into a hidden textField, I can then call the textField from the next procedure, and convert it back into an array there. Thus, I was able to "pass" the array to the next procedure without actually passing it through the button command.
Hope this is helpful to someone else if they ever come across the same situation.
Adam
Oh, and if anyone does have a real solution to the problem, I'm still all ears!
Thanks again

Adam Benson
Visual Effects Artist
http://www.SleepDeprivedProductions.com
http://www.SleepDeprivedFilms.com


Return to posts index

Steve Sayer
Re: Passing an array through a button command with MEL
on Apr 29, 2013 at 3:16:00 pm

This seems to work:

string $myArray[] = {"one", "two", "three"};

window "myWindow";
columnLayout;
button -label "Print Array" -c "printArray($myArray)";
showWindow "myWindow";

proc printArray(string $inArray[])
{
for ($each in $inArray)
print ("nElement: " + $each);
}
You only use the square brackets when you are declaring an array, so that Maya can distinguish it from a non-array variable of the same type.

When declaring a procedure, you are also declaring the input variables, which is why the square brackets get used there as well--but they don't get used when simply referring to an already-created array, such as in the button's command string.


Return to posts index


Adam Benson
Re: Passing an array through a button command with MEL
on Apr 29, 2013 at 9:45:05 pm

Hey Steve,
Yeah, that's what I figured, but for whatever reason it just wouldn't work. My hypothesis is that it may have been the data that I was passing through it. Basically, I was using fopen to grab comma delineated data from a text file, AND each entry in the text file was separated by a carriage return. SO, my array was basically composed of the data separated by the carriage return, thus each [$i] in the array was basically a long string of comma delineated values of differing types. It worked fine using it place to place within each procedure, and I could pass it to another procedure within a procedure, but as soon as I tried to make it a button command "button -c nextProcedure($thisArray)" it would crap out and give me all kinds of errors. I didn't get the same errors when I passed it by simply calling nextProcedure($thisArray);
That's why I'm guessing that it may have had something to do with the data that I was cramming into the array. In any case I did get the tool to work and do what it was supposed to do, I just had to stuff the array into hidden text element to get it to work.
Thanks, as always, for your help!
Adam

Adam Benson
Visual Effects Artist
http://www.SleepDeprivedProductions.com
http://www.SleepDeprivedFilms.com


Return to posts index

Steve Sayer
Re: Passing an array through a button command with MEL
on May 1, 2013 at 8:36:26 pm

Ah, okay. Sorry I didn't have anything more helpful for you! Thanks for posting your solution.


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]