This repository was archived by the owner on Mar 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp_plugin_loader.py
More file actions
302 lines (248 loc) · 10.5 KB
/
Copy pathwp_plugin_loader.py
File metadata and controls
302 lines (248 loc) · 10.5 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
# Copyright 2020 Wilfried Pollan
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# # http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Module for loading and managing Nuke plugins.
file=wp_plugin_loader.py
Load nuke plugins:
- Toolsets
- Gizmos
- Python tools and panels
- OFX tools
:TODO:
Manage plugins:
- Disable
- Menu path
- Shortcuts
"""
__author__ = "Wilfried Pollan"
__current_file__ = __file__
# Imports
import fnmatch
import json
import logging
import os
import re
import sys
import nuke
from string import Template
# Constants
CONFIG_TYPE = ".json"
CONFIG_ROOT = os.path.join(
os.path.dirname(os.path.abspath(os.path.realpath(__current_file__))), "configs"
)
PREFS_CONFIG_NAME = "preferences" + CONFIG_TYPE
PREFS_CONFIG_PATH = os.path.join(CONFIG_ROOT, PREFS_CONFIG_NAME)
PLUGIN_OVERRIDE_CONFIG_NAME = "plugin_overrides" + CONFIG_TYPE
PLUGIN_OVERRIDE_CONFIG_PATH = os.path.join(CONFIG_ROOT, PLUGIN_OVERRIDE_CONFIG_NAME)
def _convert2slash(string):
return string.replace("\\", "/")
class LoadPlugins(object):
"""
Query and load all plugins based on the provided configs.
"""
def __init__(self, config_root=None):
"""
Init loader with values from the prefs.
:param prefs: Optional path to the preferences
:type prefs: str
"""
# Init logging
self.logger = logging.getLogger(self.__class__.__name__)
self.logger.info("Init nuke plugin loader.")
# Check if pref given otherwise load the default it from the default location
self.plugin_overrides = None
if config_root is not None and os.path.isdir(config_root):
self.preferences = self._load_configs(
os.path.join(config_root, PREFS_CONFIG_NAME)
)
# Load optional config for plugin override if exists.
plugin_override_path = os.path.join(
config_root, PLUGIN_OVERRIDE_CONFIG_NAME
)
if os.path.isfile(plugin_override_path):
self.plugin_overrides = self._load_configs(plugin_override_path)
else:
self.preferences = self._load_configs(PREFS_CONFIG_PATH)
# Load optional config for plugin override if exists.
if os.path.isfile(PLUGIN_OVERRIDE_CONFIG_PATH):
self.plugin_overrides = self._load_configs(PLUGIN_OVERRIDE_CONFIG_PATH)
# Check the current platform and return resolved path
self.plugins_path = self._resolve_platform_paths("PLUGINS_PATH")
self.python_path = self._resolve_platform_paths("PYTHON_PATH")
# Set regex
includes = self.preferences["INCLUDES"]
includes = r"|".join([fnmatch.translate(x) for x in includes]) or r"$."
self.re_includes = re.compile(includes)
excludes = self.preferences["EXCLUDES"]
excludes = r"|".join([fnmatch.translate(x) for x in excludes]) or r"$."
self.re_excludes = re.compile(excludes)
# Get tools
self.tools_dict = self._get_tools_files()
def _load_configs(self, path):
"""
Load preferences from file.
:param path: Path to config file
:type path: str
"""
self.logger.info("Loading config: {0}".format(path))
if os.path.isfile(path) and path.endswith(CONFIG_TYPE):
try:
with open(path) as fobj:
return json.load(fobj)
except json.JSONDecodeError:
logging.exception(
"The config is not a valid {0} object!\n".format(CONFIG_TYPE)
)
exit(1)
else:
logging.exception(
"Provided config path does not exists or is not {0} file!\n".format(
CONFIG_TYPE
)
)
exit(1)
def _resolve_platform_paths(self, key):
if sys.platform.startswith("linux"):
return Template(self.preferences[key]["linux"]).safe_substitute(os.environ)
elif sys.platform.startswith("darwin"):
return Template(self.preferences[key]["darwin"]).safe_substitute(os.environ)
elif sys.platform.startswith("win"):
return Template(self.preferences[key]["win"]).safe_substitute(os.environ)
def _get_tools_files(self):
"""
Generate a dict with all tools.
:return: {relative path to tool: files}
:rtype: dict
"""
tools_files_dict = dict()
self.logger.debug("Searching for plugins in {0}".format(self.plugins_path))
for root, dirs, files in os.walk(self.plugins_path, topdown=True):
# exclude dirs
dirs[:] = [d for d in dirs if not self.re_excludes.match(d)]
# Check for special files and if they are present don't dive deeper.
for special_file in self.preferences["SPECIAL_FILES"]:
dirs[:] = [d for d in dirs if not special_file in files]
# Here we keep only the files in includes
files = [f for f in files if self.re_includes.match(f)]
files = sorted(files)
if files:
self.logger.debug("Found: {0} > {1}".format(root, files))
root = _convert2slash(root)[len(self.plugins_path) + 1 :]
tools_files_dict[root] = files
self.logger.info("Finished querying tools in: {0}\n".format(self.plugins_path))
return tools_files_dict
def _set_menus(self):
"""
Query the actual menus in Nuke and set the user menus variales.
"""
# Nuke menu and toolbar
menubar = nuke.menu("Nuke")
self.usermenu = menubar.addMenu("&{0}".format(self.preferences["MENU"]["nuke"]))
toolbar = nuke.toolbar("Nodes")
self.usertools = toolbar.addMenu(self.preferences["MENU"]["nodes"])
@staticmethod
def _add_gizmo_cmd(sub_menu, tool, tool_label):
tool_command = tool
sub_menu.addCommand(tool_label, "nuke.createNode('{}')".format(tool_command))
def _add_nk_cmd(self, sub_menu, key, tool, tool_label):
tool_command = "/".join([self.plugins_path, key, tool])
sub_menu.addCommand(tool_label, "nuke.loadToolset('{}')".format(tool_command))
def _add_py_cmd(self, key, tool, tool_path):
self.logger.debug("Not loaded python tool: {0}".format(tool_path))
def add_python_paths(self):
"""
Add the paths for python modules
"""
self.logger.info("Adding python paths.")
nuke.pluginAddPath(self.python_path)
self.logger.info("Added root path: {0}".format(self.python_path))
for mod in os.listdir(self.python_path):
if self.re_excludes.match(mod):
continue
mod_path = _convert2slash(os.path.join(self.python_path, mod))
if not os.path.isdir(mod_path):
continue
nuke.pluginAddPath(mod_path)
self.logger.info("Added python path: {0}".format(mod_path))
self.logger.info(
"Finished adding python paths from: {0}\n".format(self.python_path)
)
def add_tool_paths(self):
"""
Add the paths for all plugins.
"""
self.logger.info("Adding plugin paths.")
nuke.pluginAddPath(self.plugins_path)
self.logger.info("Added root path: {0}".format(self.plugins_path))
for key in self.tools_dict:
if key is None or key is u"":
continue
tool_path = self.plugins_path + "/" + key
if not os.path.isdir(tool_path):
continue
nuke.pluginAddPath(tool_path)
self.logger.info("Added tool path: {0}".format(tool_path))
self.logger.info(
"Finished adding tool paths from: {0}\n".format(self.plugins_path)
)
def create_tools_entry(self):
"""
Create the menu items for the found plugins in the file system
and based on the settings in the preferences.
"""
self._set_menus()
categories = sorted([key for key in self.tools_dict])
self.logger.info("Loading tools from: {0}".format(self.plugins_path))
for key in categories:
for tool in self.tools_dict[key]:
self.logger.info("Adding: {0} > {1}".format(key, tool))
tool_label = tool.split(".")[0]
# Create command entry for the gizmo
if tool.endswith(".gizmo"):
sub_menu = self.usermenu.addMenu(key, key)
sub_tools = self.usertools.addMenu(key, key)
self._add_gizmo_cmd(sub_menu, tool, tool_label)
self._add_gizmo_cmd(sub_tools, tool, tool_label)
# Create command entry for the nk templates
elif tool.endswith(".nk"):
sub_menu = self.usermenu.addMenu(key, key)
sub_tools = self.usertools.addMenu(key, key)
self._add_nk_cmd(sub_menu, key, tool, tool_label)
self._add_nk_cmd(sub_tools, key, tool, tool_label)
elif tool.endswith(".py"):
tool_path = _convert2slash(
os.path.join(self.plugins_path, key, tool)
)
if tool in ["init.py", "menu.py"]:
continue
if self.plugin_overrides and tool_path not in self.plugin_overrides:
continue
self._add_py_cmd(key, tool, tool_path)
self.logger.info("Finished loading tools from: {0}\n".format(self.plugins_path))
if __name__ == "__main__":
# The following code is for testing outside the normal setup
import codecs
import logging.config
LOGGING_CONFIG = os.path.join(
os.path.dirname(os.path.abspath(os.path.realpath(__current_file__))),
"config",
"logging" + CONFIG_TYPE,
)
with codecs.open(LOGGING_CONFIG, "r", encoding="utf-8") as fobj:
config = json.load(fobj)
logging.config.dictConfig(config["logging"])
loader = LoadPlugins()
loader.add_tool_paths()
loader.add_python_paths()