1
$\begingroup$

I would like to animate an object using vertices as anchors. Blender 2.70a implements the Laplacian deformation method for methods. I have the following code to familiarise myself with hooks. As a trial, I create a for loop to increment the frame number and set the hook location in that frame to a position chosen from a list of coordinates. However, when I scroll back through the frames, the hook location is always in the same place. So the following code is setting the hook location for the entire timeline, not frame-by-frame.

import bpy

# use default cube
ob = bpy.data.objects['Cube']
obdata = bpy.data.meshes['Cube']

# make sure everything is deselected before selecting vertex for hook
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
bpy.ops.mesh.select_all(action="DESELECT")
bpy.context.tool_settings.mesh_select_mode = [True, False, False] # (Vertex,Edge,Face)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

# select vertex, add hook, rename to Hook1
obdata.vertices[0].select=True
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
bpy.ops.object.hook_add_newob()
bpy.data.objects['Empty'].name = "Hook1"

# create a list of positions to move the hook through
positions = [(0,0,-2),(0,1,-2),(3,2,-1),(3,4,-1),(1,2,-1)]

scene = bpy.context.scene

frame=0

for position in positions:
  scene.frame_set(frame)
  bpy.data.objects['Hook1'].location = position
  print( bpy.data.objects['Hook1'].location )
  bpy.data.objects['Hook1'].keyframe_insert(data_path="location", frame=frame, index=-1)
  frame+=10

UPDATE: Added keyframe insertion.

$\endgroup$
3
  • $\begingroup$ You need to insert keyframes for the locations to be remembered, but idk how to do it from python. $\endgroup$
    – gandalf3
    May 18 '14 at 0:45
  • $\begingroup$ Thanks, I was confused by keyframe_insert but figured it out. Updated script. $\endgroup$
    – noname
    May 18 '14 at 1:09
  • $\begingroup$ Does it work now? $\endgroup$
    – gandalf3
    May 18 '14 at 2:37
0
$\begingroup$

In order for blender to store the location on each frame, you must insert a keyframe.

See the blender API reference for details.

$\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.