View Full Version : Make Variable value Increase

05 May 2012, 05:34 AM
I want to translate a selection of objects. However I want the first object in the selection to get translated by 5 and the next by 10, 15,20 and so on. The problem with my code is all the objects in my selection get translated in the Y direction by 5 and the Variable $Up is not increasing like I want it to. I put in float $Up = $Up +5; with hopes that it would change the value before looping again and applying the translate to the next object in the selection.

Thanks for any help.

float $Up =5;
string $selected[] = `ls -sl`;
for ($obj in $selected)

setAttr ($obj + ".translateY") $Up;
float $Up = $Up +5;

05 May 2012, 06:42 AM
float $Up =5;
string $selected[] = `ls -sl`;
for ($obj in $selected)

setAttr ($obj + ".translateY") $Up;
$Up += 5;

You were re-declaring the $up variable each time in the loop so it will always be the same value.


05 May 2012, 01:31 PM
Thank you!

05 May 2012, 02:39 AM
Another basic scripting question:

If I have an object's vertex selected, is there any way to select the object that that vertex belongs to?

05 May 2012, 10:05 AM
You can either use the command

ls -selection -flatten;

to retrieve the component ID in the form of "objectName.vtx". Then it's a matter of extracting the objectname, which you could do via the [I]tokenize command or other ways...

Or you could use the command

ls -hilite;

to return the objects for which component selection is currently active, though this is not guaranteed to return the correct object as any other object might also be hilited for component selection.

05 May 2012, 03:20 PM

Another problem I am facing with my first script is this:

I need to check to see if the selected object is a "mesh". If it is I want to perform some operations, if it isn't, I want to perform some other operations. objectType -isType "mesh" $ShapeNode; returns true or false if my $ShapeNode is a mesh or not. And objectType $ShapeNode; will return the name of the object type. Both of these will work in my situation but how can I store store the value that objectType returns as a variable to use later in an if statement?

I have tried to store the return value in the following ways but I always get this message Error: Invalid use of Maya object "objectType". // :
string $IsOBJ = objectType -isType "mesh" $ShapeNode;

string $IsObj = objectType $ShapeNode;

thanks again for any help

05 May 2012, 04:45 PM
you should put the command inside a couple of ` to execute it, like this

`objectType -isType "mesh" $ShapeNode`I would also assign a int variable to a true or false result (1 or 0), so the code should be

int $IsOBJ = `objectType -isType "mesh" $ShapeNode`;

05 May 2012, 07:13 PM
using ls properly is the key to good and efficient scripts. read through the ls MEL documentation and find out what all the various flags do. for example, lets say you had one or more vertices selected and you wanted to get the selected mesh, you would just do this:
string $selMeshes[] = `ls -sl -type "mesh" -dag -o`;
// Result: pCubeShape1

05 May 2012, 01:30 AM
Thanks all for the help, it's much appreciated!
I just finished my script and now I am trying to create a simple user friendly UI window before I share it online.

I need to create a field where the User can input their own string (to give a Prefix to the objects they will be creating)

I tried string $Name = `nameField`;
but when I print that the result is not the name input in the box but:

Here is my UI code.

if ( `window -exists fghajheWindow` ) {
deleteUI fghajheWindow;
window -t "fghajhe SkinObject" fghajheWindow;

frameLayout -label "SkinObjects" - collapsable 1;
string $Name = `nameField`;
text -l "Object Name";
button -l "RootJoint" -c "fghCreateRoot";
button -l "ExpandHierarchy" -c "fghExpHierarchy";
button -l "SkinObjects" -c "fghSkin";
setParent ..;
setParent ..;
showWindow fghajheWindow;

05 May 2012, 08:57 AM
the return from any control creation is the control name. if you want the data or any property of a control you have to query the control. e.g:
string $nameFieldCtrl = `nameField`;
string $object = `nameField -query -object $nameFieldCtrl`;

05 May 2012, 08:58 AM
Ah, nice tip (-dag), thanks for that Nathan

05 May 2012, 12:03 PM
Thanks Nathan.

I am afraid I am not quite getting it yet.

Looking at nameField in Help documentation, they are querying an object to get the name. Is there a way to do this without a Dag object created in the scene?

I want the namefield to always show the name "ObjectName" until the user inputs a new name. And I want $Name to be whatever is in the namefield, be it "ObjectName" or whatever the user inputs.

I have tried this but when I create the window, the namefield is completely empty and when i DO type something in there and print $Name; it doesnt return anything.

string $window = `window`;
columnLayout -adjustableColumn true;
string $defaultName = "ObjectName";
string $nameFieldCtrl = `nameField $defaultName`;
string $Name = `nameField -query $nameFieldCtrl`;
showWindow $window;
print $Name;

05 May 2012, 12:22 PM
no, you cannot do this. a nameField has to be connected to a node, that way it can always be "live".
If you really want an input box that shows "objectName" or whatever you'll have to use a standard textField, but then you will have to code all of the functionality yourself and you'll lose the live sync with the object.. which may or may not be a problem depending on your intended use.

when you declare a variable, it is given the value at that exact point in time and cannot change itself. variables cannot "update" their contents, you can only write code to update a variable. if you want $Name to always be equal to whatever text is in the nameField, you will have to write a function to set and update it everytime the nameField changes (this is known as a callback). However, you don't need a variable called $Name, because whenever you want what the name in nameField is, you just do: `nameField -query -object $nameFieldCtrl`;

does that make sense?

05 May 2012, 04:35 PM
Thanks for your explanation! textField is exactly what I was looking for.
My last issue (I hope) is this.
While my script works, every time I press the button "RootJoint" (which creates a root for my rig) I get another textField created below called "ObjectName".

I think this is becuase my global proc fghCreateRoot() redeclares variables $TextName and $Name. Is there a better way I can declare my variables? I copied $TextName and $Name variables to each proc in my script so I wouldn't get declaration errors.

Edit: Nvm I figured out how to properly use global variables.

05 May 2012, 08:15 PM
...properly use global variables.

HAH! that's an oxymoron if ever I saw one! ;)

05 May 2012, 06:22 PM
Thanks Nathan and everyone else for all the help!

Check out my terribly written fist script here:

CGTalk Moderation
05 May 2012, 06:22 PM
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.