SimplyMaya Forums

Welcome to the forum, feel free to ask questions here.

Home / forum / Animation / Mel Scripts & Programming / for-loop: how to print variable data instead of variable name?
Register FAQ Members List Calendar Mark Forums Read

New Thread
 
Thread Tools
Old 10-02-2006, 07:46 PM   #1
jpdefkt
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:

$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
jpdefkt is offline   Reply With Quote
Old 16-02-2006, 03:01 AM   #2
skywola
Registered User
 
skywola's Avatar
 
Join Date: Jan 2004
Location: Tempe, Arizona, USA
Posts: 224
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
skywola is offline   Reply With Quote
New Thread

« Previous Thread | Next Thread »

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

1 Month

$34

Month-to-month billing
$34/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
 
 

3 months

$87

Billing every 3 months
$29/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
Save $5 per month or $15 in total compared to monthly billing with this package.

12 months

$288

Billing every 12 months
$24/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
Save $10 per month or $120 in total compared to monthly billing with this package.

Your plan will automatically renew. You can cancel at any time via your PayPal account to avoid being charged again.

You can view the FAQ on our video plans here. You can contact us directly here. By clicking one of the buttons above, you agree to our Terms and Privacy Policy.