Python API 2.0 Reference
python/api1/py1MultiPlugInfoCmd.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 from builtins import range
23 from builtins import next
24 import maya.OpenMaya as OpenMaya
25 import maya.OpenMayaMPx as OpenMayaMPx
26 import sys
27 kPluginCmdName = "py1MultiPlugInfo"
28 kIndexFlag = "-i"
29 kIndexFlagLong = "-index"
30 
31 # Wrapper to handle exception when MArrayDataHandle hits the end of the array.
32 def advance(arrayHdl):
33  try:
34  next(arrayHdl)
35  except:
36  return False
37 
38  return True
39 
40 
41 # command
42 class multiPlugInfo(OpenMayaMPx.MPxCommand):
43  def __init__(self):
44  OpenMayaMPx.MPxCommand.__init__(self)
45  # setup private data members
46  self.__isIndex = False
47 
48  def doIt(self, args):
49  """
50  This method is called from script when this command is called.
51  It should set up any class data necessary for redo/undo,
52  parse any given arguments, and then call redoIt.
53  """
54  argData = OpenMaya.MArgDatabase(self.syntax(), args)
55 
56  if argData.isFlagSet(kIndexFlag):
57  self.__isIndex = True
58 
59  # Get the plug specified on the command line.
60  slist = OpenMaya.MSelectionList()
61  argData.getObjects(slist)
62  if slist.length() == 0:
63  print("Must specify an array plug in the form <nodeName>.<multiPlugName>.")
64  return
65 
66  plug = OpenMaya.MPlug()
67  slist.getPlug(0, plug)
68  if plug.isNull():
69  print("Must specify an array plug in the form <nodeName>.<multiPlugName>.")
70  return
71 
72  # Construct a data handle containing the data stored in the plug.
73  dh = plug.asMDataHandle()
74  adh = None
75  try:
77  except:
78  print("Could not create the array data handle.")
79  plug.destructHandle(dh)
80  return
81 
82  # Iterate over the values in the multiPlug. If the index flag has been used, just return
83  # the logical indices of the child plugs. Otherwise, return the plug values.
84  for i in range(adh.elementCount()):
85  try:
86  indx = adh.elementIndex()
87  except:
88  advance(adh)
89  continue
90 
91  if self.__isIndex:
92  self.appendToResult(indx)
93  else:
94  h = adh.outputValue()
95  if h.isNumeric():
96  if h.numericType() == OpenMaya.MFnNumericData.kBoolean:
97  self.appendToResult(h.asBool())
98  elif h.numericType() == OpenMaya.MFnNumericData.kShort:
99  self.appendToResult(h.asShort())
100  elif h.numericType() == OpenMaya.MFnNumericData.kInt:
101  self.appendToResult(h.asInt())
102  elif h.numericType() == OpenMaya.MFnNumericData.kFloat:
103  self.appendToResult(h.asFloat())
104  elif h.numericType() == OpenMaya.MFnNumericData.kDouble:
105  self.appendToResult(h.asDouble())
106  else:
107  print("This sample command only supports boolean, integer, and floating point values.")
108  advance(adh)
109 
110  plug.destructHandle(dh)
111 
112 # Creator
113 def cmdCreator():
114  return OpenMayaMPx.asMPxPtr(multiPlugInfo())
115 
116 
117 # Syntax creator
118 def syntaxCreator():
119  syntax = OpenMaya.MSyntax()
120  syntax.addFlag(kIndexFlag, kIndexFlagLong, OpenMaya.MSyntax.kNoArg)
121  syntax.setObjectType(OpenMaya.MSyntax.kSelectionList, 1, 1)
122  return syntax
123 
124 
125 def initializePlugin(mobject):
126  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
127  try:
128  mplugin.registerCommand(kPluginCmdName, cmdCreator, syntaxCreator)
129  except:
130  sys.stderr.write( "Failed to register command: %s\n" % kPluginCmdName)
131  raise
132 
133 def uninitializePlugin(mobject):
134  mplugin = OpenMayaMPx.MFnPlugin(mobject)
135  try:
136  mplugin.deregisterCommand(kPluginCmdName)
137  except:
138  sys.stderr.write("Failed to unregister command: %s\n" % kPluginCmdName)
139  raise
140