scripted/pyMetaDataCmd.py
4 import maya.api.OpenMaya
as om
7 To use, make sure that pyMetaDataCmd.py is in your MAYA_PLUG_IN_PATH
13 maya.cmds.loadPlugin("pyMetaDataCmd.py")
14 maya.cmds.pyMetaData()
16 There should now be an extra attribute on your node called:
18 with your user name as its string value
24 The presence of this function tells Maya that the plugin produces, and
25 expects to be passed, objects created using Maya Python API 2.0.
30 class PyMetaDataCmd(om.MPxCommand):
32 Plug-in command named 'pyMetaData'.
34 pyMetaData adds a dynamic string attribute to selected dependency nodes.
35 Or, node names or a list of node names may be passed to the command.
36 Dynamic attributes on nodes can be used as meta-data.
38 The force/f flag is used to specify the command will overwrite existing
39 dynamic string attributes of the same name.
40 False will prevent overwriting pre-existing attributes.
43 The value/v flag is used to specify a value to be set on the new attribute.
44 (default user name from USERNAME or USER envar)
46 The name/n flag is used to specify a name string used to name the attribute.
49 Undo/redo are supported.
51 This command is written using Maya Python API 2.0.
53 Example of usage, MEL:
57 pyMetaData -force -name priorityLevel -value High;
58 pyMetaData -name health -v sick pSphere1 pSphere2;
60 Example of usage, Python:
62 maya.cmds.pyMetaData()
63 maya.cmds.pyMetaData(f=True)
64 maya.cmds.pyMetaData(force=True, name='priorityLevel', value='High')
65 maya.cmds.pyMetaData('pSphere1','pSphere2',name='health', v='sick')
66 maya.cmds.pyMetaData(['pSphere1','pSphere2'],name='health', v='sick')
71 cmdName =
'pyMetaData'
74 nameFlagLong =
'-name'
77 forceFlagLong =
'-force'
80 valueFlagLong=
'-value'
84 om.MPxCommand.__init__(self)
88 return PyMetaDataCmd()
93 Using kSelectionList in setObjectType
94 and calling useSelectionAsDefault(True),
95 allows us to use either a command line list of nodes,
96 or use the active selection list if no node names are specified.
98 The force/f flag is defined as requiring no arguments.
99 By not specifying any arguments for the flag when we define it,
100 the flag will not require any arguments when used in MEL,
101 it will still require a value of 'True' or 'False' when used in Python.
103 syntax = om.MSyntax()
104 syntax.setObjectType(om.MSyntax.kSelectionList)
105 syntax.useSelectionAsDefault(
True)
106 syntax.addFlag(PyMetaDataCmd.forceFlag, PyMetaDataCmd.forceFlagLong)
107 syntax.addFlag(PyMetaDataCmd.nameFlag, PyMetaDataCmd.nameFlagLong, om.MSyntax.kString)
108 syntax.addFlag(PyMetaDataCmd.valueFlag, PyMetaDataCmd.valueFlagLong, om.MSyntax.kString)
111 def doIt(self, args):
115 argdb = om.MArgDatabase(self.syntax(), args)
117 om.MGlobal.displayError(
'Error while parsing arguments:\n#\t# If passing in list of nodes, also check that node names exist in scene.')
122 selList = argdb.getObjectList()
126 force = argdb.isFlagSet(PyMetaDataCmd.forceFlag)
130 if argdb.isFlagSet(PyMetaDataCmd.nameFlag):
131 name = argdb.flagArgumentString(PyMetaDataCmd.nameFlag, 0)
137 if argdb.isFlagSet(PyMetaDataCmd.valueFlag):
138 value = argdb.flagArgumentString(PyMetaDataCmd.valueFlag, 0)
140 if os.environ.has_key(
'USERNAME'):
141 value = os.environ[
'USERNAME']
142 elif os.environ.has_key(
'USER'):
143 value = os.environ[
'USER']
151 self.dgmod = om.MDGModifier()
155 def addMetaData(dependNode, fnDN):
158 if fnDN.hasAttribute(name):
160 plug = om.MPlug(dependNode, fnDN.attribute(name))
161 self.dgmod.newPlugValueString(plug, value)
164 print (
'passing over "' + fnDN.name()+
'": force flag not set to True')
168 fnAttr = om.MFnTypedAttribute()
169 newAttr = fnAttr.create(name, name, om.MFnData.kString)
174 self.dgmod.addAttribute(dependNode, newAttr)
179 plug = om.MPlug(dependNode, newAttr)
183 self.dgmod.newPlugValueString(plug, value)
190 for i
in range(selList.length()):
195 dependNode = selList.getDependNode(i)
196 fnDN = om.MFnDependencyNode(dependNode)
198 om.MGlobal.displayWarning(
'item %d is not a depend node'%i)
200 addMetaData(dependNode, fnDN)
208 def isUndoable(self):
211 def initializePlugin(plugin):
212 pluginFn = om.MFnPlugin(plugin)
213 pluginFn.registerCommand(
214 PyMetaDataCmd.cmdName, PyMetaDataCmd.creator, PyMetaDataCmd.createSyntax
217 def uninitializePlugin(plugin):
218 pluginFn = om.MFnPlugin(plugin)
219 pluginFn.deregisterCommand(PyMetaDataCmd.cmdName)