4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – lemon Feb 21 at 17:02
  • $\begingroup$ @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 $\endgroup$ – Pastrokkio Feb 21 at 17:15
  • 1
    $\begingroup$ ok, but you ask about bmesh and if I remember well bmesh is not about materials.Should use bpy.data instead. $\endgroup$ – lemon Feb 21 at 17:23
  • 2
    $\begingroup$ 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...). $\endgroup$ – lemon Feb 21 at 17:51
  • 1
    $\begingroup$ 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). $\endgroup$ – lemon Feb 21 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.