def export_network(data, cache=None, **kwargs): if cache is None: from cache import Cache cache = Cache() #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. # todo: after refactoring, the network cache will be merged with the import cache data_id = id(data) result = cache.get_network_by_id(data_id) if result is not None: return result # Create network # Optimisation: Use existing network if already present in scene #if hasattr(data, '_network') and is_valid_PyNode(data._network): # network = data._network #else: # Automaticly name network whenever possible try: network_name = data.__getNetworkName__() except (AttributeError, TypeError): network_name = data.__class__.__name__ network = pymel.createNode('network', name=network_name) # Monkey patch the network in a _network attribute if supported. if isinstance(data, object) and not isinstance(data, dict): data._network = network # 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)) # Cache as soon as possible since we'll use recursivity soon. cache.set_network_by_id(data_id, network) # Convert _pData to basic data dictionary (recursive for now) data_dict = core.export_dict(data, recursive=False, cache=cache, **kwargs) assert (isinstance(data_dict, dict)) fnNet = network.__apimfn__() for key, val in data_dict.items(): if _can_export_attr_by_name(key): _add_attr(fnNet, key, val, cache=cache) return network
def export_json_file(data, path, mkdir=True, indent=4, **kwargs): if mkdir: _make_dir(path) data_dict = core.export_dict(data) with open(path, 'w') as fp: json.dump(data_dict, fp, indent=indent, **kwargs) return True
def export_yaml_file(data, path, mkdir=True, **kwargs): if mkdir: os.makedirs(path) dicData = core.export_dict(data) with open(path, 'w') as fp: yaml.dump(dicData, fp) return True
def export_yaml_file(data, path, mkdir=True, **kwargs): if mkdir: dirname = os.path.dirname(path) if not os.path.exists(dirname): os.makedirs(dirname) dicData = core.export_dict(data) with open(path, 'w') as fp: yaml.dump(dicData, fp) return True
def export_json(data, indent=4, **kwargs): data = core.export_dict(data) return json.dumps(data, indent=indent, **kwargs)
def export_yaml(data, **kwargs): dicData = core.export_dict(data) return yaml.dump(dicData, **kwargs)