Community Forums

Maya Training

+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2011
    Location
    Los Angeles, CA
    Posts
    26
    DT Subscriber

    Question MEL duplicate while animating

    hi everyone,

    im trying to learn scripting, and i wanted to know how to accomplish a task. I've been banging my head over this for a while, sorry if it is in fact a simple problem. I basically want to have an animated cube leave a duplicate of itself every frame as it animates.

    I know how to select the cube and have it duplicate in mel, but i dont know how to do based on the current frame, here is what I came up with but it doesn't work, it

    Code:
    int $currentFrame = frame;
    while ($currentFrame <=24 ) {
        select pCube1;
        duplicate -rr;
        $currentFrame = $curFrame + 1;
    };
    i get an error saying

    // Error: int $currentFrame = frame; // current frame //

    can you please tell me whats wrong with the code? I would appreciate it greatly

    Thanks!
    nik


    heres an illustration of what im hoping to do
    Screen shot 2012-02-02 at 9.45.24 PM.jpg

  2. #2
    Join Date
    Oct 2006
    Location
    Digital Tutors HQ
    Posts
    2,324
    DT Staff
    I am a Face of Change

    Re: MEL duplicate while animating

    Hi nik,

    Your code was really close. Instead of using frame, I set the currentTime in the loop with a start and end. I also changed it from a While loop to a For loop since I crashed Maya a few times by getting myself into an infinite loop.

    Code:
    int $i = 1; // start frame
    for($i; $i < 24 ; $i++) { // 24 = end frame (you can put this into a variable too.)
        select pCube1;
        duplicate -rr;
        currentTime($i);
    }
    here is info for the currentTime mel command.

    If you're looking to do just this, Maya has a command called Animated Snapshot that duplicates an object based on its animation.
    Last edited by chrisg; 02-03-2012 at 11:35 AM.

  3. #3
    Join Date
    Nov 2011
    Location
    Los Angeles, CA
    Posts
    26
    DT Subscriber

    Re: MEL duplicate while animating

    Hi Chris

    Thanks a lot for looking in to the code for me. I understand what you did, and it does create the cubes along the animation. I played around with it, and as you suggested tried creating a variable with the end frame. I also renamed all of the trail cubes and added them into a group.

    Code:
    int $i = 5; // start frame
    int $end = 48; // end frame
    
    
    for($i; $i < $end ; $i = $i + 5) { 
        select pCube1;
        duplicate -rr -n pCube_trail01;
        currentTime($i);
    }
    
    select "pCube_trail*";     // select all trail cubes
    group -name grp_trails01;  //group them
    I was wondering how to use this in an expression. I notice that the currentTime actually moves the time line to the whatever $i is as fast as it can compute it. Ive been trying to figure out how to get it to run it real time. So i was thinking of somehow referencing the current frame (frame command), sort of like in #15 expressions section in The Artists Guide to MEL video. but I havent been successful at all. Im not sure if im going about this the wrong way.

    I know this code is a mess but this is what i could come up with

    Code:
    int $i;
    if (frame == 1){
        select pCube1;
        duplicate -rr -n pCube_trail01;
        select "pCube_trail*";     // select all trail cubes
        group -name grp_trails01;  //group them
        int $i = 1;
        
    }
    
    $i = $i + 1;
    Maybe you can point me in the right direction...

    i tried Animated snapshot, and it does indeed do basically what this code does, but i was looking to create the duplicates as its animating. I watched the tutorial on it, and Sunder had a neat trick with having it trail behind, but the duped trail pieces also move.

    Thanks!

    PS. Love the front page redesign!

  4. #4
    Join Date
    Nov 2011
    Location
    Los Angeles, CA
    Posts
    26
    DT Subscriber

    Re: MEL duplicate while animating

    I figured out how to duplicate an animated object using expressions on every frame... Here is what i added in the expression editor:

    Code:
    select "pCube1";           //select object to duplicate
    duplicate -rr -n pCube_trail01; //duplicate object on frame
    
    if (frame == 24)    {      // run in last frame (24)
    select "pCube_trail*";     // select all trail cubes
    group -name grp_trails01;  // group them  
    play -state off;           // stop playback
    };
    
    if(frame == 1) {            //delete the group when reset back to frame 1
    select "grp_trails*";
    delete;
    };
    Heres what it looks like:

    screeny.jpg

    this basically does what I wanted, but i hoped to be able to control when the duplication occurs, for example, ever four frames.

    Any suggestions on how to approach doing that?

    Thanks!
    N

  5. #5
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    954
    DT Moderator
    I am a Face of Change

    Re: MEL duplicate while animating

    Im not very good at MEL but...
    Yes, you may consider using the modulo command this looks like this:
    Code:
    $myVar = 10;
    
    if($myVar%3==0){
        print "True";
    }else{
        print "False";
    }
    What this does: It looks if myVar divided by 3 leaves any integer rest. ( 10/3 = 3 rest 1) so that is != 0 and therefore the script returns "false"
    You can use that to run the script only if the current frame is dividable by 4 (or any numer of frames you want to)
    So if you want to duplicate every second frame you would check the current frame (or any other variable driving your script) to see if its dividable by 2 without leaving any rest. The nice thing about that, you can set this up to be a parameter of your proc, so that you can define how much frames your script skips before duping

  6. #6
    Join Date
    Nov 2006
    Location
    Sydney Australia
    Posts
    1,004

    Re: MEL duplicate while animating

    you should refrain from using <= >= or =< => in conditional functions either test for < then == in a nested loop Modulus can be a bit icky at times as well .

    Some times its better to add an extra line of code to pre-process the value your testing than to use the conditional to make assumptions that can be hard to debug when you code gets more complicated .

  7. #7
    Join Date
    Oct 2006
    Location
    Digital Tutors HQ
    Posts
    2,324
    DT Staff
    I am a Face of Change

    Re: MEL duplicate while animating

    Quote Originally Posted by bravo2zero View Post
    you should refrain from using <= >= or =< => in conditional functions either test for < then == in a nested loop Modulus can be a bit icky at times as well.
    Hi bravo2zero,

    Why shouldn't we use the comparison+equal operators? Is it a speed issue or an order of precedence or ?

  8. #8
    Join Date
    Nov 2006
    Location
    Sydney Australia
    Posts
    1,004

    Re: MEL duplicate while animating

    precedence can be the cause of a lot of issues and crashes or bugs in general more so in c++ .

    this one can cause a lot of headaches .

    for (int i =0 ; i <= somevar ; i++)
    {
    // some code here
    }

    much better to use


    int $somevar = 24; //Set loop Value

    for (int $i = 0; $i < $somevar ; $i++)
    {
    if ( $i == 12 )
    {
    //do something when we get to frame 12
    }

    if ( $i == 24 )
    {
    //do something when we get to frame 24
    }
    }
    Last edited by bravo2zero; 02-07-2012 at 12:06 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts