Page MenuHome

T77086 Animation: Passing Dependency Graph to Drivers
ClosedPublic

Authored by Sybren A. Stüvel (sybren) on Jun 16 2020, 12:40 PM.

Details

Summary

Custom driver functions need access to the dependency graph that is triggering the evaluation of the driver. This patch passes the dependency graph pointer through all the animation-related calls.

Instead of passing the evaluation time to functions, the code now passes an AnimationEvalContext pointer. This points to a struct that's currently defined in BKE_animsys.h (I'm open to suggestions for other places to put it):

typedef struct AnimationEvalContext {
  struct Depsgraph *const depsgraph;
  const float eval_time;
} AnimationEvalContext;

These structs are read-only, meaning that the code cannot change the evaluation time. Note that the depsgraph pointer itself is const, but it points to a non-const depsgraph.

There are two functions that allow creation of AnimationEvalContext objects:

  • BKE_animsys_eval_context(Depsgraph *depsgraph, float eval_time), which creates a new context object from scratch, and
  • BKE_animsys_eval_context_at(AnimationEvalContext *anim_eval_context, float eval_time), which can be used to create a AnimationEvalContext with the same depsgraph, but at a different time. This makes it possible to later add fields without changing any of the code that just want to change the eval time.

While I was going over all the changes, I noticed that time remapping for NLA strips is done in very similar ways in various places of the code. I feel that this is something that should be refactored into a single function. This is outside the scope of this patch, though.

This also provides a fix for T75553: python unreliable result for bpy.context.view_layer.name when used in drivers, although it does require a change to the custom driver function. The driver should call custom_function(depsgraph), and the function should use that depsgraph instead of information from bpy.context.

I have tested the code on a simple walk animation from Blender Cloud, as well as the EEVEE version of Spring. The test included creating an NLA action for the simple walk animation, and moving it around and animating its evaluation time.

@Clément Foucault (fclem) was added as a reviewer for the changes to EEVEE (it evaluates animation for motion blur), and @Antonio Vazquez (antoniov) for the grease pencil code.

Diff Detail

Repository
rB Blender

Event Timeline

Sybren A. Stüvel (sybren) requested review of this revision.Jun 16 2020, 12:40 PM
Sybren A. Stüvel (sybren) created this revision.
  • Updates to comments.
Brecht Van Lommel (brecht) added inline comments.
source/blender/python/intern/bpy_rna_anim.c
337–339

My understanding is that it will never actually use the depsgraph here, because that's only used when options has INSERTKEY_DRIVER, which is not available in the Python API.

No harm in leaving the code since that would be a fragile assumption, but would be good to clarify the comment.

This revision is now accepted and ready to land.Jul 17 2020, 1:14 PM

From scrolling through seems fine. The details and need of this we've discussed here already.

Only weird thing is feels like BKE_animsys_eval_context is missing a verb, like get, init, create, construct or something, and it reads as if it will evaluate the context. I would call it BKE_animsys_eval_context_construct as it is basically a substitute for a missing constructor in C.

  • Rebased onto current master
  • Renamed BKE_animsys_eval_context and BKE_animsys_eval_context_atBKE_animsys_eval_context_construct and BKE_animsys_eval_context_construct_at

New motion blur code does not need the previous hack. so all good for me.