1 from builtins
import range
5 import maya.api.OpenMaya
as om
8 To use, make sure that pyMetaDataCmd.py is in your MAYA_PLUG_IN_PATH
14 maya.cmds.loadPlugin("pyMetaDataCmd.py")
15 maya.cmds.pyMetaData()
17 There should now be an extra attribute on your node called:
19 with your user name as its string value
25 The presence of this function tells Maya that the plugin produces, and
26 expects to be passed, objects created using Maya Python API 2.0.
31 class PyMetaDataCmd(om.MPxCommand):
33 Plug-in command named 'pyMetaData'.
35 pyMetaData adds a dynamic string attribute to selected dependency nodes.
36 Or, node names or a list of node names may be passed to the command.
37 Dynamic attributes on nodes can be used as meta-data.
39 The force/f flag is used to specify the command will overwrite existing
40 dynamic string attributes of the same name.
41 False will prevent overwriting pre-existing attributes.
44 The value/v flag is used to specify a value to be set on the new attribute.
45 (default user name from USERNAME or USER envar)
47 The name/n flag is used to specify a name string used to name the attribute.
50 Undo/redo are supported.
52 This command is written using Maya Python API 2.0.
54 Example of usage, MEL:
58 pyMetaData -force -name priorityLevel -value High;
59 pyMetaData -name health -v sick pSphere1 pSphere2;
61 Example of usage, Python:
63 maya.cmds.pyMetaData()
64 maya.cmds.pyMetaData(f=True)
65 maya.cmds.pyMetaData(force=True, name='priorityLevel', value='High')
66 maya.cmds.pyMetaData('pSphere1','pSphere2',name='health', v='sick')
67 maya.cmds.pyMetaData(['pSphere1','pSphere2'],name='health', v='sick')
72 cmdName =
'pyMetaData'
75 nameFlagLong =
'-name'
78 forceFlagLong =
'-force'
81 valueFlagLong=
'-value'
85 om.MPxCommand.__init__(self)
89 return PyMetaDataCmd()
94 Using kSelectionList in setObjectType
95 and calling useSelectionAsDefault(True),
96 allows us to use either a command line list of nodes,
97 or use the active selection list if no node names are specified.
99 The force/f flag is defined as requiring no arguments.
100 By not specifying any arguments for the flag when we define it,
101 the flag will not require any arguments when used in MEL,
102 it will still require a value of 'True' or 'False' when used in Python.
104 syntax = om.MSyntax()
105 syntax.setObjectType(om.MSyntax.kSelectionList)
106 syntax.useSelectionAsDefault(
True)
107 syntax.addFlag(PyMetaDataCmd.forceFlag, PyMetaDataCmd.forceFlagLong)
108 syntax.addFlag(PyMetaDataCmd.nameFlag, PyMetaDataCmd.nameFlagLong, om.MSyntax.kString)
109 syntax.addFlag(PyMetaDataCmd.valueFlag, PyMetaDataCmd.valueFlagLong, om.MSyntax.kString)
112 def doIt(self, args):
116 argdb = om.MArgDatabase(self.syntax(), args)
118 om.MGlobal.displayError(
'Error while parsing arguments:\n#\t# If passing in list of nodes, also check that node names exist in scene.')
123 selList = argdb.getObjectList()
127 force = argdb.isFlagSet(PyMetaDataCmd.forceFlag)
131 if argdb.isFlagSet(PyMetaDataCmd.nameFlag):
132 name = argdb.flagArgumentString(PyMetaDataCmd.nameFlag, 0)
138 if argdb.isFlagSet(PyMetaDataCmd.valueFlag):
139 value = argdb.flagArgumentString(PyMetaDataCmd.valueFlag, 0)
141 if 'USERNAME' in os.environ:
142 value = os.environ[
'USERNAME']
143 elif 'USER' in os.environ:
144 value = os.environ[
'USER']
152 self.dgmod = om.MDGModifier()
156 def addMetaData(dependNode, fnDN):
159 if fnDN.hasAttribute(name):
161 plug = om.MPlug(dependNode, fnDN.attribute(name))
162 self.dgmod.newPlugValueString(plug, value)
165 print (
'passing over "' + fnDN.name()+
'": force flag not set to True')
169 fnAttr = om.MFnTypedAttribute()
170 newAttr = fnAttr.create(name, name, om.MFnData.kString)
175 self.dgmod.addAttribute(dependNode, newAttr)
180 plug = om.MPlug(dependNode, newAttr)
184 self.dgmod.newPlugValueString(plug, value)
191 for i
in range(selList.length()):
196 dependNode = selList.getDependNode(i)
197 fnDN = om.MFnDependencyNode(dependNode)
199 om.MGlobal.displayWarning(
'item %d is not a depend node'%i)
201 addMetaData(dependNode, fnDN)
209 def isUndoable(self):
212 def initializePlugin(plugin):
213 pluginFn = om.MFnPlugin(plugin)
214 pluginFn.registerCommand(
215 PyMetaDataCmd.cmdName, PyMetaDataCmd.creator, PyMetaDataCmd.createSyntax
218 def uninitializePlugin(plugin):
219 pluginFn = om.MFnPlugin(plugin)
220 pluginFn.deregisterCommand(PyMetaDataCmd.cmdName)