0
$\begingroup$

How to attach a lot of cubes to the nearest vertices of a certain object using a script? I want to attach red cubes to the nearest vertices of yellow objects using a script. Suppose red cubes belong to the group "GroupA". And these cubes need to be attached to the nearest vertices of the yellow object

I need your help.Thanks!

enter image description here

enter image description here

$\endgroup$
  • $\begingroup$ what is your meaning of "attach"? as parent? as join? $\endgroup$ – yhoyo Nov 13 '19 at 22:42
  • $\begingroup$ Cubes must follow the vertices as join $\endgroup$ – RenBenPBR Nov 13 '19 at 22:48
1
$\begingroup$

Download the blend file and test the script (updated)

in order to parent the cubes after before

import bpy


def conectando(vertice, objeto):
    cd = main.data
    cd.vertices[vertice].select=True
    bpy.ops.object.mode_set(mode = 'EDIT')
    bpy.context.scene.objects[objeto].select_set(state=True) 
    bpy.ops.object.vertex_parent_set()
    bpy.context.scene.objects[objeto].select_set(state=False) 
    bpy.ops.object.mode_set(mode = 'OBJECT')
    cd.vertices[vertice].select=False



print("*"*50)


#get active object
main = bpy.context.active_object
mat = main.matrix_world

bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')

vertices = [v for v in main.data.vertices]

conectar=[]
for ob in bpy.context.scene.objects:
    if ob.type == 'MESH' and not ob.name == main.name:
        # select object in order to join at the end
        #ob.select_set(state=True) # select object in order to join at the end
        distance = []
        for c in vertices:
            v = c.co
            loc = mat @ v #matrix world of vertex co
            distance.append([(loc-ob.location).length, c.index, ob.name])

        distance.sort()
        conectar.append(distance[0])


for c in conectar:
    conectando(c[1],c[2])

original meshes after script

import bpy

print("*"*50)


#get active object
main = bpy.context.active_object
main_coords = [(main.matrix_world @ v.co) for v in main.data.vertices]

#move other mesh objects to nearest location in active object
for ob in bpy.data.objects:
    if ob.type == 'MESH' and not ob.name == main.name:
        ob.select_set(state=True) # select object in order to join at the end
        distance = []
        for c in main_coords:            
            distance.append([(c-ob.location).length, c])



        distance.sort()   
        ob.location = distance[0][1]    
        print(distance[0][1])
            #$print(ob.name + str(tuple(ob.location)))

#join all objects
bpy.ops.object.join()    
$\endgroup$
  • 2
    $\begingroup$ Maybe ob.matrix_world.translation since ob.location is a local coord in relation to its parent. Would also loop thru context.scene.objects rather than all objects in file to avoid complications of using objects unlinked or linked to other scene. $\endgroup$ – batFINGER Nov 14 '19 at 2:13
  • $\begingroup$ Okay thank you. And how to make them follow without combining into one object. I need a function like "vertex_parent_set". Sorry i was wrong yesterday :( $\endgroup$ – RenBenPBR Nov 14 '19 at 11:44
  • $\begingroup$ @RenBenPBR test new script $\endgroup$ – yhoyo Nov 18 '19 at 16:54
  • $\begingroup$ @yhoyo I try to run on version 2.79 and the console points to line 37, in <module> Type Error: unsupported operand type(s) for @: 'Matrix' and 'Vector' imgur.com/a/daAKxqE $\endgroup$ – RenBenPBR Nov 19 '19 at 23:46
  • $\begingroup$ @RenBenPBR test in 2.8 $\endgroup$ – yhoyo Nov 19 '19 at 23:50

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.