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