[Maya-Python] Custom Repeat Last Command

944 views
Skip to first unread message

Marcus Ottosson

unread,
Jul 31, 2018, 3:27:02 PM7/31/18
to python_in...@googlegroups.com

Hi all,

When you call something from a menu, it is added to a Recent Command List accessible via the Edit menu. Likewise, when you execute a line in the script editor, the full contents of what you ran is added to said list.

The last entry in that list is repeated on pressing G, or going Edit -> Repeat …

However, when running something outside of a menu or Script Editor, this doesn’t happen.

from Qt import QtWidgets

def onclick():
    cmds.polyCube()

button =   QtWidgets.QPushButton("Press Me")
button.show()
button.clicked.connect(onclick)

Here, a new cube is created on pressing the button, but on hitting G what you get instead is a new button; as it is repeating the script itself rather than what happened on pressing the button.

How can I make the contents of onclick trigger on hitting G or pressing Edit -> Repeat …?

I’ve tried cmds.evalDeferred("cmds.polyCube()") which I figured would do the job for me, but doesn’t. Like wise with maya.utils.executeDeferred. Then I figured I need a custom command plug-in, but then realised polyCube is also a command and Maya doesn’t care either way.

My next/last resort is creating a new but hidden Maya menu, adding a command to it and triggering that so as to force Maya into adding it to the list, and then deleting it. But would love to avoid that.

Any ideas?

Carlos Rico

unread,
Jul 31, 2018, 3:56:23 PM7/31/18
to python_in...@googlegroups.com

In MEL you can use:

Synopsis: repeatLast [flags]
Flags:
-e -edit
-q -query
-ac -addCommand String
-acl -addCommandLabel String
-cl -commandList Int
-cnl -commandNameList Int
-hl -historyLimit Int
-i -item Int
-nhi -numberOfHistoryItems

Command Type: Command

It is also available in python but it seems tha

t
only accepts MEL code, give it a try.

from maya import cmds

def create_sphere(*args):
    cmds.sphere()
    cmds.repeatLast(ac='python("from maya import cmds;cmds.sphere()")')

cmds.window(t="repeatLast", w=300)
cmds.columnLayout()
cmds.button(l='create_sphere', c=create_sphere)
cmds.showWindow()

Hope it helps. Cheers

Carlos Rico Adega

LinkedIn
carlos....@gmail.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/CAFRtmOCrODZ-1ZM_J-K2uFHYB3VpkBynFzyVkgvgDpcKmewt%2BQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Marcus Ottosson

unread,
Jul 31, 2018, 4:31:16 PM7/31/18
to python_in...@googlegroups.com

Thanks Carlos!

Once I found out about cmds.repeatLast I discovered this old gem.

Problem solved!


Reply all
Reply to author
Forward
0 new messages