def delete_curve(curve_name, curves_path=None): """ Renames the curve from the library. If not path given, all paths will be checked to find the curve :param curve_name: str, the name of the curve without extension :param curves_path: str, directory path where we want to curve the curve from """ if not curves_path or not os.path.isdir(curves_path): curve_path = find_curve_path_by_name(curve_name) else: curve_path = os.path.join(curves_path, '{}{}'.format(curve_name, consts.CURVE_EXT)) if not curve_path or not os.path.isfile(curve_path): logger.warning( 'Curve file could not be deleted because it does not exists! "{}"'. format(curve_path)) return False curve_path = path_utils.clean_path(curve_path) fileio.delete_file(curve_path) if os.path.isfile(curve_path): logger.warning( 'Was not possible to remove curve "{}" file: "{}'.format( curve_name, curve_path)) return False logger.info('Curve "{}" has been deleted successfully: "{}"'.format( curve_name, curve_path)) return True
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 clean_folder(directory): """ Removes everything in the given directory :param directory: str """ from tpDcc.libs.python import path, fileio, folder base_name = path.get_basename(directory=directory) dir_name = path.get_dirname(directory=directory) if path.is_dir(directory): files = folder.get_files(directory) for f in files: fileio.delete_file(f, directory) delete_folder(base_name, dir_name) if not path.is_dir(directory): create_folder(base_name, dir_name)
def delete_pyc_file(python_script): """ Deletes the .pyc file that corresponds to the given .py file :param python_script: str """ from tpDcc.libs.python import path, fileio script_name = path.get_basename(python_script) if not python_script.endswith('.py'): print('WARNING: Could not delete .pyc file for {}!'.format(script_name)) return compile_script = python_script + 'c' if path.is_file(compile_script): compile_name = path.get_basename(compile_script) compile_dir_name = path.get_dirname(compile_script) if not compile_name.endswith('.pyc'): return fileio.delete_file(name=compile_name, directory=compile_dir_name)
def copy(self, target_path, replace=True): # TODO :Take into account dependencies if not target_path: return if os.path.isfile(target_path): if not replace: return fileio.delete_file(target_path) _, _, file_extension = path_utils.split_path(self.format_identifier()) target_directory, target_name, target_extension = path_utils.split_path(target_path) if target_extension != file_extension: target_path = path_utils.join_path(target_directory, '{}{}'.format(target_name, file_extension)) copy_path = fileio.copy_file(self.format_identifier(), target_path) self._db.sync() return copy_path
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 update_dependencies(self, dependencies=None, recursive=True): if not dependencies or not isinstance(dependencies, dict): dependencies = dict() dependency_file_name = '{}.json'.format(self._db.get_uuid(self.format_identifier())) dependency_path = path_utils.join_path(self._db.get_dependencies_path(), dependency_file_name) if not os.path.isfile(dependency_path): fileio.create_file(dependency_path) all_dependencies = dict() current_dependencies = jsonio.read_file(dependency_path) or dict() for dependency_uuid, dependency_name in current_dependencies.items(): dependency = self._db.find_identifier_from_uuid(dependency_uuid) if not dependency: continue all_dependencies.update({dependency: dependency_name}) if dependencies: all_dependencies.update(dependencies) for dependency, dependency_name in all_dependencies.items(): self._db.add_dependency(self.format_identifier(), dependency, dependency_name) dependencies = self._db.get_dependencies(self.format_identifier(), as_uuid=True) if not dependencies: fileio.delete_file(dependency_path) return jsonio.write_to_file(dependencies, dependency_path) # We update all related dependencies if recursive: for dependency, dependency_name in all_dependencies.items(): dependency_item = self._db.get(dependency) if not dependency_item: continue dependency_item.update_dependencies( dependencies={self.format_identifier(): self.type()}, recursive=False)
def delete(self): fileio.delete_file(self.format_identifier()) self._db.remove(self.identifier())
def _export_file(self, file_path, *args, **kwargs): # Retrieve master layout file path sequence_name = self._shot.get_sequence() if not sequence_name: LOGGER.warning( 'Impossible to export shot file "{}" because is not linked to any sequence!' .format(self._shot.get_name())) return None sequence = artellapipe.SequencesMgr().find_sequence(sequence_name) if not sequence: LOGGER.warning( 'Impossible to export shot file "{}" because sequence "{}" was not found in current project' .format(self._shot.get_name(), sequence_name)) return None sequence_file_type = sequence.get_file_type('master') if not sequence_file_type: LOGGER.warning( 'Impossible to export shot file "{}" because sequence file type "master" is not defined ' 'in current project'.format(self._shot.get_name())) return None master_file_path = sequence_file_type.get_file() if not master_file_path or not os.path.exists(master_file_path): LOGGER.warning( 'Impossible to export shot file "{}" because master layout file "{}" does not exists!' .format(self._shot.get_name(), master_file_path)) return None sequence_file_type.open_file() master_locked = False if os.path.isfile(master_file_path): valid_lock = artellapipe.FilesMgr().lock_file(master_file_path) master_locked = True if not valid_lock: LOGGER.warning('Was not possible to lock file: {}'.format( master_file_path)) return False sequence_file_type.open_file() file_path_name = os.path.basename(file_path) file_path_dir = os.path.dirname(file_path) if not os.path.isdir(file_path_dir): LOGGER.info('Creating export file path directory: {}'.format( file_path_dir)) try: os.makedirs(file_path_dir) except Exception as exc: LOGGER.error( 'Error while creating export path directory: "{}" | {} | {}' .format(file_path_dir, exc, traceback.format_exc())) return None try: if os.path.isfile(file_path): fileio.delete_file(file_path_name, file_path_dir) fileio.copy_file(master_file_path, file_path) LOGGER.info('Created new Shot File: {}'.format(file_path)) except Exception as exc: LOGGER.error( 'Error while copying shot file "{}" from master layout file "{}" | {} | {}' .format(file_path, master_file_path, exc, traceback.format_exc())) return None if os.path.isfile(file_path): valid_lock = artellapipe.FilesMgr().lock_file(file_path) if not valid_lock: LOGGER.warning( 'Was not possible to lock file: {}'.format(file_path)) tp.Dcc.open_file(file_path) shot_anim_file_type = self._shot.get_file_type('shot_layout_anim') if not shot_anim_file_type: LOGGER.warning( 'Impossible to export shot "{}" because shot file type "shot_layout_anim" is not defined ' 'in current project'.format(self._shot.get_name())) return None start_frame = kwargs.get('start_frame', 101) valid_anim_export = shot_anim_file_type.export_file( start_frame=start_frame) if not valid_anim_export: LOGGER.warning('Layout Animation Export was not exported in file!') return None if master_locked: artellapipe.FilesMgr().unlock_file(master_file_path, warn_user=False) try: shot_anim_file_path = shot_anim_file_type.get_file() shot_anim_file_path_name = os.path.basename(shot_anim_file_path) shot_anim_file_path_directory = os.path.dirname( shot_anim_file_path) fileio.delete_file(shot_anim_file_path_name, shot_anim_file_path_directory) except Exception as exc: LOGGER.warning( 'Was not possible to remove Shot Layout Animation File: "{}" | {} | {}. Remove it manually' .format(shot_anim_file_path, exc, traceback.format_exc())) try: tp.Dcc.convert_fraction_keys_to_whole_keys( consider_selected_range=False) except Exception as exc: LOGGER.warning( 'Could not resolve any keyframes on fractions of a frame: {} | {}' .format(exc, traceback.format_exc())) # Clean shot nodes that are not valid anymore all_shots = tp.Dcc.all_scene_shots() shots_to_delete = [ shot for shot in all_shots if shot != self.get_name() ] tp.Dcc.delete_object(shots_to_delete) # Update timeline start_offset = self._shot.get_start_frame() - start_frame start_frame = self._shot.get_start_frame() - start_offset end_frame = self._shot.get_end_frame() - start_offset tp.Dcc.set_active_frame_range(start_frame, end_frame) # Update shot attributes self._shot.set_start_frame(start_frame) self._shot.set_end_frame(end_frame) # Remove cameras that does not belong to the shot camera_name = self._shot.get_camera() root_node = None all_cameras = tp.Dcc.get_all_cameras(full_path=True) or list() for camera in all_cameras: if not camera: continue base_name = tp.Dcc.node_short_name(camera) if base_name == camera_name: root_node = camera.split('|')[1] break if root_node: for camera in all_cameras: if not camera or not tp.Dcc.object_exists(camera): continue base_name = tp.Dcc.node_short_name(camera) if base_name == camera_name: continue reps = 0 node_to_delete = camera camera_parent = tp.Dcc.node_parent(camera) while camera_parent != '|{}'.format(root_node) and reps < 10: node_to_delete = camera_parent camera_parent = tp.Dcc.node_parent(node_to_delete) reps += 1 tp.Dcc.delete_object(node_to_delete) else: for camera in all_cameras: if not camera: continue base_name = tp.Dcc.node_short_name(camera) if base_name == camera_name: continue tp.Dcc.delete_object(camera) # Force look through to shot camera tp.Dcc.look_through_camera(camera_name) # Save scene tp.Dcc.save_current_scene(force=True) if tp.is_maya(): from tpDcc.dccs.maya.core import helpers helpers.clean_student_line() return True