PDA

View Full Version : need help with capturing from a floatfield


Freshfromthegrave
02-03-2011, 08:12 AM
hey guys, i'm very new to mel scripting and at the moment i'm trying to write a script that randomly rotates/translates/scales objects within limits that the user sets. what i'm struggling with at the moment is trying to capture the value of a floatfield to use in a variable but everything i do results in some kind of error =\ here's the script so far minus some stuff so it's shorter:

//Delete and define window
if ( `window -exists SMO_RandomizeXFormWindow` )
{
deleteUI SMO_RandomizeXFormWindow;
}
window -t "SMO_RandomizeXForm" SMO_RandomizeXFormWindow;

//Define Layout
formLayout -numberOfDivisions 100 SMO_RandomizeXForm_Form;
text "Translate";
floatField -w 50 TranslateXMin;
floatField -w 50 TranslateXMax;
floatField -w 50 TranslateYMin;
floatField -w 50 TranslateYMax;
floatField -w 50 TranslateZMin;
floatField -w 50 TranslateZMax;
button -label "OK" -command "executeSMO_RandomizeXForm" OKButton;
formLayout -edit

SMO_RandomizeXForm_Form;

showWindow SMO_RandomizeXFormWindow;

proc executeSMO_RandomizeXForm()
{
//Variables
float $InputTransXMin = `TranslateXMin -q -v`;
float $InputTransXMax = 5.2;
float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);

float $InputTransYMin = 1.1;
float $InputTransYMax = 10.1;
float $TransYRandom = rand ($InputTransYMin,$InputTransYMax);

float $InputTransZMin = 1.1;
float $InputTransZMax = 10.1;
float $TransZRandom = rand ($InputTransZMin,$InputTransZMax);

//Execute transform
xform -t $TransXRandom $TransYRandom $TransZRandom;
}

it works perfectly if i use values in the variables and the UI comes up and it works when i press OK. but as soon as i try and capture the value of one of the fields it throws up (in this case) a // Error: Cannot find procedure "TranslateXMin". i'm confused as to why it's looking for a procedure called TranslateXMin and not the value of the floatfield. is there a way i'm supposed to define it a floatfield? i've tried using: float $InputTransXMin = `floatField -q -v -w 50 TranslateXMin`; when creating the floatfield but then i get
// Error: float $InputTransXMin = `floatField -q -v -w 50 TranslateXMin`;
//
// Error: Invalid redeclaration of variable "$InputTransXMin" as a different type. //
// Error: float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);
//
// Error: "$InputTransXMin" is an undeclared variable. //

anybody got any suggestions as to what i'm doing wrong? i've been sitting with this since yesterday and i'm not getting anywhere xD thanks in advance guys :)

haggi
02-03-2011, 08:30 AM
There is a sign "#" here which can be used to do code formatting, helps to create readable code.

float $InputTransXMin = `floatField -q -v TranslateXMin`;


This is exact the way how to query a value from a field. Try it again, it should work now.
But it helps a lot if you encapsulate your window creation into a function like this:

global proc executeSMO_RandomizeXForm()
{
...
}
global proc createMyWindow()
{
...
...
}

createMyWindow();


This way all variables are local in the procedure and you will have no problems with redeclaration.

Freshfromthegrave
02-03-2011, 09:08 AM
There is a sign "#" here which can be used to do code formatting, helps to create readable code.

float $InputTransXMin = `floatField -q -v TranslateXMin`;


This is exact the way how to query a value from a field. Try it again, it should work now.
But it helps a lot if you encapsulate your window creation into a function like this:

global proc executeSMO_RandomizeXForm()
{
...
}
global proc createMyWindow()
{
...
...
}

createMyWindow();


This way all variables are local in the procedure and you will have no problems with redeclaration.

hey haggi thanks for the reply man :) will use the # wrap from now on ;)

i'm still having issues with the script though. so in it's entirety it looks like this now:

global proc SMO_RandomizeXFormWindowPROC()
{
//Delete and define window
if ( `window -exists SMO_RandomizeXFormWindow` )
{
deleteUI SMO_RandomizeXFormWindow;
}
window -t "SMO_RandomizeXForm" SMO_RandomizeXFormWindow;

//Define Layout
formLayout -numberOfDivisions 100 SMO_RandomizeXForm_Form;
text "Translate";
float $InputTransXMin = `floatField -q -v -w 50 TranslateXMin`;
floatField -w 50 TranslateXMax;
floatField -w 50 TranslateYMin;
floatField -w 50 TranslateYMax;
floatField -w 50 TranslateZMin;
floatField -w 50 TranslateZMax;
button -label "OK" -command "executeSMO_RandomizeXForm" OKButton;
formLayout -edit
-af Translate "top" 25
-af Translate "left" 10
-af TranslateXMin "top" 20
-af TranslateXMin "left" 75
-af TranslateXMax "top" 20
-af TranslateXMax "left" 135
-af TranslateYMin "top" 20
-af TranslateYMin "left" 225
-af TranslateYMax "top" 20
-af TranslateYMax "left" 285
-af TranslateZMin "top" 20
-af TranslateZMin "left" 375
-af TranslateZMax "top" 20
-af TranslateZMax "left" 435
-af OKButton "top" 50
-af OKButton "left" 250
SMO_RandomizeXForm_Form;

showWindow SMO_RandomizeXFormWindow;
}

global proc executeSMO_RandomizeXForm()
{
//Variables
//float $InputTransXMin = 7.2;
float $InputTransXMax = 5.2;
float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);

float $InputTransYMin = 1.1;
float $InputTransYMax = 10.1;
float $TransYRandom = rand ($InputTransYMin,$InputTransYMax);

float $InputTransZMin = 1.1;
float $InputTransZMax = 10.1;
float $TransZRandom = rand ($InputTransZMin,$InputTransZMax);

//Execute transform
xform -t $TransXRandom $TransYRandom $TransZRandom;
}

but gives me the following error:

// Error: float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);
//
// Error: "$InputTransXMin" is an undeclared variable. //

looks like it's taking issue with the variable only further down and it's happy enough with

float $InputTransXMin = `floatField -q -v -w 50 TranslateXMin`;

but then doesn't like this anymore

float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);

but i'm still not sure why =\

haggi
02-03-2011, 10:53 AM
//float $InputTransXMin = 7.2;
float $InputTransXMax = 5.2;
float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);

This cannot work because you have no variable called $InputTransXMin.
There is a comment before the definition.

Freshfromthegrave
02-03-2011, 11:02 AM
//float $InputTransXMin = 7.2;
float $InputTransXMax = 5.2;
float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);

This cannot work because you have no variable called $InputTransXMin.
There is a comment before the definition.

no no i put that there on purpose, that was just for testing that it worked with values. now i'm trying to replace that with

float $InputTransXMin = `floatField -q -v -w 50 TranslateXMin`;

so i can capture the input value from the floatfield rather than a static value.

haggi
02-03-2011, 12:53 PM
So whats now happening? Should work.

Freshfromthegrave
02-03-2011, 01:07 PM
still a no go i'm afraid xD

the script works when i don't try to capture the value from the floatfield and i take those comment // away from that line. the trouble starts as soon as i change

floatField -w 50 TranslateXMin;

to

float $InputTransXMin = `floatField -q -v -w 50 TranslateXMin`;

and remove

float $InputTransXMin = 7.2;

i'm not sure if undeclared variable means that it doesn't recognise the variable at all or if means it's not recognising it as a float. either way i'm not sure why it's giving me errors =\

thanks for sticking around haggi :)

NaughtyNathan
02-03-2011, 02:50 PM
Hi Steven, I think your problems are related to scope. Have a read of this post and see it that helps:

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

:nathaN

Freshfromthegrave
02-04-2011, 08:28 AM
Hi Steven, I think your problems are related to scope. Have a read of this post and see it that helps:

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

:nathaN

hey nathan thanks so much man :) i think you're right it is to with scope (something i didn't know about!). it's still not working at the moment but i think i'm getting somewhere.

so if i take all of the variable out of the procedure and place them after the window commands (so the proc only runs the xform) it still doesn't work. i'm guessing anything inside the proc can't see variables outside it?

if i make them all global it also doesn't work but it seems that's generally not a good idea anyways.

another stange thing is that if i get rid of the ok button and the proc entirely it will only work if i DON'T define the variable as float. so i just use $InputTransXMin = 7.2; it's fine but if i use float float $InputTransXMin = 7.2; it shouts invalid redeclaration of variable as a different type. (i'm still not trying to capture from the interface just yet) so i tried removing all the floats in front of the variables and putting the proc back over the xform but that was still a no go.

so i guess my question now is, is the only reason this is not working because the proc cannot see the variables outside it? if so how do i solve that?

//Delete and define window
if ( `window -exists SMO_RandomizeXFormWindow` )
{
deleteUI SMO_RandomizeXFormWindow;
}
window -t "SMO_RandomizeXForm" SMO_RandomizeXFormWindow;

//Define Layout
formLayout -numberOfDivisions 100 SMO_RandomizeXForm_Form;
text "Translate";
float $InputTransXMin = `floatField -q -v -w 50 TranslateXMin`;
floatField -w 50 TranslateXMax;
floatField -w 50 TranslateYMin;
floatField -w 50 TranslateYMax;
floatField -w 50 TranslateZMin;
floatField -w 50 TranslateZMax;
button -label "OK" -command "executeSMO_RandomizeXForm" OKButton;
formLayout -edit
-af Translate "top" 25
-af Translate "left" 10
-af TranslateXMin "top" 20
-af TranslateXMin "left" 75
-af TranslateXMax "top" 20
-af TranslateXMax "left" 135
-af TranslateYMin "top" 20
-af TranslateYMin "left" 225
-af TranslateYMax "top" 20
-af TranslateYMax "left" 285
-af TranslateZMin "top" 20
-af TranslateZMin "left" 375
-af TranslateZMax "top" 20
-af TranslateZMax "left" 435
-af OKButton "top" 50
-af OKButton "left" 250
SMO_RandomizeXForm_Form;

showWindow SMO_RandomizeXFormWindow;

//Variables
float $InputTransXMax = 5.2;
float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);

float $InputTransYMin = 1.1;
float $InputTransYMax = 10.1;
float $TransYRandom = rand ($InputTransYMin,$InputTransYMax);

float $InputTransZMin = 1.1;
float $InputTransZMax = 10.1;
float $TransZRandom = rand ($InputTransZMin,$InputTransZMax);

proc executeSMO_RandomizeXForm()
{
//Execute transform
xform -t $TransXRandom $TransYRandom $TransZRandom;
}

this gives me Error: Invalid redeclaration of variable "$InputTransXMin" as a different type on all the float variables and Error: "$TransYRandom" is an undeclared variable for the xform

but thanks nathan at least now i know why it's not working :) just gotta figure out how to fix it now ;)

Freshfromthegrave
02-04-2011, 08:34 AM
curious, once again if i remove the floats before the variables that clears up those errors :) so now:

//Delete and define window
if ( `window -exists SMO_RandomizeXFormWindow` )
{
deleteUI SMO_RandomizeXFormWindow;
}
window -t "SMO_RandomizeXForm" SMO_RandomizeXFormWindow;

//Define Layout
formLayout -numberOfDivisions 100 SMO_RandomizeXForm_Form;
text "Translate";
$InputTransXMin = `floatField -q -v -w 50 TranslateXMin`;
floatField -w 50 TranslateXMax;
floatField -w 50 TranslateYMin;
floatField -w 50 TranslateYMax;
floatField -w 50 TranslateZMin;
floatField -w 50 TranslateZMax;
button -label "OK" -command "executeSMO_RandomizeXForm" OKButton;
formLayout -edit
-af Translate "top" 25
-af Translate "left" 10
-af TranslateXMin "top" 20
-af TranslateXMin "left" 75
-af TranslateXMax "top" 20
-af TranslateXMax "left" 135
-af TranslateYMin "top" 20
-af TranslateYMin "left" 225
-af TranslateYMax "top" 20
-af TranslateYMax "left" 285
-af TranslateZMin "top" 20
-af TranslateZMin "left" 375
-af TranslateZMax "top" 20
-af TranslateZMax "left" 435
-af OKButton "top" 50
-af OKButton "left" 250
SMO_RandomizeXForm_Form;

showWindow SMO_RandomizeXFormWindow;

//Variables
$InputTransXMax = 5.2;
$TransXRandom = rand ($InputTransXMin,$InputTransXMax);

$InputTransYMin = 1.1;
$InputTransYMax = 10.1;
$TransYRandom = rand ($InputTransYMin,$InputTransYMax);

$InputTransZMin = 1.1;
$InputTransZMax = 10.1;
$TransZRandom = rand ($InputTransZMin,$InputTransZMax);

proc executeSMO_RandomizeXForm()
{
//Execute transform
xform -t $TransXRandom $TransYRandom $TransZRandom;
}

produces:

// Error: xform -t $TransXRandom $TransYRandom $TransZRandom;
//
// Error: "$TransXRandom" is an undeclared variable. //
// Error: xform -t $TransXRandom $TransYRandom $TransZRandom;
//
// Error: "$TransYRandom" is an undeclared variable. //
// Error: xform -t $TransXRandom $TransYRandom $TransZRandom;
//
// Error: "$TransZRandom" is an undeclared variable. //

it's only taking issue with the xform in the proc now. yay getting somewhere :D

NaughtyNathan
02-04-2011, 09:48 AM
Hi Steven, you have to understand that you cannot really write MEL code and functions effectively by inputting directly into the script editor like this. Anything you input outside a scope is effectively global HOWEVER, any variables redeclared inside a scope are a) unique instances and b) never considered global unless explicitely declared as such.

consider:

proc executeSMO_RandomizeXForm()
{ // SCOPE STARTS
xform -t $TransXRandom $TransYRandom $TransZRandom;
} // SCOPE ENDS
where are those variables defined? nowhere within the scope of that procedure, and you've not told the procedure that they are global variables! (which you should really avoid anyway, and are certainly unnecessary in this example).

//Delete and define window
if ( `window -exists SMO_RandomizeXFormWindow` )
{
deleteUI SMO_RandomizeXFormWindow;
}
window -t "SMO_RandomizeXForm" SMO_RandomizeXFormWindow;
//Define Layout
formLayout -numberOfDivisions 100 SMO_RandomizeXForm_Form;
text "Translate";
// you CANNOT DO THIS HERE! the float field: "TranslateXMin" DOES NOT EXIST AT THIS POINT!
// $InputTransXMin = `floatField -q -v -w 50 TranslateXMin`;
floatField -w 50 TranslateXMin;
floatField -w 50 TranslateXMax;
floatField -w 50 TranslateYMin;
floatField -w 50 TranslateYMax;
floatField -w 50 TranslateZMin;
floatField -w 50 TranslateZMax;
button -label "OK" -command "executeSMO_RandomizeXForm" OKButton;
formLayout -edit
-af Translate "top" 25
-af Translate "left" 10
-af TranslateXMin "top" 20
-af TranslateXMin "left" 75
-af TranslateXMax "top" 20
-af TranslateXMax "left" 135
-af TranslateYMin "top" 20
-af TranslateYMin "left" 225
-af TranslateYMax "top" 20
-af TranslateYMax "left" 285
-af TranslateZMin "top" 20
-af TranslateZMin "left" 375
-af TranslateZMax "top" 20
-af TranslateZMax "left" 435
-af OKButton "top" 50
-af OKButton "left" 250
SMO_RandomizeXForm_Form;
showWindow SMO_RandomizeXFormWindow;
proc executeSMO_RandomizeXForm()
{
//Variables - ALWAYS TYPE-CAST your variables:
float $InputTransXMin = `floatField -q -v TranslateXMin`;
float $InputTransXMax = `floatField -q -v TranslateXMax`;
float $InputTransYMin = `floatField -q -v TranslateYMin`;
float $InputTransYMax = `floatField -q -v TranslateYMax`;
float $InputTransZMin = `floatField -q -v TranslateZMin`;
float $InputTransZMax = `floatField -q -v TranslateZMax`;
float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);
float $TransYRandom = rand ($InputTransYMin,$InputTransYMax);
float $TransZRandom = rand ($InputTransZMin,$InputTransZMax);
//Execute transform
xform -t $TransXRandom $TransYRandom $TransZRandom;
}

:nathaN

Freshfromthegrave
02-04-2011, 10:20 AM
Hi Steven, you have to understand that you cannot really write MEL code and functions effectively by inputting directly into the script editor like this. Anything you input outside a scope is effectively global HOWEVER, any variables redeclared inside a scope are a) unique instances and b) never considered global unless explicitely declared as such.

consider:

proc executeSMO_RandomizeXForm()
{ // SCOPE STARTS
xform -t $TransXRandom $TransYRandom $TransZRandom;
} // SCOPE ENDS
where are those variables defined? nowhere within the scope of that procedure, and you've not told the procedure that they are global variables! (which you should really avoid anyway, and are certainly unnecessary in this example).

//Delete and define window
if ( `window -exists SMO_RandomizeXFormWindow` )
{
deleteUI SMO_RandomizeXFormWindow;
}
window -t "SMO_RandomizeXForm" SMO_RandomizeXFormWindow;
//Define Layout
formLayout -numberOfDivisions 100 SMO_RandomizeXForm_Form;
text "Translate";
// you CANNOT DO THIS HERE! the float field: "TranslateXMin" DOES NOT EXIST AT THIS POINT!
// $InputTransXMin = `floatField -q -v -w 50 TranslateXMin`;
floatField -w 50 TranslateXMin;
floatField -w 50 TranslateXMax;
floatField -w 50 TranslateYMin;
floatField -w 50 TranslateYMax;
floatField -w 50 TranslateZMin;
floatField -w 50 TranslateZMax;
button -label "OK" -command "executeSMO_RandomizeXForm" OKButton;
formLayout -edit
-af Translate "top" 25
-af Translate "left" 10
-af TranslateXMin "top" 20
-af TranslateXMin "left" 75
-af TranslateXMax "top" 20
-af TranslateXMax "left" 135
-af TranslateYMin "top" 20
-af TranslateYMin "left" 225
-af TranslateYMax "top" 20
-af TranslateYMax "left" 285
-af TranslateZMin "top" 20
-af TranslateZMin "left" 375
-af TranslateZMax "top" 20
-af TranslateZMax "left" 435
-af OKButton "top" 50
-af OKButton "left" 250
SMO_RandomizeXForm_Form;
showWindow SMO_RandomizeXFormWindow;
proc executeSMO_RandomizeXForm()
{
//Variables - ALWAYS TYPE-CAST your variables:
float $InputTransXMin = `floatField -q -v TranslateXMin`;
float $InputTransXMax = `floatField -q -v TranslateXMax`;
float $InputTransYMin = `floatField -q -v TranslateYMin`;
float $InputTransYMax = `floatField -q -v TranslateYMax`;
float $InputTransZMin = `floatField -q -v TranslateZMin`;
float $InputTransZMax = `floatField -q -v TranslateZMax`;
float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);
float $TransYRandom = rand ($InputTransYMin,$InputTransYMax);
float $TransZRandom = rand ($InputTransZMin,$InputTransZMax);
//Execute transform
xform -t $TransXRandom $TransYRandom $TransZRandom;
}

:nathaN

ah nathan thanks so much man :) I think my main problem was not realising that float $InputTransXMax = `floatField -q -v TranslateXMax`; needed to placed after the window inside the procedure. i think i must've misunderstood the example i was trying to learn from and thought that the line created the floatField while at the same time defined the variable (which i suppose is silly lol).

i think i've pretty much dove in the deep end with scripting so i'll probably have more hitches along the way xD

but thanks so much nathan i really appreciate the effort! :bowdown: :beer:

*edit i think i also misunderstood how the query flag works! so when you use -q it stops floatField from creating one and instead queries the one that already exists! well it makes sense now ;)

CGTalk Moderation
02-04-2011, 10:20 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.