1

I'm looking for a way to automatically invoke a custom Python script upon (manually) moving one or more vertices in a mesh (in the 3D Viewport or in the UV Editor). I've come across a couple of options while doing some searches with keywords including callback, listener and handler (see below), though I'm not yet sure how to approach it efficiently.

One option would be to make the triggering event less specific, e.g. invoking the Python script every time the 3D view is updated. In that case, bpy.types.SpaceView3D.draw_handler_add might be the way to go, though it does not sound optimal.

Alternatively, there is the relatively new msgbus, though on https://docs.blender.org/api/2.83/bpy.msgbus.html the following is mentioned:

The following updates do not trigger message bus notifications:

  • Moving objects in the 3D Viewport.

This seems to rule out using msgbus for my purposes. Any other suggestions on how to approach this?

Ailurus
  • 207
  • 1
  • 7
  • 2
    Sub-optimally-related https://blender.stackexchange.com/questions/160909/handle-vertex-property-changes-with-msgbus-subscribe-rna – batFINGER Apr 27 '20 at 16:07

1 Answers1

1

You need to set up a handler upon depsgraph update and check whether the update is related to a change in the geometry:

import bpy

def on_mesh_update(obj, scene): pass # Do whatever you want here

def on_depsgraph_update(scene): depsgraph = bpy.context.evaluated_depsgraph_get() edit_obj = bpy.context.edit_object for update in depsgraph.updates: if update.id.original == edit_obj and update.is_updated_geometry: on_mesh_update(edit_obj, scene)

Make blender call on_depsgraph_update after each

update of Blender's internal dependency graph

bpy.app.handlers.depsgraph_update_post.append(on_depsgraph_update)

Lithy
  • 281
  • 2
  • 5