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