2

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()

Gorgious
  • 30,723
  • 2
  • 44
  • 101
Michele
  • 21
  • 2
  • 1
    Related: https://blender.stackexchange.com/questions/209342/batch-rename-add-on-inverted-sequential-numbering/ – Markus von Broady Apr 12 '21 at 08:46
  • 1
    Related https://blender.stackexchange.com/questions/209232/blender-python-to-hook-vertex-and-empty – batFINGER Apr 12 '21 at 11:12
  • 1
    & with modifiers https://blender.stackexchange.com/q/196746/15543 using operator to add empty. Recommend method of former, (adding via bpy.data.objects.new(...)) to avoid performance issues related to blender operators. – batFINGER Apr 12 '21 at 11:24

2 Answers2

2

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')

batFINGER
  • 84,216
  • 10
  • 108
  • 233
0

just add this line as last line:

bpy.data.objects['Empty'].name = "Hook1"
Chris
  • 59,454
  • 6
  • 30
  • 84
  • It just renames the cube, not the plain axes enter image description here – Michele Apr 12 '21 at 08:52
  • sorry, you are right...i will search for the right solution – Chris Apr 12 '21 at 08:54
  • i updated my answer – Chris Apr 12 '21 at 08:57
  • @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. – Chris Apr 12 '21 at 11:43
  • 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. – batFINGER Apr 12 '21 at 11:47