SirJames SirJames - 2 days ago 4
Python Question

Auto-completing "Create Polygon Tool" command in Maya

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
actually doesn't let you exit this command. Would anyone know a workaround?

Here's a snip-bit of my code:

import maya.cmds as cmds
import maya.mel as mm
from pymel.core import *

def codeToExecute():

# 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 )
codeToExecute()


However, since it's run together, it sets the tool to create a polygon and quits it automatically.

Any help would be appreciated.

Answer

You can force exit the context with

cmds.setToolTo('selectSuperContext') 

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.