tips,tricks & geekie stuff

BATCH GIZMO TO GRIZMO

tl;dr: Batch convert your favourite Nuke Gizmos into “Grizmos”.

Hi folks,
 
So here I have a new little Python script for those who find a little boring converting Gizmos into “Grizmos” manually. Just in case, I´ll remind you that one of the best ways to make sure all your fancy Gizmos are going to render in the farm, or that a user that does not have a particular Gizmo can still open a comp that has it and work on it, is simply converting that Gizmo into a Group, thus getting the so-called  “Grizmo” (as Hugo Lévéille would call´em).
What this script does, in its first version, is basically creating a Grizmo per each Gizmo found in a passed directory. AKA a batch Gizmo-to-Group converter, or a batch Grizmo Creator… And only for Windows users, at the moment.
How to use it?
1 – Store and name the Python script in a folder that Nuke is going to look at when it comes to importing Python scripts. Try the command “print nuke.pluginPath()” in the script editor if you are not sure where your Nuke installation is looking for Plugins, Python Scripts and such.
2 – Import the script you have just saved from Nuke´s script editor like “import module_name“.
3 – Call the main()  function from the imported script like “module_name.main()“.
4 – If everything goes ok, you should immediately see a pop-up window asking for you to select the “Gizmos Path” directory. Navigate to the folder where you store all your fancy Gizmos and click ok.
5 – Now you should have as many Grizmos as Gizmos you had in the former directory, with the “_grizmed” extension added to the original name. For instance:
    – Cryptomatte.gizmo (original Gizmo)
    – Cryptomatte_grizmed.gizmo (Grizmed Gizmo)
6 – Enjoy your brand new fancy Grizmos :D

important: If you find this usefull, it´s always better to create a Nuke menu from where to call the function.

snippet
import nuke
import os
import nukescripts

def transform_line(line):
    """ basic character replacement function.
    :param line: a string to be passed to perform character replacement.
    :return: the line mutated with the new replaced character.
    """
    word_replacements = {'Gizmo': 'Group'}
    for key, value in word_replacements.iteritems():
        line = line.replace(key, value)
    return line


def gizmo_to_grizmo(i):
    """ this function creates a "grouped" .gizmo file.
    :param i: input file to open
    :return: None
    """
    o = str.split(i,".")[0] + "_grizmed" + "." + str.split(i,".")[-1]
    with open(o, "w") as output_file, open(i) as input_file:
        for line in input_file:
            output_file.write(transform_line(line))
    return None


def grizmo_my_folder(dir):
    """ this fuction parses a directory and applies the gizmo_to_grizmo to its files, if convenient.
       :param dir: directory to be parsed.
       :return: None
       """
    for subdir, dirs, files in os.walk(dir):
        for file in files:
            # print os.path.join(subdir, file)
            filepath = subdir + os.sep + file
            if "grizmed" in filepath:
                print "already grizmed"
                return
            elif filepath.endswith(".gizmo") and "grizmed" not in filepath:
                gizmo_to_grizmo(filepath)
                print "gizmos grizmed succesfully"
            else:
                print "no valid file found, bruh"
                return
            

class Panel(nukescripts.PythonPanel):
    def __init__(self, name):
        super(Panel, self).__init__(name)
        # self.setMinimumSize(500,500)
        self.path = nuke.File_Knob('gizmos_path', 'Gizmos Path')

        self.addKnob(self.path)

    def show_modal_dialog(self):
        nukescripts.PythonPanel.showModalDialog(self)


def main():
    """
    wrapper function. From here we open the Nuke Panel to find the Gizmo Directory and launch the rest of the program
    :return: None
    """
    p = Panel("Grizmo Maker")
    p.show_modal_dialog()
    path = p.path.getValue()
    grizmo_my_folder(path)

    if path:
        panel = nuke.message("Grizmification completed")
        print "Grizmification completed"
    else:
        panel = nuke.message("Grizmification aborted")
        print "Grizmification aborted"
    return


if __name__ == '__main__':
    main()