Пример #1
0
    def test_isolation(self):
        """
        Test that two tk instances use separate caches
        """
        tk2 = tank.sgtk_from_path(self.tk.pipeline_configuration.get_path())
        tk = self.tk

        self.assertEqual(tk.get_cache_item("foo"), None)
        self.assertEqual(tk2.get_cache_item("foo"), None)

        tk.set_cache_item("foo", 123)

        self.assertEqual(tk.get_cache_item("foo"), 123)
        self.assertEqual(tk2.get_cache_item("foo"), None)

        tk2.set_cache_item("foo", 456)

        self.assertEqual(tk.get_cache_item("foo"), 123)
        self.assertEqual(tk2.get_cache_item("foo"), 456)

        tk.set_cache_item("foo", None)

        self.assertEqual(tk.get_cache_item("foo"), None)
        self.assertEqual(tk2.get_cache_item("foo"), 456)

        tk2.set_cache_item("foo", None)

        self.assertEqual(tk.get_cache_item("foo"), None)
        self.assertEqual(tk2.get_cache_item("foo"), None)
Пример #2
0
    def start_engine(engine_name="tk-shell"):
        """Start the given engine in the given context. If no context is provided, start
        the engine in the project context

        :param engine_name:  Name of the engine to start
        :returns:            The running engine
        """
        import tank

        current_engine = tank.platform.current_engine()
        if current_engine:
            return current_engine

        pc_path = os.environ.get("TANK_CURRENT_PC")
        #---------------------------------------------------------------------------
        try:
            tk = tank.sgtk_from_path(pc_path)
            ctx = tk.context_from_path(tk.project_path)
            current_engine = tank.platform.start_engine(engine_name, tk, ctx)
            return current_engine

        except Exception as e:
            log.error("Failed to start Engine - %s" % str(e))
            traceback.print_exc()
        return None
Пример #3
0
    def test_isolation(self):
        """
        Test that two tk instances use separate caches
        """
        tk2 = tank.sgtk_from_path(self.tk.pipeline_configuration.get_path())
        tk = self.tk

        self.assertEqual(tk.get_cache_item("foo"), None)
        self.assertEqual(tk2.get_cache_item("foo"), None)

        tk.set_cache_item("foo", 123)

        self.assertEqual(tk.get_cache_item("foo"), 123)
        self.assertEqual(tk2.get_cache_item("foo"), None)

        tk2.set_cache_item("foo", 456)

        self.assertEqual(tk.get_cache_item("foo"), 123)
        self.assertEqual(tk2.get_cache_item("foo"), 456)

        tk.set_cache_item("foo", None)

        self.assertEqual(tk.get_cache_item("foo"), None)
        self.assertEqual(tk2.get_cache_item("foo"), 456)

        tk2.set_cache_item("foo", None)

        self.assertEqual(tk.get_cache_item("foo"), None)
        self.assertEqual(tk2.get_cache_item("foo"), None)
Пример #4
0
def versionUp(source_file):
    tk = tank.sgtk_from_path(source_file)
    template = tk.templates.get('maya_shot_work')
    fields = template.get_fields(source_file)
    path = source_file
    while os.path.exists(path):
        fields['version'] += 1
        path = template.apply_fields(fields)
    return pm.system.saveAs(path)
Пример #5
0
    def _add_render_files(self, path, items):
        """
        Adds the wip render files on disk associated with this maya scene
        """
        tk = tank.sgtk_from_path(path)
        template = tk.template_from_path(path)
        ctx = tk.context_from_path(path)

        fields = template.get_fields(path)
        version = fields["version"]
        name = fields["name"]

        # get the wip render files path
        if ctx.entity["type"] == "Asset":
            type = fields["sg_asset_type"]
            asset = fields["Asset"]
            maya_asset_render = tk.templates["maya_asset_render"]
            wip_renders = tk.paths_from_template(
                maya_asset_render, {
                    "sg_asset_type": type,
                    "Asset": asset,
                    "name": name,
                    "version": version
                })
        elif ctx.entity["type"] == "Shot":
            spot = fields["Sequence"]
            shot = fields["Shot"]
            maya_shot_render = tk.templates["maya_shot_render"]
            wip_renders = tk.paths_from_template(maya_shot_render, {
                "Sequence": spot,
                "Shot": shot,
                "name": name,
                "version": version
            })

        if wip_renders:
            wip_render_path = wip_renders[0]
            render_files = [name for name in os.listdir(wip_render_path)]
            baseFilename = ""
            for filename in render_files:
                newBaseFilename = filename.split('.')[0]
                if baseFilename != newBaseFilename:
                    items.append({
                        "name": newBaseFilename,
                        "type": "render_file",
                        "description": filename,
                        "other_params": {
                            "source_folder": wip_render_path,
                            "scene_path": path
                        }
                    })

                baseFilename = newBaseFilename
    def _setup_project(self, is_localized):
        """
        Setups a Toolkit classic pipeline configuration with a localized or not core.
        """

        # Create the project's destination folder.
        locality = "localized" if is_localized else "studio"
        project_folder_name = "config_with_%s_core" % locality
        config_root = os.path.join(self.tank_temp, project_folder_name, "pipeline_configuration")

        os.makedirs(os.path.join(self.tank_temp, project_folder_name))

        # Mock a core that will setup the project.
        core_root = os.path.join(self.tank_temp, "%s_core" % locality)
        core_install_folder = os.path.join(core_root, "install", "core")
        os.makedirs(core_install_folder)

        # Mock a localized core if required.
        if is_localized:
            self.create_file(os.path.join(core_root, "config", "core", "interpreter_Darwin.cfg"))
            self.create_file(os.path.join(core_root, "config", "core", "interpreter_Windows.cfg"))
            self.create_file(os.path.join(core_root, "config", "core", "interpreter_Linux.cfg"))
            self.create_file(os.path.join(core_root, "config", "core", "shotgun.yml"))
            self.create_file(os.path.join(core_root, "config", "core", "roots.yml"))
            self.create_file(os.path.join(core_install_folder, "_core_upgrader.py"))
            self.assertEqual(tank.pipelineconfig_utils.is_localized(core_root), True)

        # We have to patch these methods because the core doesn't actually exist on disk for the tests.
        with patch("sgtk.pipelineconfig_utils.get_path_to_current_core", return_value=core_root):
            with patch("sgtk.pipelineconfig_utils.resolve_all_os_paths_to_core", return_value={
                "linux2": core_root if sys.platform == "linux2" else None,
                "win32": core_root if sys.platform == "win32" else None,
                "darwin": core_root if sys.platform == "darwin" else None
            }):
                command = get_command("setup_project", self.tk)
                command.set_logger(logging.getLogger("test"))
                command.execute(
                    dict(
                        config_uri=os.path.join(self.fixtures_root, "config"),
                        project_id=self._project["id"],
                        project_folder_name=project_folder_name,
                        config_path_mac=config_root if sys.platform == "darwin" else None,
                        config_path_win=config_root if sys.platform == "win32" else None,
                        config_path_linux=config_root if sys.platform == "linux2" else None,
                        check_storage_path_exists=False,
                    )
                )

        tk = tank.sgtk_from_path(config_root)
        pc = tk.pipeline_configuration

        return pc, config_root, core_root
    def test_update_metadata(self):
        """
        Tests if updating the pipeline to site config actually updates it.
        """
        self.assertFalse(self.tk.pipeline_configuration.is_site_configuration())

        # Make sure the project has been concerted to a site config.
        self.tk.pipeline_configuration.convert_to_site_config()
        self.assertTrue(self.tk.pipeline_configuration.is_site_configuration())

        # Make sure that the setting was correctly written to disk by recreating
        # another instance of the pipeline configuration object so that it reloads
        # it from disk.
        tk2 = tank.sgtk_from_path(self.tk.pipeline_configuration.get_path())
        self.assertTrue(tk2.pipeline_configuration.is_site_configuration())
    def test_update_metadata(self):
        """
        Tests if updating the pipeline to site config actually updates it.
        """
        self.assertFalse(self.tk.pipeline_configuration.is_site_configuration())

        # Make sure the project has been concerted to a site config.
        self.tk.pipeline_configuration.convert_to_site_config()
        self.assertTrue(self.tk.pipeline_configuration.is_site_configuration())

        # Make sure that the setting was correctly written to disk by recreating
        # another instance of the pipeline configuration object so that it reloads
        # it from disk.
        tk2 = tank.sgtk_from_path(self.tk.pipeline_configuration.get_path())
        self.assertTrue(tk2.pipeline_configuration.is_site_configuration())
 def _add_render_files(self, path, items):
     """
     Adds the wip render files on disk associated with this maya scene
     """
     tk = tank.sgtk_from_path(path)
     template = tk.template_from_path(path)
     ctx = tk.context_from_path(path)
     
     fields = template.get_fields(path)
     version = fields["version"]
     name = fields["name"]
     
     # get the wip render files path
     if ctx.entity["type"] == "Asset":
         type = fields["sg_asset_type"]
         asset = fields["Asset"]
         maya_asset_render = tk.templates["maya_asset_render"]
         wip_renders = tk.paths_from_template(maya_asset_render, {"sg_asset_type": type, "Asset": asset, "name": name, "version": version})
     elif ctx.entity["type"] == "Shot":
         spot = fields["Sequence"]
         shot = fields["Shot"]
         maya_shot_render = tk.templates["maya_shot_render"]
         wip_renders = tk.paths_from_template(maya_shot_render, {"Sequence": spot, "Shot": shot, "name": name, "version": version})
     
     if wip_renders:
         wip_render_path = wip_renders[0]
         render_files = [name for name in os.listdir(wip_render_path)]
         baseFilename = ""
         for filename in render_files:
             newBaseFilename = filename.split('.')[0]
             if baseFilename != newBaseFilename:
                 items.append({"name": newBaseFilename,
                               "type": "render_file",
                               "description":filename,
                               "other_params": {"source_folder": wip_render_path,
                                                "scene_path": path}})
                
             baseFilename = newBaseFilename
Пример #10
0
    def _setup_project(self, is_localized):
        """
        Setups a Toolkit centralized pipeline configuration with a localized or not core.
        """

        # Create the project's destination folder.
        locality = "localized" if is_localized else "studio"
        project_folder_name = "config_with_%s_core" % locality
        config_root = os.path.join(self.tank_temp, project_folder_name,
                                   "pipeline_configuration")

        os.makedirs(os.path.join(self.tank_temp, project_folder_name))

        # Mock a core that will setup the project.
        core_root = os.path.join(self.tank_temp, "%s_core" % locality)
        core_install_folder = os.path.join(core_root, "install", "core")
        os.makedirs(core_install_folder)

        # Mock a localized core if required.
        if is_localized:
            self.create_file(
                os.path.join(core_root, "config", "core",
                             "interpreter_Darwin.cfg"))
            self.create_file(
                os.path.join(core_root, "config", "core",
                             "interpreter_Windows.cfg"))
            self.create_file(
                os.path.join(core_root, "config", "core",
                             "interpreter_Linux.cfg"))
            self.create_file(
                os.path.join(core_root, "config", "core", "shotgun.yml"))
            self.create_file(
                os.path.join(core_root, "config", "core", "roots.yml"))
            self.create_file(
                os.path.join(core_install_folder, "_core_upgrader.py"))
            self.assertEqual(tank.pipelineconfig_utils.is_localized(core_root),
                             True)

        # We have to patch these methods because the core doesn't actually exist on disk for the tests.
        with patch("sgtk.pipelineconfig_utils.get_path_to_current_core",
                   return_value=core_root):
            with patch(
                    "sgtk.pipelineconfig_utils.resolve_all_os_paths_to_core",
                    return_value={
                        "linux2":
                        core_root if sys.platform == "linux2" else None,
                        "win32":
                        core_root if sys.platform == "win32" else None,
                        "darwin":
                        core_root if sys.platform == "darwin" else None
                    }):
                command = get_command("setup_project", self.tk)
                command.set_logger(logging.getLogger("test"))
                command.execute(
                    dict(
                        config_uri=os.path.join(self.fixtures_root, "config"),
                        project_id=self._project["id"],
                        project_folder_name=project_folder_name,
                        config_path_mac=config_root
                        if sys.platform == "darwin" else None,
                        config_path_win=config_root
                        if sys.platform == "win32" else None,
                        config_path_linux=config_root
                        if sys.platform == "linux2" else None,
                        check_storage_path_exists=False,
                    ))

        tk = tank.sgtk_from_path(config_root)
        pc = tk.pipeline_configuration

        return pc, config_root, core_root
Пример #11
0
def refresh_engine():
    """
    refresh the current engine
    """

    logger.debug("Refreshing the engine")

    engine = tank.platform.current_engine()

    if not engine:
        # If we don't have an engine for some reason then we don't have
        # anything to do.
        logger.debug(
            "%s Refresh_engine | No currently initialized engine found; aborting the refresh of the engine\n"
            % APPLICATION_NAME
        )
        return

    active_doc_path = None
    active_doc = rumba.active_document()
    if active_doc:
        # determine the tk instance and context to use:
        active_doc_path = rumba.active_document_filename()

    if not active_doc_path:
        logger.debug("File has not been saved yet, aborting the refresh of the engine.")
        return

    # make sure path is normalized
    active_doc_path = os.path.abspath(active_doc_path)

    # we are going to try to figure out the context based on the
    # active document
    current_context = tank.platform.current_engine().context

    ctx = current_context

    # this file could be in another project altogether, so create a new
    # API instance.
    try:
        # and construct the new context for this path:
        tk = tank.sgtk_from_path(active_doc_path)
        logger.debug(
            "Extracted sgtk instance: '%r' from path: '%r'", tk, active_doc_path
        )

    except tank.TankError:
        # could not detect context from path, will use the project context
        # for menus if it exists
        message = (
            "Shotgun %s Engine could not detect the context\n"
            "from the active document. Shotgun menus will be  \n"
            "stay in the current context '%s' "
            "\n" % (APPLICATION_NAME, ctx)
        )
        display_warning(message)
        return

    ctx = tk.context_from_path(active_doc_path, current_context)
    logger.debug(
        "Given the path: '%s' the following context was extracted: '%r'",
        active_doc_path,
        ctx,
    )

    # default to project context in worse case scenario
    if not ctx:
        project_name = engine.context.project.get("name")
        ctx = tk.context_from_entity_dictionary(engine.context.project)
        logger.debug(
            (
                "Could not extract a context from the current active project "
                "path, so we revert to the current project '%r' context: '%r'"
            ),
            project_name,
            ctx,
        )

    # Only change if the context is different
    if ctx != current_context:
        try:
            engine.change_context(ctx)
        except tank.TankError:
            message = (
                "Shotgun %s Engine could not change context\n"
                "to '%r'. Shotgun menu will be disabled!.\n"
                "\n" % (APPLICATION_NAME, ctx)
            )
            display_warning(message)
            engine.create_shotgun_menu(disabled=True)
Пример #12
0
def refresh_engine():
    """
    refresh the current engine
    """

    logger.debug("Refreshing the engine")

    engine = tank.platform.current_engine()

    if not engine:
        # If we don't have an engine for some reason then we don't have
        # anything to do.
        logger.debug(
            "%s Refresh_engine | No currently initialized engine found; aborting the refresh of the engine\n"
            % APPLICATION_NAME)
        return

    # _fix_tk_multi_pythonconsole(logger)

    # get the current package filename
    current_package_filename = None

    ctx = sd.getContext()
    app = ctx.getSDApplication()
    pm = app.getPackageMgr()
    uiMgr = app.getQtForPythonUIMgr()

    current_graph = uiMgr.getCurrentGraph()
    if current_graph:
        pck = current_graph.getPackage()
        current_package_filename = pck.getFilePath()

    if not current_package_filename:
        logger.debug(
            "File has not been saved yet, aborting the refresh of the engine.")
        return

    # make sure path is normalized
    current_package_filename = os.path.abspath(current_package_filename)

    # we are going to try to figure out the context based on the
    # active document
    current_context = tank.platform.current_engine().context

    ctx = current_context

    # this file could be in another project altogether, so create a new
    # API instance.
    try:
        # and construct the new context for this path:
        tk = tank.sgtk_from_path(current_package_filename)
        logger.debug(
            "Extracted sgtk instance: '%r' from path: '%r'",
            tk,
            current_package_filename,
        )

    except tank.TankError:
        # could not detect context from path, will use the project context
        # for menus if it exists
        message = ("Shotgun %s Engine could not detect the context\n"
                   "from the active document. Shotgun menus will be  \n"
                   "stay in the current context '%s' "
                   "\n" % (APPLICATION_NAME, ctx))
        display_warning(message)
        return

    ctx = tk.context_from_path(current_package_filename, current_context)
    logger.debug(
        "Given the path: '%s' the following context was extracted: '%r'",
        current_package_filename,
        ctx,
    )

    # default to project context in worse case scenario
    if not ctx:
        project_name = engine.context.project.get("name")
        ctx = tk.context_from_entity_dictionary(engine.context.project)
        logger.debug(
            ("Could not extract a context from the current active project "
             "path, so we revert to the current project '%r' context: '%r'"),
            project_name,
            ctx,
        )

    # Only change if the context is different
    if ctx != current_context:
        try:
            engine.change_context(ctx)
        except tank.TankError:
            message = ("Shotgun %s Engine could not change context\n"
                       "to '%r'. Shotgun menu will be disabled!.\n"
                       "\n" % (APPLICATION_NAME, ctx))
            display_warning(message)
            engine.create_shotgun_menu(disabled=True)