Run when an bone moves

Hi everyone,

I need a bit of help on a script. What would be the best way to have a script run when an object moves. Either in the viewport or on play?

I want to have a bone control the name of the texture to kind of create a stop motion effect.

Thank you for your help.

Mathias.

I found that code that I changed a tiny bit from Batfinger I think. But I’m not sure about the scene_update_pre, I’m wondering if there would not be a better way.


import bpy


data = {}  #keep data in a dictionary
TOL = 1.0e-4 #how much the cube needs to move in a frame


def loc_change(scene):
    # run some scripts if the cube has moved.
    cube = scene.objects.get("Cube")
    loc = cube.location
    
    dloc = loc - data["CubeLoc"]
    
    
    if dloc.length > TOL:
        print("FRAME:%d Cube has moved" % scene.frame_current)
        data["CubeLoc"] = loc.copy()
        
bpy.app.handlers.scene_update_pre.append(loc_change)


# initialise data
data["CubeLoc"] = bpy.data.objects.get("Cube").location.copy()