nuke: refactor Create Gizmo plugin

This commit is contained in:
Jakub Jezek 2022-12-12 10:32:57 +01:00
parent 3f4be3aa69
commit 2413c8ca78
No known key found for this signature in database
GPG key ID: 730D7C02726179A7

View file

@ -1,87 +1,69 @@
import nuke
from openpype.hosts.nuke.api import plugin
from openpype.hosts.nuke.api.lib import (
maintained_selection,
select_nodes,
set_avalon_knob_data
from openpype.hosts.nuke.api import (
NukeCreator,
NukeCreatorError,
maintained_selection
)
class CreateGizmo(plugin.OpenPypeCreator):
"""Add Publishable "gizmo" group
class CreateGizmo(NukeCreator):
"""Add Publishable Group as gizmo"""
The name is symbolically gizmo as presumably
it is something familiar to nuke users as group of nodes
distributed downstream in workflow
"""
name = "gizmo"
label = "Gizmo"
identifier = "create_gizmo"
label = "Gizmo (group)"
family = "gizmo"
icon = "file-archive-o"
defaults = ["ViewerInput", "Lut", "Effect"]
default_variants = ["ViewerInput", "Lut", "Effect"]
def __init__(self, *args, **kwargs):
super(CreateGizmo, self).__init__(*args, **kwargs)
self.nodes = nuke.selectedNodes()
self.node_color = "0x7533c1ff"
return
def process(self):
if (self.options or {}).get("useSelection"):
nodes = self.nodes
self.log.info(len(nodes))
if len(nodes) == 1:
select_nodes(nodes)
node = nodes[-1]
# check if Group node
if node.Class() in "Group":
node["name"].setValue("{}_GZM".format(self.name))
node["tile_color"].setValue(int(self.node_color, 16))
return set_avalon_knob_data(node, self.data)
else:
msg = ("Please select a group node "
"you wish to publish as the gizmo")
self.log.error(msg)
nuke.message(msg)
if len(nodes) >= 2:
select_nodes(nodes)
nuke.makeGroup()
gizmo_node = nuke.selectedNode()
gizmo_node["name"].setValue("{}_GZM".format(self.name))
gizmo_node["tile_color"].setValue(int(self.node_color, 16))
# add sticky node with guide
with gizmo_node:
sticky = nuke.createNode("StickyNote")
sticky["label"].setValue(
"Add following:\n- set Input"
" nodes\n- set one Output1\n"
"- create User knobs on the group")
# add avalon knobs
return set_avalon_knob_data(gizmo_node, self.data)
# plugin attributes
node_color = "0x7533c1ff"
def create_instance_node(
self,
node_name,
knobs=None,
parent=None,
node_type=None
):
with maintained_selection():
if self.selected_nodes:
node = self.selected_nodes[0]
if node.Class() != "Group":
raise NukeCreatorError(
"Creator error: Select only 'Group' node type")
created_node = node
else:
msg = "Please select nodes you wish to add to the gizmo"
self.log.error(msg)
nuke.message(msg)
return
created_node = nuke.collapseToGroup()
created_node["tile_color"].setValue(
int(self.node_color, 16))
created_node["name"].setValue(node_name)
self.add_info_knob(created_node)
return created_node
def create(self, subset_name, instance_data, pre_create_data):
if self.check_existing_subset(subset_name, instance_data):
raise NukeCreatorError(
("Subset name '{}' is already used. "
"Please specify different Variant.").format(subset_name))
instance = super(CreateGizmo, self).create(
subset_name,
instance_data,
pre_create_data
)
return instance
def set_selected_nodes(self, pre_create_data):
if pre_create_data.get("use_selection"):
self.selected_nodes = nuke.selectedNodes()
if self.selected_nodes == []:
raise NukeCreatorError("Creator error: No active selection")
elif len(self.selected_nodes) > 1:
NukeCreatorError("Creator error: Select only one 'Group' node")
else:
with maintained_selection():
gizmo_node = nuke.createNode("Group")
gizmo_node["name"].setValue("{}_GZM".format(self.name))
gizmo_node["tile_color"].setValue(int(self.node_color, 16))
# add sticky node with guide
with gizmo_node:
sticky = nuke.createNode("StickyNote")
sticky["label"].setValue(
"Add following:\n- add Input"
" nodes\n- add one Output1\n"
"- create User knobs on the group")
# add avalon knobs
return set_avalon_knob_data(gizmo_node, self.data)
self.selected_nodes = []