# mission by Seppo Silaste
# http://blender.stackexchange.com/questions/13484/using-python-to-create-a-curve-and-attach-its-endpoints-with-hooks-to-two-sphere
import bpy
def hookCurve(o1, o2, scn):
curve = bpy.data.curves.new("link", 'CURVE')
curve.bevel_depth = 0.2
curve.bevel_resolution = 4
spline = curve.splines.new('BEZIER')
spline.bezier_points.add(1)
p0 = spline.bezier_points[0]
p1 = spline.bezier_points[1]
p0.co = o1.location
p0.handle_right_type = 'VECTOR'
p1.co = o2.location
p1.handle_left_type = 'VECTOR'
obj = bpy.data.objects.new("link", curve)
m0 = obj.modifiers.new("alpha", 'HOOK')
m0.object = o1
m1 = obj.modifiers.new("beta", 'HOOK')
m1.object = o2
scn.objects.link(obj)
scn.objects.active = obj
# using anything in bpy.ops is a giant pain in the butt
bpy.ops.object.mode_set(mode='EDIT')
p0 = curve.splines[0].bezier_points[0]
p1 = curve.splines[0].bezier_points[1]
# Because the API on the hook modifier is incomplete,
# we must resort to using bpy.ops.
p0.select_control_point=True
bpy.ops.object.hook_assign(modifier="alpha")
p0.select_control_point = False
p1.select_control_point = True
bpy.ops.object.hook_assign(modifier="beta")
bpy.ops.object.mode_set(mode='OBJECT')
return obj
o1 = bpy.data.objects['atom 1']
o2 = bpy.data.objects['atom 2']
obj = hookCurve(o1,o2, bpy.context.scene)
bpy.context.scene.objects.active = obj
|
Blender python API quick-start
Syntax highlighting by Pygments.