0
$\begingroup$

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')
spline.points.add(len(coords_list)-1)
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)
bpy.context.scene.collection.objects.link(Path)

enter image description here

Setting bpy.data.objects['_Path'].modifers["....ID...."].vertex_indices[0] is read only.

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

any Ideas ?

THX Martin

$\endgroup$
5
  • $\begingroup$ looking for hook.vertex_indices_set([ths indices])? $\endgroup$ – lemon Sep 13 '20 at 14:41
  • $\begingroup$ 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 ........... $\endgroup$ – martburg Sep 13 '20 at 18:38
  • $\begingroup$ 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. $\endgroup$ – lemon Sep 13 '20 at 18:40
  • $\begingroup$ in complement, as far as i know, curves have no vertex groups $\endgroup$ – lemon Sep 13 '20 at 18:41
  • $\begingroup$ Hi lemon, why did you suggest the set ? $\endgroup$ – martburg Sep 13 '20 at 19:10
2
$\begingroup$

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


# 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='NURBS')
spline.points.add(len(coords_list)-1)
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)
    bpy.ops.object.empty_add(
        location=new_co,
        )
    hm = path_ob.modifiers.new(
        name=f"Hook{i}",
        type='HOOK',
        )
    hm.object = context.object
    hm.vertex_indices_set([i]) 


context.collection.objects.link(path_ob)
$\endgroup$
3
  • $\begingroup$ Avoid edit mode but using the operator to create the empty? Just kidding... $\endgroup$ – brockmann Oct 15 '20 at 14:11
  • $\begingroup$ Cheers brockers Aware of the irony. It's such a simple way to add object set location link to scene in one fell swoop. $\endgroup$ – batFINGER Oct 15 '20 at 14:22
  • $\begingroup$ 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. $\endgroup$ – brockmann Oct 15 '20 at 15:45
1
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$ – lemon Sep 13 '20 at 19:23

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.