# Instantiating objects and applying different material to them

I have a scene with hundreds of cylinders of different sizes and colours, which I create with a python script. Needless to say that they take a lot of memory. I just learnt about object instantiation for this kind of cases (yes, I'm pretty new to Blender of CGI in general).

However, while I can scale them independently, I cannot seem to give each of them a different material (as they need to have there own colour). I find this strange, because, in my understanding, they are the same instance of a mesh, which could well be (in my logic) separated from the materials which are simply applied to them.

Am I missing something here? If instantiated meshes cannot indeed have their own materials applied to, what is the proper way to display hundred (and eventually thousands) of primitives in a scene?

Thanks!

Both the Object and Mesh classes actually have place to store materials. For object it is Object.material_slots and Mesh.materials. This means that Mesh always has their own "default" materials but they could be overridden on Object level. I don't know the origin of this but it comes in handy.

Normally the settings are paired but to override material per object, you need to set the object's material slot from "DATA" to "OBJECT". This makes Blender store the material per object instead of reading it from the mesh data. It is easiest to set this up for the source Object, all clones will inherit this setting afterwards.

In python you can set this via.

object.material_slots[i].link = 'OBJECT'


After you make the change, you need re-assign the material slot as setting this clears any reference.

Here are links to official Blender python API in case you want to dig around some more: https://docs.blender.org/api/current/bpy.types.Mesh.html?highlight=materials https://docs.blender.org/api/current/bpy.types.Object.html?highlight=material_slots

Depending on your case though, if you only want simple color variation instead of having completely different materials, it might be better way to set up your material so that you get a random per object color using Object Info > Random and color ramp for example. Or Object Info > Location plugged into textures like Musgrave or Voronoi for patterned variance.

• EDIT: Expanded the answer with alternative way to achieve per object variance, using only one material. – kheetor Apr 20 '18 at 16:18