-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
86 lines (63 loc) · 3.36 KB
/
Copy pathmenu.py
File metadata and controls
86 lines (63 loc) · 3.36 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
import nuke, os
import importlib.util
class colors:
RESET = '\033[0m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ITALIC = '\x1B[3m'
print(colors.BOLD, colors.UNDERLINE)
print(' MassieVFX ')
print(colors.RESET)
# Get the path to the plugins directory where menu.py is located
plugins_directory = os.path.dirname(__file__)
mvfx_icon_folder = os.path.join(plugins_directory, 'icons')
# Path to the tools directory relative to the plugins directory
tools_directory = os.path.join(plugins_directory, 'tools')
nuke.pluginAddPath('./tools')
nuke.pluginAddPath('./icons')
# Create parent menu under 'Nodes' menu
mvfx_menu = nuke.menu('Nodes').addMenu('MassieVFX', icon = os.path.join(mvfx_icon_folder, 'mvfx.png'))
def import_module_from_path(module_path, module_name):
'''
The proper way to execute python scripts.
This will only execute the <main> function inside of a .py file
'''
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
module.main() # Execute the main function
return module
def add_tools_to_menu(menu, directory):
'''
Scans all the given folder and makes a menu out of it
'''
for dir_name in os.listdir(directory):
dir_path = os.path.join(directory, dir_name)
if dir_name != '__pycache__': # to avoid having __pycache__ files and folders.
if os.path.isdir(dir_path): # if the folder exists, scan it
submenu = menu.addMenu(dir_name, icon = dir_name+'.png') # Assign submenu if it's a directory
print(f'{colors.BLUE}{colors.BOLD} Submenu found: {dir_name}{colors.RESET}')
add_tools_to_menu(submenu, dir_path) # run the funciton recursivly for the new folders that might be found
else:
icon = dir_name[:-3]+'.png' # Define default icon
if dir_name.endswith('.nk'):# if its a .nk file, then its a tool, so have it ready for import
if os.path.exists(os.path.join(mvfx_icon_folder,icon)) == False: # if the icon doesnt exist, replace it with desired image
icon = 'nuke_script.png'
menu.addCommand(dir_name[:-3], lambda file_path=dir_path: nuke.nodePaste(file_path), icon = icon)
elif dir_name.endswith('.py'): # if its a pure python script
if os.path.exists(os.path.join(mvfx_icon_folder,icon)) == False: # if the icon doesnt exist, replace it with desired image
icon = 'python_script.png'
menu.addCommand(dir_name, lambda file_path=[dir_path, dir_name]: import_module_from_path(file_path[0], file_path[1]), icon = icon)
print(f'{colors.GREEN}{colors.ITALIC} ADDED: {dir_path.replace(tools_directory+os.sep,"")+colors.WHITE}\n Icon: {icon}{colors.RESET}')
# Add tools to the menu recursively
add_tools_to_menu(mvfx_menu, tools_directory)
print(colors.BOLD)
print('---> Setup Successful !')
print(colors.RESET)