View Full Version : Translating group of cubes individually in the y direction..

 coolman8511 November 2007, 06:10 PMhey guys .. can u please help me out ? i need to move set of cubes in the y direction like from 10 to 0, cube after cube ... so thnx to ur site i came out with this script: string \$cubes[] = `ls -sl`; select \$cubes[0]; for(\$i=0;\$i<=10;\$i++){ for(\$k=0;\$k<=1000;\$k+=100){ setKeyframe -t (\$k) -v 10 -at ty \$cubes[\$i]; setKeyframe -t (\$k+70) -v 0 -at ty \$cubes[\$i]; }} but it moves the whole group together ... can u please tell me y :surprised ?? thnx in advance:thumbsup:
Gnimmel
11 November 2007, 07:16 PM
You are looping through each cube and setting keyframes between frames 0 and 1000 on all of them the same, so they will all move the same.

You also get the cubes by listing selected and then forcing the cube loop to 10. If you only select 5 cubes this script will error.

If all you are trying to do is move the cubes from 10 to 0, one after the other, then you only need one loop. Looking at your script you seem to be moving the cubes from 10 to 0 over 70 frames.

One way to do this would be to loop through the cube list and have your \$k var update after each loop. Somthing like;

string \$cubes[] = `ls -sl`;
string \$EachCube;
float \$k= 0;
for(\$EachCube in \$cubes)
{
setKeyframe -t \$k -v 10 -at ty \$EachCube ;
setKeyframe -t (\$k + 70) -v 0 -at ty \$EachCube ;
\$k+= 70;
};

Thanks,

Richard

coolman85
11 November 2007, 07:45 PM
thnx Richard .. the script worked pretty well & believe it or not i spent bout 6 hours tryin to figure out how to make it & i reached the point that i sent u ... well 1 thing i'd like to mention that in ur script 2nd line there's (string \$EachCube;) isn't the "string" command workin with "[]"? pardon me am sort of beginner in MEL...
& "for(\$EachCube in \$cubes)" couldn't i use the loop command "for(\$i=0;\$i<=9;\$i++) to loop through the cubes[] string ? know that i'd selected all the 10'th cubes b4 running the script ..
sorry 4 askin to many but i just wanted to get things right ...
thnx 4 ur afforts ..
god blesses.

Gnimmel
11 November 2007, 08:06 PM
The second line:
string \$EachCube;
is just to declare the varible as a string before I use it. MEL can work out what type varibles should be without declairing, but I've ran into a few problems with this where I wanted a float and it declaired a int, so I always declair stuff before using it.

Yes you could use for(\$i=0;\$i<=9;\$i++) instead of for(\$EachCube in \$cubes), but that limits you to a pre set amount of cubes. It sounds like in your case all you need is 10 cubes so it really doesn't matter here, either way would work.

Thanks,

Richard

coolman85
11 November 2007, 08:18 PM
thnx alot man .. i really appreciate ur time .. by the way i have more than 4000 cubes to move in this script so thnx 4 ur help..next thing i tried to make & i couldn't is to make the transparency of these cubes = 1 when they'r lets say above y=5 & equals 0 below 1 but it didn't work either .. take a look if u can & i'll tell u wut happend 2 me:

string \$cubes[] = `ls -sl`;
for (\$i = 0; \$i<=9; \$i++){

float \$position = `getAttr (\$cubes[\$i] + ".ty")`;

if (\$position >=10){

else{

}

so applied a new blinn shader to all the cubes & renamed it "shader" .. b4 i put the "
setAttr "shader.transparency" 1;" command i used different print commands in the both conditions & worked properly but when it comes to the shader it didn't work .. can u tell me plz ?

thnx again man ..

Gnimmel
11 November 2007, 09:15 PM
To do it that way you would need a different shader for each cube. If you have 4000 cubes, thats a lot of shaders. If you just want them to disapear with no fade, then you could use the cubes visibility attribute instead, which saves on a lot of shaders. Then you could write a simple expression like;

float \$position = pCube1.translateY;
if(\$position >= 10) pCube1.visibility = 1;
else pCube1.visibility = 0;

of course thats a big task in itself to write that 4000 times, so you'd want to script to create it;

string \$cubes[] = `ls -sl`;
string \$Each;

for(\$Each in \$cubes)
{
expression -s ("float \$Pos = " + \$Each + ".translateY;\nif(\$Pos >= 10) " + \$Each + ".visibility = 1;\nelse " + \$Each + ".visibility = 0;") -o \$Each ;

}

If you want a fade as it moves, a different way would be to add a ramp to the transparency of a shader and make sure it's a projection, not UV based. You can then position the place3dTexture so that the cubes fades as they move. This can then be fine tweaked by altering the ramp. This means it's only 1 shader for all 4000 cubes.

Lastly you could also do this easily by adding to the original script that sets up the movement, to keyframe the visibility. As they start at 10 and the move to 0 over 70 frames and you know from the script (\$k) the keyframes this happens at, you could add 2 more lines that keyframe the visibility at the correct time. For example;

setKeyframe -t (\$k + 30) -v 0 -at v \$EachCube ;
setKeyframe -t (\$k + 31) -v 1 -at v \$EachCube ;

Thanks,

Richard

CGTalk Moderation
11 November 2007, 09:15 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.

1