1
$\begingroup$

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
$\endgroup$
2
  • 1
    $\begingroup$ Hard to know without seeing the code you already have. $\endgroup$
    – batFINGER
    Commented Jan 23, 2021 at 18:19
  • $\begingroup$ IMO while i < c: is more readable. However would not do it this way since can iterate vertices and get there index from property. $\endgroup$
    – batFINGER
    Commented Jan 24, 2021 at 2:46

1 Answer 1

1
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ 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 $\endgroup$ Commented Jan 24, 2021 at 18:51
  • $\begingroup$ see link at bottom of answer. $\endgroup$
    – batFINGER
    Commented Jan 25, 2021 at 0:31
  • $\begingroup$ 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. $\endgroup$ Commented Jan 25, 2021 at 1:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .