Search code examples
pythonpython-2.7polygonmayapymel

Auto-completing "Create Polygon Tool" Maya


I'm trying to figure out a way to create an auto-completing "Create Polygon Tool" in Maya, in 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 Poly tool actually doesn't let you exit its command.

Would anyone know a way around this?

Here's a snip-bit of my code:

from pymel.core import *
def codeToExecute():
    #lists, combines, does a few other things like deleting history
polyCreateFaceCtx('newCtx',mp=4)
setToolTo('newCtx')                      #allows you to create a polygon
maya.mel.eval('CompleteCurrentTool')     #need this to stop it from continuing its loop
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.


Solution

  • 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.