Anyone have a good solution for querying the current maya viewport renderer (Viewport 2.0, Default Viewport, High Quality Viewport) in python?
How to query current viewport renderer in maya
8.1k views Asked by TheBeardedBerry At
3
There are 3 answers
0
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)
@Tomek's answer will perfectly work. If you are going the PyMEL route, you can do this:
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:
A concise, pseudo-PyMEL way would be to just PyMEL-ify Tomek's answer:
To get a list of viewport renderers:
Here is some extra info on model editors and viewport renderers. To get their "friendly" names:
To set them, concisely, I would use PyMEL and do:
To affect all viewports (modelPanels) I would do:
To affect just the viewport in focus: