scripted/multiPlugInfoCmd.py

scripted/multiPlugInfoCmd.py
1 #-
2 # ==========================================================================
3 # Copyright 2010 Autodesk, Inc. All rights reserved.
4 #
5 # Use of this software is subject to the terms of the Autodesk
6 # license agreement provided at the time of installation or download,
7 # or which otherwise accompanies this software in either electronic
8 # or hard copy form.
9 # ==========================================================================
10 #+
11 
12 #
13 # This plugin prints out the child plug information for a multiPlug.
14 # If the -index flag is used, the logical index values used by the plug
15 # will be returned. Otherwise, the plug values will be returned.
16 #
17 # import maya.cmds as cmds
18 # cmds.loadPlugin("multiPlugInfoCmd.py")
19 # cmds.multiPlugInfo("myObj.myMultiAttr", index=True)
20 #
21 
22 import maya.OpenMaya as OpenMaya
23 import maya.OpenMayaMPx as OpenMayaMPx
24 import sys
25 kPluginCmdName = "multiPlugInfo"
26 kIndexFlag = "-i"
27 kIndexFlagLong = "-index"
28 
29 # Wrapper to handle exception when MArrayDataHandle hits the end of the array.
30 def advance(arrayHdl):
31  try:
32  arrayHdl.next()
33  except:
34  return False
35 
36  return True
37 
38 
39 # command
40 class multiPlugInfo(OpenMayaMPx.MPxCommand):
41  def __init__(self):
42  OpenMayaMPx.MPxCommand.__init__(self)
43  # setup private data members
44  self.__isIndex = False
45 
46  def doIt(self, args):
47  """
48  This method is called from script when this command is called.
49  It should set up any class data necessary for redo/undo,
50  parse any given arguments, and then call redoIt.
51  """
52  argData = OpenMaya.MArgDatabase(self.syntax(), args)
53 
54  if argData.isFlagSet(kIndexFlag):
55  self.__isIndex = True
56 
57  # Get the plug specified on the command line.
58  slist = OpenMaya.MSelectionList()
59  argData.getObjects(slist)
60  if slist.length() == 0:
61  print "Must specify an array plug in the form <nodeName>.<multiPlugName>."
62  return
63 
64  plug = OpenMaya.MPlug()
65  slist.getPlug(0, plug)
66  if plug.isNull():
67  print "Must specify an array plug in the form <nodeName>.<multiPlugName>."
68  return
69 
70  # Construct a data handle containing the data stored in the plug.
71  dh = plug.asMDataHandle()
72  adh = None
73  try:
75  except:
76  print "Could not create the array data handle."
77  plug.destructHandle(dh)
78  return
79 
80  # Iterate over the values in the multiPlug. If the index flag has been used, just return
81  # the logical indices of the child plugs. Otherwise, return the plug values.
82  for i in range(adh.elementCount()):
83  try:
84  indx = adh.elementIndex()
85  except:
86  advance(adh)
87  continue
88 
89  if self.__isIndex:
90  self.appendToResult(indx)
91  else:
92  h = adh.outputValue()
93  if h.isNumeric():
94  if h.numericType() == OpenMaya.MFnNumericData.kBoolean:
95  self.appendToResult(h.asBool())
96  elif h.numericType() == OpenMaya.MFnNumericData.kShort:
97  self.appendToResult(h.asShort())
98  elif h.numericType() == OpenMaya.MFnNumericData.kInt:
99  self.appendToResult(h.asInt())
100  elif h.numericType() == OpenMaya.MFnNumericData.kFloat:
101  self.appendToResult(h.asFloat())
102  elif h.numericType() == OpenMaya.MFnNumericData.kDouble:
103  self.appendToResult(h.asDouble())
104  else:
105  print "This sample command only supports boolean, integer, and floating point values."
106  advance(adh)
107 
108  plug.destructHandle(dh)
109 
110 # Creator
111 def cmdCreator():
112  return OpenMayaMPx.asMPxPtr(multiPlugInfo())
113 
114 
115 # Syntax creator
116 def syntaxCreator():
117  syntax = OpenMaya.MSyntax()
118  syntax.addFlag(kIndexFlag, kIndexFlagLong, OpenMaya.MSyntax.kNoArg)
119  syntax.setObjectType(OpenMaya.MSyntax.kSelectionList, 1, 1)
120  return syntax
121 
122 
123 def initializePlugin(mobject):
124  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
125  try:
126  mplugin.registerCommand(kPluginCmdName, cmdCreator, syntaxCreator)
127  except:
128  sys.stderr.write( "Failed to register command: %s\n" % kPluginCmdName)
129  raise
130 
131 def uninitializePlugin(mobject):
132  mplugin = OpenMayaMPx.MFnPlugin(mobject)
133  try:
134  mplugin.deregisterCommand(kPluginCmdName)
135  except:
136  sys.stderr.write("Failed to unregister command: %s\n" % kPluginCmdName)
137  raise
138