I've been trying to import an FBX file with instanced meshes. In the FBX file, materials are linked to objects. But in the Blender import script, it's linking the materials to the mesh. Since multiple objects can share the same mesh, the mesh ends up having materials from all the objects that use the same mesh!
Here's an example to better show what I mean. In the FBX file, we have the following structure:
Object_A
Mesh_A
Material_A
Object_B
Mesh_A
Material_B
But when I import into Blender, this is what I end up with:
Object_A
Mesh_A
Material_A
Material_B
Object_B
Mesh_A
Material_A
Material_B
I'm trying to import the FBX into Blender, while maintaining the original structure in the FBX. I looked into the import script (import_fbx.py
) and I think I found the relevant code segment starting at line 3079:
# In Blender, we link materials to data, typically (meshes), while in FBX they are linked to objects...
# So we have to be careful not to re-add endlessly the same material to a mesh!
# This can easily happen with 'baked' dupliobjects, see T44386.
# TODO: add an option to link materials to objects in Blender instead?
done_materials = set()
for (fbx_lnk, fbx_lnk_item, fbx_lnk_type) in connection_filter_forward(fbx_uuid, b'Model'):
# link materials
fbx_lnk_uuid = elem_uuid(fbx_lnk)
for (fbx_lnk_material, material, fbx_lnk_material_type) in connection_filter_reverse(fbx_lnk_uuid, b'Material'):
if material not in done_materials:
mesh.materials.append(material)
done_materials.add(material)
My question is - how can I modify this code to link the material to the object, instead of the mesh data?
I'm quite lost at this step, since I haven't done any Blender scripting before. If any Blender gurus could point the way, it'd be much appreciated!
Here's the entire file, for reference: https://github.com/sobotka/blender-addons/blob/master/io_scene_fbx/import_fbx.py#L3079
bpy.context.object.material_slots['Material'].link = 'OBJECT'
$\endgroup$