Main Index : Tutorials :

Writing delta time plugins


Normally an expression or animation plugin should act like a mathematical function. You put in the current state of the scene, and out comes a new state in a predictable way. With delta time dependent plugins, this isn't so. The new state is dependent not only on the current state of the scene, but also on previous states of the scene.

Note: Even if you don't plan to write true delta time plugins, the following rules are still a good checklist for all kinds of plugins.

An example is the particle system in C4D. Every frame, each particle is moved depending on how much time has elapsed since the last frame. Thus the new state depends on the time between frames, the delta time.

When writing delta time plugins, or other kinds of plugins that behave in this way, there a few important considerations to make the plugin work under all circumstances (e.g. net rendering and MP rendering). This tutorial will present the most important requirements.

Note: If you feel that these requirements are too complicated, then fine; your plugin will probably work if handled with care. But be sure to tell other users in the readme file that it will break when net rendering, MP rendering or whatever!

1. Self-contained and copyable state

C4D must at any time be able to copy the full state of your plugin to another instance, for example when distributing a scene for net rendering. If all of your data (for example settings and the last frame's time) is stored in your plugin's BaseContainer then you are lucky, the data will be copied automatically.

However, if you have any data (that is important to the state of the plugin) that is stored in another way (e.g. in a member variable), you must provide Copy(), Load() and Save() functions.

2. No random randomity

This is really included in the above requirement, but might be worth pointing out separately. If you have a random number generator in your algorithm, it's state must also be copyable. This means that you can't just seed it with time() in every instance, because then it will give different random numbers on different machines when net rendering.

3. Well-defined initial state

The first frame of the scene must have a well-defined state. This is important, since C4D always pre-rolls the renderer from the first frame. The easiest way to do this is to just check in the Execute() or Animate() function if the current frame is the first one, and then reset all objects to their starting positions if it is.

4. Use the real delta time

Don't just count on the time since your plugin was last evaluated always to be equal to 1/30th of a second, or even equal to doc->GetFps(). Always store the time of the last evaluation, and calculate the delta time yourself using (delta time) = (current time) - (previous time). The delta time might even be negative, if the user is randomly dragging the time slider.

Examples: randomRotation.cof, gravity.cof