help scripting the hook modifier - blender25

I can not seem to get my hook modifier to bind to an object, anybody else had any luck with hook? I got as far as selecting points, going into edit mode, using the operator to create a new hook modifier - but it seems like its not respecting my point selection.


#mesh = bpy.data.meshes.new(name="Foo")
#obj = bpy.data.objects.new(name="Foo",object_data=mesh)
#bpy.context.scene.objects.link(obj)

import bpy, random, sys

#bpy.ops.mesh.primitive_ico_sphere_add()
for o in bpy.data.objects: o.selected=False
#bpy.ops.object.select_all()
#bpy.ops.object.select_all()
bpy.context.scene.update()


scn = bpy.context.scene
if 0:
	arm_data = bpy.data.armatures.new("MyBVH")
	arm_ob = bpy.data.objects.new("MyBVH", arm_data)
	scn.objects.link(arm_ob)

	arm_ob.selected = True
	scn.objects.active = arm_ob

	bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
	bpy.ops.object.mode_set(mode='EDIT', toggle=False)

	## sloppy
	bpy.ops.armature.delete()
	bpy.ops.armature.select_all()
	bpy.ops.armature.delete()


data = bpy.data.curves.new(name='brain', type='CURVE')
for i in range(4):
	spline = data.splines.new('BEZIER')

	bez = spline.bezier_points[-1]
	bez.co.x = i * 0.4

	prevbone = None
	for j in range(4):
		x = i * 0.4; y = j * 0.1
		spline.bezier_points.add()
		bez = spline.bezier_points[-1]
		bez.co.x = x
		bez.co.y = y
		bez.handle1.x = i * 0.4 + random.uniform(-.1,.1)
		bez.handle1.y = j * 0.1 + random.uniform(-.1,.1)
		bez.handle2.x = i * 0.4 + random.uniform(-.1,.1)
		bez.handle2.y = j * 0.1 + random.uniform(-.1,.1)

		bez.selected_control_point = bez.selected_handle1 = bez.selected_handle2 = True

		if 0:
			bpy.ops.armature.bone_primitive_add(name="Bone")
			bone = arm_data.edit_bones[-1]
			bone.head = [x,y,.0]
			bone.tail = [(j+1)*0.4, (j+1)*0.1, .0]
			if prevbone: bone.parent = prevbone; bone.connected = True
			prevbone = bone

		# and 2 = Vector, what is bez.radius?

bpy.context.scene.update()

		
obj = bpy.data.objects.new(name='brain', object_data=data)
bpy.context.scene.objects.link(obj)
bpy.context.scene.objects.active = obj

#m = obj.modifiers.new(name='armature', type="ARMATURE")
#m.object = arm_ob
#obj.selected = True

#grp = obj.add_vertex_group()
#print( dir(grp) )
#obj.add_vertex_to_group( group=grp, vertex_index=0, weight=1.0, type='ADD' )

#readonly#obj.active_vertex_group = grp

#bpy.ops.object.editmode_toggle()
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
bpy.ops.object.mode_set(mode='EDIT', toggle=True)

#bez.selected_control_point = bez.selected_handle1 = bez.selected_handle2 = True
#obj.update(bpy.context.scene)
bpy.context.scene.update()
#bpy.ops.object.modifier_add('EXEC_DEFAULT',type="HOOK")


#print( dir(obj) )
#print( dir(obj.data) )

hook = obj.modifiers[-1]
#hook = obj.modifiers.new(name='hook', type="HOOK")
#print( dir(hook) )
#hook.vertex_group = grp.name
hook.object = bpy.data.objects['Cube']



1 Like

this code works on small numbers of hooks, but segfaults with too many
http://pastebin.com/qAFuQyEu