Dynamic Follow MEL Script

Posted by Duncan Brinsmead, 3 May 2007 5:27 am

Here is a script that allows you to easily have one object follow the position of another, but with damping and a lag. I find it is very useful when you wish to have a camera follow an object automatically without following too closely or in too jerky a fashion. Note how in the example avi the camera follows the general motion of the sphere but not so closely as to bounce up and down with the sphere.

To use, first save the following MEL file in your maya scripts folder then run Maya.
dynamicFollow.mel (scroll to the bottom to download related materials)

You can now select first a leader and then a follower object( the order of selection matters ). The lead object may be a single vertex selection if desired. Make the second object your camera if you wish to have it follow the lead object. Type "dynamicFollow" into the command line and it will set up an expression and create a parent for the follower object. When you playback it will animate the position of this parent node but still allow you to edit the relative position of the follower object. With a camera one can still track and tumble the view even while it is following the object position. There is usage info in the attribute editor notes for the generated "follower" node, as well as the extra attributes "damp" and "drag". The expression creates a simple dynamic spring that pulls the follower towards the leader. Note that you must bake out the translation on the follower to keyframes if you wish to render out of sequence. The expression treats frame 1 as a start frame by default (resetting the position on rewind), but you can edit the expression if you want it start at a different frame.

If you typically wish to use this for cameras and would rather it just used the current camera you can also add the following to your scripts folder.
cameraFollow.mel (scroll to the bottom to download related materials)

Then you simply need to select the object or vertex and type: "cameraFollow".

If you don't like typing, then simply select the text "dynamicFollow" or "cameraFollow" and leftmouse drag onto the shelf to create a button.

I find this script is particularily useful when iterating over dynamic simulations. For example in a dynamic parachute animation the speed at which the character falls is dependant on when the parachute opens, which changes when I adjust the simulation. This script lets me simply have the camera follow the parachute smoothly with a little lag so that I can best see what is happening. Otherwise the camera would need to be too far away to capture the entire fall.

Note that if you group the lead object and rotate at the parent level then follower will only work correctly when the lead is a vertex. In this case it is better to make the parent or group tranform the lead object if possible.

Feel free to modify and share these scripts as desired.




Posted 27 June 2012 4:57 pm

what happened? a lot of posts are down now!!! I was hoping to get this mel again !


Posted 22 May 2013 4:55 pm

hello sir,
I have a question to ask. I want to add two different events on same particle shape, when particles hit one object it emit 1 particle with no inherit velocity but when the same particles hit the second object it should emit 10 particles with inherent velocity. Is it possible ??? Any expression or something else ??

Duncan Brinsmead

Posted 22 May 2013 6:43 pm

The particle event method is somewhat limited. It is more flexible if instead you handle things with expressions, using the emit command to generate particles. To test for collisions add a collisionGeometryIndex particle attribute(under addDynamicAttributes on the particle shape) and check its value inside the runtime expression. If you are using nParticles then the collision attributes were improved in maya2014 (or possibly 2013)... in earlier versions it was harder to get the exact object id for the collision. (although it always worked with the classic particles)


Posted 23 May 2013 6:25 pm

Thank you sir for this quick help, Its been very helpful.

Add Your Comment

You must be logged in to post a comment.

Please only report comments that are spam or abusive.