When using the “depsgraph_update” handler, making actions with objects passed to the handler does not give the full result. For example, properties of an object referring to it through the “object.id”, could be not completely changed.
Let’s define the “depsgraph_update_post” handler, inside which we will change some object’s property, for example, the viewport color of the object.
1 2 3 4 5 6 7 8 |
def on_depsgraph_update(scene, depsgraph): for obj in depsgraph.updates: if isinstance(obj.id, Object): col = (0.0, 1.0, 1.0, 1.0) obj.id.color = col depsgraph_update_post.append(on_depsgraph_update) |
After registering the handler let’s make any changes in the current mesh (to execute the handler).
In this example, to change the color of an object, we referred to it through the “object.id”:
1 |
obj.id.color = col |
As we can see, the color of the object in the viewport changes, but the color in the parameter on the object properties panel – doesn’t. After unregistering our handler, when the mesh changes, its color is restored with the original color from the parameter.
To avoid this behavior, it is better to refer to the object by name through the “bpy.data.objects” list.
The same handler, with accessing an object through the “bpy.data.objects”, changes the color of the object completely both in the viewport and in the parameter:
1 2 3 4 5 6 7 8 |
def on_depsgraph_update(scene, depsgraph): for obj in depsgraph.updates: if isinstance(obj.id, Object): col = (0.0, 1.0, 1.0, 1.0) bpy.data.objects[obj.id.name].color = col depsgraph_update_post.append(on_depsgraph_update) |
*.blend file with the example code for my Patreon subscribers.
Hi
is there anyway to determine the source of the update? ie. Whether the event is happened on user dragged the object or through they changed location through property window?
Thanks
Hi!
You can get updates from the UI by the MsgBus.
Hi, I have a question related to the depsgraph update for rendering an object from multiple cameras from different angles. Is there a way to stop the depsgraph update? as there is no actual change to the object itself, it is just the camera selection been changed.
Hi!
I doubt this is possible. Most likely the depsgraph is updated in any case. When changing the camera, the render engine, without even recalculating the physical position of objects, must still recalculate, for example, lighting and highlights.