My main goal is to separate multiple geometries the same way by vertice selection. All the objects differ only a little bit in their vertice position, because they only are deformed by already applied shape keys originating from the same origin object (amount and distribution of vertices are the same). My current strategy (corresponding to the bpy script below): Starting with a selection of vertices on the initial geometry, I would like to additionally import multiple very similar ones. By saving the vertice selection, the separation should be applied one object after another. (edited)
Please see my code below. Since it did not work to directly compare vertice objects, that initially have been stored to a list (previous version), I now try to compare list indexes.
Unfortunately, my example code only separates one object in the two geometrical parts:
import bpy
import os, glob
# script to separate same edit mode selection on multiple imported .fbx files in directory
class Import(object):
def __init__(self):
pass
@staticmethod
def import_fbxfiles_from_dir(path = "C:\\Users\\user\\Desktop\\test_parts\\"):
os.chdir(path)
for fbx in [f for f in os.listdir(path) if f.endswith(".fbx")]:
print ("imported file: " + fbx)
bpy.ops.import_scene.fbx(filepath=fbx, filter_glob=".fbx;", axis_forward="-Z", axis_up="Y")
class Separate_test(object):
def __init__(self):
pass
def separate_initially_selected_verts(self):
obj = bpy.context.scene.objects.active # active object
mesh = obj.data
self.selected_vert_list_IDs = [key for key,v in enumerate(mesh.vertices) if v.select]
print("selected verts:", self.selected_vert_list_IDs)
bpy.ops.object.mode_set(mode='OBJECT') # exit edit mode
obNr = 0
for ob in bpy.context.scene.objects:
if ob.type == 'MESH':
print("-- Object:", obNr, "/", len(bpy.context.scene.objects))
bpy.context.scene.objects.active = ob
# set to edit mode
bpy.ops.object.mode_set(mode='EDIT')
# select verts from list
count_select = 1
for key_ob,v in enumerate(bpy.context.scene.objects.active.data.vertices):
if key_ob in self.selected_vert_list_IDs:
count_select += 1
# print("vertices separated in obj: {0} of {1}".format(count_select, len(self.selected_vert_list_IDs)))
v.select = True
bpy.ops.mesh.separate(type='SELECTED')
bpy.ops.object.mode_set(mode='OBJECT') # exit edit mode
obNr += 1
if __name__ == "__main__":
Import.import_fbxfiles_from_dir()
separate_obj = Separate_test()
separate_obj.separate_initially_selected_verts()
input("press return to quit.")