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