Maya Python: "selectFromScreen" not working in new window?

I’m animating a camera, and at each frame I’m trying to get the polygons in view on a sphere that the camera is aiming at. I’m creating a new Panel to do all this “capturing” in. However, Maya keeps capturing things from the MainWindow panel viewport and not the new one I’m creating. The focus seems to be set properly, too, to this new one.

If I use the Main Window’s viewports/panels rather than a new window, everything is good e.g. “modelPanel4” aka persp. But for some reason selectFromScreen doesn’t do it for the new window. Is there some kind of UI update command I’m missing?

Any help/tips here are definitely appreciated!

import maya.cmds as cmds
import maya.mel as mel

# turn backface culling on
cmds.selectPref(useDepth=True)
cmds.selectPref(paintSelect=not(True))
mel.eval("updateHighlightSelectIcon;")

# create sphere
sphere = cmds.polySphere(r=1)[0]

# create camera and animate it around the sphere
camera = cmds.camera(
    centerOfInterest=5,
    focalLength=35,
    nearClipPlane=0.1,
    farClipPlane=10000,
    orthographic=0,
    orthographicWidth=30,
    panZoomEnabled=0,
    horizontalPan=0,
    verticalPan=0,
    zoom=1)[0]
mel.eval("objectMoveCommand")
mel.eval("cameraMakeNode 2 " + camera)
cmds.xform(camera + "_aim", t=(0,0,0))

cmds.setKeyframe(camera, t=(0,0), at="tx", v=0)
cmds.setKeyframe(camera, t=(0,0), at="tz", v=5)
cmds.setKeyframe(camera, t=(30,30), at="tx", v=5)
cmds.setKeyframe(camera, t=(30,30), at="tz", v=5)
cmds.setKeyframe(camera, t=(60,60), at="tx", v=5)
cmds.setKeyframe(camera, t=(60,60), at="tz", v=0)


# window panel setup with camera
window = cmds.window('testWindow#')
form = cmds.formLayout()
editor = cmds.modelEditor()
column = cmds.columnLayout('true')
cmds.window( window, edit=True, widthHeight=(1920, 1280) )
cmds.formLayout( form, edit=True, attachForm=[(column, 'top', 0), (column, 'left', 0), (editor, 'top', 0), (editor, 'bottom', 0), (editor, 'right', 0)], attachNone=[(column, 'bottom'), (column, 'right')], attachControl=(editor, 'left', 0, column))
cmds.modelEditor(editor, edit=True, camera=camera )
cmds.modelEditor(editor, e=True, displayAppearance="smoothShaded")
cmds.modelEditor(editor, e = True, polymeshes = True)
cmds.modelEditor(editor, e = True, xray = False)
cmds.showWindow( window )

# face mode on sphere
cmds.select(sphere, r=True)
mel.eval("SelectFacetMask")

# select all visible faces on sphere while animating the camera
for key in range(0, 60):
    cmds.currentTime(key, e=True)
    view = omu.M3dView.active3dView()
    om.MGlobal.selectFromScreen(0, 0, view.portWidth(), view.portHeight(), om.MGlobal.kAddToList)

I added this just before your loop:

cmds.setFocus(editor)

This sets the focus to your new panel and the selectFromScreen command then works as expected.

It’s worth mentioning that you should try to pick variable names that do not clash with commands or keywords. You’ve used ‘camera’, ‘editor’ and ‘sphere’ which are all commands (MEL commands, technically). In the future you may inadvertently overwrite something in memory that you shouldn’t. Just a heads-up.

Also, you don’t need paintSelect=not(True). paintSelect=False is more appropriate.

That was it! Thank you so much @gonzalimator . And thanks for the pointers on the clashing names with MEL. Learning more every day…

Cheers!