PDA

View Full Version : [MEL] GUI -> GUI help


th3m3nt4l
11-08-2012, 05:57 PM
Having a small issue moving between to GUIs. Primary gui has a button that makes a call to the secondary GUI's procedure. the Secondary GUI's procedure requires an Array (of string values) to be passed into it. Now this is where my problem is.

in the secondary GUI I have a button that prints the array (testing purposes to make sure things are working)- however I get the undeclared variable error once the button is pressed. I've tried making a variable just before my if statements to delete the window but that doesn't help either (string $temp[] = $array)

example code for the 2nd gui:


global proc string[] mySecondary window(string $array[]){
if((`window -ex mySecWinNm`) == true) deleteUI -window mySecWinNm;

window -title "blah"
-width 200
-height 200
"mySecWinNm"

string $base2 = `formLayout -numberOfDivision 100`;

button -label "Blah 2"
-command "print $array"
-w 115
-h 20
-vis true
-enable true
"tstBttn"
formlayout -e
-attachForm tstBttn "top" 100
-attachForm tstbttn "left" 50
$base2;


showWindow;
}

Nyro
11-08-2012, 09:33 PM
THe problem is this:

if you declare 'print $array' as a command for a button, then $array is not what you presumably expect it to be.

$array in that context is not the variable you passed into the procedure. The string inside the -command flag gets parsed at runtime. Only then is $array interpreted, and thus the interpreter looks for what $array is. But the command exists in its own little scope.

I'm finding it difficult to explain exactly what I mean by "scope" here. To clarify, try this:

Replace the string in the -command flag with something else, anything you can think of, but make sure it has some form of syntax error in it.
If you now run the procedure, you'll notice that Maya doesn't complain. It just parses your procedure, at the moment the string inside the -command flag is just that: a string. Only when you press the button does that string get evaluated, and Maya notices the syntax error. You'll get an error and, if you have "show line numbers" enabled, Maya will say the error occured on line 1 (instead of line 12 as it was in the original procedure).

So, long story short: if you want to use the actual value of the variable passed into the procedure, you will have to escape the command string, insert the variable's value, and continue with the command string:

button -command ("print \" " + $array[0] + "\"");

Note how I inserted ' " ' inside the print statement. Since the string will be interpreted as is, Maya expects strings to be encapsulated in quotation marks, so they have to be passed on with the command string.
Say $array[0] were the word "blarf", then after pressing the button, this is the command that gets executed (character for character, i.e. including quoattion marks):

print "blarf";

Note also that unlike in the script editor, you cannot print a whole array by simply stating

print $array // where $array is a variable of type string[]

it will return an error when you're building a command string like this.

th3m3nt4l
11-08-2012, 10:41 PM
I understand what your saying entirely, however I still get the error that $array is an undeclared variable when doing

-command "myProcedure($array)", I didn't try the \" \" idea on $array when testing, but doing myProcedure(); gives the expected error of expected arguments not met or something along those lines.

Nyro
11-08-2012, 11:22 PM
No...

This will not work:

-command "myProcedure($array)"
because, as explained, when evaluated as a button command, the interpreter doesn't know what $array is, because it doesn't exist in the scope of the button.

This, on the other hand, would work:

-command "string $array[] = {"argh", "blargh", "nargh"}; myProcedure($array);"
because now the variable $array has been declared in the scope of the button. It would be a totally new variable and pretty much useless.

You have to use the syntax I provided earlier:

-command ("myProcedure(" + $array + ")")

Though in this case you might run in to trouble because you can't catenate string arrays like that in MEL, but that's a different issue.

th3m3nt4l
11-09-2012, 01:32 AM
see thats weird because it worked in the Main gui when i declared the arra before the window and deletion info, it will be tomorrow but I can post the main gui code for you too look over.

zaskar
11-09-2012, 09:22 AM
You can "hardcode" the array into the button command at GUI creation with something like

-command ("print {\"" + `stringArrayToString $array "\",\""` + "\"}")

NaughtyNathan
11-09-2012, 10:34 AM
on the subject of "scope", see also:

http://forums.cgsociety.org/showpost.php?p=6538895&postcount=17

th3m3nt4l
11-13-2012, 03:11 PM
Sorry I didn't get this up in the past 4 days- been rather busy Learning Linux and starting python but here is the main gui where I declare a string array in the gui, and use same array in a button command:


global proc myGuiName(){
if((`window -ex winName`) == true) deleteui -window winName;
if((`windowPref -ex winName`) == true) windowPref -remove winName;

string $exsits;
string $locNms[];


window -title "A Window Title"
-width ###
-height ###
-menuBar True
-rtf true
"winName"

menu -label "aLabel" -tearOff false;
menuItem -label "AnotherLabel"
-c "showHelp -a \"www.autodesk.com\""
-ann "testing if web connection works"

string $base = `formLayout -numberOfDivisions 100`;
string $dfltTxt = "select a mesh, click load"

textFieldButtonGrp -label ""
-cw 3 0 150 50
-buttonLabel "Bah"
-text $dfltTxt
-buttonCommand ("textFieldButtonGrp -e -text \`ls-sl -typ transform\` nmeSet;" + "string $text = `textFieldButtonGrp -q -text nmeSet`;" + "GuiModder($text);" + "textFieldButtonGrp -e -ed false \"nmeSet\";")
nmeSet;

formlayout -e
-attachForm nmeSet "left" ##
-attachForm nmeSet "top" ##

radioButtonGrp
-nrb 2
-la2 "Test1" "Test2"
-cw2 ## ##
-on1 "GuiMod(\"Test1\")"
-on2 "GuiMOd(\"Test2\")"
-ct2 "left" "right"
-ed false
"rBG1"

button -label "Do Something"
-command (String $choice = GuiMod(\"LocCreat\");" + "$locNms = LocatorCre8r($choice);")
-width ###
-height ##
-vis false
-enable false
-al "center"
"myButton";
formlayout -e
-attachForm myButton"left" ##
-attachForm myButton"top" ##



showWindow;

}


This isn't the whole code but it does show that I created an array and use it to get the return value of a procedure in a button command with out declaring what the array in the button command.

zaskar
11-14-2012, 09:02 AM
Besides those million bugs inside your code you provide us (or is it meant as pseudo code?) your approach of storing the selection inside a textField text wont work. `ls -sl` returns a string array, textField text requires a string. So if you really want to compress this whole getting and setting inside a single command string, you rather try this: "$selArray[] = `get selection list`; textField text = `stringArrayToString selArray " "`; $selArrayAsString = "{" + `stringArrayToString $selArray "",""` + "}"; GuiModder($selArrayAsString); textField -editable off"
This is pseudo code.

th3m3nt4l
11-14-2012, 11:30 AM
Besides those million bugs inside your code you provide us (or is it meant as pseudo code?) your approach of storing the selection inside a textField text wont work. `ls -sl` returns a string array, textField text requires a string. So if you really want to compress this whole getting and setting inside a single command string, you rather try this: "$selArray[] = `get selection list`; textField text = `stringArrayToString selArray " "`; $selArrayAsString = "{" + `stringArrayToString $selArray "",""` + "}"; GuiModder($selArrayAsString); textField -editable off"
This is pseudo code.

yes its pseudo code, but the only bugs that should have come up are the ones dealing with the places I didn't specify numbers and used #(pound sign) to represent some number went there. as for the area your talking about... it works for now.. I planned on changing later once I was more "finished" with the core of the script so that it matched other portions of the gui code- IE buttons only having 1-2 commands in them- preferably one.

While your input is helpful, I need to resolve my current issue first before I go back and implement changes in code I know works.

Nyro
11-14-2012, 11:43 AM
Second line:

if((`window -ex winName`) == true) deleteui -window winName;
no way that worked. Just saying ;)

th3m3nt4l
11-14-2012, 12:28 PM
Seriously, going to nit pick a simple typo? Back on topic- Why did declaring a var and using it in a button command work in one gui and not the next?

Nyro
11-14-2012, 06:00 PM
Well, to stray offtopic again (though this is an issue, trust me):

It is considered good etiquette if you're looking for help on programming issues to provide working examples of code and highlight which specific area (e.g. routine, logic or set of lines) are causing you trouble.
You pasted code that was littered with syntax errors. I already pointed out one in a previous post, there's also a bunch of missing semicolons and then you also leave it to whoever offers to try and help to fill out numbers for various control positions and size (why not do that yourself?).
This means that before I can even try to understand someone else's code and the issue at hand I first have to debug the whole thing for simple syntax- and, frankly, laziness-induced errors.

What is also mildly upsetting is that you then claim
... but the only bugs that should have come up are the ones dealing with the places I didn't specify numbers and used #(pound sign) to represent some number went there
This indicates that you didn't even try to run your own code...


Now, more on-topic, the biggest issue with your code is this:

-command (String $choice = GuiMod(\"LocCreat\");" + "$locNms = LocatorCre8r($choice);")

the -command flag expects a string argument, which in MEL and most other programming languages are distinguished by surrounding quotation marks: "**". So the first thing after the opening paranthesis has to be just that:
-command ("
Then you have to write "string", with a non-capital 's' or Maya doesn't know what you mean.
Finally, whenever you try to encapsulate strings within escaped strings within a string, it's always helpful to write out each individual, consecutive string into its own variable and the concatenate them inside one final string variable, which you then use as the -command flag's argument. That way you don't have keep track of "\"(\"escaped quotes\")"+"\"within strings\"\"".

Back to the off-topic issue: now I've spent all this time just going over the syntax errors that I don't have time left to solve your actual issue... you see why it actually is kind of a problem? ;)

th3m3nt4l
11-14-2012, 07:04 PM
EDIT: Had to rush my post do to a mini Earth Quake, its ended shortly after.

Well, to stray offtopic again (though this is an issue, trust me):

It is considered good etiquette if you're looking for help on programming issues to provide working examples of code and highlight which specific area (e.g. routine, logic or set of lines) are causing you trouble.
You pasted code that was littered with syntax errors. I already pointed out one in a previous post, there's also a bunch of missing semicolons and then you also leave it to whoever offers to try and help to fill out numbers for various control positions and size (why not do that yourself?).

snip

the -command flag expects a string argument, which in MEL and most other programming languages are distinguished by surrounding quotation marks: "**". So the first thing after the opening paranthesis has to be just that:
-command ("
Then you have to write "string", with a non-capital 's' or Maya doesn't know what you mean.
Finally, whenever you try to encapsulate strings within escaped strings within a string, it's always helpful to write out each individual, consecutive string into its own variable and the concatenate them inside one final string variable, which you then use as the -command flag's argument. That way you don't have keep track of "\"(\"escaped quotes\")"+"\"within strings\"\"".

Back to the off-topic issue: now I've spent all this time just going over the syntax errors that I don't have time left to solve your actual issue... you see why it actually is kind of a problem?

First off I followed "good etiquette" in my OP where I stated:
1. The problem I'm having
2. Properly Formatted Code that could be tested by any other.

Which IS where the problem is.

Second of all, you are attacking the code I'm merely providing as proof that Logically speaking, what I did in my OP should work, but isn't.

Following that, it was already agreed that what was provided was just pseudo code- aka not 100% accurate, but gets the point across.

Lastly, I don't appreciate you being a condescending ass, especially talking down to me over quite obvious typos, especially over the code that was merely being provided as a proof of a working concept. Plus, the time you spent complaining on said pseudo Code, you could have actually been more helpful with the issue at hand.

The issues at hand IS that this system works


global proc string[] myMainWindow(string $array[]){

string $locNms[];

window creation crap

button -label "Blah 1"
-command ("string $choice = GuiMod(\"LocCreat\");" + "$locNms =LocatorCre8r($choice);")
-w 115
-h 20
-vis true
-enable true
"tstBttn"

layout for button crap

showWindow;
}


Which used a pre declared, empty array.The array $locNms gets used more times through out the Main Gui code with out any issues, and is the very array I pass into the 2nd gui.

while This


global proc string[] mySecondwindow(string $array[]){

string $temp[];

window creation crap;

button -label "Blah 2"
-command "print $array" //OR OR OR "$temp = anotherProcedure($array);"
-w 115
-h 20
-vis true
-enable true
"tstBttn"

layout for button crap;

showWindow;
}


Produces an error. I also tried:

global proc string[] mySecondwindow(string $array[]){

string $Temp[];

window creation crap

button -label "Blah 2"
-command "$temp = $array" + "$temp = anotherProc($temp)"


AnotherProc is set to work just like LocatorCre8r with a return of string $array[].

CGTalk Moderation
11-14-2012, 07:04 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.