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