scripted/zoomCameraCmd.py

scripted/zoomCameraCmd.py
1 
2 #-
3 # ==========================================================================
4 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
5 # rights reserved.
6 #
7 # The coded instructions, statements, computer programs, and/or related
8 # material (collectively the "Data") in these files contain unpublished
9 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
10 # licensors, which is protected by U.S. and Canadian federal copyright
11 # law and by international treaties.
12 #
13 # The Data is provided for use exclusively by You. You have the right
14 # to use, modify, and incorporate this Data into other products for
15 # purposes authorized by the Autodesk software license agreement,
16 # without fee.
17 #
18 # The copyright notices in the Software and this entire statement,
19 # including the above license grant, this restriction and the
20 # following disclaimer, must be included in all copies of the
21 # Software, in whole or in part, and all derivative works of
22 # the Software, unless such copies or derivative works are solely
23 # in the form of machine-executable object code generated by a
24 # source language processor.
25 #
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
27 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
28 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
29 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
30 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
31 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
32 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
33 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
34 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
35 # OR PROBABILITY OF SUCH DAMAGES.
36 #
37 # ==========================================================================
38 #+
39 
40 ########################################################################
41 # DESCRIPTION:
42 #
43 # Produces the Python command "spZoomCamera".
44 #
45 # This is a simple plug-in which divides the horizontal field of view for the
46 # current active camera by 2.0. It is a good example of getting the current active
47 # view, and of modifying the camera.
48 #
49 # To use this plug-in, do the following:
50 #
51 # (1) Create any object for the scene.
52 # (2) Select Create > Cameras > Camera.
53 # (3) Direct the camera to face an object in the scene.
54 # (4) Select Panels > Perspective > camera1.
55 # (5) When you are in a view that looks through the camera, execute these commands:
56 #
57 # import maya.cmds
58 # maya.cmds.loadPlugin("zoomCameraCmd.py")
59 # maya.cmds.spZoomCamera()
60 #
61 # Your view through the camera will zoom-in by a factor of 2.
62 #
63 ########################################################################
64 
65 import sys
66 import maya.OpenMaya as OpenMaya
67 import maya.OpenMayaUI as OpenMayaUI
68 import maya.OpenMayaMPx as OpenMayaMPx
69 
70 kPluginCmdName = "spZoomCamera"
71 
72 print "zoomCameraCmd.py has been imported...."
73 
74 # command
75 class scriptedCommand(OpenMayaMPx.MPxCommand):
76  camera = OpenMaya.MDagPath()
77  def __init__(self):
78  OpenMayaMPx.MPxCommand.__init__(self)
79 
80  def redoIt(self):
81  global camera
82  fnCamera = OpenMaya.MFnCamera(camera)
83  f1 = fnCamera.focalLength()
84  fnCamera.setFocalLength(f1 * 2.0)
85 
86  def undoIt(self):
87  global camera
88  fnCamera = OpenMaya.MFnCamera(camera)
89  f1 = fnCamera.focalLength()
90  fnCamera.setFocalLength(f1 / 2.0)
91 
92  def doIt(self, args):
93  global camera
94  camera = OpenMaya.MDagPath()
95  try:
96  OpenMayaUI.M3dView.active3dView().getCamera(camera)
97  except:
98  sys.stderr.write( "ERROR: getting camera \n" )
99  else:
100  self.redoIt()
101 
102  def isUndoable(self):
103  return True
104 
105 #Cmd Creator
106 def cmdCreator():
107  return OpenMayaMPx.asMPxPtr( scriptedCommand() )
108 
109 # Initialize the script plug-in
110 def initializePlugin(obj):
111  plugin = OpenMayaMPx.MFnPlugin(obj)
112  try:
113  plugin.registerCommand( kPluginCmdName, cmdCreator)
114  except:
115  sys.stderr.write( "Failed to register command: %s\n" % kPluginCmdName )
116 
117 # Uninitialize the script plug-in
118 def uninitializePlugin(obj):
119  plugin = OpenMayaMPx.MFnPlugin(obj)
120  try:
121  plugin.deregisterCommand(kPluginCmdName)
122  except:
123  sys.stderr.write( "Failed to unregister command: %s\n" % kPluginCmdName )
124 
125