Sami Ben
Sami Ben

Reputation: 556

Toggle between two commands with one key at a time

I'd like to make maya switch between top and bottom orthographic views with one hotkey, front and back with another hotkey, and left and right with a third, like in MODO. That is two commands with one key at a time. I would like to know how to do that, in Python or Mel in the runtime command editor, and preferably with any other commands that I choose in the future. Thanks.

Upvotes: 1

Views: 1180

Answers (1)

Regnareb
Regnareb

Reputation: 144

It is actually quite simple, I don't really like to play with the default Maya cameras, but I don't think that is a problem here. All you have to do is multiply by -1 the translate coordinates. and add 180 degrees to the corresponding axis for each camera you want.

 def getActiveViewport():
    """Return the active 3D viewport if any"""
    panel = cmds.getPanel(withFocus=True)
    if cmds.getPanel(typeOf=panel) == 'modelPanel':
        return panel
    return ''


def switchcamera(cam):
    viewport = getActiveViewport()
    if viewport:
        orient = {'top': 'X', 'front': 'Y', 'side': 'Y'}
        translate = cmds.getAttr(cam + '.translate')[0]
        translate = [i*-1 for i in translate]
        rotate = cmds.getAttr(cam + '.rotate' + orient[cam])
        rotate = (rotate + 180) % 360
        if rotate < 0:
            rotate = rotate + 360        

        cmds.setAttr(cam + '.translate', *translate, type='double3')
        cmds.setAttr(cam + '.rotate' + orient[cam], rotate)
        cmds.modelPanel(viewport, edit=True, camera=cam)  # Set the camera to the active viewport

You can then call those commands with a viewport in focus, and it will automatically switch to the specified camera.

switchcamera('top')
switchcamera('front')
switchcamera('side')

You could also create new cameras for each orientation -if they don't exists already- and switch back and forth between the default and non-default ones. Without forgetting to copy their translate/rotate attribute, that's the tricky and less elegant part of that solution.

Upvotes: 2

Related Questions