I'm trying to figure out a way to create an auto-completing "Create Polygon Tool" command in Autodesk Maya via Python. It's so when you build geometry, it will select the previous geometry, and the current one you made, and run a line of commands.
I've run into a snag, though, in which the
Create Polygon Tool
import maya.cmds as cmds
import maya.mel as mm
from pymel.core import *
# Lists, combines, does a few other things like deleting history
polyCreateFaceCtx( 'newCtx', mp = 4 )
# Allows you to create a polygon
setToolTo( 'newCtx' )
# Need this to stop it from continuing the loop
mm.eval( 'CompleteCurrentTool' )
geo = ls( sl = True )
You can force exit the context with
which will switch you to the select tool and complete the poly tool. However that's not going to work as you've laid it out here, I think: you'll swicth to the tool and immediately switch out without waiting for the user to create stuff.
You might have an easier time setting up a one-time scriptJob that looks for new object creation, which will run when the user exits the command on their own:
def do_something(*_): print cmds.ls(sl=True) cmds.scriptJob(e=('DagObjectCreated', do_something), runOnce =True) cmds.polyCreateFaceCtx('newCtx',mp=4) cmds.setToolTo('newCtx')
That will fire the scriptJob when the user completes the tool on their own.