# Split large mesh into small pieces efficiently using Python

I have use case where I need split a large mesh into small pieces. When I select and separate each of the desired pieces separately, the consolidation of the indices of the large mesh (i.e. building new packed vertex and face lists) takes alot of time. The consolidation is not really necessary in my use case, as I will be splitting off more pieces right away. If I mark all the different split regions using different materials and separate by material, the runtime suggest to me that this approach is also splitting of the regions piece by piece and consolidating the larger mesh in each iteration. So what is the most efficient way to tackle this problem?

This can be done by separating the big mesh into small meshes using the below script:

import bpy
ob = bpy.data.objects['MyMesh'] #### replace MyMesh with the name of your Mesh object
bpy.ops.object.select_all(action='DESELECT')
ob.select = True
bpy.context.scene.objects.active = ob
bpy.ops.mesh.separate(type='LOOSE')


UPDATE

If the object has no loose parts and you know the list of faces to separate then you can loop on them then separate using selection using the script below which I tested and it is working:

import bpy
ob = bpy.data.objects['MyMesh'] #### replace MyMesh with the name of your Mesh object
bpy.ops.object.select_all(action='DESELECT')
ob.select = True
bpy.context.scene.objects.active = ob
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
faces_to_separate = [0,2,4,6,8,10] #### add the list of faces you want to separate
mesh = ob.data
bpy.ops.object.mode_set(mode='OBJECT')
for face in mesh.polygons:
if face.index in faces_to_separate:
face.select = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.separate(type='SELECTED')
bpy.ops.object.mode_set(mode='OBJECT')

• @ideasman42 sorry, I updated my answer. – Tak Feb 6 '17 at 2:29
• This wont work if ob is hidden, or its layer isn't visible - better to pass in a custom context. – ideasman42 Feb 6 '17 at 2:37
• I don't understand your suggestion. Just to clarify: The input mesh has no loose elements, it's a connected mesh that is to be split in specific parts. I know the indices of the faces that go into the split meshes and right now assign different materials to those faces and separate using the 'MATERIAL' type. – DrPepperJo Feb 8 '17 at 12:36
• @DrPepperJo answer updated to separate using a list of faces – Tak Feb 8 '17 at 23:32
• Thanks, this is exactly what I do, but as I mention in the question, this is highly inefficient, as the source mesh structure is consolidated in each iteration. As mentioned above, this also seems to happen if I asign materials instead of selecting and separating all parts in one run using the 'MATERIAL' type in the separate function. – DrPepperJo Feb 13 '17 at 23:58

The general case can get pretty messy, but if you just want to copy geometry and materials you can start from the code at http://web.purplefrog.com/~thoth/blender/python-cookbook/split-copy-mesh.html . It splits the mesh according to the material. It avoids bpy.ops and instead uses mesh.from_python to build the geometry of each fragment from a subset of the original.

Here is a copy of the relevant section:

import bpy
from math import *
from mathutils import *

def split_copy_mesh(obj, discriminant):

accumulator = {}

for polygon in obj.data.polygons:
tag = discriminant(polygon)

acc = accumulator.get(tag)
if acc is None:
acc = {
"tag":tag,
"verts":[],
"faces":[],
"materials":[],
"vertMap": {}
}
accumulator[tag] = acc

face = []
for vi in polygon.vertices:
vi2 = acc["vertMap"].get(vi)
if vi2 is None:
verts = acc["verts"]
vi2 = len( verts )
acc["vertMap"][vi] = vi2
verts.append(obj.data.vertices[vi].co)
face.append(vi2)
acc["faces"].append(face)
acc["materials"].append(polygon.material_index)

rval = []
for tag,acc in accumulator.items():
print(tag)

mesh = bpy.data.meshes.new(tag)
#print(acc)
mesh.from_pydata(acc["verts"], [], acc["faces"])
mesh.validate(True)

for mat in obj.data.materials:
mesh.materials.append(mat)

for i in range(len(mesh.polygons)):
mesh.polygons[i].material_index = acc["materials"][i]

obj = bpy.data.objects.new(tag, mesh)