How to get a script to run when a Blender object is moved...

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

How to get a script to run when a Blender object is moved...

Post by dvochin » Tue May 21, 2013 8:46 pm

Hello!

I need to get a Python script to run when the user moves some objects in my blender scene (during standard Blender scene editing, not in BGE). In other words I'd like to 'trap move events' on specified objects, hopefully without polling.

Can anyone set me in the right direction?

Many thanks!

Dan,

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Tue May 21, 2013 10:41 pm

would a constraint or driver be applicable? 'cause purely scripted solution might add quite some overhead.
I'm sitting, waiting, wishing, building Blender in superstition...

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

Post by dvochin » Tue May 21, 2013 11:51 pm

CoDEmanX wrote:would a constraint or driver be applicable? 'cause purely scripted solution might add quite some overhead.
Hi CoDEmanX, thanks for taking the time...

In my situation, I need to move spline vertices and bezier point coordinates, but as there is a lot of symetry in that spline, I'm hoping to get code running everytime a spline vertex or bezier point moves to update the other side.

So far, the modal operator sample code at http://wiki.blender.org/index.php/Dev:2 ... l_operator appears closest.

Do you think a constraint or drive can do the above? Any example code that could help drive multiple objects from the movement of the currently edited one?

Thanks!

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Tue May 21, 2013 11:57 pm

hm don't know, rather no i guess. A frame_change_pre handler seems appropriate:

http://www.blender.org/documentation/bl ... dlers.html
I'm sitting, waiting, wishing, building Blender in superstition...

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

Post by dvochin » Wed May 22, 2013 5:49 am

Hi, in case this helps someone, I was able to get very good results with the following technique:

def OnFrameChange(scene):
if (oPin.location != mathutils.Vector(oPin['PosOld'])):
print("OnPinPosChanged: ", oPin.location)
oCutterPts[0].co = oPin.location
oPin['PosOld'] = oPin.location
bpy.context.scene.update()

oCutterCurve = bpy.data.objects['Cutter'].data
oCutterPts = oCutterCurve.splines[0].bezier_points
oPin = bpy.data.objects['Pin0']
oPin['PosOld'] = oPin.location

bpy.app.handlers.scene_update_post.append(OnFrameChange)


(You wouldn't want to keep the scene too long with hooks into 'scene_update_post' as it's a high overhead, but for small durations you'd get very responsive & smooth results! :)

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Wed May 22, 2013 9:24 am

you should replace the scene update handler by a frame change handler, as scene updates occur 20-40 times second (on windows at least), the frame change event occurs only on frame changes (also for playback) which should be suitable for your case
I'm sitting, waiting, wishing, building Blender in superstition...

Post Reply