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.