Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® Maya® / MEL / Procedure executed by button issues...
  RSS 2.0 ATOM  

Procedure executed by button issues...
Rate this thread
 
38758
 
Permlink of this thread  
avatar
  • Total Posts: 3
  • Joined: 22 April 2008 02:47 AM

Hello Everyone,
First, I just want to say that I have around 5 days of MEL experience at this point, so I am an extreme novice and most likely will not understand some of the answers I will receive. So please forgive my naivety on the subject. But with that being said, I have learned quite a bit thus far.

I am trying to create a window with two buttons. One button to read “Get TransX”. The second button to read “Set TransX”. With the “Get TransX” button, I want to get the translateX value from a currently selected object. With the second “Set TransX” button, I want to be able to assign that same translateX value (the value returned from the the first button) to a second/newly selected object. Hope that makes since. I have only attempted to deal with the first button at this point...with failure I might add.

Here is my code thus far:



/////////////////////////////////////////////////////////////////////////////

//delete UI if it exists already

if (`window -exists modelHelper` == 1)
{
deleteUI modelHelper;
}

//Declare Variables Needed
string $SelObj01[] =`ls-sl`;
float $FirstSelObjTranX;
//Back UP float $FirstSelObjTranX = `getAttr $selObj.translateX`;


//Create the UI
window
-title “Model Helper”
-widthHeight 325 600
-sizeable 0
-backgroundColor .8 .9 1
modelHelper;
rowColumnLayout -numberOfColumns 2
-columnWidth 1 150
-columnWidth 2 150
-columnSpacing 1 8;
text -label “Translates” -align “left” -font “boldLabelFont”;
text -label “ “ -align “right” -font “boldLabelFont”;
button -label “Get Trans X” -command “getTransXproc” getTransX;
button -label “Set Trans X” -command setTransX;
showWindow;



proc getTransXproc()
{
string $SelObj01[] =`ls-sl`;
float $FirstSelObjTranX = `getAttr $SelObj01.translateX`;
}
////////////////////////////////////////////////////////////////////////////

The problem I have is that my procedure will run/update the translate X value on my current selected object if I execute it line by line by highlighting and hitting my enter key on the num key pad. But if I click on the first “Get TransX” button in the UI, which is suppose to execute the proc by a -command flag, it does not update the translateX value at all. Again, hope that makes sense.

Can any kind MEL souls explain to me my errors in the coding?
Thanks! :)
Dave



Replies: 0
avatar
  • THNKR
  • Posted: 16 January 2010 04:23 AM

You have a scope problem. Scope is explained in User Guide > General > MEL and Expressions > Procedures > Global and local variables

try something like this

global proc float getTransXproc()
{
global float $gFirstSelObjTranX;
string $SelObj[] =`ls-sl`;
$gFirstSelObjTranX = `getAttr ($SelObj[0] + “.translateX")`;
return 0;
}

global proc float setTransX()
{
global float $gFirstSelObjTranX;
string $SelObj[] =`ls-sl`;
setAttr ($SelObj[1] + “.translateX") $gFirstSelObjTranX;
return 0;
}



Replies: 0
avatar

That makes sense. Thank you mduvekot, I will give it a go and see what happens :)



Replies: 0
avatar

Hi Michiel,
A couple quick questions. I know that when you want the procedure to return a value, you need to give it a variable type, such as a float, as well as the return command with in the procedure. But why do you have the return command set to “0”? Also, its my understanding that making your variables global is generally bad practice. But if I have multiple procedures that need to revolve around common variables used, I don’t see how I can NOT use global variables. Am I wrong?

Thanks!

Dave



Replies: 0