How to query current viewport renderer in maya

8.1k views Asked by At

Anyone have a good solution for querying the current maya viewport renderer (Viewport 2.0, Default Viewport, High Quality Viewport) in python?

3

There are 3 answers

3
kartikg3 On BEST ANSWER

@Tomek's answer will perfectly work. If you are going the PyMEL route, you can do this:

import pymel.core.uitypes as pmui

model_panel = pmui.ModelEditor(pm.getPanel(withFocus=True))
print pmui.ModelEditor.getRendererName(model_panel)

Although, that snippet seems to be temperamental. It sometimes throws an error complaining that the model editor was not found, especially when executed from the shelf.

Another way would be a bit wordy, but it works everytime:

import pymel.core as pm
import pymel.core.uitypes as pmui

modelPanelList = []
modelEditorList = pm.lsUI(editors=True)
for myModelPanel in modelEditorList:
    if myModelPanel.find('modelPanel') != -1:
        modelPanelList.append(myModelPanel)

for modelPanel in modelPanelList:    
    if pmui.ModelEditor(modelPanel).getActiveView():
        try:
            # Always better to try in the case of active panel operations
            # as the active panel might not be a viewport.
            print pmui.ModelEditor(modelPanel).getRendererName()
        except Exception as e:
            # Handle exception
            print e

A concise, pseudo-PyMEL way would be to just PyMEL-ify Tomek's answer:

import pymel.core as pm
print pm.modelEditor(pm.getPanel(wf=True), q=True, rnm=True)

To get a list of viewport renderers:

import pymel.core.uitypes as pmui
print pmui.ModelEditor().getRendererList()

Here is some extra info on model editors and viewport renderers. To get their "friendly" names:

import pymel.core.uitypes as pmui
print pmui.ModelEditor().getRendererListUI()

To set them, concisely, I would use PyMEL and do:

import pymel.core.uitypes as pmui

# assuming you know which modelPanel you want to affect
pmui.ModelEditor("modelPanel4").setRendererName("ogsRenderer")

To affect all viewports (modelPanels) I would do:

import pymel.core as pm
import pymel.core.uitypes as pmui

modelPanelList = []
modelEditorList = pm.lsUI(editors=True)
for myModelPanel in modelEditorList:
    if myModelPanel.find('modelPanel') != -1:
        modelPanelList.append(myModelPanel)

for modelPanel in modelPanelList:
    pmui.ModelEditor(modelPanel).setRendererName("ogsRenderer")

To affect just the viewport in focus:

import pymel.core as pm
import pymel.core.uitypes as pmui

modelPanelList = []
modelEditorList = pm.lsUI(editors=True)
for myModelPanel in modelEditorList:
    if myModelPanel.find('modelPanel') != -1:
        modelPanelList.append(myModelPanel)

for modelPanel in modelPanelList:    
    if pmui.ModelEditor(modelPanel).getActiveView():
        try:
            # Always better to try in the case of active panel operations
            # as the active panel might not be a viewport.
            pmui.ModelEditor(modelPanel).setRendererName("ogsRenderer")
        except Exception as e:
            # Handle exception
            print e
1
Tomek On

This script returns renderer for focused panel:

import maya.cmds as cmds

print cmds.modelEditor( cmds.getPanel(wf=True), q=True, rnm=True )
0
bafly On

Iterate over all modelPanels, find one that active (with gray contour) and ask for rendereName(rnm)

mel:

// find current modelEditor
string $cur_mp;
for ($mp in `getPanel -typ "modelPanel"`){
    // check if active
    if (`modelEditor -q -av $mp`){
        $cur_mp = $mp;
        break;
    }
}

if ($cur_mp != ""){
    // your coude next
    print $cur_mp;
    $new_rndr = "ogsRenderer";
    // set new renderer
    modelEditor -e -rnm $new_rndr $cur_mp;
}

py:

import maya.cmds as cm

cur_mp = None
for mp in cm.getPanel(type="modelPanel"):
    if cm.modelEditor(mp, q=1, av=1):
        cur_mp = mp
        break

if cur_mp:
    # do your stuff
    print cur_mp
    new_rndr = "ogsRenderer"
    cm.modelEditor(cur_mp, e=1, rnm=new_rndr)

pymel:

import pymel.core as pm

# maybe its not the best solution to iterate over all existing modelPanels
mp_ls = [x
         for x in pm.lsUI() 
         if type(x).__name__ == "ModelEditor"
         and type(x.getParent()).__name__ == "PyUI"
         and x.getActiveView()]

if mp_ls:
    # do your stuff
    print mp_ls[0]    # current modelEditor
    new_rndr = "ogsRenderer"
    mp_ls[0].setRendererName(new_rndr)