How do I set the current material for an object in Python?
I have a NMesh with 3 materials added and it’s linked to an object. So How do I set which one I want to the object?
How do I set the current material for an object in Python?
I have a NMesh with 3 materials added and it’s linked to an object. So How do I set which one I want to the object?
.i have been wondering about that as well, someone told me you have to set it per face in the Nmesh.face stuff, but i couldnt work it out…
Hi Levon -
I figured it out last night. Yes, you have to add it to each face manually. The idea is that there is a “materials” list member of NMesh. Each member of the materials list is a pointer to a material. You will refer to the material’s index in the list when assigning it to a face. If you have added 3 materials to a nmesh, the first material is index 0, the second materis is index 1, and the 3rd material is index 2 (I think you can only have up to 16 materials for any given nmesh). Each face has a data member called “mat”. All you have to do is assing “mat” to one of the material indexes.
Here’s some code that will add 3 materials to a mesh with 3 faces:
import Blender
from Blender import NMesh, Object, Material
# The following 2 things are alerady created in my scene
obj=Object.Get("obj_3Faces")
zmesh=NMesh.GetRaw("mesh_3Faces")
mat1=Material.New("mat1")
mat2=Material.New("mat2")
mat3=Material.New("mat3")
mat1.rgbCol=[.9,0,0]
mat2.rgbCol=[0,.9,0]
mat3.rgbCol=[0,0,.9]
zmesh.addMaterial(mat1)
zmesh.addMaterial(mat2)
zmesh.addMaterial(mat3)
for x in range(len(zmesh.faces)):
zmesh.faces[x].mat=x
zmesh.update()