demoRender.py

demoRender.py
1 import MaxPlus
2 import os
3 import math
4 
5 # Copied from the enum ViewType in maxapi.h. This is the closest thing we have
6 # to an enum in python.
7 
8 
9 class ViewType(object):
10  VIEW_LEFT = 0
11  VIEW_RIGHT = 1
12  VIEW_TOP = 2
13  VIEW_BOTTOM = 3
14  VIEW_FRONT = 4
15  VIEW_BACK = 5
16  VIEW_ISO_USER = 6
17  VIEW_PERSP_USER = 7
18  VIEW_CAMERA = 8
19  VIEW_GRID = 9
20  VIEW_NONE = 10
21  VIEW_TRACK = 11
22  VIEW_SPOT = 12
23  VIEW_SHAPE = 13
24  VIEW_SCHEMATIC = 14
25  VIEW_RENDER = 15
26  VIEW_SCENEEXPLORER = 16
27  VIEW_OTHER = 17
28 
29  @staticmethod
30  # Looks up a Key in the class dictionary given a Value
31  # Given a value for one of the members in the class above, returns a string representation of
32  # the variable name. So for instance, if 14 is passed in, this function
33  # returns the string "VIEW_SCHEMATIC"
34  def GetKey(val):
35  result = -1
36  for item in ViewType.__dict__:
37  theValue = ViewType.__dict__[item]
38  if (theValue == val):
39  result = item
40  break
41  return result
42 
43 
44 def RemoveRenderedFile(file):
45  if os.path.exists(file):
46  os.remove(file)
47 
48 
49 def CreateSpheres():
50  '''Creates an array of spheres in a cone like shape'''
51  sphere_radius = 6.0 # for the sphere
52  theSphere = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.Sphere)
53  theSphere.ParameterBlock.Radius.Value = sphere_radius
54  revolutions = 9 * 360
55  radius = 40.0
56  z = 0.0
57  for i in range(0, revolutions, 20):
58  node = MaxPlus.Factory.CreateNode(theSphere)
59  radians = math.radians(i) # convert degrees to radians
60  x = radius * math.cos(radians)
61  y = radius * math.sin(radians)
62  node.Position = MaxPlus.Point3(x, y, z)
63  z += 1.0
64  radius -= 0.20
65 
66 
67 def MaximizePerspective():
68  '''This function finds the first perspective viewport, makes it active and maximizes it.'''
69  allViewports = True
70  skipPerspective = False
71  # zoom out to view all the geometry, in all viewports
72  MaxPlus.ViewportManager.ViewportZoomExtents(allViewports, skipPerspective)
73 
74  index = 0
75  perspIndex = -1
76  found = False
77  # Find the first perspective view and make that viewport is active
78  for view in MaxPlus.ViewportManager.Viewports:
79  viewType = view.GetViewType()
80  print "%d - %s - %s (%d)" % (index, MaxPlus.ViewportManager.getViewportLabel(index), ViewType.GetKey(viewType), viewType)
81  if ((viewType == ViewType.VIEW_PERSP_USER) and (not found)):
82  typeString = ViewType.GetKey(viewType)
83  perspIndex = index
84  found = True
85  index += 1
86  # set the active viewport
87  print "Found Perspective Viewport. Index:", perspIndex
88  if (perspIndex != -1):
90  # now maximize that view
92 
93 
94 def SetRenderParameters():
95  '''Set some common render parameters'''
96  outputPath = os.path.join(
98 
99  # render settings is a static class. There is no instance of it.
100  render = MaxPlus.RenderSettings
101  render.SetOutputFile(outputPath)
102  print "Saving file to:", render.GetOutputFile()
103  render.SetSaveFile(True)
104  return outputPath
105 
106 
107 def demoRender():
109  CreateSpheres()
110  MaximizePerspective()
111  path = SetRenderParameters()
112  # Renders only one frame at the current time.
113  RemoveRenderedFile(path)
115 
116 demoRender()