Creating and assigning a new material

Creating a default material

The Factory class allows you to create a new default material as follows:

Tip: After creating your material m, you can use help(type(m)) to obtain the list of attributes that you can set when creating a new standard default material.

Setting its attributes

You can then set your attributes, for example:

m.Diffuse = MaxPlus.Color(0, 0, 1)
m.Specular = MaxPlus.Color(1, 1, 1)
...

Assigning a material

You can then assign it to the node of the object as follows:

node.Material = m

See demoApplyMaterial.py for an example of how to create and assign a new standard default material.

Creating a material using its ClassId

The example demoMaterials.py demonstrates how to create materials using their ClassIds and SuperClassIds.

The example iterates through the list of API classes, and for each of those that are applicable to materials, it provides its SuperClassId and ClassId to create an Animatable. It then casts it from an Animatable to a Mtl. A list of materials is created:

for cd in MaxPlus.PluginManager.GetClassList().Classes:
    if cd.SuperClassId == sid:
        anim = MaxPlus.Factory.CreateAnimatable(sid, cd.ClassId, False)
        if anim:
            inst = cls._CastFrom(anim)
            if inst:
                yield inst

The example iterates through each of the created materials; then, for each material, creates a corresponding teapot and assigns the material to the teapot via INode.Material.

For each material, a text 'label' is created and drawn beside the teapot in the viewport to identify its material:

def CreateText(x,y, quat, message):
    tex = MaxPlus.Factory.CreateShapeObject(MaxPlus.ClassIds.text)
    tex.ParameterBlock.size.Value = 10.0
    tex.ParameterBlock.text.Value = message
    node = MaxPlus.Factory.CreateNode(tex)
    node.Position = MaxPlus.Point3(x, y,0)
    node.SetLocalRotation( quat )
    node.WireColor = MaxPlus.Color(1.0,0.5,1.0)

It also enumerates across each of the material parameters and prints them out, as well as prints out the material name and class:

def PrintMaterialProperties(material_instance):
    print "[%s] %s" % (material_instance.GetClassName(), material_instance.GetName())
    for p in material_instance.ParameterBlock.Parameters:
        print "\t" + p.Name + " = " + str(p.Value)

Tip: You can also create a new material using its ClassId by calling Factory.CreateMaterial().

Opening and updating the Material Editor

To open the Material Editor, call MaterialEditor.OpenMtlDlg(). It accepts a parameter that determines which material editor to open: the basic/legacy Material Editor or the advanced/schematic Material Editor. For convenience, the example provides a MtlDlgMode class that lists the acceptable values that can be passed to OpenMtlDlg :

When iterating through the created materials, the example also adds each of the materials to the Material Editor using one of two methods:

To change the active, selected slot in the Material Editor, call the following. This is the equivalent of selecting a material slot with your mouse: