def task_preset():
    # get context
    path = os.getcwd()

    context = kttk.path_cache_manager.context_from_path(path)

    # make sure path was registered and we have a context
    if not context:
        print_result("No Context registered for path {}".format(path))
        return

    # make sure context has project and entity
    assert context.project and context.entity

    # get task presets for entity
    presets = kttk.get_task_presets(context.entity["type"])

    # now create presets
    kt = ktrack_api.get_ktrack()

    for preset in presets:
        logger.info(
            "Creating task {} of step {}".format(preset["name"], preset["step"])
        )
        task = kt.create(
            "task",
            {
                "project": context.project,
                "entity": context.entity,
                "name": preset["name"],
                "step": preset["step"],
            },
        )
        kttk.init_entity(task["type"], task["id"])
Esempio n. 2
0
 def open_file(self, file_to_open):
     # type: (dict) -> None
     """
     Opens given workfile in DCC. Opened file will become current workfile. Will call change file
     :param file_to_open: workfile to open
     """
     logger.info("Opening file {}".format(file_to_open))
     self.change_file(file_to_open)
Esempio n. 3
0
 def update_file_for_context(self):
     """
     Updates all outputs paths for this file, for example Alembic output pats / render output paths.
     Will also call serialize_context_to_file
     :return:
     """
     logger.info("Updating file for new context..")
     logger.info("Serialize current context to file..")
     self.serialize_context_to_file()
Esempio n. 4
0
 def save_as(self, file_to_save_to):
     # type: (dict) -> None
     """
     Saves current file to given workfile. Context and current_file will ge changed to given workfile
     :param file_to_save_to: workfile, path field will be used for file location
     :return:
     """
     logger.info("Save as to path {}".format(file_to_save_to["path"]))
     self.change_file(file_to_save_to)
     self.update_file_for_context()
def bootstrap_project():
    # type: () -> Tuple[Dict, Dict]
    """
    Bootstraps a project. Project can be used for testing or demonstration purposes
    :return: the bootstrapped project and a dict with information about the created entites,
    example:
    {
        'project': {project_dict [...]},
        'Hank': {asset_dict [...]},
        'Hank_modelling': {task_dict [...]}
        [...]
    }
    ['shot040_lsr', 'shot020_anim', 'shot040_anim', 'shot030_comp', 'Fluke_and_Rudder_modelling', 'Dory_lookdev', 'Dory_modelling', 'shot010_comp', 'shot030', 'Hank', 'project', 'shot010_anim', 'shot030_anim', 'Dory', 'Hank_lookdev', 'Hank_modelling', 'shot010_lsr', 'shot020', 'shot040', 'Fluke_and_Rudder_lookdev', 'shot030_lsr', 'Fluke_and_Rudder', 'shot020_comp', 'shot010', 'shot040_comp', 'shot020_lsr']
    """
    project_data = {}
    kt = ktrack_api.get_ktrack()

    # sanitize project name
    project_name = name_sanitizer.sanitize_name(data["project_name"])

    # create project
    project = kt.create("project", {"name": project_name})
    project_data["project"] = project

    # init project
    kttk.init_entity(project["type"], project["id"])

    entity_types = ["asset", "shot"]

    for entity_type in entity_types:
        entity_presets = task_presets_manager.get_task_presets(entity_type)

        for entity in data["{}_names".format(entity_type)]:
            # sanitize name
            entity_name = name_sanitizer.sanitize_name(entity)

            # create the entity

            entity_data = {"code": entity_name, "project": project}

            # make sure each asset has an entity type
            if entity_type == "asset":
                entity_data["asset_type"] = "character"

            entity = kt.create(entity_type, entity_data)
            project_data[entity_name] = entity

            # init asset
            kttk.init_entity(entity["type"], entity["id"])

            # apply task preset
            for preset in entity_presets:
                logger.info("Creating task {} of step {}".format(
                    preset["name"], preset["step"]))
                task = kt.create(
                    "task",
                    {
                        "project": project,
                        "entity": entity,
                        "name": preset["name"],
                        "step": preset["step"],
                    },
                )

                kttk.init_entity(task["type"], task["id"])
                project_data["{}_{}".format(entity_name,
                                            preset["name"])] = task

    return project, project_data
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)
Esempio n. 7
0
 def open_file_by_path(self, path):
     # type: (str) -> None
     """
     Opens file path in DCC
     """
     logger.info("Opening file {}".format(path))
Esempio n. 8
0
    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()
Esempio n. 9
0
def start_engine(engine_class):
    # type: (Type[AbstractEngine]) -> None
    global g_current_engine
    logger.info("starting engine {}..".format(engine_class.name))
    g_current_engine = engine_class()
    logger.info("engine {} started!".format(engine_class.name))
Esempio n. 10
0
def create(entity_type, entity_name, project_id=None, asset_type=None, task_step=None):
    """
    Creates a new entity if given type in database and initialises it in disk kttk.init_entity
    :param entity_type: type of the new entity to create
    :param entity_name: name the new entity will get, will be used for code if entity has no name
    :param project_id: optional, will link project entity to project with this id
    :param asset_type: type a newly created asset will get
    :param task_step: step a newly created task will get
    :return: None
    """
    # init:
    # if project:
    #   checken ob es namen gibt, dann project in db erstellen und dann initialisieren
    # else:
    #   checken ob aktueller ordner einen context hat, wenn ja, dann context holen, wenn nicht, abbruch
    #   neuen entity erstellen und mit context richtig verlinken, dann initialisieren
    # todo add some more documentation
    logger.info(
        "creation entity of type {} with name {} for project {}".format(
            entity_type, entity_name, project_id
        )
    )
    logger.info("init cmd")
    logger.info("Connecting to database..")
    kt = ktrack_api.get_ktrack()

    if entity_type == "project":
        logger.info("initialise project")
        logger.info("create project in database..")

        project_data = {}
        project_data["name"] = entity_name

        project = kt.create("project", project_data)

        logger.info(
            "Created project {} with id {}".format(project["name"], project["id"])
        )

        logger.info("initialise project on disk..")
        kttk.init_entity("project", project["id"])

        logger.info(
            "{entity_type} with id {entity_id} initialised. Done.".format(
                entity_type="project", entity_id=project["id"]
            )
        )
    else:
        is_asset = entity_type == "asset"
        is_task = entity_type == "task"

        if is_asset:
            if not asset_type:
                print_result("no asset type")
                # todo hints for asset_type
                return

        if is_task:
            if not task_step:
                # todo hints for task_step
                print_result("no task step")
                return

        logger.info("initialise {}".format(entity_type))

        current_location = os.getcwd()
        # current_location = r"M:\Projekte\2018\my_awesome_project"
        logger.info("Restore context from location {}..".format(current_location))

        context = kttk.context_from_path(current_location)
        if not context:
            print_result("No context provided for path")
            return

        logger.info("Context is {context}".format(context=context))

        logger.info("create {} in database..".format(entity_type))

        entity_data = {}
        entity_data["code"] = entity_name
        entity_data["project"] = context.project

        if is_asset:
            entity_data["asset_type"] = asset_type

        if is_task:
            entity_data["name"] = entity_name
            entity_data["step"] = task_step
            if not context.entity:
                print_result("No entity provided for task")
                return

            entity_data["entity"] = context.entity

            entity_data["assigned"] = {
                "type": "user",
                "id": "5af33abd6e87ff056014967a",
            }  # todo dont hardcode user id, use kttk.restore_user()

        entity = kt.create(entity_type, entity_data)

        logger.info(
            "created {entity_type} {entity_name} with id {entity_id}.".format(
                entity_type=entity["type"],
                entity_name=entity.get("code")
                if entity.get("code")
                else entity["name"],
                entity_id=entity["id"],
            )
        )

        logger.info("initialise entity on disk..")

        kttk.init_entity(entity_type, entity["id"])

        logger.info(
            "{entity_type} with id {entity_id} initialised. Done.".format(
                entity_type=entity["type"], entity_id=entity["id"]
            )
        )