Here is a sample code which creates a primitive bezier spline (location1 and 2 are the control points' location):

def createBezierCurve(location1, location2):
    dirVect = getNormalizedDirVector(convertDictCoordToTuple(tableCenterCoords), convertDictCoordToTuple(refTableCenterCoords))
    obj = bpy.context.object
    obj.data.dimensions = '3D'
    obj.data.fill_mode = 'FULL'
    obj.data.bevel_depth = 0.02
    obj.data.bevel_resolution = 12

    obj.data.splines[0].bezier_points[0].co = location1
    obj.data.splines[0].bezier_points[0].handle_left = addVectors(location1, dirVect)
    obj.data.splines[0].bezier_points[0].handle_right = subtractVectors(location1, dirVect)
    obj.data.splines[0].bezier_points[0].handle_left_type = 'VECTOR'

    empty1 = bpy.data.objects.new("EmptyObject", None)
    empty1.location = location1
    hookmod1 = obj.data.splines[0].bezier_points[0].modifiers.new("Hook", type='HOOK')
    hookmod1.object = empty1

    obj.data.splines[0].bezier_points[1].co = location2
    obj.data.splines[0].bezier_points[1].handle_left = addVectors(location2, dirVect)
    obj.data.splines[0].bezier_points[1].handle_right = subtractVectors(location2, dirVect)
    obj.data.splines[0].bezier_points[1].handle_left_type = 'VECTOR'

    empty2 = bpy.data.objects.new("EmptyObject", None)
    empty2.location = location2
    hookmod2 = obj.data.splines[0].bezier_points[1].modifiers.new("Hook", type='HOOK')
    hookmod2.object = empty2

What I couldn't find is how to hook the points to these empties?


To add a hook modifier

Following code adds a hook modifier. named Hook1, to the curve object CurveOB and sets the hook object to Empty1

hookmod = curveOB.modifiers.new("Hook1", type='HOOK')
hookmod.object = empty1
  • $\begingroup$ (Take in consideration that I'm very new to Blender) Okay, but how can I hook to a vertex of a curve, not the whole curve? How can I "get" a curve object's vertex? $\endgroup$ – Arklur Jan 4 '18 at 15:25
  • $\begingroup$ I "revised" my idea, and I got to the point where I know have to code this: First select 1 of the 2 control points of a bezier_point (i.imgur.com/ocuPq9G.png), then "Hook to a New Object" (i.imgur.com/ClG4dQC.png). Could you edit your code so it can work? I edit my code, but of course it doesn't work because it throws "AttributeError: 'BezierSplinePoint' object has no attribute 'modifiers'" error. $\endgroup$ – Arklur Jan 5 '18 at 11:46
  • $\begingroup$ In your new code above: Use obj.modifiers.new(...) , where obj is the curve object created via operator. The modifiers collection (obj.modifiers) is on the curve object, not the bezier point of the curve objects data. Setting the location of the empty positions the hook. Suggest your method above return obj, empty1, empty2 $\endgroup$ – batFINGER Jan 5 '18 at 13:53
  • $\begingroup$ .. oh and give them a different name otherwise blender will auto name the second "Hook.0001" Finally, could use the Hook to New Object operator, but off top of my head - without checking, will need to be run with the curve in edit mode. As a rule of thumb try avoiding operators when an API method is available. $\endgroup$ – batFINGER Jan 5 '18 at 14:07

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.