scripted/pyFrameContextTest.py

scripted/pyFrameContextTest.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[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  projection = frameCtx.getMatrix(omr.MFrameContext.kProjectionMtx)
97  print('> Projection matrix:' + str(projection))
98  projection = frameCtx.getMatrix(omr.MFrameContext.kViewMtx)
99  print('> View matrix:' + str(projection))
100 
101  bparams = frameCtx.getBackgroundParameters();
102  print("> Background:")
103  print("> - gradient = " + str(bparams[0])) # displayGradient
104  print("> - colorColor1 = " + str(bparams[4])) # clearColor1
105  print("> - colorColor2 = " + str(bparams[5])) # clearColor2
106 
107  print("> Display style: " + str(frameCtx.getDisplayStyle()))
108  print("> Wire on shaded: " + str(frameCtx.wireOnShadedMode()))
109  print("> Lighting mode: " + str(frameCtx.getLightingMode()))
110  print("> Light limit: " + str(frameCtx.getLightLimit()))
111 
112  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kAmbientOcclusion ):
113  print("> SSAO enabled")
114  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kMotionBlur ):
115  print("> Motion blur enabled");
116  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kViewColorTransformEnabled ):
117  print("> Color management enabled")
118  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kDepthOfField ):
119  dofParams = frameCtx.getDOFParameters()
120  print("> Depth-of-field enabled: " + str(dofParams[0]))
121  print("> - Focus distance: " + str(dofParams[1]))
122  print("> - Alpha: " + str(dofParams[2]))
123  if frameCtx.getPostEffectEnabled( omr.MFrameContext.kAntiAliasing ):
124  print("> Anti-aliasing enabled")
125  fogParams = frameCtx.getHwFogParameters()
126  if (fogParams[0]):
127  print("> Fog Enabled:")
128  print("> - Fog mode: " + str(fogParams[1]))
129  print("> - Fog start: " + str(fogParams[2]))
130  print("> - Fog end: " + str(fogParams[3]))
131  print("> - Fog density: " + str(fogParams[4]))
132  print("> - Fog color: " + str(fogParams[5]))
133 
134  # Create render operations (if not exist)
135  if not self.mRenderOperations:
136  self.mRenderOperations['SceneRender'] = omr.MSceneRender('SceneRender')
137  self.mRenderOperations['Present'] = omr.MPresentTarget('Present')
138 
139  def cleanup(self):
140  self.mCurrentOperationIndex = -1
141 
142 mFrameContextInstance = None
143 
144 def initializePlugin(obj):
145  plugin = om.MFnPlugin(obj)
146  global mFrameContextInstance
147  mFrameContextInstance = frameContextTest('my_frame_context_test')
148  omr.MRenderer.registerOverride(mFrameContextInstance)
149 
150 def uninitializePlugin(obj):
151  plugin = om.MFnPlugin(obj)
152  global mFrameContextInstance
153  if mFrameContextInstance is not None:
154  omr.MRenderer.deregisterOverride(mFrameContextInstance)
155  mFrameContextInstance = None
156