PDA

View Full Version : floatFields


Xinos
10-26-2010, 12:15 AM
I have been looking and looking for information on floatFields, but I just can't figure them out. Mayas Technical Documentation tells me what flags it uses, but nothing on how to actually use it.

I tried to follow this (http://wiki.bk.tudelft.nl/toi-pedia/MEL_-_Interfaces#Reading_Controls) tutorial but doing the examples just gives me this:// Error: Object 'MyFloatField' not found. // I suppose it's some kind of incomplete example that's not meant to be copied, but it just leaves me with more questions than answers.

And when I try to define a variable as the field, I get a nice warning message.
float $value = `floatField -enable on -value 0.1`;

// Warning: line 3: Converting string "window6|columnLayout47|floatField59"
to a float value of 0. //
What am i not getting..?

/* Side question: I have made some global variables but when I try to access them from other procedures Maya still sees them as undefined. Why is that? All my procedures are global too. */

nessus
10-26-2010, 12:24 AM
When you create any UI element in Maya, controls/layouts, the return value is always a string which is the name of the UI element. So your code should be like this:

string $floatFieldName = `floatField -enable on -value 0.1`;


However if you just execute code alone you would still get an error. Basically a control cannot be "alive" on its own. The basic Maya UI element hierarchy:
window -> layout -> control

This is the sample from Maya document, you can paste it in script editor and run it:
string $window = `window`;
columnLayout;
floatField;
floatField -editable false;
floatField -minValue -10 -maxValue 10 -value 0;
floatField -minValue 0 -maxValue 1 -precision 2;
floatField -minValue -1 -maxValue 1 -precision 4 -step .01;
showWindow $window;

ewerybody
10-26-2010, 02:21 PM
additionally to what nessus said:

If you want to acces the values of your control at an arbitrary time and you can be sure that there is only ONE control of this type you better name your control directly instead of remembering the maya created name. I always do that. Its better to try to avoid global variables everywhere you can.

window;
columnLayout;
floatField myFloatField;
button -c "printValueOfmyFloatField";
showWindow;

global proc printValueOfmyFloatField()
{
float $value = `floatField -q -v myFloatField`;
print("$value: \"" + $value + "\"\n");
}

Xinos
10-26-2010, 08:09 PM
Thank you both, I really appreciate it.
The last example works fine by itself, however, trying to mimic it in my script is still giving me problems. Pressing the button prints 0, regardless of what I put into the field. So the problem might lie elsewere?

If it helps here is how my script is set up.
global proc UVD(){
////////////////////////////////////////////////////////
//A whole bunch of UI layout stuff and other functions//
////////////////////////////////////////////////////////
floatField ratioField;
float $ratioValue = `floatField -q -value ratioField`;
//button -label "Set Ratio" -command "UVDsetRatio $ratioValue";
button -c "print $ratioValue";
setParent ..;
setParent ..;
showWindow;
}
UVD();

Adding an -enable flag to the float field here gives me // Error: Invalid object or value: 1//

Also, doing it nessus' way also just prints 0 when I press the button.

string $ratioValue = `floatField -w 108 -h 20 -enable on -value 0.1`;
button -c "print $ratioValue";


MEL is so unkind :-\

ewerybody
10-26-2010, 10:36 PM
He Erik!
Nahh Mel is ok. But Python is cooler. But the Maya interface is crappy as well :D so... whatever

I think when we write your code and analyse it line by line you might recognise whats wrong:

// ok, you create the floatField named "ratioField"
floatField ratioField;
// now the floatField is called already to put the value in a variable
float $ratioValue = `floatField -q -value ratioField`;
//now you assign a command string to the button to print this var WHEN PRESSED!
button -c "print $ratioValue";

Got it? ok, first you get the value right after you created the control. Of course its 0 if you didn't set it to something else (like -v 5). Second: When you press the button the code where you created it was already executed long before. Its over. That means if you press the button it has nothing to do with the execution before! So the variable is not available anymore. We hereby speak of another "scope". All commands on controls use global scope.

Of course one can fix this:

button -c ("print " + $ratioValue);

Works first of all but you know: It would just print the value from control creation time. To have the fresh value you need to query the control freshly as well:

button -c ("print (`floatField -q -value ratioField`)";

In your case: just don't query the floatField when creating it but in your UVDsetRatio procedure.

bendingiscool
10-26-2010, 11:05 PM
haven't used MEL in a while, but I believe if you declare a global variable outside a proc, you have to reference it inside the proc like below...


global string $blah = "some word";

proc yay(){
global string $blah;

do whatever...
}

cheers,
Chris

Xinos
10-26-2010, 11:27 PM
Yes, finally got it now. Thanks a bunch! :)

Kind of a strange way of accessing a control.. To me it looks we're trying to create a new floatField inside the button (or unfold, as it is) :p. But I guess it makes sense.

bendingiscool: Cool, got that working now too. Thanks!

ewerybody
10-27-2010, 10:09 AM
why strange? A lot of Mel commands can be queried via -q or -query. So you write the command:

floatField -q -value ratioField;

thats not creating or editing the control. Its just querying it! But this would just give you the result when in the creepy Maya Script Editor.
So if you embedd it in another command like print you can do this as well:

print (`floatField -q -value ratioField`)

this will just print the returns from the floatField query.
And you can even put this into a button command string:

button -c ("print (`floatField -q -value ratioField`)");

But this is just an example how things could be done.
The control name ratioField can be accessed globally as well. So you don't need extra global variables. Just put the query in your procedure.:window;
columnLayout;
floatField ratioField;
button -c ("myQueryRatioFieldProcedure");
showWindow;

global proc myQueryRatioFieldProcedure()
{
float $value = `floatField -q -v ratioField`;
print("$value: \"" + $value + "\"\n");
}

CGTalk Moderation
10-27-2010, 10:09 AM
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.