Search code examples
scriptingsimulationmayamel

What is the function to run simulation in MEL?


I would like to create a MEL script that creates a scene, setting up ncloth and passive collider objects and run the simulation up to a certain frame.

In the script editor, I can see the scene set-up but there is no function starting the simulation.


Solution

  • The technique that @Andreas suggests is sometimes called "command harvesting". It is a great way to learn what and how Maya is doing things. But to answer your specific question:

    You can use cmds.play() to start playing back on Maya. See the docs for options.

    You might want to set the start frame and end frame of the playback range using the cmds.playbackOptions() command. See the docs for options.

    So you would do: (relevant explanatory comments added)

    # egs. to play from frame 1 to 120
    # also note that the playbackSpeed flag is used
    # we need to set this to 0 to "play every frame".
    # setting maxPlaybackSpeed to 0 will result in free playback, so the playback isn't clamped.
    # At this point, playback wouldn't be realtime, but it will be accurate.
    # Dynamics and simulations must be played back like this or the nucleus will not evaluate properly.
    
    cmds.playbackOptions(animationStartTime=1, animationEndTime=120, playbackSpeed=0, maxPlaybackSpeed=0)
    
    # now start playback
    cmds.play(forward=True)
    

    EDIT: I just noticed that you had asked for MEL commands. Just take the above commands and MEL-ify them, like so:

    playbackOptions -e animationStartTime 1 animationEndTime 120 playbackSpeed 0;
    play -f 1;
    

    Suggestion: It is best to playblast this playback to watch it in proper fps and playback speed.

    Hope that was useful.