def _create_workfile_from(self, context, workfile, comment=""): """ Creates a new workfile based on given workfile. Will increase version number. Workfile and context have to match for project etc. :param context: :return: the new workfile """ # initial version number is 1 version_number = (workfile["version_number"] + 1 if workfile["version_number"] else 1) # get template for file name workfile_file_name_template = template_manager.get_route_template( "workfile_file_name") tokens = context.get_avaible_tokens() tokens["dcc_extension"] = self._engine.file_extension tokens["dcc_name"] = self._engine.name tokens["version"] = version_number # format template for file name workfile_file_name = template_manager.format_template( workfile_file_name_template, tokens) # get and format template for workfile folder workfile_location_template = template_manager.get_route_template( "dcc_scenes_location_{}_{}".format(context.entity["type"], self._engine.name.lower())) workfile_location = template_manager.format_template( workfile_location_template, tokens) # combine location and name to workfile path path = os.path.join(workfile_location, workfile_file_name) workfile_data = {} workfile_data["project"] = context.project workfile_data["entity"] = context.task workfile_data["version_number"] = version_number workfile_data["comment"] = comment if len(workfile.keys()) > 1: workfile_data["created_from"] = workfile workfile_data["name"] = workfile_file_name workfile_data["path"] = path # create new workfile kt = ktrack_api.get_ktrack() new_workfile = kt.create( "workfile", workfile_data) # todo register path for workfile # return newly created workfile return new_workfile
def _get_template_file_path(self): """ Returns formatted template file based on context :return: """ template_file_template = template_manager.get_route_template( "template_file_dcc") tokens = {"dcc_extension": self._engine.file_extension} return template_manager.format_template(template_file_template, tokens)
def get_avaible_tokens(self): # type: () -> dict avaible_tokens = {} kt = ktrack_api.get_ktrack() if self.project: project = kt.find_one("project", self.project["id"]) avaible_tokens["project_name"] = project["name"] avaible_tokens["project_year"] = project["created_at"].year # make sure to query all fields from ktrack, because we might only have id and type if self.entity: entity = kt.find_one(self.entity["type"], self.entity["id"]) avaible_tokens["code"] = entity["code"] if entity["type"] == "asset": avaible_tokens["asset_type"] = entity["asset_type"] if self.step: avaible_tokens["step"] = self.step if self.task: task = kt.find_one("task", self.task["id"]) avaible_tokens["task_name"] = task["name"] if self.workfile: workfile = kt.find_one("workfile", self.workfile["id"]) avaible_tokens["work_file_name"] = workfile["name"] avaible_tokens["work_file_path"] = workfile["path"] avaible_tokens["work_file_comment"] = workfile["comment"] avaible_tokens["version"] = "v{}".format("{}".format( workfile["version_number"]).zfill(3)) if self.user: user = kt.find_one("user", self.user["id"]) avaible_tokens["user_name"] = user["name"] avaible_tokens["project_root"] = template_manager.get_route_template( "project_root") return avaible_tokens
def remove_bootstrapped_project(project_id): # type: (KtrackIdType) -> None """ Removes the bootstrapped project. Removes entites from database, deletes folders and unregisters folders in database :return: """ # get all entities kt = ktrack_api.get_ktrack() entities = [] # get project project = kt.find_one("project", project_id) entities.append(project) # get shots shots = kt.find("shot", [["project", "is", project]]) entities.extend(shots) # get assets assets = kt.find("asset", [["project", "is", project]]) entities.extend(assets) # get asset tasks asset_tasks = [] for asset in assets: asset_tasks.extend(kt.find("task", [["entity", "is", asset]])) entities.extend(asset_tasks) # get shot tasks shot_tasks = [] for shot in shots: shot_tasks.extend(kt.find("task", [["entity", "is", shot]])) entities.extend(shot_tasks) # workfiles workfiles = kt.find( "workfile", [["project", "is", { "type": "project", "id": project_id }]]) entities.extend(workfiles) # walk project folder and unregister each path project_folder_template = template_manager.get_route_template( "project_folder") project_root_template = template_manager.get_route_template("project_root") project_folder = template_manager.format_template( project_folder_template, { "project_name": project["name"], "project_root": project_root_template, "project_year": project["created_at"].year, }, ) logger.info("Unregister paths...") for root, dirs, files in os.walk(project_folder): path = root if kttk.path_cache_manager.unregister_path(path): logger.info("Unregistered path {}".format(path)) # delete all entities logger.info("Deleting entities...") for entity in entities: kt.delete(entity["type"], entity["id"]) logger.info("Deleted {} with id {}".format(entity["type"], entity["id"])) # delete project folder and subfolders logger.info("Remove project folder {}".format(project_folder)) shutil.rmtree(project_folder)
def update_file_for_context(self): super(MayaEngine, self).update_file_for_context() # check in which context we are is_asset = self.context.entity["type"] == "asset" is_shot = self.context.entity["type"] == "shot" is_unsupported_context = is_asset == False and is_shot == False # set maya project if is_asset: maya_workspace_location_template = template_manager.get_route_template( "asset_maya_workspace_location" ) elif is_shot: maya_workspace_location_template = template_manager.get_route_template( "shot_maya_workspace_location" ) elif is_unsupported_context: raise Exception( "Unsupported context: entity is {}, should be an asset or shot".format( self.context.entity ) ) # todo create specific exception for this # This is handled be workspace.mel: # - alembic # - sourceimages # - renderoutput folder # - playblast folder # format the workspace location template maya_workspace_location = template_manager.format_template( maya_workspace_location_template, self.context.get_avaible_tokens() ) maya_workspace_location = maya_workspace_location.replace("\\", "/") # now change to correct location, workspace.mel is is created together with all other folders logger.info("Set Maya project to {}".format(maya_workspace_location)) pm.mel.eval(' setProject "{}"'.format(maya_workspace_location)) # get and format image name template image_name_template = template_manager.get_route_template( "render_image_file_name" ) image_name = template_manager.format_template( image_name_template, self.context.get_avaible_tokens() ) # set filename for vray vray_settings_node = self.__get_vray_settings() if vray_settings_node: logger.info("Setting Vray fileNamePrefix to {}".format(image_name)) vray_settings_node.fileNamePrefix.set(image_name) vray_settings_node.imageFormatStr.set("exr") # set standart fileNamePrefix logger.info("Setting standart fileNamePrefix to {}".format(image_name)) settings_node = self.__get_default_render_globals() settings_node.imageFilePrefix.set(image_name) # store context in file self.serialize_context_to_file()