8
$\begingroup$

Is there a way to access an objects location data from the previous frame in cycles? I want to make a shader that colors the object accordingly to the distance the object traveled between the current frame and the last one. My goal is an animation with moving cubes colored by the "speed" they have.

Also, I guess thats information that is not available in osl?

$\endgroup$
6
$\begingroup$

See the docs about drivers, they're comprehensive.
Also see TextEditor -> Templates -> Python -> Driver Functions

Add a Driver

If your object is key-framed, you can evaluate the F-Curve using a frame number like fcurve[index].evaluate(frame-1)

  • Add a Driver to a custom property of your object. enter image description here

  • Make a python expression text block with something like the code below, and Run the code so the expression is added to the driver_namespace

    import bpy
    from mathutils import Vector
    
    def driver_delta_to_RED(frame):
        # triggered by a frame change, any code inside here gets run.
    
        p = bpy.data.objects['Plane']
        current_xyz = p.location
        fcurve = p.animation_data.action.fcurves
    
        x = fcurve[0].evaluate(frame-1)
        y = fcurve[1].evaluate(frame-1)
        z = fcurve[2].evaluate(frame-1)
    
        # may want to find the top speed first, and normalize
        # this value using that information
        delta = (current_xyz - Vector((x, y, z))).length
    
        nodes = bpy.data.materials[0].node_tree.nodes
        nodes[1].inputs[0].default_value = (delta, 0, 0, 1.0)
    
        # the return value is of no relevance and can be static.
        return 0.0
    
    bpy.app.driver_namespace['driver_delta_to_RED'] = driver_delta_to_RED
    
  • Add the function call to the Driver enter image description here

if it's a frame behind or ahead of what you want, then instead of the Driver, it might make sense to add an application_handler specifically the frame_change_pre or frame_change_post handlers.

The code for Drivers and frame_change handlers is slightly different, but the app handler docs will describe how to get the current frame from inside the handler function.

$\endgroup$
4
$\begingroup$

You can simply use the animation nodes addon from Jacques Lucke Blender Artists Post (for further informations you can search on youtube) To accomplish you objective you need to use a frame input and a cycles material output.

Node Setup:

Node Setup

You need to turn on the "Frame" Input and you need to turn off the "Use Current Transforms" under the Advanced Node Settings to access the location from the previous frame.

Result:

Example

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.