def _create_node(self, node_type, path, sg_publish_data, asset_parameter="file"):
        """
        Generic node creation method.
        """
        if not os.path.exists(path):
            raise Exception("File not found on disk - '%s'" % path)
                
        project_path = "/mnt/netdev/sam.saxon/shotgun/Shotgun_Katana_Test/big_buck_bunny" # TODO: Hardcoded == not cool
        tk = sgtk.tank_from_path( project_path )

        # Setup shotgun asset string
        template = tk.template_from_path( path )
        fields = template.get_fields( path )
        # TODO Temporary fix for the SEQ field which seems to mess up the search
        if fields.has_key("SEQ"):
            fields.pop("SEQ")
        parameter_dict = {}
        parameter_dict["template"] = template.name
        parameter_dict["fields"] = fields

        # Create node
        root = NodegraphAPI.GetRootNode()
        node = NodegraphAPI.CreateNode(node_type, parent=root)
        parm = node.getParameter(asset_parameter)
        parm.setValue(str(parameter_dict), 0)
        return node
Beispiel #2
0
def __tank_on_scene_event_callback(**kwargs):
    """
    Callback that fires every time a file is saved or loaded.

    Carefully manage exceptions here so that a bug in Tank never
    interrupts the normal workflows in Katana.
    """
    # get the new file name
    file_name = FarmAPI.GetKatanaFileName()

    if not file_name:   # New scene
        return

    try:
        # this file could be in another project altogether, so create a new Tank
        # API instance.
        try:
            tk = sgtk.tank_from_path(file_name)
        except sgtk.TankError as error:
            __create_tank_disabled_menu(error)
            return

        # try to get current ctx and inherit its values if possible
        curr_ctx = None
        if sgtk.platform.current_engine():
            curr_ctx = sgtk.platform.current_engine().context

        # and now extract a new context based on the file
        new_ctx = tk.context_from_path(file_name, curr_ctx)

        # now restart the engine with the new context
        __engine_refresh(tk, new_ctx)
    except Exception:
        __create_tank_error_menu()
Beispiel #3
0
    def _tank_instance(cls, env, paths, settings):
        """@return the initialized tank package that exists at TANK_STUDIO_INSTALL_TREE, and the context path which created
        the instance
        @param env enviornment of the to-be-started process
        @param paths from which to pull the context. They should be sorted from most specialized to to least 
        specialized
        @param settings matching the tank_engine_schema
        @throws EnvironmentError if we couldn't find it"""
        sgtk = cls._sgtk_module(env)

        if settings.force_tank_from_entity:
            if not (settings.entity_type and settings.entity_id):
                msg = "Was forced to create tank from entity, but didn't get entity information"
                raise AssertionError(msg)
            # end 

            return sgtk.tank_from_entity(settings.entity_type, settings.entity_id), 'context_path_not_set'
        else:
            errors = list()
            for path in paths:
                try:
                    return sgtk.tank_from_path(path), path
                except Exception as err:
                    errors.append(err)
            # end for each path to try
        # end handle tank instantiation mode

        raise EnvironmentError("Failed to initialize tank from any of the given context paths: %s\nErrors: %s" 
                               % (', '.join(paths), '\n'.join(str(err) for err in errors)))
    def launchFromPath(self, path):
        #path = "//sledge/vol1/Projects/EventTrackingProject/Compositing/Setups/Nuke/EV001/Comp/work/EV001_comp_v004.nk"

        app = self.SGTK_ENGINE.apps[self.launchApp]

        tk_i = sgtk.tank_from_path(path)
        context = tk_i.context_from_entity("Task", self.sgTaskDict['id'])
        try:

            print "TASK ", context.task
            print "STEP ", context.step
            print "PROJ ", context.project
            print "TYPE ", context.entity
            print "USER ", context.user
        except:
            pass
        print self.launchApp, "launch ->", context, path

        #app._launch_app(context, version= self.version )
        app._launch_app(context, file_to_open=path, version=self.version)
Beispiel #5
0
    def _on_project_load_callback(self, event):
        """
        Callback executed after project load in Hiero and Nuke Studio. This
        triggers an attempt to change the SGTK context to that of the newly
        opened project file.

        :param event:   The event object from Hiero/NS.
        """
        import hiero.core

        project = hiero.core.projects()[-1]
        script_path = project.path()

        # We're going to just skip doing anything if this fails
        # for any reason. It would be nice to swap to the error
        # menu item, but unfortunately a project open event is
        # triggered on launch when Hiero/Nuke Studio loads the
        # "Untitled" project from the Nuke install location. There
        # isn't a way to distinguish between that and something the
        # user purposefully opened, and we don't want to hose the
        # toolkit context with that.
        try:
            tk = sgtk.tank_from_path(script_path)

            # Extract a new context based on the file and change to that
            # context.
            new_context = tk.context_from_path(
                script_path,
                previous_context=self.context,
            )

            if new_context != self.context:
                sgtk.platform.change_context(new_context)
        except Exception:
            self.logger.debug("Unable to determine context for file: %s",
                              script_path)
Beispiel #6
0
def _on_file_change_timeout():
    """
    Checks to see if the current file has changed. If it has, try to set the
    new context for the file.
    """

    import hou

    cur_file = hou.hipFile.path()

    global g_current_file
    if cur_file == g_current_file:
        # the current file is the same as it was last time. no file change,
        # no need to proceed
        return

    # update the current file global so that the next timeout won't do anything
    # it isn't supposed to
    g_current_file = cur_file

    # if the file name is untitled.hip, don't automatically destroy the engine.
    # allow the user to continue working in the same context
    file_name = os.path.split(cur_file)[-1]
    if file_name.lower() == "untitled.hip":
        return

    import sgtk

    cur_engine = None

    # attempt to get the current engine and context
    try:
        cur_engine = sgtk.platform.current_engine()
        cur_context = cur_engine.context
        engine_name = cur_engine.name
    except Exception:
        engine_name = "tk-houdini"
        cur_context = None

    try:
        tk = sgtk.tank_from_path(cur_file)
    except sgtk.TankError:
        # Unable to get tk api instance from the path. won't be able to get a
        # new context. if there is an engine running, destroy it.
        if cur_engine:
            cur_engine.destroy()
        return

    # get the new context from the file
    new_context = tk.context_from_path(cur_file, cur_context)

    # if the contexts are the same, either the user has not changed context or
    # the context change has already been handled, for example by workfiles2
    if cur_context == new_context:
        return

    # try to create new engine
    try:
        if cur_engine:
            sgtk.platform.change_context(new_context)
        else:
            sgtk.platform.start_engine(engine_name, tk, new_context)
    except sgtk.TankEngineInitError as e:
        msg = (
            "There was a problem starting a new instance of the '%s' engine "
            "for context '%s'\n"
            "Error: %s" % (engine_name, new_context, e)
        )
        hou.ui.displayMessage(msg, severity=hou.severityType.Error)
        return
        return

    import sgtk
    cur_engine = None

    # attempt to get the current engine and context
    try:
        cur_engine = sgtk.platform.current_engine()
        cur_context = cur_engine.context
        engine_name = cur_engine.name
    except Exception, e:
        engine_name = "tk-houdini"
        cur_context = None

    try:
        tk = sgtk.tank_from_path(cur_file)
    except sgtk.TankError, e:
        # Unable to get tk api instance from the path. won't be able to get a
        # new context. if there is an engine running, destroy it.
        if cur_engine:
            cur_engine.destroy()
        return

    # get the new context from the file
    new_context = tk.context_from_path(cur_file, cur_context)

    # if the contexts are the same, either the user has not changed context or
    # the context change has already been handled, for example by workfiles2
    if cur_context == new_context:
        return
Beispiel #8
0
def _on_file_change_timeout():
    """
    Checks to see if the current file has changed. If it has, try to set the
    new context for the file.
    """

    import hou

    cur_file = hou.hipFile.path()

    global g_current_file
    if cur_file == g_current_file:
        # the current file is the same as it was last time. no file change,
        # no need to proceed
        return

    # update the current file global so that the next timeout won't do anything
    # it isn't supposed to
    g_current_file = cur_file

    # if the file name is untitled.hip, don't automatically destroy the engine.
    # allow the user to continue working in the same context
    file_name = os.path.split(cur_file)[-1]
    if file_name.lower() == "untitled.hip":
        return

    import sgtk

    cur_engine = None

    # attempt to get the current engine and context
    try:
        cur_engine = sgtk.platform.current_engine()
        cur_context = cur_engine.context
        engine_name = cur_engine.name
    except Exception:
        engine_name = "tk-houdini"
        cur_context = None

    try:
        tk = sgtk.tank_from_path(cur_file)
    except sgtk.TankError:
        # Unable to get tk api instance from the path. won't be able to get a
        # new context. if there is an engine running, destroy it.
        if cur_engine:
            cur_engine.destroy()
        return

    # get the new context from the file
    new_context = tk.context_from_path(cur_file, cur_context)

    # WWFX: Custom logic to job into a task if pipeline step only has 1 task
    # See https://wwfx.shotgunstudio.com/detail/Ticket/416
    if new_context.entity and new_context.step and not new_context.task:
        filters = [
            ["project", "is", cur_context.project],
            ["step", "is", new_context.step],
            ["entity", "is", new_context.entity],
        ]
        possible_tasks = tk.shotgun.find("Task", filters, fields=["content"])
        if possible_tasks:
            task = possible_tasks[0]
            task_message = "Jobbing into task {0[content]!r} (id:{0[id]})"

            # Choose from task names if many unique tasks names exist
            task_names = {t["content"] for t in possible_tasks}
            if len(task_names) > 1 and hou.isUIAvailable():
                kwargs = {
                    "exclusive": True,
                    "message": "Choose from a task to job into",
                    "title": "Tasks",
                }
                choices = [
                    '"{0[content]}" id:{0[id]}'.format(t)
                    for t in possible_tasks
                ]
                indices_chosen = hou.ui.selectFromList(choices, **kwargs)
                if indices_chosen:
                    task = possible_tasks[indices_chosen[0]]
                else:
                    hou.ui.displayMessage(
                        task_message.format(task),
                        severity=hou.severityType.Warning,
                    )

            cur_engine.logger.info(task_message.format(task))
            new_context = tk.context_from_entity("Task", task["id"])
        else:
            message = "Unable to job into a Shotgun TASK! Please do not publish."
            if hou.isUIAvailable():
                hou.ui.displayMessage(message,
                                      severity=hou.severityType.Warning)
            else:
                cur_engine.logger.info(message)

    # if the contexts are the same, either the user has not changed context or
    # the context change has already been handled, for example by workfiles2
    if cur_context == new_context:
        return

    # try to create new engine
    try:
        if cur_engine:
            sgtk.platform.change_context(new_context)
        else:
            sgtk.platform.start_engine(engine_name, tk, new_context)
    except sgtk.TankEngineInitError as e:
        msg = (
            "There was a problem starting a new instance of the '%s' engine "
            "for context '%s'\n"
            "Error: %s" % (engine_name, new_context, e))
        hou.ui.displayMessage(msg, severity=hou.severityType.Error)
        return
        return

    import sgtk
    cur_engine = None

    # attempt to get the current engine and context
    try:
        cur_engine = sgtk.platform.current_engine()
        cur_context = cur_engine.context
        engine_name = cur_engine.name
    except Exception, e:
        engine_name = "tk-houdini"
        cur_context = None

    try:
        tk = sgtk.tank_from_path(cur_file)
    except sgtk.TankError, e:
        # Unable to get tk api instance from the path. won't be able to get a
        # new context. if there is an engine running, destroy it.
        if cur_engine:
            cur_engine.destroy()
        return

    # get the new context from the file
    new_context = tk.context_from_path(cur_file, cur_context)

    # if the contexts are the same, either the user has not changed context or
    # the context change has already been handled, for example by workfiles2
    if cur_context == new_context:
        return