def take_snapshot(path=None): '''Create snapshot file for selected nodes in given path. Attributes: path -- Path to create snapshot file. str node -- Node to create snapshot for. nt.Transform typ -- Type of node. ncloth | nucleus. str ''' general.check_type(path, 'path', [str]) typs = ['transform', 'nucleus'] results = [] nodes = pm.ls(sl=1) for node in nodes: typ = node.type() msg = ('Node: %s is of invalid type: %s' % (node, typ)) if typ not in typs: pm.warning(msg) if typ == 'transform': typ = 'ncloth' elif typ == 'nucleus': typ = 'nucleus' else: pm.warning(msg) d = make_dict(node, typ) name = gen_filename(node) full_name = path + os.sep + name make_file(str(full_name), d) results.append('Created: %s' % full_name) return results
def make_file(name=None, data=None): '''Make a snapshot file from a given dictionary Format: AttributeName:value:type Attributes: name -- Full path + name for snapshot file. str data -- Dictionary: {'attr': value} ''' general.check_type(name, 'name', [str]) general.check_type(data, 'data', [dict]) lines = [] for key in data.keys(): lines.append( str(key) + ':' + str(data[key]) + ':' + str(type(data[key])) + '\n') if not os.path.exists(os.path.dirname(name)): raise errors.InputError(name, 'name', 'Path does not exist.') try: f = open(name, 'w') f.writelines(lines) except Exception, e: raise Exception(e)
def make_file(name=None, data=None): '''Make a snapshot file from a given dictionary Format: AttributeName:value:type Attributes: name -- Full path + name for snapshot file. str data -- Dictionary: {'attr': value} ''' general.check_type(name, 'name', [str]) general.check_type(data, 'data', [dict]) lines = [] for key in data.keys(): lines.append(str(key) + ':' + str(data[key]) + ':' + str(type(data[key])) + '\n') if not os.path.exists(os.path.dirname(name)): raise errors.InputError(name, 'name', 'Path does not exist.') try: f = open(name, 'w') f.writelines(lines) except Exception, e: raise Exception(e)
def gen_filename(node=None): '''Given Transform node, generate a file name for a snapshot Format: nodeName_yyyy_mm_dd_hh_mm Attributes: node -- Transform node. nt.Transform ''' general.check_type(node, 'node', [pm.nt.Transform, pm.nt.Nucleus]) now = datetime.now() return node.name() + '_' + \ str(now.year) + '-' + str(now.month) + '-' + \ str(now.day) + '_' + \ str(now.hour) + '-' + str(now.minute) + '.txt'
def get_dict(snapshot=None): '''Given a full path to a snapshot file, set the selected node to the values in the snapshot. Attributes: snapshot -- Full path to a snapshot file. str ''' general.check_type(snapshot, 'snapshot', [str]) if not os.path.exists(snapshot): raise errors.InputError('snapshot', snapshot, 'File does not exist.') lines = [] try: f = open(snapshot, 'r') lines = f.readlines() except Exception, e: raise Exception(e)
def apply_dict(node=None, d=None): '''Given dictionay d, apply values to attributes of node d format: {'aatribute': 'value'} Attributes: node -- Node to apply values to. nt.Transform | nt.Nucleus d -- Dictionary of attributes, values ''' general.check_type(node, 'node', [pm.nt.Transform, pm.nt.Nucleus]) general.check_type(d, 's', [dict]) for attr in d.keys(): try: a = getattr(node, attr) a.set(d[attr]) except Exception, e: raise Exception(e)
def make_dict(node=None, typ=None): # dict: {'attr':value} '''Make a dictionary for the given node attributes and values. Expects to find: nucleus_attrs.txt or ncloth_attrs.txt file in directory this py file is in. Attributes: node -- Node. nt.Transform typ -- Type of node. ncloth | nucleus. str ''' general.check_type(node, 'node', [pm.nt.Transform, pm.nt.Nucleus]) general.check_type(typ, 'typ', [str]) if typ not in ['ncloth', 'nucleus']: raise errors.InputError(typ, 'typ', 'Must be either: ncloth or nucleus') if typ == 'ncloth': fileName = os.path.dirname(__file__) + os.sep + 'ncloth_attrs.txt' elif typ == 'nucleus': fileName = os.path.dirname(__file__) + os.sep + 'nucleus_attrs.txt' if not os.path.exists(fileName): msg = 'File not found: ', fileName raise IOError(msg) f = open(fileName, 'r') attrs = f.readlines() f.close() d = {} if typ == 'ncloth': shape = node.getShape() elif typ == 'nucleus': shape = node for attr in attrs: attr = attr.strip() d[attr] = getattr(shape, attr).get() return d
def get_diff(file1=None, file2=None): '''Given two snapshot files, return the diff as a dictionary File format: attr:value:type Dict: {'attr': [value1, value2]} Attributes: file1 -- Full path to snapshot file1. str file2 -- Full path to snapshot file2. str ''' general.check_type(file1, 'file1', [str]) general.check_type(file2, 'file2', [str]) if not os.path.exists(file1): raise errors.InputError('file1', file1, 'File does not exist.') if not os.path.exists(file2): raise errors.InputError('file2', file2, 'File does not exist.') try: f1 = open(file1, 'r') f1_lines = f1.readlines() except Exception, e: raise Exception(e)
def get_diff(file1=None, file2=None): """Given two snapshot files, return the diff as a dictionary File format: attr:value:type Dict: {'attr': [value1, value2]} Attributes: file1 -- Full path to snapshot file1. str file2 -- Full path to snapshot file2. str """ general.check_type(file1, "file1", [str]) general.check_type(file2, "file2", [str]) if not os.path.exists(file1): raise errors.InputError("file1", file1, "File does not exist.") if not os.path.exists(file2): raise errors.InputError("file2", file2, "File does not exist.") try: f1 = open(file1, "r") f1_lines = f1.readlines() except Exception, e: raise Exception(e)