2
$\begingroup$

I need to rename each new empties I've made. Can you help me?

import bpy, bmesh

#select vertex

bpy.ops.object.mode_set(mode = 'OBJECT')
obj = bpy.context.active_object
bpy.ops.object.mode_set(mode = 'EDIT') 
bpy.ops.mesh.select_mode(type="VERT")
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')
for vert in range(len(obj.data.vertices)):
    obj.data.vertices[vert].select = True
    bpy.ops.object.mode_set(mode = 'EDIT') 
    bpy.ops.object.hook_add_newob()
$\endgroup$
3
2
$\begingroup$

The hook is the object of last modifier.

When a modifier is added it is going to be the last one on the modifiers stack. Since the operator has added both the modifier and the empty and assigned it to the object property of the modifier, then

new_mod = obj.modifiers[-1]
new_mod.object.name = "Foo"

The whole process of adding hooks can be achieved without using the operator as demonstrated here, or any operator had the empties not been added with one

How can I animate a vertex translation of a triangular mesh?

and the "opposite" ie parenting empties to verts so they follow verts

Blender python to hook vertex and empty

Have used foreach_set to deselect all edges and faces and each vert. Basically for a 3 vertex object

me.vertices.foreach_set("select", [True, False, False])

is equivalent to

me.vertices[0].select = True
me.vertices[1].select = False
me.vertices[2].select = False

Test Script.

import bpy

obj = bpy.context.object
me = obj.data
# deselect all faces and edges
me.polygons.foreach_set("select", (False,) * len(me.polygons))
me.edges.foreach_set("select", (False,) * len(me.edges))
n = len(me.vertices)
for i in range(n):
    bpy.ops.object.mode_set(mode='OBJECT')
    me.vertices.foreach_set("select", [i == j for j in range(n)])
    bpy.ops.object.mode_set(mode='EDIT') 
    bpy.ops.object.hook_add_newob()
    obj.modifiers[-1].object.name = f"Hook_{i}"
    
bpy.ops.object.mode_set(mode = 'OBJECT')
$\endgroup$
0
$\begingroup$

just add this line as last line:

bpy.data.objects['Empty'].name = "Hook1"
$\endgroup$
5
  • $\begingroup$ It just renames the cube, not the plain axes [![enter image description here][1]][1] [1]: i.stack.imgur.com/JWXmp.png $\endgroup$ – Michele Apr 12 at 8:52
  • $\begingroup$ sorry, you are right...i will search for the right solution $\endgroup$ – Chris Apr 12 at 8:54
  • $\begingroup$ i updated my answer $\endgroup$ – Chris Apr 12 at 8:57
  • $\begingroup$ @batFINGER: i did indent it - it didn't work. It changed the cubes name, but not the empty name. And i have no idea why. i just tested it. $\endgroup$ – Chris Apr 12 at 11:43
  • $\begingroup$ Yep my bad, forgot it was edit mode. Anyway recommend using API method to add and wire up the hooks and remove the need for edit mode altogether. See my last link under OP. Test for any preexisting objects named "Empty" first if using method above. $\endgroup$ – batFINGER Apr 12 at 11:47

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.