forked from dbr/tabtabtab-nuke
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabtabtab_nuke.py
More file actions
165 lines (136 loc) · 5.52 KB
/
tabtabtab_nuke.py
File metadata and controls
165 lines (136 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import re
import nuke
try:
from PySide6 import QtGui
except ImportError:
from PySide2 import QtGui
from tabtabtab_nuke_core import TabTabTabPlugin, launch
def _extract_node_class_from_script(script_text):
"""Parse a menu item's command string for the actual node class name."""
if not script_text:
return None
# Python form: any module calling createNode or createNodeLocal
# e.g. nuke.createNode("ScanlineRender2") or nukescripts.createNodeLocal("ScanlineRender2")
match = re.search(r'createNode(?:Local)?\s*\(\s*["\'](\w+)["\']', script_text)
if match:
return match.group(1)
# Tcl form: bare class name as the entire command
match = re.match(r'^(\w+)$', script_text.strip())
if match:
return match.group(1)
return None
def _find_nuke_menu_items(menu, _path=None, is_node=True):
"""Extracts items from a given Nuke menu
Returns a list of {'menuobj': ..., 'menupath': str, 'is_node': bool,
'actual_class': str or None, 'shortcut': str or None} dicts.
Ignores divider lines and hidden items (ones like "@;&CopyBranch" for shift+k)
"""
found = []
mi = list(menu.items())
for i in mi:
if isinstance(i, nuke.Menu):
# Sub-menu, recurse
mname = i.name().replace("&", "")
subpath = "/".join(x for x in (_path, mname) if x is not None)
if "ToolSets/Delete" in subpath:
# Remove all ToolSets delete commands
continue
sub_found = _find_nuke_menu_items(menu=i, _path=subpath, is_node=is_node)
found.extend(sub_found)
elif isinstance(i, nuke.MenuItem):
if i.name() == "":
# Skip dividers
continue
if i.name().startswith("@;"):
# Skip hidden items
continue
# Extract the actual node class from the command script
actual_class = None
try:
script_text = i.script()
actual_class = _extract_node_class_from_script(script_text)
except Exception:
pass
if actual_class is None:
actual_class = i.name()
# Extract keyboard shortcut if available
shortcut = None
try:
shortcut_str = i.shortcut()
if shortcut_str:
shortcut = shortcut_str
except Exception:
pass
subpath = "/".join(x for x in (_path, i.name()) if x is not None)
found.append({
'menuobj': i,
'menupath': subpath,
'is_node': is_node,
'actual_class': actual_class,
'shortcut': shortcut,
})
return found
class NukePlugin(TabTabTabPlugin):
def __init__(self):
self._menuobj_metadata = {} # id(menuobj) -> {is_node, actual_class}
def get_items(self):
self._menuobj_metadata = {}
node_items = _find_nuke_menu_items(nuke.menu("Nodes"), is_node=True)
menu_items = _find_nuke_menu_items(nuke.menu("Nuke"), is_node=False)
all_items = node_items + menu_items
for item in all_items:
self._menuobj_metadata[id(item['menuobj'])] = {
'is_node': item['is_node'],
'actual_class': item['actual_class'],
}
return all_items
def get_weights_file(self):
return os.path.expanduser("~/.nuke/tabtabtab_weights.json")
def invoke(self, thing):
thing['menuobj'].invoke()
def get_icon(self, menuobj):
icon_str = menuobj.icon()
if not icon_str:
return None
# Search Nuke's plugin paths for the icon file
for search_path in nuke.pluginPath():
candidate = os.path.join(search_path, icon_str)
if os.path.exists(candidate):
return QtGui.QIcon(candidate)
return None
def get_color(self, menuobj):
metadata = self._menuobj_metadata.get(id(menuobj), {})
if not metadata.get('is_node', False):
return (None, None)
actual_class = metadata.get('actual_class', menuobj.name())
try:
packed_color = nuke.defaultNodeColor(actual_class)
if packed_color == 0:
return (None, None)
# Skip nodes whose colour comes from the global preference default
# rather than a class-specific setting. We detect this by comparing
# against what Nuke returns for a class name that cannot exist.
global_default_color = nuke.defaultNodeColor("__tabtabtab_sentinel__")
if packed_color == global_default_color:
return (None, None)
r = (packed_color >> 24) & 0xFF
g = (packed_color >> 16) & 0xFF
b = (packed_color >> 8) & 0xFF
# Use the un-dimmed tile colour as the left-block background (drawn
# behind the icon) and as the text-area tint.
tile_color = QtGui.QColor(r, g, b)
return (tile_color, tile_color)
except Exception:
return (None, None)
_plugin = NukePlugin()
if nuke.NUKE_VERSION_MAJOR >= 9:
_getParentMenu = lambda: nuke.menu("Node Graph")
else:
_getParentMenu = lambda: nuke.menu("Nuke").findItem("Edit")
def registerNukeAction():
menu = _getParentMenu()
if menu.findItem("Tabtabtab") is None:
menu.addCommand("Tabtabtab", lambda: launch(_plugin), "Tab")
def unregisterNukeAction():
_getParentMenu().removeItem("Tabtabtab")