# Copy materials and material indices to instances which aren't copies of anotther object

From this question: link I learned how to transfer UV maps from one object into a new instance. How can this be done with materials and material indices?

AN in 2.8 seems to have a new system but I'm still stuck in 2.79.

EDIT

@Omar Ahmad offered a solution which works great for the materials but material indices aren't transfered correctly here is how the graph looks like.

• Use linking. Select them and hit CTRL+L, then choose material. – FFeller Feb 25 '19 at 20:22

The Mesh Object Input node have a Material Indices output, you can use that as the input material indices of the Mesh Object Output node as follows:

Alternatively, you can use the following expression to get the material indices:

[p.material_index for p in object.data.polygons]


Where object is your object. To apply modifiers first, you can use the following expression:

object.an.getMesh(bpy.context.depsgraph, True).an.getPolygonMaterialIndices()


As for copying materials themselves, the 2.8 version have a system to set materials to object. But if you are on an older version, you will have to use a python script. Something like this should work:

target.data.materials.clear()
for material in source.data.materials:
target.data.materials.append(material)


Where source and target are you source and target objects.

• I accepted the answer too fast… sorry, but the material indices aren't been copied. I managed to transfer all the materials correctly though. Check the edit to see how the graph looks. – Juan Manuel Lynch Feb 26 '19 at 9:39
• @JuanManuelLynch I see. This would be a bug in Animation Nodes. I just fixed it, but the fix won't be available for older versions. I edited the answer to include another way to get the indices. – Omar Emara Feb 26 '19 at 11:37
• now works as expected. There is still an issue... my root object, from where the instances are created, has a modifier (bevel), this modifier seems to get applied when the object instancer gets fed with my root mesh mixing all the polygon indices. Is there a workaround for this? – Juan Manuel Lynch Feb 26 '19 at 12:48
• @JuanManuelLynch I would have to see the node tree responsible for the instancing. But some nodes/options return meshes that have modifiers applied, could you make sure that no such option is enabled? If not, maybe you can ask another question with a minimal example illustrating the issue. – Omar Emara Feb 26 '19 at 13:47
• you can check the tree in the EDIT of my question. Yes, I have the Apply modifier option enabled in my meshObjectInput node, and it actually works, the output mesh has the modifier correctly applied but the with incorrect polygon indices. I'll try to create a new question later with a more simple example. – Juan Manuel Lynch Feb 26 '19 at 13:55