PDA

View Full Version : MEL looping ...


MrWeeeee
10-02-2005, 07:59 PM
So, I have this dynamics project where we are not allowed to set any keyframes but can script all we want. That being said I have an event where a sphere rolls down a slide and falls onto a button which turns on this little ring of clown heads attached to it. Here's where the issue comes in ... I've been trying to script the jaws moving up and down while the whole ring rotates. I wrote a script that had some nested loops but I think it was written incorrectly because it infinite looped and crashed Maya. Here's what I have so far ...

//declaring variable
int $counter;

//starting up the rotating ring after frame 275
if (frame <= 275)
button.translateY = 42.3;
else if (frame >= 275) {
button.translateY = 41.832;
buttonGroup.rotateY = (frame + 1) *2; }

//initializing counter to 0
$counter = 0;

//setting conditional for loop
if (frame >= 275)
do {
clownJaw1.translateY = (frame + 1) - 0.1;
clownJaw2.translateY = (frame + 1) - 0.1;
clownJaw3.translateY = (frame + 1) - 0.1; //move jaw group down by 0.1 while
clownJaw4.translateY = (frame + 1) - 0.1; //counter < 100
$counter = $counter + 1;
} while ($counter < 100);

I orignally had a "for loop" inside the "do ... while" which is where my problems came from. also, can you script rigid bodies (setting contitionals on them)?

Thanks for the help!

-Bryant

swardson
10-03-2005, 05:32 AM
ok, first we will look at your code, and I will attempt to explain the problems that exist within it. Then I will suggest an alternative way to do the process you are trying to do, which will maybe help you in the future when thinking about expressions and keyframeless animation.

ok, on this first chunk you have this code:
//declaring variable
int $counter;

//starting up the rotating ring after frame 275
if (frame <= 275)
button.translateY = 42.3;
else if (frame >= 275) {
button.translateY = 41.832;
buttonGroup.rotateY = (frame + 1) *2; }

first strange thing I see is that you have an:
if (frame <= 275) and then an else if (frame >= 275). This is infact correct just slows down calculations because when frame is at 275, both conditionals will be run. Again your way is correct, just will be slow if you have more calculations. A better way to do this would be:

int $counter;
if (frame < 275){
button.translateY = 42.3;
}
else if (frame >= 275){
button.translateY = 41.832;
buttonGroup.rotateY = (frame + 1) *2;
}

Next thing is you run two seperate conditional statements to check the same thing. You run the "if (frame >= 275)" twice. It would be faster and better for you to only check this once and then do everything reliant on that conditional at that time.

The next issue, and what I am guessing caused your crash in maya is the do while statement.

you have this:
$counter = 0;

//setting conditional for loop
if (frame >= 275)
do {
clownJaw1.translateY = (frame + 1) - 0.1;
clownJaw2.translateY = (frame + 1) - 0.1;
clownJaw3.translateY = (frame + 1) - 0.1; //move jaw group down by 0.1 while
clownJaw4.translateY = (frame + 1) - 0.1; //counter < 100
$counter = $counter + 1;
} while ($counter < 100)

I am not that familiar with do whiles in mel, but it looks like as soon as it hits frame 275 it sets the rotation of the whole group and then attempts to drop the jaw a bit over 100 frames. I think it loops over the whole do while over and over because it resets the counter to 0 then checks again so counter never gets to 100.

First I will give you my suggestion for this code to work. Then I will give you my suggestion for what I would do to make this code to do what you want.


int $counter;

// check if frame is less than 275
if (frame < 275){
button.tranlsateY = 42.3;
$counter = 1;
}

//once frame is atleast 275 this code will run.
else if (frame >= 275){
button.translateY = 41.832;
buttonGroup.rotateY = (frame + 1) *2;

if ($counter < 100){
clownJaw1.translateY = (frame + 1) - 0.1;
clownJaw2.translateY = (frame + 1) - 0.1;
clownJaw3.translateY = (frame + 1) - 0.1;
clownJaw4.translateY = (frame + 1) - 0.1;
$counter++; //increments counter by 1.
}
}


What is really great in doing repeditive motion like the clown jaws moving up and down is best achieved using a sin or cos function. like so:

// check if frame is less than 275
if (frame < 275){
button.tranlsateY = 42.3;
}

//once frame is atleast 275 this code will run.
else if (frame >= 275){
button.translateY = 41.832;
buttonGroup.rotateY = (frame + 1) *2;

$frequency = 10;
$amplitude = .3;
$jawOpen = sin(time * $frequency) * $amplitude;

clownJaw1.translateY = $jawOpen;
clownJaw2.translateY = $jawOpen;
clownJaw3.translateY = $jawOpen;
clownJaw4.translateY = $jawOpen;
}


the nice thing about animating the jaws with the sin function is that it doesnt matter what frame you are on, it just follows the sin function of time. Definately check out trigonometry when you get a chance... some good stuff in there to use in motion and expressions.

as for setting conditionals on rigid bodies, it depends on what you want to condition. If it is a collision or speed, or position etc, then yes. Anything that has a value in maya you can condition. If you describe what you are looking for specifically, I could try and help you.

Sorry if this is so long. I was just feeling in an extra helpful mood tonight. At least I hope I was helpful. Let me know if you have any other questions.

Brad

MrWeeeee
10-03-2005, 05:54 AM
Brad, you are such the man. I took a couple c++ class in college and was kind of formatting my code to what I had done then (which wasn't a whole lot). I'm glad for the extra explainations! I really need to think more outside of the box because that sin wave section is great. well with the rigid body thing, my instructor said you cannot write expressions with rigid bodies because that input is taken up by the solver. maybe I'm not understanding her correctly ... thick Spanish accent. Anyway, that helps a lot. I'm going overboard with this project I just want to practice this stuff as much as possible. Thanks again for your help.

-Bryant

CGTalk Moderation
10-03-2005, 05:54 AM
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.