def export_network(_data, **kwargs): log.debug('CreateNetwork {0}'.format(_data)) # We'll deal with two additional attributes, '_network' and '_uid'. # Thoses two attributes allow us to find the network from the value and vice-versa. # Note that since the '_uid' refer to the current python context, it's value could be erroned when calling import_network. # However the change of collisions are extremely improbable so checking the type of the python variable is sufficient. # Please feel free to provide a better design if any if possible. # Optimisation: Use existing network if already present in scene if hasattr(_data, '_network') and libPymel.is_valid_PyNode(_data._network): network = _data._network else: # Automaticly name network whenever possible if hasattr(_data, '__getNetworkName__') and _data.__getNetworkName__ is None: networkName = _data.__class__.__name__ else: networkName = _data.__getNetworkName__() if hasattr( _data, '__getNetworkName__') else _data.__class__.__name__ _data._network = networkName network = pymel.createNode('network', name=networkName) # Ensure the network have the current python id stored if not network.hasAttr('_uid'): pymel.addAttr(network, longName='_uid', niceName='_uid', at='long') # todo: validate attributeType # network._uid.set(id(_data)) # Convert _pData to basic data dictionary (recursive for now) dicData = core._export_basicData(_data, _bRecursive=False, **kwargs) assert (isinstance(dicData, dict)) fnNet = network.__apimfn__() for key, val in dicData.items(): if val is not None: if key == '_class' or key[ 0] != '_': # Attributes starting with '_' are protected or private _addAttr(fnNet, key, val) return network
def export_network(_data, **kwargs): log.debug('CreateNetwork {0}'.format(_data)) # We'll deal with two additional attributes, '_network' and '_uid'. # Thoses two attributes allow us to find the network from the value and vice-versa. # Note that since the '_uid' refer to the current python context, it's value could be erroned when calling import_network. # However the change of collisions are extremely improbable so checking the type of the python variable is sufficient. # Please feel free to provide a better design if any if possible. # Optimisation: Use existing network if already present in scene if hasattr(_data, '_network') and libPymel.is_valid_PyNode(_data._network): network = _data._network else: # Automaticly name network whenever possible if hasattr(_data, '__getNetworkName__') and _data.__getNetworkName__ is None: networkName = _data.__class__.__name__ else: networkName = _data.__getNetworkName__() if hasattr(_data, '__getNetworkName__') else _data.__class__.__name__ _data._network = networkName network = pymel.createNode('network', name=networkName) # Ensure the network have the current python id stored if not network.hasAttr('_uid'): pymel.addAttr(network, longName='_uid', niceName='_uid', at='long') # todo: validate attributeType # network._uid.set(id(_data)) # Convert _pData to basic data dictionary (recursive for now) dicData = core._export_basicData(_data, _bRecursive=False, **kwargs) assert(isinstance(dicData, dict)) fnNet = network.__apimfn__() for key, val in dicData.items(): if val is not None: if key == '_class' or key[0] != '_': # Attributes starting with '_' are protected or private _addAttr(fnNet, key, val) return network
def _createAttribute(_name, _val): if isinstance(_val, basestring): fn = OpenMaya.MFnTypedAttribute() fn.create(_name, _name, OpenMaya.MFnData.kString) return fn kType = type(_val) if issubclass(kType, bool): fn = OpenMaya.MFnNumericAttribute() fn.create(_name, _name, OpenMaya.MFnNumericData.kBoolean) return fn if issubclass(kType, int): fn = OpenMaya.MFnNumericAttribute() fn.create(_name, _name, OpenMaya.MFnNumericData.kInt) return fn if issubclass(kType, float): fn = OpenMaya.MFnNumericAttribute() fn.create(_name, _name, OpenMaya.MFnNumericData.kFloat) return fn if isinstance(_val, dict): fn = OpenMaya.MFnMessageAttribute() fn.create(_name, _name) return fn if isinstance(_val, list) or isinstance(type, tuple): if len(_val) < 1: pymel.warning( "Can't create attribute {0}, empty array are unsuported". format(_name)) return None # TODO: Throw error when the array have multiple types fn = _createAttribute(_name, _val[0]) fn.setArray(True) return fn if issubclass(kType, pymel.datatypes.Matrix): # HACK fn = OpenMaya.MFnMatrixAttribute() fn.create(_name, _name) return fn if issubclass(kType, pymel.Attribute): if not libPymel.is_valid_PyNode(_val): log.warning( "Can't serialize {0} attribute because of non-existent pymel Attribute!" .format(_name)) return None elif _val.type() == 'doubleAngle': fn = OpenMaya.MFnUnitAttribute() fn.create(_name, _name, OpenMaya.MFnUnitAttribute.kAngle) return fn elif _val.type() == 'time': fn = OpenMaya.MFnUnitAttribute() fn.create(_name, _name, OpenMaya.MFnUnitAttribute.kTime) return fn #elif _val.type() == '???': # fn = OpenMaya.MFnUnitAttribute() # fn.create(_name, _name, OpenMaya.MFnUnitAttribute.kDistance) # return fn # If the attribute doesn't represent anything special, we'll check it's value to know what attribute type to create. else: return _createAttribute(_name, _val.get()) if hasattr(_val, '__melobject__'): # TODO: Really usefull? fn = OpenMaya.MFnMessageAttribute() fn.create(_name, _name) return fn if hasattr(_val, '__dict__'): fn = OpenMaya.MFnMessageAttribute() fn.create(_name, _name) return fn pymel.error("Can't create MFnAttribute for {0} {1} {2}".format( _name, _val, kType))
def _createAttribute(_name, _val): if isinstance(_val, basestring): fn = OpenMaya.MFnTypedAttribute() fn.create(_name, _name, OpenMaya. MFnData.kString) return fn kType = type(_val) if issubclass(kType, bool): fn = OpenMaya.MFnNumericAttribute() fn.create(_name, _name, OpenMaya.MFnNumericData.kBoolean) return fn if issubclass(kType, int): fn = OpenMaya.MFnNumericAttribute() fn.create(_name, _name, OpenMaya.MFnNumericData.kInt) return fn if issubclass(kType, float): fn = OpenMaya.MFnNumericAttribute() fn.create(_name, _name, OpenMaya.MFnNumericData.kFloat) return fn if isinstance(_val, dict): fn = OpenMaya.MFnMessageAttribute() fn.create(_name, _name) return fn if isinstance(_val, list) or isinstance(type, tuple): if len(_val) < 1: pymel.warning("Can't create attribute {0}, empty array are unsuported".format(_name)) return None # TODO: Throw error when the array have multiple types fn = _createAttribute(_name, _val[0]) fn.setArray(True) return fn if issubclass(kType, pymel.datatypes.Matrix): # HACK fn = OpenMaya.MFnMatrixAttribute() fn.create(_name, _name) return fn if issubclass(kType, pymel.Attribute): if not libPymel.is_valid_PyNode(_val): log.warning("Can't serialize {0} attribute because of non-existent pymel Attribute!".format(_name)) return None elif _val.type() == 'doubleAngle': fn = OpenMaya.MFnUnitAttribute() fn.create(_name, _name, OpenMaya.MFnUnitAttribute.kAngle) return fn elif _val.type() == 'time': fn = OpenMaya.MFnUnitAttribute() fn.create(_name, _name, OpenMaya.MFnUnitAttribute.kTime) return fn #elif _val.type() == '???': # fn = OpenMaya.MFnUnitAttribute() # fn.create(_name, _name, OpenMaya.MFnUnitAttribute.kDistance) # return fn # If the attribute doesn't represent anything special, we'll check it's value to know what attribute type to create. else: return _createAttribute(_name, _val.get()) if hasattr(_val, '__melobject__'): # TODO: Really usefull? fn = OpenMaya.MFnMessageAttribute() fn.create(_name, _name) return fn if hasattr(_val, '__dict__'): fn = OpenMaya.MFnMessageAttribute() fn.create(_name, _name) return fn pymel.error("Can't create MFnAttribute for {0} {1} {2}".format(_name, _val, kType))