Resolution Per Task

Resolution Per Task#

This example plugin let’s you define a custom resolution per task.
When opening or saving the scenefile in a specific task, the user will be prompted with an option to set the task specific resolution in his scenefile.
In this example the resolution is queried from Shotgrid, but other sources can be used by editing the getResolutionForCurrentTask function.

See the section for Single File Plugins on how to load this example.

# ResolutionPerTask.py

name = "ResolutionPerTask"
classname = "ResolutionPerTask"


import os
from qtpy.QtWidgets import *


class ResolutionPerTask:
    def __init__(self, core):
        self.core = core
        self.version = "v1.0.0"

        self.core.plugins.monkeyPatch(self.core.sanities.checkResolution, self.checkResolution, self)

    def checkResolution(self):
        # this function is a modified version of the same function in SanityChecks.py of the Prism Core scripts

        forceRes = self.core.getConfig(
            "globals", "forceResolution", configPath=self.core.prismIni
        )
        if not forceRes:
            return

        if not self.core.fileInPipeline():
            return

        fileName = self.core.getCurrentFileName()
        entity = self.core.getScenefileData(fileName)
        metaData = self.core.entities.getMetaData(entity)
        resDef = "project"
        pRes = self.core.getConfig(
            "globals", "resolution", configPath=self.core.prismIni
        )

        resX = None
        resY = None
        if pRes:
            resX = pRes[0]
            resY = pRes[1]

        if "resolution_x" in metaData:
            resDef = "entity"
            try:
                resX = int(metaData["resolution_x"]["value"])
            except:
                pass

        if "resolution_y" in metaData:
            resDef = "entity"
            try:
                resY = int(metaData["resolution_y"]["value"])
            except:
                pass

        # get resolution from Shotgrid task
        taskRes = self.getResolutionForCurrentTask()
        if taskRes:
            resX, resY = taskRes

        if not resX or not resY:
            return

        curRes = self.core.getResolution()
        if not curRes:
            return

        if [resX, resY] == curRes:
            return

        vInfo = [
            ["Resolution of current scene:", "%s x %s" % (curRes[0], curRes[1])],
            ["Resolution of %s" % resDef, "%s x %s" % (resX, resY)],
        ]
        lay_info = QGridLayout()
        msgString = "The resolution of the current scene doesn't match the resolution of the %s:" % resDef

        for idx, val in enumerate(vInfo):
            l_infoName = QLabel(val[0] + ":\t")
            l_info = QLabel(val[1])
            lay_info.addWidget(l_infoName)
            lay_info.addWidget(l_info, idx, 1)

        lay_info.addItem(
            QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding)
        )
        lay_info.addItem(
            QSpacerItem(10, 10, QSizePolicy.Expanding, QSizePolicy.Minimum), 0, 2
        )

        lay_info.setContentsMargins(10, 10, 10, 10)
        w_info = QWidget()
        w_info.setLayout(lay_info)

        msg = self.core.popupQuestion(
            msgString,
            title="Resolution mismatch",
            buttons=["Set %s resolution in current scene" % resDef, "Skip"],
            widget=w_info,
            escapeButton="Skip",
            default="Skip",
            doExec=False,
        )
        if not self.core.isStr(msg):
            msg.buttonClicked.connect(lambda x: self.core.sanities.onCheckResolutionClicked(x, [resX, resY]))
            msg.show()

    def getResolutionForCurrentTask(self):
        # this functions gets the resolution for this task from Shotgrid.
        # it can be modified to query the information from other sources

        sgPlug = self.core.getPlugin("Shotgrid")
        if not sgPlug:
            return

        fileName = self.core.getCurrentFileName()
        entity = self.core.getScenefileData(fileName)
        sgTask = sgPlug.getTask(entity, dep=entity.get("department"), task=entity.get("task"))
        if not sgTask:
            return

        fields = ["sg_description"]  # the name of the custom field, which stores the resolution for this task
        filters = [["id", "is", sgTask["id"]]]
        sgTaskData = sgPlug.makeDbRequest("find_one", ["Task", filters, fields])
        sgResolution = sgTaskData["sg_description"]
        if not sgResolution:
            return

        try:
            resX, resY = [int(val) for val in sgResolution.split("x")]
            return resX, resY
        except:
            pass