demoMaterials.py

demoMaterials.py
1 '''
2  Demonstrates how to iterate through materials and and apply them to objects.
3  It shows how to open the material editor and put materials in the editor.
4 '''
5 import MaxPlus
6 import math
7 
8 
9 def GeneratePlugins(sid, cls):
10  for cd in MaxPlus.PluginManager.GetClassList().Classes:
11  if cd.SuperClassId == sid:
12  anim = MaxPlus.Factory.CreateAnimatable(sid, cd.ClassId, False)
13  if anim:
14  inst = cls._CastFrom(anim)
15  if inst:
16  yield inst
17 
18 
19 def CreateMaterials():
20  materials = GeneratePlugins(MaxPlus.SuperClassIds.Material, MaxPlus.Mtl)
21  materialList = list(materials)
22  numMaterials = len(materialList)
23  # for m in materialList:
24  # print m
25  print "%d materials" % numMaterials
26  return materialList
27 
28 
29 def CreatePlane():
30  plane = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.Plane)
31  plane.ParameterBlock.Width.Value = 120.0
32  plane.ParameterBlock.Length.Value = 120.0
33  node = MaxPlus.Factory.CreateNode(plane)
34 
35 
36 def PrintMaterialProperties(material_instance):
37  print "[%s] %s" % (material_instance.GetClassName(), material_instance.GetName())
38  for p in material_instance.ParameterBlock.Parameters:
39  print "\t" + p.Name + " = " + str(p.Value)
40 
41 
42 def CreateText(x, y, quat, message):
43  tex = MaxPlus.Factory.CreateShapeObject(MaxPlus.ClassIds.text)
44  tex.ParameterBlock.size.Value = 10.0
45  tex.ParameterBlock.text.Value = message
46  node = MaxPlus.Factory.CreateNode(tex)
47  node.Position = MaxPlus.Point3(x, y, 0)
48  node.SetLocalRotation(quat)
49  node.WireColor = MaxPlus.Color(1.0, 0.5, 1.0)
50 
51 
52 class MtlDlgMode(object):
53  ''' Enumeration that determines what kind of material dialog to display'''
54  basic = 0 # Basic mode, basic parameter editing of material and textures
55  advanced = 1 # Advanced mode, schematic graph editing of material and texture connections
56 
57 
58 def CreateAndAssignMaterials(materials):
59  numMaterials = len(materials)
60  diff = 360.0 / numMaterials
61  teapot_radius = 5.0
62  radius = 50.0
63  text_radius = 90.0
64  index = 0
65  i = 0
66  MaxPlus.MaterialEditor.OpenMtlDlg(MtlDlgMode.basic)
67 
68  for m in materials:
69  angle_radians = math.radians(i)
70  x = radius * math.cos(angle_radians)
71  y = radius * math.sin(angle_radians)
72  position = MaxPlus.Point3(x, y, 0)
73 
74  teapot = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.Teapot)
75  teapot.ParameterBlock.Radius.Value = teapot_radius
76  node = MaxPlus.Factory.CreateNode(teapot)
77  node.Position = position
78  angle_rotate = 180 - i
79  angle_axis_rotation = MaxPlus.AngAxis(
80  0, 0, 1, math.radians(angle_rotate))
81  quat = MaxPlus.Quat(angle_axis_rotation)
82  node.SetLocalRotation(quat)
83 
84  x = text_radius * math.cos(angle_radians)
85  y = text_radius * math.sin(angle_radians)
86  CreateText(x, y, quat, m.GetClassName())
87  if (index < 24):
93 
94  # Now assign the material
95  node.Material = m
96  PrintMaterialProperties(m)
97  i += diff
98  index += 1
100 
101 
102 def DoStuff():
104  # maximize the view
106  CreatePlane()
107  materials = CreateMaterials()
108  CreateAndAssignMaterials(materials)
109 
110 DoStuff()