This topic discusses ways to perform rendering tasks using pymxs.
The render()
function invokes the 3ds Max renderer, and takes many optional parameters to control the rendering independent of the current Render Settings Dialog settings.
By default this function returns an in-memory render as a bitmap value.
# this will display the render in a virtual file buffer window
bmp = pymxs.runtime.render(pos=pymxs.runtime.name('vfb_upper_left'))
# the bitmap can also be displayed later:
pymxs.runtime.display(bmp, caption='My Bitmap')
The render()
function can save the render to a file if the outputFile:
parameter is specified.
For example, this function (from the render.py
pymxs sample) saves the rendered output as a single-frame jpeg file to the system default #renderoutput
directory, removing the current render if it exists:
def render():
'''Render in the renderoutput directory.'''
output_path = os.path.join(pymxs.runtime.getDir(pymxs.runtime.Name("renderoutput")), 'foo.jpg')
if os.path.exists(output_path):
os.remove(output_path)
pymxs.runtime.render(outputFile=output_path)
There are two mechanisms to register pre- and post-render scripts:
preRendScript
(string) and usePreRendScript
(boolean), or postRendScript
(string) and usePostRendScript
(boolean), which correspond to the settings under Render Setup Dialog > Common Parameters > Scripts in the user interface. This mechanism only works when the user presses the render button in the UI, these scripts are not called when the pymxs.runtime.render()
function is called.pymxs.runtime.render()
is called.An example of the latter mechanism:
mxs = pymxs.runtime
def myPreRenderCallback():
print('PreRender Callback Called')
mxs.callbacks.removeScripts(id=mxs.Name('MyCallbacks'))
mxs.callbacks.addScript(mxs.name('preRender'), myPreRenderCallback, id=mxs.name('MyCallbacks'))
# this will run the registered script first:
mxs.render()
You can change rendering settings using the pymxs.runtime.renderSceneDialog
struct. Note that changes to render settings should be made with the dialog closed (which you can check with the renderSceneDialog.isOpen()
function). See the "Render Scene Dialog" group of topics in the MAXScript Help for information about properties and functions in this struct.
The renderer assigned to each type of render target is held in the renderers struct. For example, by default, renderers.production is the scanline renderer:
>>> pymxs.runtime.renderers.production
<Default_Scanline_Renderer<Default_Scanline_Renderer:Default_Scanline_Renderer>>
The list of all available renderer classes is held in the rendererClass.classes
list, and can be assigned to a render target:
>>> pymxs.runtime.renderers.current = pymxs.runtime.rendererClass.classes[5]()
The viewport
struct exposes properties and methods for working with viewports.
You can use the viewport.numviews
property to iterate through the available views. For example, to list the viewports and their current layout types:
mxs = pymxs.runtime
for v in range(1,mxs.viewport.numviews+1):
viewtype = mxs.viewport.getType(index=v)
print(f'index:{v} view type:{viewtype}')
The MAXScript command to maximize or minimize the current viewport is a "Max Command", in the form of:
max tool maximize
As noted in Calling 3ds Max Commands, Max commands are not directly callable in pymxs, so to invoke this command, use:
pymxs.runtime.execute('max tool maximize')
Setting the zoom level of the current viewport is exposed through the viewport.zoom()
function, which takes a float value setting the zoom factor, where 2.0 zooms "out" two times, while 0.5 zooms "in" two times. If the viewport is in perspective mode, you can also use viewport.zoomPerspective()
.
To "zoom extents", once again a Max command is used:
max tool zoomextents
max tool zoomextents all
The viewport.getViewportDib()
function returns a snapshot of a viewport's window image as bitmap. If no additional parameters are provided, it returns a bitmap for the currently active viewport. It takes these optional parameters:
index
- specifies the index of the viewport to capture; the default is the currently active viewportviewPanelIndex
- specifies the index of the view panel to capture; the default is the currently active view panelcaptureAlpha
(boolean) - specifies whether to capture the Alpha channel as well (ARGB); the default is FalsegammaCorrect
(boolean) - specifies whether gamma correction is performed; the default is TrueFor example:
my_bitmap = pymxs.runtime.viewport.getViewportDib(captureAlpha=True)
pymxs.runtime.display(my_bitmap)