Skip to content

Commit

Permalink
fix for string attributes default values
Browse files Browse the repository at this point in the history
- see mottosso#34
- handles Compounds recursively
  • Loading branch information
Benoit Fouletier committed Nov 4, 2020
1 parent 55187ad commit 826f949
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cmdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,10 +1022,20 @@ def addAttr(self, attr):
"""

if isinstance(attr, _AbstractAttribute):
attr = attr.create()
attr_obj = attr.create() if isinstance(attr, _AbstractAttribute) else attr
self._fn.addAttribute(attr_obj)

# workaround for string attribute default value, see String.default() for details
def setAttr(attr):
if isinstance(attr, String):
default = _AbstractAttribute.default(attr)
if default:
self.findPlug(attr["name"]).setString(default)
elif isinstance(attr, Compound):
for child in attr["children"]:
setAttr(child)
setAttr(attr)

self._fn.addAttribute(attr)

def hasAttr(self, attr):
"""Return whether or not `attr` exists
Expand Down Expand Up @@ -5119,9 +5129,11 @@ class String(_AbstractAttribute):
Type = om.MFnData.kString
Default = ""

# string attributes can't have default values (http://help.autodesk.com/cloudhelp/2020/ENU/Maya-Tech-Docs/Commands/addAttr.html#flagdefaultValue),
# or at least it isn't saved in scenes (https://github.com/mottosso/cmdx/issues/34)
# -> don't pass the default to OpenMaya, instead set it during Node.addAttr()
def default(self, cls=None):
default = str(super(String, self).default(cls))
return om.MFnStringData().create(default)
return None

def read(self, data):
return data.inputValue(self["mobject"]).asString()
Expand Down

0 comments on commit 826f949

Please sign in to comment.