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