0

How can I create a segment with for example 30 verices and then connect an empty with an Hook parenting to every veritces in the segment, all this via Python in Blender 3d?

Fabrizio
  • 1,138
  • 4
  • 18
  • 41

1 Answers1

0

I'm going to say that this was frustrating but after trying several different approaches this is the one way that I got to work.

import bpy
import bmesh

num_verts = 30

scn = bpy.context.scene
D = bpy.data.objects

verts = []
edges = []
for i in range(num_verts):
    verts += [(i, 0.0, 0.0)]
    if i > 0:
        edges += [(i, i-1)]

mesh_data = bpy.data.meshes.new("hooked verts")
mesh_data.from_pydata(verts, edges, [])
mesh_data.update()
obj = D.new("Hooked line", mesh_data)
obj.select = True
scn.objects.link(obj)
scn.objects.active = obj

bpy.ops.object.mode_set(mode='EDIT')

for i in range(len(obj.data.vertices)):
    bm = bmesh.from_edit_mesh(obj.data)
    bpy.ops.mesh.select_all(action='DESELECT')
    bm.verts.ensure_lookup_table()
    bm.verts[i].select = True
    bpy.ops.object.hook_add_newob()
    bpy.context.selected_objects[0].name = 'Hook'
    bm.free()

bpy.ops.object.mode_set(mode='OBJECT')

To assign a hook to a vertex, the object needs to be in edit mode with the desired vertex selected. It would seem that the add hook operators make a mess of the edit mesh data so that after the first hook modifier is created the mesh data is no longer valid. The solution - re-create the bmesh data and select a vertex after creating each hook.

sambler
  • 6,917
  • 1
  • 16
  • 23