2
$\begingroup$

I'm looking for a sort of event listener that triggers after a transform has finished. So far i found this:

def scene_update(context):
    if bpy.data.objects.is_updated:
        print("One or more objects were updated!")
        for ob in bpy.data.objects:
            if ob.is_updated:
                print("=>", ob.name)
bpy.app.handlers.scene_update_post.append(scene_update)

I need to add something so that the print only happens after the translation of a vertex is completed (not while i'm still dragging the vertices around). Any idea what ?

| improve this question | | | | |
$\endgroup$
1
$\begingroup$

This is a mix of the answers here Find out if Mesh Operator (bpy.ops.mesh...) was cancelled or not via python and How to get the active vertex within a Handler?

Looks for bpy.ops.transform.translate as the active operator and if used prints out the selected verts and amount transformed.

import bpy, bmesh
from bpy.app.handlers import persistent
from mathutils import Vector

def check_operators(bm):
    op = bpy.context.active_operator
    if op:
        if op.bl_idname == 'TRANSFORM_OT_translate':  #bpy.ops.transform.translate.idname()
            #print("bpy.ops.transform.translate(%s)" % " ,".join(["%s=%s" % (k, str(v)) for k, v in op.properties.items()]))
            print(op.name)
            v = Vector(op.properties["value"])
            if v.length:
                print("value", str(v))
                print("verts", [v for v in bm.verts if v.select])
            else:
                print("operator canceled")

dic = {}
@persistent
def edit_object_change_handler(scene):

    obj = scene.objects.active
    if obj is None:
        return None

    if obj.mode == 'EDIT' and obj.type == 'MESH':
        bm = dic.setdefault(obj.name, bmesh.from_edit_mesh(obj.data))
        check_operators(bm)
        return None

    dic.clear()
    return None

def register():
    bpy.app.handlers.scene_update_post.clear() # for testing
    bpy.app.handlers.scene_update_post.append(edit_object_change_handler)

register()
| improve this answer | | | | |
$\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.