# Adding a Hook to a Nurbs Path with Python

Python scripting

I would like to add a hook to an Nurbs Path while scripting

In Layout :

delete 'Cube' Add Curve -> Path change to Edit Mode select a Vertex ctrl+h

all set and done

Scripting:

# Add Path
coords_list = ([[0,0,0], [1,0,0]])
path = bpy.data.curves.new('_path', 'CURVE')
path.dimensions = "3D"
spline = path.splines.new(type='POLY')
for p, new_co in zip(spline.points, coords_list):
p.co = (new_co + [1.0]) # (add nurbs weight)
Path = bpy.data.objects.new('_Path', path)



Trying to add vertex_groups to the Path just returns a 'None Type'

any Ideas ?

THX Martin

• looking for hook.vertex_indices_set([ths indices])? – lemon Sep 13 '20 at 14:41
• Go ahead claim it as an Answer I only found a hackish way to do it blender.stackexchange.com/questions/13484/… which had to have an extra hack as in 2.83 the operator blocks the setting of p0=False and you have to leave editmode select something ........... – martburg Sep 13 '20 at 18:38
• mmm... please do (you can answer to your own question). That's no problem. And if hackish way give it also: all can be intersting. – lemon Sep 13 '20 at 18:40
• in complement, as far as i know, curves have no vertex groups – lemon Sep 13 '20 at 18:41
• Hi lemon, why did you suggest the set ? – martburg Sep 13 '20 at 19:10

Avoid edit mode if you can.

Suggest this can be done via API calls with no operators. Have lazilly added the empties with the op, add them as you have other objects if so desired.

Test script adds an empty hook to each end of your poly. Note have used NURBS as per question title. To have end points set the use_endpoints_u and v properties of the spline.

import bpy
from bpy import context

coords_list = ([[0,0,0], [1,0,0]])
path = bpy.data.curves.new('_path', 'CURVE')
path.dimensions = "3D"
spline = path.splines.new(type='NURBS')
spline.use_endpoint_u = True
spline.use_endpoint_v = True
path_ob = bpy.data.objects.new('_Path', path)
for i, (p, new_co) in enumerate(
zip(spline.points, coords_list)
):
p.co = (new_co + [1.0]) # (add nurbs weight)
location=new_co,
)
hm = path_ob.modifiers.new(
name=f"Hook{i}",
type='HOOK',
)
hm.object = context.object
hm.vertex_indices_set([i])


• Avoid edit mode but using the operator to create the empty? Just kidding... – brockmann Oct 15 '20 at 14:11
• Cheers brockers Aware of the irony. It's such a simple way to add object set location link to scene in one fell swoop. – batFINGER Oct 15 '20 at 14:22
• I think it's time for: EasyBPY. Be aware: the author "will read pull requests on GitHub, but I will not accept them unless they are perfect." from: curtisholt.online/blog/easybpy... lol. – brockmann Oct 15 '20 at 15:45

After lemon prompted me to answer myself:

The hackish way I is based on Using python to create a curve and attach its endpoints with hooks to two spheres where Mutant Bob is reposting http://web.purplefrog.com/~thoth/blender/python-cookbook/hook-modifier-curve.html The Idea behind this hack seems to be changing into Edit Mode

set P0.select = True and use
bpy.ops.object.hook_assign(modifier = ...) to set the vertex_indices

This was 2014 in Blender 2.83

the first hook_assign operator somehow (don't ask me - it's an observation) blocks P0.select = False

So:

after changing to object mode -> selecting something there -> selecting the Path once more -> changing to edit mode again -> set P0.select = False -> set P1.select = True -> call the operator to assign the hook -> change back to Object Mode

What a disgrace spent some hours to come up with this.

• well.. upvoted it... but... was the complicated way (if I understand well). Why did'nt you just hook_modifier.vertex_indices_set([the indices you want]) ? avoid bpy.ops when you can. oh... ok... you can: hook = obj.modifiers.new( "hook", "HOOK"), then hook.vertex_indices_set, etc. – lemon Sep 13 '20 at 19:23