def _add_attr(fnDependNode, name, data, cache=None): data_type = core.get_data_type(data) # Skip empty list is_multi = data_type == core.TYPE_LIST if is_multi and len(filter(None, data)) == 0: return plug = None # Get attribute arguments try: # TODO: Is a try/catch really the best way to know if the plug exists? plug = fnDependNode.findPlug(name) except: pass if plug is None: fnAtt = _create_attr(name, data) if fnAtt is None: return # In case of invalid value like missing pymel PyNode & Attributes fnAtt.setNiceNameOverride(name) moAtt = fnAtt.object() if moAtt is not None: try: fnDependNode.addAttribute(moAtt) plug = OpenMaya.MPlug(fnDependNode.object(), moAtt) except Exception as e: log.warning('Error adding attribute {0}: {1}'.format(name, e)) if plug is not None: _set_attr(plug, data, cache=cache)
def _set_attr(_plug, data, cache=None): data_type = core.get_data_type(data) if data_type == core.TYPE_LIST: num_elements = len(data) _plug.setNumElements( num_elements) # TODO: MAKE IT WORK # TODO: NECESSARY??? for i in range(num_elements): _set_attr(_plug.elementByLogicalIndex(i), data[i], cache=cache) elif data_type == core.TYPE_BASIC: # Basic types if isinstance(data, bool): _plug.setBool(data) elif isinstance(data, int): _plug.setInt(data) elif isinstance(data, float): _plug.setFloat(data) elif isinstance(data, basestring): _plug.setString(data) # pymel.Attribute(_plug).set(_val) elif data_type == core.TYPE_COMPLEX: network = export_network(data, cache=cache) plug = network.__apimfn__().findPlug('message') # Use a dag modifier to connect the attribute. TODO: Is this really the best way? dagM = OpenMaya.MDagModifier() dagM.connect(plug, _plug) dagM.doIt() elif data_type == core.TYPE_DAGNODE: plug = None if isinstance(data, pymel.Attribute): # pymel.Attribute # Hack: Don't crash with non-existent pymel.Attribute if not data.exists(): log.warning( "Can't setAttr, Attribute {0} don't exist".format(data)) return plug = data.__apimfn__() elif isinstance(data, pymel.datatypes.Matrix): fn = OpenMaya.MFnMatrixData() mo = fn.create(data.apicls(data)) _plug.setMObject(mo) return True elif isinstance(data, pymel.datatypes.Vector): _plug.child(0).setFloat(data.x) _plug.child(1).setFloat(data.y) _plug.child(2).setFloat(data.z) return True elif hasattr(data, 'exists'): # pymel.PyNode # Hack: Don't crash with non-existent pymel.Attribute if not pymel.objExists(data.__melobject__()): log.warning( "Can't setAttr, PyNode {0} don't exist".format(data)) return plug = data.__apimfn__().findPlug('message') if plug is not None: dagM = OpenMaya.MDagModifier() # if pymel.attributeQuery(pymel.Attribute(_val), writable=True): dagM.connect(plug, _plug) dagM.connect(plug, _plug) # is connecting two times necessary? dagM.doIt() else: raise Exception("Unknow TYPE {0}, {1}".format(type(data), data)) elif data_type == core.TYPE_NONE: return else: print data, data_type raise NotImplementedError
def _set_attr(_plug, data, cache=None): data_type = core.get_data_type(data) if data_type == core.TYPE_LIST: num_elements = len(data) _plug.setNumElements(num_elements) # TODO: MAKE IT WORK # TODO: NECESSARY??? for i in range(num_elements): _set_attr(_plug.elementByLogicalIndex(i), data[i], cache=cache) elif data_type == core.TYPE_BASIC: # Basic types if isinstance(data, bool): _plug.setBool(data) elif isinstance(data, int): _plug.setInt(data) elif isinstance(data, float): _plug.setFloat(data) elif isinstance(data, basestring): _plug.setString(data) # pymel.Attribute(_plug).set(_val) elif data_type == core.TYPE_COMPLEX: network = export_network(data, cache=cache) plug = network.__apimfn__().findPlug('message') # Use a dag modifier to connect the attribute. TODO: Is this really the best way? dagM = OpenMaya.MDagModifier() dagM.connect(plug, _plug) dagM.doIt() elif data_type == core.TYPE_DAGNODE: plug = None if isinstance(data, pymel.Attribute): # pymel.Attribute # Hack: Don't crash with non-existent pymel.Attribute if not data.exists(): log.warning("Can't setAttr, Attribute {0} don't exist".format(data)) return plug = data.__apimfn__() elif isinstance(data, pymel.datatypes.Matrix): fn = OpenMaya.MFnMatrixData() mo = fn.create(data.apicls(data)) _plug.setMObject(mo) return True elif isinstance(data, pymel.datatypes.Vector): _plug.child(0).setFloat(data.x) _plug.child(1).setFloat(data.y) _plug.child(2).setFloat(data.z) return True elif hasattr(data, 'exists'): # pymel.PyNode # Hack: Don't crash with non-existent pymel.Attribute if not pymel.objExists(data.__melobject__()): log.warning("Can't setAttr, PyNode {0} don't exist".format(data)) return plug = data.__apimfn__().findPlug('message') if plug is not None: dagM = OpenMaya.MDagModifier() # if pymel.attributeQuery(pymel.Attribute(_val), writable=True): dagM.connect(plug, _plug) dagM.connect(plug, _plug) # is connecting two times necessary? dagM.doIt() else: raise Exception("Unknow TYPE {0}, {1}".format(type(data), data)) elif data_type == core.TYPE_NONE: return else: print data, data_type raise NotImplementedError