PDA

View Full Version : How to use navigation commands (zoom, rotate, translate etc..) in a python script


Mega69
10-10-2011, 03:48 PM
Hello,
in Maya there are several actions that let you navigate the current view, for example you can zoom in and out (Mouse Wheel), you can rotate the view (Alt + Left Mouse Click), and you can also translate the view (Alt + Mouse Wheel Click)

It is possible to do the same things but from a script? For example to zoom by using a Python script.

Mega69
10-15-2011, 07:21 PM
I have resolved, you can use dolly to zoom, track to translate the view and orbit to rotate, everything is in the documentation.. What the docs don't say is that in order to see a result you have to create a transitory object and then delete it, otherwise the view remains the same:
import maya.cmds as cmds
cmds.sphere(n='UpdateViewThenDeleteIt')
cmds.dolly( 'perspShape', d=21 ) #perspShape could also be omitted, infact it is the default camera
cmds.delete('UpdateViewThenDeleteIt')

NaughtyNathan
10-15-2011, 07:38 PM
you shouldn't have to create or delete anything for the dolly command to work and in fact, if you execute your example code in the script editor, it works fine. The fact you report it not updating makes me think you're running it from inside a script (well I guess that's obvious either way) in which case you may simply just need to use the refresh command to force a viewport refresh. Maya doesn't bother updating the viewport during script execution in most cases as this is simply a waste of performance time.
cmds.refresh(cv=1):nathaN

Mega69
10-16-2011, 09:22 AM
Strange, I'm running it from the script editor, are you sure you have put a # before the lines of the code that create and delete the sphere?
However your solution is very good thanks! the only thing is that I have to set the flag f to true:
cmds.refresh(cv=1, f=1)
without this flag it doesn't work, at least for me

CGTalk Moderation
10-16-2011, 09:22 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.