# Blender python to hook vertex and empty

I am looking at how to use python and make a code that automatically detects an empty on a vertex point and hooks them together. How can I do this and add it to the code I already have?

import bpy
from bpy import context
i = 0
c = 8

obj = context.active_object
while not i == c:
v = obj.data.vertices[i]
co_final = obj.matrix_world @ v.co
obj_empty = bpy.data.objects.new("Point", None)
context.collection.objects.link(obj_empty)
obj_empty.location = v.co
obj_empty.parent = obj
i += 1

• Hard to know without seeing the code you already have. – batFINGER Jan 23 at 18:19
• IMO while i < c: is more readable. However would not do it this way since can iterate vertices and get there index from property. – batFINGER Jan 24 at 2:46

## 1 Answer

Set parent type to vertex.

Going by code in question it appears you wish to set each vertex as a parent of an empty.

In this case moving the vertex moves the empty.

Snaffled code from this answer

Notice that there is no requirement to set the empty location to the vertex, rather let the parenting take care of this. Having zero local translation from the location of the parent. Using the UI confuses the issue somewhat as it sets the parent inverse

import bpy
from bpy import context

ob = context.object
coll = context.collection

for v in ob.data.vertices:
mt = bpy.data.objects.new(
f"Vert{v.index}",
None,
)
mt.empty_display_type = 'CIRCLE'
mt.empty_display_size = 0.2
mt.parent = ob
mt.parent_type = 'VERTEX'
mt.parent_vertices = [v.index] * 3
coll.objects.link(mt)


Re "hook" generally infers hooking a vertex to the empty such that moving the empty moves the vert.

How to iterate through mesh vertices and add hooks using script

• I want to be able to have code that puts the empties on the vertices and be able to have the empty move the vert it is closest to – GoldenGlitch Jan 24 at 18:51
• see link at bottom of answer. – batFINGER Jan 25 at 0:31
• I am trying to make a code that can combine two separate meshes and make its own unique mesh. I believe I am going about it all wrong. – GoldenGlitch Jan 25 at 1:20