Example #1
0
    def execute(self, operation, file_path, **kwargs):
        """
        Main hook entry point

        :operation: String
                    Scene operation to perform

        :file_path: String
                    File path to use if the operation
                    requires it (e.g. open)

        :returns:   Depends on operation:
                    'current_path' - Return the current scene
                                     file path as a String
                    all others     - None
        """
        active_doc = rumba.active_document()

        if operation == "current_path":
            current_project_filename = rumba.active_document_filename()
            return current_project_filename

        elif operation == "open":
            rumba.load_document(file_path)

        elif operation == "save":
            if active_doc:
                current_project_filename = rumba.active_document_filename()
                if current_project_filename != "untitled":
                    active_doc.write(current_project_filename)
Example #2
0
    def update(self, items):
        """
        Perform replacements given a number of scene items passed from the app.

        Once a selection has been performed in the main UI and the user clicks
        the update button, this method is called.

        The items parameter is a list of dictionaries on the same form as was
        generated by the scan_scene hook above. The path key now holds
        the that each attribute should be updated *to* rather than the current
        path.
        """
        app = self.parent
        engine = app.engine
        engine.log_debug("items: %s" % items)

        active_document = rumba.active_document()
        if active_document:
            rumba.modify_begin("Shotgun Update References")
            for i in items:
                new_path = i["path"]
                node_name = i["node"].node
                node = active_document.child(node_name)
                engine.log_debug("Updating node: %s to path %s" %
                                 (node_name, new_path))
                set_node_file_path(node, new_path)
            rumba.modify_end()
Example #3
0
 def _export(self, settings, item, path):
     node_full_document_name = item.properties.get("node_full_document_name")
     active_document = rumba.active_document()
     if active_document:
         node = active_document.child(node_full_document_name)
         if node:
             folder = os.path.dirname(path)
             ensure_folder_exists(folder)
             node.write(path)
Example #4
0
def _save_session(path):
    """
    Save the current session to the supplied path.
    """

    # Ensure that the folder is created when saving
    folder = os.path.dirname(path)
    ensure_folder_exists(folder)

    active_doc = rumba.active_document()
    if active_doc:
        main_window = rumbapy.widget("MainWindow")
        main_window.save_at(path)
Example #5
0
    def scan_scene(self):
        """
        The scan scene method is executed once at startup and its purpose is
        to analyze the current scene and return a list of references that are
        to be potentially operated on.

        The return data structure is a list of dictionaries. Each scene
        reference that is returned should be represented by a dictionary with
        three keys:

        - "attr": The filename attribute of the 'node' that is to be operated
           on. Most DCCs have a concept of a node, attribute, path or some
           other way to address a particular object in the scene.
        - "type": The object type that this is. This is later passed to the
           update method so that it knows how to handle the object.
        - "path": Path on disk to the referenced object.

        Toolkit will scan the list of items, see if any of the objects matches
        any templates and try to determine if there is a more recent version
        available. Any such versions are then displayed in the UI as out of
        date.
        """
        # this is a bit dogy, but works, we hide the update
        # button as it is not needed.
        refs = []

        active_document = rumba.active_document()
        if active_document:
            nodes = find_nodes_with_reference(active_document) or []
            nodes += find_nodes_with_plug(active_document,
                                          "file_path",
                                          type_names=["UsdAsset"])
            nodes += find_nodes_with_plug(active_document,
                                          "file",
                                          type_names=["Media"])

            for node in nodes:
                ref_path = get_node_file_path(node)
                if ref_path:
                    refs.append({
                        "node": BreakdownSceneItem(node, ref_path),
                        "type": "file",
                        "path": ref_path,
                    })

        app = self.parent
        engine = app.engine
        engine.log_debug("refs: %s" % refs)

        return refs
Example #6
0
    def get_frame_range(self, **kwargs):
        """
        get_frame_range will return a tuple of (in_frame, out_frame)
        :returns: Returns the frame range in the form (in_frame, out_frame)
        :rtype: tuple[int, int]
        """

        current_in = 0
        current_out = 0

        active_doc = rumba.active_document()

        if active_doc:
            current_in = active_doc.start_frame.value().as_int()
            current_out = active_doc.end_frame.value().as_int()

        return (current_in, current_out)
Example #7
0
def _rumba_find_additional_session_dependencies():
    """
    Find additional dependencies from the session
    """
    # Figure out what read nodes are known to the engine and use them
    # as dependencies

    additional_dependencies = []
    active_document = rumba.active_document()
    if active_document:
        nodes = _find_nodes_with_reference(active_document) or []
        for node in nodes:
            ref_path = node.reference_filename()
            if ref_path:
                additional_dependencies.append(ref_path)

    return additional_dependencies
    def _import_into_current_document(self, path, sg_publish_data):
        """
        Imports a file into the current document

        :param path: Path to file.
        :param sg_publish_data: Shotgun data dictionary with all the standard
                                publish fields.
        """
        app = self.parent
        active_document = rumba.active_document()

        if active_document:
            if os.path.exists(path):
                app.log_debug("Importing path: %s" % path)
                rumba.modify_begin("Shotgun Import File")
                rumba.load_node(active_document, path, "")
                rumba.modify_end()
            else:
                app.log_warning("Path to import not found!: %s" % path)
Example #9
0
    def set_frame_range(self, in_frame=None, out_frame=None, **kwargs):
        """
        set_frame_range will set the frame range using `in_frame` and `out_frame`
        :param int in_frame: in_frame for the current context
            (e.g. the current shot, current asset etc)
        :param int out_frame: out_frame for the current context
            (e.g. the current shot, current asset etc)
        """

        active_doc = rumba.active_document()

        if active_doc:
            start = int(in_frame)
            end = int(out_frame)

            rumba.modify_begin("Shotgun Update Frame Range")
            active_doc.start_frame.set_value(start)
            active_doc.end_frame.set_value(end)
            active_doc.range_start_frame.set_value(start)
            active_doc.range_end_frame.set_value(end)
            rumba.modify_end()
    def _import_media_into_new_media_layer(self, path, sg_publish_data):
        """
        Imports a file into the current document

        :param path: Path to file.
        :param sg_publish_data: Shotgun data dictionary with all the standard
                                publish fields.
        """
        app = self.parent
        active_document = rumba.active_document()

        if active_document:
            if os.path.exists(path):
                rumba.modify_begin("Shotgun Import File")
                layer_name = sg_publish_data.get("code",
                                                 os.path.basename(path))
                media_layer = rumbapy.add_media_layer(layer_name)
                frame = rumba.current_frame()
                media_node = rumbapy.get_media(path)
                rumbapy.add_media_clip(media_layer, media_node, frame)
                rumba.modify_end()
            else:
                app.log_warning("Path to import not found!: %s" % path)
Example #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)
Example #12
0
    def execute(self, operation, file_path, context, parent_action,
                file_version, read_only, **kwargs):
        """
        Main hook entry point

        :param operation:       String
                                Scene operation to perform

        :param file_path:       String
                                File path to use if the operation
                                requires it (e.g. open)

        :param context:         Context
                                The context the file operation is being
                                performed in.

        :param parent_action:   This is the action that this scene operation is
                                being executed for.  This can be one of:
                                - open_file
                                - new_file
                                - save_file_as
                                - version_up

        :param file_version:    The version/revision of the file to be opened.  If this is 'None'
                                then the latest version should be opened.

        :param read_only:       Specifies if the file should be opened read-only or not

        :returns:               Depends on operation:
                                'current_path' - Return the current scene
                                                 file path as a String
                                'reset'        - True if scene was reset to an empty
                                                 state, otherwise False
                                all others     - None
        """
        active_doc = rumba.active_document()

        if operation == "current_path":
            current_project_filename = rumba.active_document_filename()
            return current_project_filename

        elif operation == "open":
            rumba.load_document(file_path)

        elif operation == "save":
            if active_doc:
                current_project_filename = rumba.active_document_filename()
                if current_project_filename != "untitled":
                    active_doc.write(current_project_filename)

        elif operation == "save_as":
            if active_doc:
                main_window = rumbapy.widget("MainWindow")
                main_window.save_at(file_path)

        elif operation == "reset":
            if active_doc:
                rumba.new_document()
            return True

        elif operation == "prepare_new":
            rumba.new_document()
            return True