View Full Version : Lets debug this little Script ( realy simple )
ozioz 11-13-2009, 07:20 AM Hi Guys;
I think I have some problems with collecting user inputs with MEL scripting.
I wrote a little script that allows the user to create a nurbs sphere by validating the inputs.
( name, sweep angle, radius )
UI works correctly. Unfortunately, when I try to create the sphere, Maya uses the defult settings for name, radius, and sweep angle flags.
Buy the way, If I highlight the $r, $s and $n strings and exacute them, they work.
I want them work with UI whenever I change values .
I know it is a simple script but It totally explains my problem.
If you can explain what you do to get this worked, I'll be very happy.
Here is the script:
//************************************************************//
//************************************************************//
//************************************************************//
// UI starts here//
if (`window -exists $example_window`)
deleteUI $example_window;
string $example_window = `window -t "Hello"`;
string $example_form = `formLayout -numberOfDivisions 100`;
string $radius_grp = `floatSliderGrp -label "Radius:"
-field true
-minValue 0.0
-maxValue 10.0
-value 1 -adjustableColumn 2`;
string $sweep_grp =`floatSliderGrp -label "Sweep Angle:"
-field true
-minValue 0.0
-maxValue 360.0
-value 360.0 -adj 2`;
string $name_grp = `textFieldGrp -label "Name:" -adj 2`;
string $create_button =`button -label "Cretae" -c "sphere -name $n -radius $r -endSweep $s"`;
string $cancel_button =`button -label "Cancel" -c "deleteUI $example_window"`;
formLayout -edit
-attachPosition $radius_grp "top" 0 20
-attachForm $radius_grp "left" 10
-attachNone $radius_grp "bottom"
-attachForm $radius_grp "right" 30
-attachControl $sweep_grp "top" 5 $radius_grp
-attachForm $sweep_grp "left" 10
-attachNone $sweep_grp "bottom"
-attachForm $sweep_grp "right" 30
-attachControl $name_grp "top" 5 $sweep_grp
-attachForm $name_grp "left" 10
-attachNone $name_grp "bottom"
-attachForm $name_grp "right" 30
-attachForm $create_button "bottom" 12
-attachPosition $create_button "right" 2 50
-attachForm $create_button "left" 5
-attachNone $create_button "top"
-attachForm $cancel_button "bottom" 12
-attachForm $cancel_button "right" 5
-attachPosition $cancel_button "left" 2 50
-attachNone $cancel_button "top"
$example_form;
showWindow $example_window;
//collect the user's inputs//
float $r = `floatSliderGrp -q -value $radius_grp`;
float $s = `floatSliderGrp -q -value $sweep_grp`;
string $n = `textFieldGrp -q -text $name_grp`;
//*****************************************************//
//*****************************************************//
Best Regards;
Ozan
|
|
norbertnacu
11-13-2009, 08:44 AM
Hello,
What you did is catenate the default values in your command:
// command - assignment
-c ("sphere -r 1 -s 0 -n")
That's what you've assigned to your create button, to fix this issue, just create a simple function to get the controls actual values. That code is done in Notepad, and if there's any typo I apologize
//************************************************** **********//
//************************************************** **********//
//************************************************** **********//
global proc fooSphere()
{
// UI starts here//
if (`window -exists $example_window`)
deleteUI $example_window;
string $example_window = `window -t "Hello"`;
string $example_form = `formLayout -numberOfDivisions 100`;
string $radius_grp = `floatSliderGrp -label "Radius:"
-field true
-minValue 0.0
-maxValue 10.0
-value 1 -adjustableColumn 2
radius_fsg`;
string $sweep_grp =`floatSliderGrp -label "Sweep Angle:"
-field true
-minValue 0.0
-maxValue 360.0
-value 360.0 -adj 2
sweep_fsg`;
string $name_grp = `textFieldGrp -label "Name:" -adj 2 name_tfg`;
// string $create_button =`button -label "Cretae" -c "sphere -name $n -radius $r -endSweep $s"`;
// This is wrong, what you're doing is catenating the value of $n, $r, and $s (default values)
// to your button command.
// The user can keep tweaking those controls
// and it wouldn't matter, cause its using the default values.
string $create_button =`button -label "Cretae" -c ("fooSphereCmd()")`;
string $cancel_button =`button -label "Cancel" -c "deleteUI $example_window"`;
formLayout -edit
-attachPosition $radius_grp "top" 0 20
-attachForm $radius_grp "left" 10
-attachNone $radius_grp "bottom"
-attachForm $radius_grp "right" 30
-attachControl $sweep_grp "top" 5 $radius_grp
-attachForm $sweep_grp "left" 10
-attachNone $sweep_grp "bottom"
-attachForm $sweep_grp "right" 30
-attachControl $name_grp "top" 5 $sweep_grp
-attachForm $name_grp "left" 10
-attachNone $name_grp "bottom"
-attachForm $name_grp "right" 30
-attachForm $create_button "bottom" 12
-attachPosition $create_button "right" 2 50
-attachForm $create_button "left" 5
-attachNone $create_button "top"
-attachForm $cancel_button "bottom" 12
-attachForm $cancel_button "right" 5
-attachPosition $cancel_button "left" 2 50
-attachNone $cancel_button "top"
$example_form;
showWindow $example_window;
}
// Create another function to collect the user's inputs
global proc fooSphereCmd()
{
//collect the user's inputs//
float $r = `floatSliderGrp -q -value radius_fsg`;
float $s = `floatSliderGrp -q -value sweep_fsg`;
string $n = `textFieldGrp -q -text name_tfg`;
sphere -name $n -radius $r -endSweep $s;
}
//************************************************** ***//
//************************************************** ***//
Hope this help,
Norbert Nacu
ozioz
11-13-2009, 10:37 AM
Unfortunately, I am in office now and I don't have any acces to Maya. So I couldn't try your new script. I am sure It works.
I think that the problem is caused by getting user inputs without a procedure or a function.
As far As I can see, nothing wrong with where the code lines are.( $r, $s, $n variables )
Tahnk you so much. You are enough kind to answer such an easy question like that.
ozioz
11-13-2009, 11:05 AM
Is it realy necessary to give the variables their uniqe names? That is What you did in new code lines.
What I mean is this;
string $n = `textFieldGrp -q -text $name_grp`; ----- This one is mines
string $n = `textFieldGrp -q -text name_tfg`; -------And this one is yours.
You had given the name "name_tfg" while declaring the $name_grp variable.
Why did you do that?
norbertnacu
11-13-2009, 01:50 PM
Hello There,
If you're using dynamic variables, then sure pass the dynamic $name, but if you're not then use the actual name rad_ff;
string $myIntField = `intField`;
You dont have to specify your control names, because it's either inside your proc or you're probably going to pass the name in a different proc.
// Dynamic
ie..
button -c ("doSomething(\"" + $myIntField + "\")");
proc doSomething( string $ctrl )
{
int $val = `intField -q -v $ctrl`;
confirmDialog -m $val;
}
Why do you wanna do this? If you're building dynamic ctrls in your GUI:
// ie..
global proc fooDynamicCtrl()
{
string $win = "myWin";
if( `window -ex $win` )
{
deleteUI $win;
}
string $selNodes[] = `ls -sl -l -typ "transform"`;
window -mxb false $win;
columnLayout -adj true;
for( $selNode in $selNodes )
{
string $btn = `button -l $selNode`;
button -e -c ("fooDynamicCtrl_doSomething(\"" + $btn + "\")") $btn;
}
setParent..;
showWindow $win;
}
global proc fooDynamicCtrl_doSomething( string $btn )
{
string $ctrlName = `button -q -l $btn`;
if( `objExists $ctrlName` )
{
select -r $ctrlName;
}
else
{
deletUI $btn;
}
}
// Unique names
// Code here, blah blah blah
button -l "Do Something" -c ("doSomething()") ds_foo_btn;
global proc doSomething()
{
string $btnName = `button -q -l ds_foo_btn`;
confirmDialog -m $btnName;
}
If you're controls are not going to change, then you can hard-code the name, and why? to keep everything simple, why whould you pass the name($btn)? Here's my rule:
**If the ctrls are not dynamic, then you don't have to pass the dynamic name to your proc. (some programmers will argue about this, and I know why).
If you hard-code the name, and later on you decide to change your ctrl names, because some other script is using the same name, then that's gonna create name clashing.
IMHO just be smart on how you name your ctrls. I've been doing scripting in Maya for a long time and so far I've never experienced name clashing. Here's the 1 million dollar question, are you gonna bring your old code from your previous company to your new company :P?
Here's my proc and ctrl naming convention:
Company Name + Script Name + Script Type
lets say Activision Blizzard
// Proc name
global proc AB_BipedGen()
global proc AB_BipedGen_UI()
global proc AB_BipedGen_RecallUI( int $mode );
etc...
etc...
// Ctrl/Layout names (I usually get the main proc name and do lowercase to all my ctrls)
// intField
ab_bg_objName_if
etc..
// frameLayout
ab_bg_
main_frame;
// formLayout
ab_bg_main_form
etc..
// var
string $objRadius;
Hopefully this makes sense. I dont have Maya infront of me, and if there's any typo I apologize. My point here is to keep it simple, if you're system is going to be simple why pass your ctrl everywhere? If it's gonna be dynamic, then sure pass your ctrl name, I'm not gonna talk about global variables, optionVar, and storing your ctrl names in the same GUI, cause that's another technique to pass/get the ctrl names.
Thank You,
Norbert Nacu
ozioz
11-13-2009, 02:27 PM
Norbertnacu
You are fantastic. I was not expecting such a great answer like yours. It has become much better than enough.
Evenn though our answer is great, I have to apply What you suggested me with Maya If I want to learn and understand correctly.
All I can do is just wait untill I get home.
Then I am gonno fill you in about What I get and keep on asking If you do not mind. :)
Best Regards;
Ozan
ozioz
11-13-2009, 04:51 PM
It works succesfully as expected.
But it works after I remocve the main UI procedure from the begining of the script. I do not know why but the codes did not answer the exacute command adn Maya said that $example_window was an undeclared variable.
If you guys know something about that, please share it with me.
You have been so helpfull so far. Espacially you "norbertnacu".
Best Regards;
Ozan
Keilun
11-13-2009, 07:20 PM
Your $example_window does not exist error comes from this line:
string $cancel_button =`button -label "Cancel" -c "deleteUI $example_window"`;
Because you have it in quotes, it's placed the $example_window literally as the command to run. So it will execute as follows:
deleteUI $example_window;
But what you really want to do is ensure that the command has the actual contents of $example_window, because the scope when deleteUI is run will not know what $example_window is. So you should do this:
string $cancel_button =`button -label "Cancel" -c ("deleteUI " + $example_window)`;
That will properly dereference $example_window, and it will properly invoke deleteUI as follows:
deleteUI window1; // or whatever string $example_window actually holds
ozioz
11-13-2009, 07:54 PM
OMG!!
guys I do not know how to thank all of you!
This thread has become a MEL course for me!
Then , here is another question.
How can I exacute two or more procedures with a single command flag?
For example, let's go back to my script;
//**************************************************************//
string $create_button =`button -label "Cretae" -c ("fooSphereCmd()")`;
//**************************************************************//
As you cab see, command flag has a one procedure. Lets suppose I want this command flag to have more procedures? Maybe I should say, I want this button to exacute more than one function at once.
What should I do? What is the syntax rule for this particular case?
I have tried many things but unfortunately nothing has worked so far...
Best Regards;
Ozan
norbertnacu
11-17-2009, 06:17 AM
Hi There,
If you want to call multiple functions in your -command flag, just add the ';' semicolon:
ie..
button -c ("func1(); func2(true, false); func3( \"save\" ); func4( 123456 )");
if you're calling those functions frequently why not create 1 function to call them?
proc MyFunc()
{
func1();
func2( true, false );
func3( "save" );
func4( 123456 );
}
Thank You,
Norbert Nacu
ozioz
11-17-2009, 06:27 AM
norbertnacu (http://forums.cgsociety.org/member.php?u=398895);
you are amazing man. I like your idea . Using a single function for multiple functions would be greate.
I will also try the first solution.
Thank you so much.
Best Regards;
Ozan
CGTalk Moderation
11-17-2009, 06:27 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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.