# Is there a low level alternative for bpy.ops.mesh.separate()?

I am dealing with a great model, I have a problem, I want to divide it by parts, but bpy.ops.mesh.separate(type = 'LOOSE') seems to have a big problem, that is not takes into account the materials and keeps all the materials connected to all the generated objects, this is a big nuisance, I created a small script that eliminates the unused slots, but at this point I think the best solution is to create a small operator that does this I work because I think it is faster to create an object with materials already assigned correctly. I don't know what kind of approach can be used, if it is the case to use bmesh, it should basically do the same as bpy.ops, with some small variations.

I hope I'm not wrong, but I don't see any option that does this

Edit:

My research continues to lead me in this post:

It seems to me that it comes close to what I should do, I would like to use the bmesh module, but I think I should navigate to the connected faces, and get a list of material index on each face, then create a list with (face, material_index, # uv ?) and probably create a new object in that location that contains that data. But I find it difficult to put this into practice

• Finally, what you ask is not about separate but about cleaning unused materials? If that there is bpy.ops.object.material_slot_remove_unused() which is available from the UI. And this is also a searchable operator. Feb 21 '20 at 17:02
• @lemon Hi, I would like to create from 0, an operator or function that separates objects, and that only maintains the materials that are actually used on the objects. I think it can become faster than bpy.ops + cleaning unused materials Feb 21 '20 at 17:15
• ok, but you ask about bmesh and if I remember well bmesh is not about materials.Should use bpy.data instead. Feb 21 '20 at 17:23
• also sorry to be 'negative', but I'm doubtful about recoding 'separate' in Python and having better performance than via ops (so via C). And there are many things to take into account (UV, vertex groups, modifiers, shape keys...). Feb 21 '20 at 17:51
• The linked question/answers are useful to identify the loose parts (was involved in it). But what you need here is a Python efficient way to partially duplicate (separate) an object with all its inner data preserved (and material slot cleaning). I hope someone can imagine that from the Python APIs. Some kind of obj.data.copy() but partially copy the mesh. If not the code will involve mesh duplication and mesh part removal which can't be efficient (as I imagine it). Feb 21 '20 at 18:36