Python API 2.0 Reference
python/api2/py2FrameContextTest.py
1 import sys
2 import maya.api.OpenMayaRender as omr
3 import maya.api.OpenMaya as om
4 import collections # for OrderedDict
5 
6 # Description:
7 # This plug-in shows the usage of MRenderOverride.getFrameContext() to be able
8 # to extract various per-frame data. This is performed within the MRenderOverride.setup() method.
9 #
10 # Sample out from a 3d viewport will look like this:
11 #
12 # MRenderOverride Frame Information Extraction Trace
13 # > --------------------------------------------------
14 # > Destination: modelPanel4
15 # > Viewport: 0,0,885,541
16 # > Camera path: |persp|perspShape
17 # > Projection matrix:(((1.94444, 0, 0, 0), (0, 3.18084, 0, 0), (0, 0, -1.00001, -1), (0, 0, -0.100001, 0)))
18 # > View matrix:(((-0.656059, 0.459493, -0.598709, 0), (-0, 0.793297, 0.608834, 0), (0.75471, 0.399431, -0.52045, -0), (2.87398e-012, 1.02103e-012, -169.54, 1)))
19 # > Background:
20 # > - gradient = False
21 # > - colorColor1 = (0.106538, 0.106538, 0.106538, 0)
22 # > - colorColor2 = (0.106538, 0.106538, 0.106538, 0)
23 # > Display style: 1
24 # > Wire on shaded: 0
25 # > Lighting mode: 2
26 # > Light limit: 8
27 # > Color management enabled
28 # > Depth-of-field enabled: True
29 # > - Focus distance: 100.0
30 # > - Alpha: 0.00782441254705
31 # > Anti-aliasing enabled
32 # > Fog Enabled:
33 # > - Fog mode: 0
34 # > - Fog start: 83.9160842896
35 # > - Fog end: 335.664337158
36 # > - Fog density: 0.146853148937
37 # > - Fog color: (0.477876, 0.477876, 0.477876, 0.307692)
38 #
39 # From the render view would look like this:
40 # > MRenderOverride Frame Information Extraction Trace
41 # > --------------------------------------------------
42 # > Destination: renderView
43 # > Viewport: 0,0,960,540
44 # > Camera path: |persp|perspShape
45 # > Projection matrix:(((1.94444, 0, 0, 0), (0, 3.45679, 0, 0), (0, 0, -1, -1), (0, 0, -0.001, 0)))
46 # etc...
47 
48 # Define maya_useNewAPI to indicate using Maya Python API 2.0.
49 maya_useNewAPI = True
50 
51 class frameContextTest(omr.MRenderOverride):
52  ''' Sample class which prints some trace information about the current frame context during setup()
53  '''
54  def __init__(self, name):
55  omr.MRenderOverride.__init__(self, name)
56  self.mRenderOperations = collections.OrderedDict()
57  self.mUIName = "Frame Context Test Override"
58  self.mCurrentOperationIndex = -1
59 
60  def supportedDrawAPIs(self):
61  return ( omr.MRenderer.kAllDevices )
62 
63  def startOperationIterator(self):
64  self.mCurrentOperationIndex = 0
65  return True
66 
67  def renderOperation(self):
68  if self.mCurrentOperationIndex >= 0 and self.mCurrentOperationIndex < len(self.mRenderOperations):
69  return self.mRenderOperations[list(self.mRenderOperations.keys())[self.mCurrentOperationIndex]]
70  else:
71  return None
72 
73  def nextRenderOperation(self):
74  self.mCurrentOperationIndex += 1 # increment iterator index
75  return self.mCurrentOperationIndex < len(self.mRenderOperations)
76 
77  def setup(self, panelName ):
78  # Print out some information by using the getFrameContext() method
79  # Note that any per object information will not be relevant since
80  # we have not begin executing the render pipeline at this point.
81  #
82  frameCtx = self.getFrameContext()
83  if frameCtx:
84  print('> MRenderOverride Frame Information Extraction Trace')
85  print('> --------------------------------------------------')
86  dim = frameCtx.getViewportDimensions()
87  originX = dim[0]
88  originY = dim[1]
89  width = dim[2]
90  height = dim[3]
91  print('> Destination: ' + panelName)
92  print('> Viewport: ' + str(originX) + ',' + str(originY) + ',' + str(width) + ',' + str(height))
93  cameraPath = frameCtx.getCurrentCameraPath()
94  cameraName = cameraPath.fullPathName()
95  print('> Camera path: ' + cameraName)
96  ufeCameraPath = frameCtx.getCurrentUfeCameraPath()
97  print('> Ufe Camera path: ' + ufeCameraPath)
98  projection = frameCtx.getMatrix(omr.MFrameContext.kProjectionMtx)
99  print('> Projection matrix:' + str(projection))
100  projection = frameCtx.getMatrix(omr.MFrameContext.kViewMtx)
101  print('> View matrix:' + str(projection))
102 
103  bparams = frameCtx.getBackgroundParameters();
104  print("> Background:")
105  print("> - gradient = " + str(bparams[0])) # displayGradient
106  print("> - colorColor1 = " + str(bparams[4])) # clearColor1
107  print("> - colorColor2 = " + str(bparams[5])) # clearColor2
108 
109  print("> Display style: " + str(frameCtx.getDisplayStyle()))
110  print("> Wire on shaded: " + str(frameCtx.wireOnShadedMode()))
111  print("> Lighting mode: " + str(frameCtx.getLightingMode()))
112  print("> Light limit: " + str(frameCtx.getLightLimit()))
113 
114  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kAmbientOcclusion ):
115  print("> SSAO enabled")
116  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kMotionBlur ):
117  print("> Motion blur enabled");
118  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kViewColorTransformEnabled ):
119  print("> Color management enabled")
120  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kDepthOfField ):
121  dofParams = frameCtx.getDOFParameters()
122  print("> Depth-of-field enabled: " + str(dofParams[0]))
123  print("> - Focus distance: " + str(dofParams[1]))
124  print("> - Alpha: " + str(dofParams[2]))
125  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kAntiAliasing ):
126  print("> Anti-aliasing enabled")
127  fogParams = frameCtx.getHwFogParameters()
128  if (fogParams[0]):
129  print("> Fog Enabled:")
130  print("> - Fog mode: " + str(fogParams[1]))
131  print("> - Fog start: " + str(fogParams[2]))
132  print("> - Fog end: " + str(fogParams[3]))
133  print("> - Fog density: " + str(fogParams[4]))
134  print("> - Fog color: " + str(fogParams[5]))
135 
136  # Create render operations (if not exist)
137  if not self.mRenderOperations:
138  self.mRenderOperations['SceneRender'] = omr.MSceneRender('SceneRender')
139  self.mRenderOperations['Present'] = omr.MPresentTarget('Present')
140 
141  def cleanup(self):
142  self.mCurrentOperationIndex = -1
143 
144 mFrameContextInstance = None
145 
146 def initializePlugin(obj):
147  plugin = om.MFnPlugin(obj)
148  global mFrameContextInstance
149  mFrameContextInstance = frameContextTest('my_frame_context_test_py')
150  omr.MRenderer.registerOverride(mFrameContextInstance)
151 
152 def uninitializePlugin(obj):
153  plugin = om.MFnPlugin(obj)
154  global mFrameContextInstance
155  if mFrameContextInstance is not None:
156  omr.MRenderer.deregisterOverride(mFrameContextInstance)
157  mFrameContextInstance = None
158