View Full Version : How to pass arguments when button is pressed


xcomb
10 October 2007, 10:09 AM
Hi i am kinda lost on how to pass my string argument with float argumets?
string argument is $selected
int $numberObjs
and all the rest are floats arguments

string $selectedObject = $selected[0];

float $minX = `floatFieldGrp -q -value1 xMinField`;
float $maxX = `floatFieldGrp -q -value1 xMaxField`;
float $minY = `floatFieldGrp -q -value1 yMinField`;
float $maxY = `floatFieldGrp -q -value1 yMaxField`;
float $minZ = `floatFieldGrp -q -value1 zMinField`;
float $maxZ = `floatFieldGrp -q -value1 zMaxField`;

button -l "Randomize All" -c "randomizeObjects($selectedObject, $numberObjs, $minX, $maxX, $minY, $maxY, $minZ, $maxZ)";

I was able only to get the name printed but the rest code didn't seem to accomplish the job, to randomize objects in space.
I have also checked that i get all the values queried from floatFieldGRP
if i manually specify the arguments in function, everything works.
spreadObjects("pCube1", 10, 4, 3, 4, 3, 4, 3);
pCube1 duplicated and randomly Placed 10 times.

I have also tryed to manually query those values then executing button and it worked, not sure why that happens.

global proc spreadObjects(
string $object,
int $duplicateNumber,
float $Xmin,
float $Xmax,
float $Ymin,
float $Ymax,
float $Zmin,
float $Zmax
){
for($n = 0; $n < $duplicateNumber; $n++){
float $randomX = `rand $Xmin $Xmax`;
float $randomY = `rand $Ymin $Ymax`;
float $randomZ = `rand $Zmin $Zmax`;

string $duplicatedObject[] = `duplicate $object`;
move $randomX $randomY $randomZ $duplicatedObject;
}
print($object + " duplicated and randomly Placed " + $duplicateNumber + " times.\n");
}

########################################################
It works perfectly this way, which is not really i wanned, i wanned to pass those queired values as arguments.

global proc spreadObjects(string $object){
float $minX = `floatFieldGrp -q -value1 xMinField`;
float $maxX = `floatFieldGrp -q -value1 xMaxField`;
float $minY = `floatFieldGrp -q -value1 yMinField`;
float $maxY = `floatFieldGrp -q -value1 yMaxField`;
float $minZ = `floatFieldGrp -q -value1 zMinField`;
float $maxZ = `floatFieldGrp -q -value1 zMaxField`;
int $duplicateNumber = `intSliderGrp -q -v mainIntSlider`;

for($n = 0; $n < $duplicateNumber; $n++){
float $randomX = `rand $minX $maxX`;
float $randomY = `rand $minY $maxY`;
float $randomZ = `rand $minZ $maxZ`;

string $duplicatedObject[] = `duplicate $object`;
move $randomX $randomY $randomZ $duplicatedObject;
}
print($object + " duplicated and randomly Placed " + $duplicateNumber + " times.\n");
}

JackSMillenium
10 October 2007, 12:16 AM
What you're doing is creating a button that, when clicked, executes the following code:
"randomizeObjects($selectedObject, $numberObjs, $minX, $maxX, $minY, $maxY, $minZ, $maxZ)"

It's not going to "set" any of the variables to anything new: if the those variables ($selectedObject, $maxX, etc.) HAPPEN to be declared at the global MEL scope, then it will pass to the function whatever value they are currently set to. I'm guessing what you want is the current value in the fields to be passed in, so you really want the button to call all the "floatFieldGrp -q" commands when it's clicked. Here's what that would look like:

button -l "Randomize All" -c "randomizeObjects($selectedObject, $numberObjs, `floatFieldGrp -q -value1 xMinField`,`floatFieldGrp -q -value1 xMaxField`, `floatFieldGrp -q -value1 yMinField`, `floatFieldGrp -q -value1 yMaxField`, `floatFieldGrp -q -value1 zMaxField`, `floatFieldGrp -q -value1 zMaxField`)";

Now, you still have a problem with the $selectedObject variable... if I were you, I'd change the function to take as a first argument an ARRAY of selected objects like so:

global proc spreadObjects(string $objects[], int $duplicateNumber, ...)
{
for ($object in $objects)
{
// put old code here
}
}

Then the button command is:
button -l "Randomize All" -c "randomizeObjects(`ls -sl`, $numberObjs, `floatFieldGrp -q -value1 xMinField`,`floatFieldGrp -q -value1 xMaxField`, `floatFieldGrp -q -value1 yMinField`, `floatFieldGrp -q -value1 yMaxField`, `floatFieldGrp -q -value1 zMaxField`, `floatFieldGrp -q -value1 zMaxField`)";


Finally, where are you getting the $numberObjs from ? Also from a field in a UI ? In that case you'd have something like:
button -l "Randomize All" -c "randomizeObjects(`ls -sl`, `intField -q -v numDuplicatesFieldName`, `floatFieldGrp -q -value1 xMinField`,`floatFieldGrp -q -value1 xMaxField`, `floatFieldGrp -q -value1 yMinField`, `floatFieldGrp -q -value1 yMaxField`, `floatFieldGrp -q -value1 zMaxField`, `floatFieldGrp -q -value1 zMaxField`)";

Hope that helps a bit...

xcomb
10 October 2007, 04:50 AM
I c how you did it, i've tryed that once, but i maid mistakes.
Thank you it works!

Here is full script if someone is interested to learn some basic mel!

if(`window -exists mainWindow`)
deleteUI mainWindow;
window -width 200 -height 300 -title "Duplicate Objects" mainWindow;

columnLayout -adj 0 mainCol;

string $selected[] = `ls -sl`;
text " ";

textFieldGrp -adjustableColumn 1
-label "Object Name: "
-text $selected
-editable false
mainTextFieldGrp;
text " ";
intSliderGrp -label "Number of Objects" -field true -adj 1
-minValue 0 -maxValue 1000
-fieldMinValue 0 -fieldMaxValue 1000
-value 0
mainIntSlider;

floatFieldGrp -numberOfFields 1 -adj 1
-label "Xmin "
-precision 3
-value1 0
xMinField;
floatFieldGrp -numberOfFields 1 -adj 1
-label "Xmax "
-precision 3
-value1 0
xMaxField;
separator -w 500 -height 10 -style "in";
floatFieldGrp -numberOfFields 1 -adj 1
-label "Ymin "
-precision 3
-value1 0
yMinField;
floatFieldGrp -numberOfFields 1 -adj 1
-label "Ymax "
-precision 3
-value1 0
yMaxField;
separator -w 500 -height 10 -style "in";
floatFieldGrp -numberOfFields 1 -adj 1
-label "Zmin "
-precision 3
-value1 0
zMinField;
floatFieldGrp -numberOfFields 1 -adj 1
-label "Zmax "
-precision 3
-value1 0
zMaxField;
separator -w 500 -height 10 -style "in";

string $selectedObject = $selected[0];


button -l "Randomize All" -c "spreadObjects($selectedObject, `intSliderGrp -q -v mainIntSlider`, `floatFieldGrp -q -value1 xMinField`,`floatFieldGrp -q -value1 xMaxField`, `floatFieldGrp -q -value1 yMinField`, `floatFieldGrp -q -value1 yMaxField`, `floatFieldGrp -q -value1 zMinField`, `floatFieldGrp -q -value1 zMaxField`)";
button -l "resetState All" -c "resetState()";
showWindow mainWindow;

global proc spreadObjects(string $object, int $duplicateNumber, float $minX, float $maxX, float $minY, float $maxY, float $minZ, float $maxZ){
for($n = 0; $n < $duplicateNumber; $n++){
float $randomX = `rand $minX $maxX`;
float $randomY = `rand $minY $maxY`;
float $randomZ = `rand $minZ $maxZ`;

string $duplicatedObject[] = `duplicate $object`;
move $randomX $randomY $randomZ $duplicatedObject;
}
print($object + " duplicated and randomly Placed " + $duplicateNumber + " times.\n");
}

global proc resetState()
{
floatFieldGrp
-e
-value1 0
xMinField;
floatFieldGrp
-e
-value1 0
xMaxField;
floatFieldGrp
-e
-value1 0
yMinField;
floatFieldGrp
-e
-value1 0
yMaxField;
floatFieldGrp
-e
-value1 0
zMinField;
floatFieldGrp
-e
-value1 0
xMaxField;
floatFieldGrp
-e
-value1 0
zMinField;
floatFieldGrp
-e
-value1 0
zMaxField;
intSliderGrp -e
-v 0
mainIntSlider;
}

CGTalk Moderation
10 October 2007, 04:50 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.


1