Maya Training

Creating characters In Maya and Zbrush

Maya Training

Creating character renders in Maya 2017 and Arnold

Maya Training

Spach-Alspaugh House the complete courseware
You are here > Home > SimplyMaya Community Forums

Welcome to Simply Maya

Please Sign in or Sign up for an account

Member Login

Lost your password?

Not a member yet? Sign Up!

Old 10-02-2006, 08:46 PM   #1
Registered User
Join Date: Feb 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default for-loop: how to print variable data instead of variable name?

hi, i've got a new problem. it seems as if there was a simple solution, but i just don't figure out how to find it.
what i've got:

6 variables:


and i want to print their value from a for-loop

for ($i=2; $i<=7; $i++) {
print ?????;

but the only thing i can think of is to write it like this: print ("$digit" + $i);
and that of course only prints out the variable names instead of the values i want...

i hope you can help me
thanks in advance,

jpdefkt is offline   Reply With Quote
Old 16-02-2006, 04:01 AM   #2
Registered User
skywola's Avatar
Join Date: Jan 2004
Location: Tempe, Arizona, USA
Posts: 224
Thanks: 0
Thanked 0 Times in 0 Posts

I had the feeling when I saw your post last night, that the response that you received did not quite give you the answer you wanted, however, I did not have the time to respond to your question, as the answer is not just a short simple thing.

The problem is related to the handling variables, objects, and attributes. You have a variable, which and you want to run it through the "for" loop and print out the actual value that the variable contains.

It's been a while since I have read up on all this, so my terminology may not be exact, but with MEL, when you are storing data, within code that is "run-time", you need to use
attributes. It sounds like a pain, but after you learn about it and become familiar with it, you would want it no other way. I have written programs in four other programming languages, and by far, MEL is easier to use and work with than any of them.

Let's create an attribute to go along with an object . . . just like when you look in the expression editor at a selected sphere, you will see:

pSphere.translateX, pSphere.translateY, pSphere.translateZ
pSphere.rotateX, pSphere.rotateY, pSphere.rotateZ

What gives? . . Well this is how they store data with the object. As an attribute.

You can add these attributes - (goto --> modify > add attribute) - to various objects you have created in MAYA, like a sphere, a cube, a locator . . . or, you can just create a node, and add attrubutes to it.

string $iterate;
string $s;

createNode "transform" -n "thisNode"; // create a node
for ($i=1; $i<=6; $i++) {

// $s = "torpedo1", "torpedo2", etc
$s = "torpedo" + $i;

// add an attribute for each torpedo
// and set its default value to $i
addAttr -longName $s -dv $i "thisNode";

// $iterate = "thisNode.torpedo" + torpedo # as a string
$iterate = "thisNode.torpedo" + $i;

// print the value in the attribute "torpedo#"
print `getAttr $iterate`;
print "\n";

You can use:
int $num = `getAttr thisNode.torpedo1`;
setAttr thisNode.torpedo1 3;

to get and set attributes.

However . . .
I am still not yet that familiar with just adding attributes to nodes, you can add them by type . . . but why make it difficult?

There is always geometry in your scene and you normally have the geometry in the scene effected by the data, so why not just go to "modify > add attribute", then you can select the type, integer, float, etc, and even specify the range that the attribute can cover. Then all you have to do is use setAttr if you want to change its value, and getAttr if you want to retrieve it.

// get the value stored
int $num = `getAttr sphere.torpedo1`;

// set the attribute
setAttr sphere.torpedo1 3;

By the way, I ran this code in MAYA, in the script editor . . . you can do the same step by step to see it in action.

Ok, I'm getting bored, and didn't mean to write a book, but you can find examples of this and all the technical crap too in the Maya help library . . . . This sample code contains most of what you asked about though, if you study it and the Maya help manuel, you should be able to formulate your code to do whatever it is you are attempting to accomplish.
"The Sage as an Astronomer: If you still see the stars as something above you, you lack the eye of knowledge." Friedrich Nietzsche
skywola is offline   Reply With Quote

A little bit about who we are
Links you might find useful
Catch up with SimplyMaya
SimplyMaya specialises in Maya tutorials. We offer over 1,000 individual Maya training videos, ranging from basic Maya tutorials through to intermediate Maya tutorials. Our tutorials are created by instructors with industry experience and are designed to get you up and running in Maya quickly without making it seem like hard work.

Copyright © 1999-2017 SimplyMaya - vBulletin® Copyright © 2000-2017, Jelsoft Enterprises Ltd.