Best way to check if current panel is viewport

46 views
Skip to first unread message

kiteh

unread,
Jun 4, 2019, 7:06:50 PM6/4/19
to Python Programming for Autodesk Maya
What is the best way to check if the current panel is a viewport type?
i am trying to set the 'Image Plane' option (under Show > Image Planes) and currently I am doing it as follows:

panel = cmds.getPanel(withFocus=True)
cmds.modelEditor(panel, edit=True, imagePlane=True)

with the assumption that the current panel is a 'valid' viewport.

But this will fails, eg. if I am running the above code in my scriptEditor as that is my current focus panel.

tomas mikulak

unread,
Jun 5, 2019, 2:52:00 AM6/5/19
to python_in...@googlegroups.com
I use python line (lower left )to test that code and when it works I know it will once viewport is active. When you use script editor it is used as active panel.
tomas

st 5. 6. 2019 o 1:06 kiteh <kiteh...@gmail.com> napísal(a):
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/46a4a173-e0a2-41a0-bcac-038118ce7c2b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kiteh

unread,
Jun 5, 2019, 1:37:31 PM6/5/19
to Python Programming for Autodesk Maya
Hi Tomas,

I totally get what you mean.
But say, if I have make my commands into a shelf icon command..

And if my last panel, which could be the Outliner or the Attribute Editor, the mostly used panels (viewports too of course) and I hit on that shelf icon command that I did, it will also error out.
As such, I asked this question..




On Tuesday, June 4, 2019 at 11:52:00 PM UTC-7, tomas mikulak wrote:
I use python line (lower left )to test that code and when it works I know it will once viewport is active. When you use script editor it is used as active panel.
tomas

st 5. 6. 2019 o 1:06 kiteh <kite...@gmail.com> napísal(a):
What is the best way to check if the current panel is a viewport type?
i am trying to set the 'Image Plane' option (under Show > Image Planes) and currently I am doing it as follows:

panel = cmds.getPanel(withFocus=True)
cmds.modelEditor(panel, edit=True, imagePlane=True)

with the assumption that the current panel is a 'valid' viewport.

But this will fails, eg. if I am running the above code in my scriptEditor as that is my current focus panel.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

tomas mikulak

unread,
Jun 5, 2019, 1:48:21 PM6/5/19
to python_in...@googlegroups.com
yes, that is true, you might query if panel has some attrs only vieport could have, I might check it later

On Wed, 5 Jun 2019 at 19:37, kiteh <kiteh...@gmail.com> wrote:
Hi Tomas,

I totally get what you mean.
But say, if I have make my commands into a shelf icon command..

And if my last panel, which could be the Outliner or the Attribute Editor, the mostly used panels (viewports too of course) and I hit on that shelf icon command that I did, it will also error out.
As such, I asked this question..




On Tuesday, June 4, 2019 at 11:52:00 PM UTC-7, tomas mikulak wrote:
I use python line (lower left )to test that code and when it works I know it will once viewport is active. When you use script editor it is used as active panel.
tomas

st 5. 6. 2019 o 1:06 kiteh <kite...@gmail.com> napísal(a):
What is the best way to check if the current panel is a viewport type?
i am trying to set the 'Image Plane' option (under Show > Image Planes) and currently I am doing it as follows:

panel = cmds.getPanel(withFocus=True)
cmds.modelEditor(panel, edit=True, imagePlane=True)

with the assumption that the current panel is a 'valid' viewport.

But this will fails, eg. if I am running the above code in my scriptEditor as that is my current focus panel.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/726fa1b7-fb2b-4e0b-aa6a-70e49a91d5eb%40googlegroups.com.

tomas mikulak

unread,
Jun 5, 2019, 2:41:56 PM6/5/19
to python_in...@googlegroups.com
I come up with this:

import maya.cmds as mc

viewports = mc.getPanel(type='modelPanel')
visible = mc.getPanel(vis=True)
active_panel=''

for i in viewports:
    if i in visible:
        print('\n[vieport]: '+i)
        active_panel = i

vieportCam = mc.modelPanel(active_panel, q=True, camera = True)

mc.setFocus(i)

if you have only one viewport in layout it is good solution, don't know about other scenario.

tomas

st 5. 6. 2019 o 19:48 tomas mikulak <mikula...@gmail.com> napísal(a):

kiteh

unread,
Jun 5, 2019, 8:18:00 PM6/5/19
to Python Programming for Autodesk Maya
Hey Tomas, thanks for getting back.

I actually managed to get it working by using the following (too much?):

    model_panels = []
    focus_panel = cmds.getPanel(withFocus=True)
    # Check if current panel is a modelPanel
    if cmds.objectTypeUI(focus_panel) == "modelEditor":
        cmds.modelEditor(focus_panel, edit=True, imagePlane=True)
    else:
        for panel in cmds.lsUI(editors=True):
            if "modelPanel" in panel:
                model_panels.append(panel)

        for model_panel in model_panels:
            if cmds.modelEditor(model_panel, query=True, activeView=True):
                try:
                    cmds.modelEditor(model_panel, edit=True, imagePlane=True)
                except Exception as e:
                    pass

Basically it checks if the current focus panel is of modelPanel. If it is not, it will query all modelPanels and sets the main viewport (not the tear off etc)

If anyone has a better approach to this, do feel free to chip in :)

Tim Fowler

unread,
Jun 5, 2019, 11:12:06 PM6/5/19
to python_in...@googlegroups.com
I'm typing this off the top of my head while watching the basketball game, but I think there are a couple options...

1. Pretty sure the getPanel command has a "type" flag where you can give it the name of the panel you got back from the "active" call.  That should return something like "modelPanel" if it is a viewport.
2. The M3dView class has an active3dView static function that I'm pretty sure returns the last active viewport if the current active panel is some other type.  If that's the case then this is the one you want.
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

AK Eric

unread,
Jun 25, 2019, 12:02:44 AM6/25/19
to Python Programming for Autodesk Maya
I've used this for a looong time:

import maya.cmds as mc
modelPanel = mc.playblast(activeEditor=True)

That's it:  The playblast command always knows the last active panel it could use, so querying that has always worked for me.
Reply all
Reply to author
Forward
0 new messages