scripted/pyWhatIsCmd.py

scripted/pyWhatIsCmd.py
1 ########################################################################
2 # DESCRIPTION:
3 #
4 # Produces the Python command "pyWhatIs".
5 #
6 # This simple command prints a message describing the API types of Maya objects.
7 # If no Maya objects are passed to the command, then it lists the types of
8 # all of the currently selected objects.
9 #
10 # To use, make sure that pyWhatIsCmd.py is in your MAYA_PLUG_IN_PATH,
11 # then do the following:
12 #
13 # import maya.cmds
14 # maya.cmds.loadPlugin("pyWhatIsCmd.py")
15 # maya.cmds.pyWhatIs()
16 #
17 # For each object, the following information will be printed:
18 # - name of the object
19 # - API type for the object
20 # - API function sets that can be used on the object. Note that not every
21 # function set listed actually exists.
22 #
23 # This list is essentially the class derivation list containing all parent classes of this object.
24 #
25 # For example, create a NURBS sphere, make sure the sphere is selected and enter the command:
26 #
27 # maya.cmds.pyWhatIs()
28 #
29 # The command results in the following output in the script editor:
30 #
31 # Name: nurbsSphere1
32 #
33 # Type: kTransform
34 #
35 # Function Sets: kBase, kNamedObject, kDependencyNode, kDagNode , kTransform
36 #
37 ########################################################################
38 
39 import sys
40 import maya.api.OpenMaya as om
41 
42 def maya_useNewAPI():
43  """
44  The presence of this function tells Maya that the plugin produces, and
45  expects to be passed, objects created using the Maya Python API 2.0.
46  """
47  pass
48 
49 
50 # command
51 class PyWhatIsCmd(om.MPxCommand):
52  kPluginCmdName = "pyWhatIs"
53 
54  def __init__(self):
55  om.MPxCommand.__init__(self)
56 
57  @staticmethod
58  def cmdCreator():
59  return PyWhatIsCmd()
60 
61  def doIt(self, args):
62  selectList = om.MGlobal.getActiveSelectionList()
63  depFn = om.MFnDependencyNode()
64 
65  for i in range(selectList.length()):
66  node = selectList.getDependNode(i)
67 
68  depFn.setObject(node)
69 
70  types = om.MGlobal.getFunctionSetList(node)
71 
72  print "Name: %s" % depFn.name()
73  print "Type: %s" % node.apiTypeStr
74  sys.stdout.write( "Function Sets: " )
75  sys.stdout.write(", ".join(types) + '\n')
76 
77 
78 # Initialize the script plug-in
79 def initializePlugin(plugin):
80  pluginFn = om.MFnPlugin(plugin)
81  try:
82  pluginFn.registerCommand(
83  PyWhatIsCmd.kPluginCmdName, PyWhatIsCmd.cmdCreator
84  )
85  except:
86  sys.stderr.write(
87  "Failed to register command: %s\n" % PyWhatIsCmd.kPluginCmdName
88  )
89  raise
90 
91 # Uninitialize the script plug-in
92 def uninitializePlugin(plugin):
93  pluginFn = om.MFnPlugin(plugin)
94  try:
95  pluginFn.deregisterCommand(PyWhatIsCmd.kPluginCmdName)
96  except:
97  sys.stderr.write(
98  "Failed to unregister command: %s\n" % PyWhatIsCmd.kPluginCmdName
99  )
100  raise
101 
102 #-
103 # ==========================================================================
104 # Copyright (C) 2011 Autodesk, Inc. and/or its licensors. All
105 # rights reserved.
106 #
107 # The coded instructions, statements, computer programs, and/or related
108 # material (collectively the "Data") in these files contain unpublished
109 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
110 # licensors, which is protected by U.S. and Canadian federal copyright
111 # law and by international treaties.
112 #
113 # The Data is provided for use exclusively by You. You have the right
114 # to use, modify, and incorporate this Data into other products for
115 # purposes authorized by the Autodesk software license agreement,
116 # without fee.
117 #
118 # The copyright notices in the Software and this entire statement,
119 # including the above license grant, this restriction and the
120 # following disclaimer, must be included in all copies of the
121 # Software, in whole or in part, and all derivative works of
122 # the Software, unless such copies or derivative works are solely
123 # in the form of machine-executable object code generated by a
124 # source language processor.
125 #
126 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
127 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
128 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
129 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
130 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
131 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
132 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
133 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
134 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
135 # OR PROBABILITY OF SUCH DAMAGES.
136 #
137 # ==========================================================================
138 #+