Maya 2020 fundamentals - modelling the real world
Get halfway through a model and find it's an unworkable mess? Can't add edge loops where you need them? Can't subdivide a mesh properly? If any of this sounds familiar check this course out.
# 1 10-02-2006 , 08:46 PM
Registered User
Join Date: Feb 2006
Posts: 2

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:

$digit2=0;
$digit3=1;
$digit4=0;
$digit5=1;
$digit6=0;
$digit7=1;

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,
yours

joh

# 2 16-02-2006 , 04:01 AM
skywola's Avatar
Registered User
Join Date: Jan 2004
Location: Tempe, Arizona, USA
Posts: 224
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`;
and
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
Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads