def copy(source, target, description=''): """ Copies given file or files and creates a new version of the file with the given description :param source: str, source file or folder we want to copy :param target: str, destination file or folder we want to copy into :param description: str, description of the new version """ is_source_a_file = path_utils.is_file(source) if is_source_a_file: copied_path = fileio.copy_file(source, target) else: if not path_utils.exists(source): LOGGER.info( 'Nothing to copy: {}\t\tData was probably created but not saved yet.' .format(path_utils.get_dirname(is_source_a_file))) return if path_utils.exists(target): folder.delete_folder(target) copied_path = folder.copy_folder(source, target) if not copied_path: LOGGER.warning('Error copying {}\t to\t{}'.format(source, target)) return if copied_path > -1: LOGGER.info('Finished copying {} from {} to {}'.format( description, source, target)) version_file = version.VersionFile(copied_path) version_file.save('Copied from {}'.format(source))
def delete(self): """ Removes data folder from disk """ folder_name = path_utils.get_basename(self.folder_path) data_directory = path_utils.get_dirname(self.folder_path) folder.delete_folder(folder_name, data_directory)
def delete_version(self, version_number): """ Deletes specific version file :param version_number: int """ version_path = self.get_version_path(version_number) if path.is_file(version_path): fileio.delete_file(version_path) else: folder.delete_folder(path)
def remove(self): from tpDcc.libs.qt.core import qtutils if not path.is_dir(self.full_path): LOGGER.warning('Impossible to remove Project Path: {}'.format( self.full_path)) return False project_name = self.project_data.name project_path = self.project_data.path result = qtutils.get_permission( message='Are you sure you want to delete project: {}'.format( self.name), title='Deleting Project', cancel=False) if not result: return valid_delete = folder.delete_folder(folder_name=project_name, directory=project_path) if valid_delete is None: return False return True
def delete_item(self): """ Deletes the selected item """ item = self._current_item item_path = self.get_item_directory(item) name = path.get_basename(item_path) item_directory = path.get_dirname(item_path) if path.is_dir(item_path): folder.delete_folder(name, item_directory) elif path.is_file(item_path): fileio.delete_file(name, item_directory) if item_path.endswith('.py'): fileio.delete_file(name + '.c', item_directory) parent = item.parent() if parent: parent.removeChild(item) else: index = self.indexOfTopLevelItem(item) self.takeTopLevelItem(index)
def save(self, *args, **kwargs): """ Saves NG Skin weights file """ dependencies = dict() filepath = self.format_identifier() if not filepath: logger.warning( 'Impossible to save NGSkin Cluster Weights file because save file path not defined!' ) return objects = kwargs.get('objects', None) if not objects: objects = dcc.client().selected_nodes(full_path=True) if not objects: logger.warning( 'Nothing selected to export skin weights of. Please, select a mesh,' ' curve, NURBS surface or lattice with skin weights to export') return False logger.debug('Saving {} | {}'.format(filepath, kwargs)) try: if not dcc.is_plugin_loaded('ngSkinTools2'): dcc.load_plugin('ngSkinTools2') import ngSkinTools2 from ngSkinTools2 import api as ngst_api except ImportError: logger.warning( 'NgSkinTools 2.0 is not installed. Impossible to export ngSkin data' ) return False valid_nodes = list() # Check that all objects that we are going to export have at least one skin cluster node associated # Make sure also that all objects skin output folder have been created obj_dirs = OrderedDict() skin_nodes = OrderedDict() geo_paths = OrderedDict() skin_weights = OrderedDict() for obj in objects: if dcc.client().node_is_a_shape(obj): obj = dcc.client().node_parent(obj, full_path=True) obj_filename = obj if obj.find('|') > -1: obj_filename = obj_filename.replace('|', '.') if obj_filename.startswith('.'): obj_filename = obj_filename[1:] if obj_filename.find(':') > -1: obj_filename = obj_filename.replace(':', '-') skin = dcc.client().find_deformer_by_type(obj, 'skinCluster') if not skin: logger.warning( 'Skip skin weights export for object because no skinCluster found!' .format(obj)) continue valid_nodes.append((obj, obj_filename, skin)) if not valid_nodes: logger.warning( 'Skin exported failed! No objects found with skinClusters applied!' ) return False # Create skin folder only is valid nodes are available file_name = '.{}'.format(os.path.basename(filepath)) file_folder = path_utils.join_path(os.path.dirname(filepath), file_name) if not os.path.isdir(file_folder): skin_folder = folder.create_folder(file_folder, make_unique=True) dependencies[skin_folder] = 'skin_folder' for node_data in valid_nodes: obj, obj_filename, skin = node_data geo_path = path_utils.join_path(file_folder, obj_filename) if path_utils.is_dir(geo_path): folder.delete_folder(obj_filename, file_folder) geo_path = folder.create_folder(obj_filename, file_folder) if not geo_path: logger.error( 'Unable to create skin weights directory: "{}" in "{}"'. format(obj_filename, file_folder)) return False dependencies[geo_path] = 'geo_path' weights = dcc.client().get_skin_weights(skin) obj_dirs[obj] = obj_filename skin_nodes[obj] = skin geo_paths[obj] = geo_path skin_weights[obj] = weights for (obj, skin_node), (_, geo_path), (_, skin_weights) in zip( skin_nodes.items(), geo_paths.items(), skin_weights.items()): logger.info('Exporting weights: {} > {} --> "{}"'.format( obj, skin_node, geo_path)) info_lines = list() info_file = fileio.create_file('influence.info', geo_path) for influence in skin_weights: if influence is None or influence == 'None': continue weight_list = skin_weights[influence] if not weight_list: continue influence_name = skin_utils.get_skin_influence_at_index( influence, skin_node) if not influence_name or not dcc.node_exists(influence_name): continue influence_position = dcc.node_world_space_translation( influence_name) influence_line = "{'%s' : {'position' : %s}}" % ( influence_name, str(influence_position)) info_lines.append(influence_line) writer = fileio.FileWriter(info_file) writer.write(info_lines) settings_file = fileio.create_file('settings.info', geo_path) setting_lines = list() if shape_utils.has_shape_of_type(obj, 'mesh'): self._export_mesh_obj(obj, geo_path) setting_lines.append("['skinNodeName', '{}']".format( dcc.node_short_name(skin_node))) if dcc.attribute_exists(skin_node, 'blendWeights'): blend_weights = skin_utils.get_skin_blend_weights(skin_node) setting_lines.append( "['blendWeights', {}]".format(blend_weights)) if dcc.attribute_exists(skin_node, 'skinningMethod'): skin_method = dcc.get_attribute_value(skin_node, 'skinningMethod') setting_lines.append( "['skinningMethod', {}]".format(skin_method)) write_settings = fileio.FileWriter(settings_file) write_settings.write(setting_lines) ng_skin_file_name = os.path.join(geo_path, 'ngdata.json') ngst_api.export_json(obj, file=ng_skin_file_name) logger.info( 'Skin weights exported successfully: {} > {} --> "{}"'.format( obj, skin_node, geo_path)) data_to_save = OrderedDict() for obj, obj_filename in obj_dirs.items(): data_to_save[obj] = {'enabled': True, 'folder': obj_filename} with open(filepath, 'w') as fh: json.dump(data_to_save, fh) logger.info('Skin weights export operation completed successfully!') return True
def delete(self): folder.delete_folder(self.format_identifier()) self._db.remove(self.identifier())
def save(self, *args, **kwargs): dependencies = dict() filepath = self.format_identifier() if not filepath.endswith(SkinWeightsData.EXTENSION): filepath = '{}{}'.format(filepath, SkinWeightsData.EXTENSION) if not filepath: logger.warning( 'Impossible to save Maya Skin Weights file because save file path not defined!' ) return objects = kwargs.get('objects', None) if not objects: objects = dcc.client().selected_nodes(full_path=True) if not objects: logger.warning( 'Nothing selected to export skin weights of. Please, select a mesh,' ' curve, NURBS surface or lattice with skin weights to export') return False logger.debug('Saving {} | {}'.format(filepath, kwargs)) valid_nodes = list() # Check that all objects that we are going to export have at least one skin cluster node associated # Make sure also that all objects skin output folder have been created obj_dirs = OrderedDict() skin_nodes = OrderedDict() geo_paths = OrderedDict() skin_weights = OrderedDict() for obj in objects: if dcc.client().node_is_a_shape(obj): obj = dcc.client().node_parent(obj, full_path=True) obj_filename = obj if obj.find('|') > -1: obj_filename = obj_filename.replace('|', '.') if obj_filename.startswith('.'): obj_filename = obj_filename[1:] if obj_filename.find(':') > -1: obj_filename = obj_filename.replace(':', '-') skin = dcc.client().find_deformer_by_type(obj, 'skinCluster') if not skin: logger.warning( 'Skip skin weights export for object because no skinCluster found!' .format(obj)) continue valid_nodes.append((obj, obj_filename, skin)) if not valid_nodes: logger.warning( 'Skin exported failed! No objects found with skinClusters applied!' ) return False # Create skin folder only is valid nodes are available file_name = '.{}'.format(os.path.basename(filepath)) file_folder = path_utils.join_path(os.path.dirname(filepath), file_name) if not os.path.isdir(file_folder): skin_folder = folder.create_folder(file_folder, make_unique=True) dependencies[skin_folder] = 'skin_folder' for node_data in valid_nodes: obj, obj_filename, skin = node_data geo_path = path_utils.join_path(file_folder, obj_filename) if path_utils.is_dir(geo_path): folder.delete_folder(obj_filename, file_folder) geo_path = folder.create_folder(obj_filename, file_folder) if not geo_path: logger.error( 'Unable to create skin weights directory: "{}" in "{}"'. format(obj_filename, file_folder)) return False dependencies[geo_path] = 'geo_path' weights = dcc.client().get_skin_weights(skin) obj_dirs[obj] = obj_filename skin_nodes[obj] = skin geo_paths[obj] = geo_path skin_weights[obj] = weights for (obj, skin_node), (_, geo_path), (_, skin_weights) in zip( skin_nodes.items(), geo_paths.items(), skin_weights.items()): logger.info('Exporting weights: {} > {} --> "{}"'.format( obj, skin_node, geo_path)) info_lines = list() info_file = fileio.create_file('influence.info', geo_path) dependencies[info_file] = 'info_file' for influence in skin_weights: if influence is None or influence == 'None': continue weight_list = skin_weights[influence] if not weight_list: continue thread = LoadWeightFileThread() influence_line, weight_path = thread.run( influence, skin_node, skin_weights[influence], geo_path) if influence_line: info_lines.append(influence_line) if weight_path and os.path.isfile(weight_path): dependencies[weight_path] = 'weight' writer = fileio.FileWriter(info_file) writer.write(info_lines) settings_file = fileio.create_file('settings.info', geo_path) dependencies[settings_file] = 'settings' setting_lines = list() if dcc.client().node_has_shape_of_type(obj, 'mesh'): mesh_path = self._export_mesh_obj(obj, geo_path) if mesh_path and os.path.isfile(mesh_path): dependencies[mesh_path] = 'geo_file' if dcc.client().attribute_exists(skin_node, 'blendWeights'): blend_weights = dcc.client().get_skin_blend_weights(skin_node) setting_lines.append( "['blendWeights', {}]".format(blend_weights)) if dcc.client().attribute_exists(skin_node, 'skinningMethod'): skin_method = dcc.client().get_attribute_value( skin_node, 'skinningMethod') setting_lines.append( "['skinningMethod', {}]".format(skin_method)) write_settings = fileio.FileWriter(settings_file) write_settings.write(setting_lines) logger.info( 'Skin weights exported successfully: {} > {} --> "{}"'.format( obj, skin_node, geo_path)) data_to_save = OrderedDict() for obj, obj_filename in obj_dirs.items(): data_to_save[obj] = {'enabled': True, 'folder': obj_filename} with open(filepath, 'w') as fh: json.dump(data_to_save, fh) logger.info('Skin weights export operation completed successfully!') return dependencies