Parent Lattice verts to a mesh

Ok, I have a problem that requires me to parent 63 lattice verts to a Mesh that is identical. ie, each lattice vert has a corresponding vert in a mesh object sat directly on top of it.

Currently I have solved the problem by individually selecting every vert of the lattice and adding a hook, then selecting every hook and parenting the empty to a vert on the mesh object. but this is slow and ugly and does not scale well if i have a more complex lattice.

So I want to write a script that does this for me, and removes the need for the hook empties by parenting the lattice verts directly to the mesh object verts.

I have some really bad sudo code, I have not coded for blender since 2.4x and i have been working in C# since then so if someone could translate this from gibberish into python that would help me get my head around it again.

lattice = selection.lattice
object = selection.object
tolerance = 0.1

foreach (vert lattice_vert in lattice.verts){

        foreach (vert object_vert in object.verts){
            
            if ( Distance(lattice_vert.position_world, object_vert.position_world) < tolerance ){

                lattice_vert.parent = object_vert

}}}

Thank you

Doing a bit more research its not going to be as simple as I first thought. You don’t seem to be able to parent a vertex to something, you can only use a hook to do that, as far as I can tell. So I will still have to add the mass of empties and hook modifiers, unless someone can think of a better way of doing it.

any ideas.

You can actually parent vertices:

mesh_ob = bpy.data.objects['Cube']
lattice_ob = bpy.data.objects['Lattice']

mesh_ob.parent = lattice_ob # you may need to reset loc/rot/scale afterwards, or apply transforms on lattice object

mesh_ob.parent = 'VERTEX'
mesh_ob.parent_vertices = (3,3,3) # parent to vertex index 3
# use 3 different indices if you want to parent to a virtual triangle (includes rotation)

The order of setting the parent properties matters!

@CoDEmanX
Would that not parent the mesh to the lattice? I want it so each vertex of the lattice is parented to a vertex on the mesh. so its not an object parented to a vertex.

thanks for your help

Ok guys I kinda solved the issue without needing to code jack.

first of all on the Blender Sushi website is some awesome scripts for interacting with a lattice:

The one that creates a hook for every lattice vert is the one that sped-up my workflow a lot.

But, using a later version of blender from graphicall.org alowed me to use the mesh deform modifier on the lattice, something that was not possible in the version of blender i was using. Which obviously solves the problem I was having.

You can also parent lattice verts to mesh verts, but it’s only useful if you parent another object to the lattice. The top mesh element will effect the lattice, and that lattice the other mesh object.