-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmax_tools.py
175 lines (138 loc) · 4.96 KB
/
max_tools.py
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
# -*- coding: utf-8 -*-
# Adam Thompson 2018
import os
import project_launcher
import importer
import saver
import software_tools
import MaxPlus
try:
# < Nuke 11
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
import PySide.QtGui as QtGuiWidgets
import PySide.QtUiTools as QtUiTools
except:
# >= Nuke 11
import PySide2.QtCore as QtCore
import PySide2.QtGui as QtGui
import PySide2.QtWidgets as QtGuiWidgets
import PySide2.QtUiTools as QtUiTools
SOFTWARE = "max"
WORKSPACE_FILE = "maxWorkspace.mxp"
class MaxTools(software_tools.SoftwareTools):
def __init__(self):
self.software = SOFTWARE
def debug_msg(self, msg):
print(msg)
def _save_as(self, path):
try:
MaxPlus.FileManager.Save(path)
return True
except:
return False
def _save(self):
try:
MaxPlus.FileManager.Save()
return True
except:
return False
def get_project_path(self):
return MaxPlus.FileManager.GetFileNameAndPath()
def is_project_modified(self):
return MaxPlus.FileManager.IsSaveRequired()
def set_environment(self, config_reader, template, token_dict):
""" Find the workspace file and implements it. """
# Find workspace file
filepath = self.find_env_file(config_reader.get_path(
template, token_dict), WORKSPACE_FILE)
path = os.path.dirname(filepath)
filepath = filepath.replace(os.path.sep, '/')
self.debug_msg("Trying to load this workspace: " + path)
if os.path.isfile(filepath):
self.debug_msg("Loading this workspace: " + path)
# Create maxscript to run to load project workspace
maxscript = ('pathConfig.load "' + filepath + '"')
print("Maxscript to run: " + maxscript)
MaxPlus.Core.EvalMAXScript(maxscript)
else:
self.debug_msg("That's not a valid workspace!")
class MaxProjectLauncher(project_launcher.ProjectLauncher):
def __init__(self):
self.max_tools = MaxTools()
super(MaxProjectLauncher, self).__init__(
QtGuiWidgets.QApplication.activeWindow(), self.max_tools)
def launchProject(self, filePath):
tokenDict = self.get_token_dict()
self.max_tools.debug_msg("Setting environment variables: ")
for token in tokenDict:
os.environ[token] = tokenDict[token]
self.max_tools.debug_msg(token + " = " + tokenDict[token])
# nuke.scriptOpen(filePath)
fm = MaxPlus.FileManager
fm.Open(filePath)
self.max_tools.set_environment(
self.configReader, self.template, self.get_token_dict())
return True
class MaxImporter(importer.Importer):
def __init__(self):
self.max_tools = MaxTools()
super(MaxImporter, self).__init__(
QtGuiWidgets.QApplication.activeWindow(), self.max_tools)
self.max_tools.debug_msg("Starting max importer...")
def import_file(self, file_path):
root, ext = os.path.splitext(file_path)
ext = ext.lower()
fm = MaxPlus.FileManager
try:
if ext == '.max':
fm.Merge(file_path)
else:
fm.Import(file_path)
return True
except:
return False
class MaxSaver(saver.Saver):
def __init__(self):
self.max_tools = MaxTools()
super(MaxSaver, self).__init__(
QtGuiWidgets.QApplication.activeWindow(), self.max_tools)
self.max_tools.debug_msg("Starting max saver...")
def save_file(self, file_path):
fm = MaxPlus.FileManager
try:
fm.Save(file_path)
self.max_tools.set_environment(
self.configReader, self.template, self.get_token_dict())
return True
except:
return False
def open_project_launcher():
MaxProjectLauncher()
def open_importer():
MaxImporter()
def open_saver():
MaxSaver()
def version_up():
MaxTools().version_up()
def publish():
MaxTools().publish()
def addMenu():
menu_name = u"Carbon Pipeline"
MaxPlus.MenuManager.UnregisterMenu(menu_name)
if not MaxPlus.MenuManager.MenuExists(menu_name):
mb = MaxPlus.MenuBuilder(menu_name)
mb.AddItem(MaxPlus.ActionFactory.Create(
'Do something', 'Launch Project', open_project_launcher))
mb.AddItem(MaxPlus.ActionFactory.Create(
'Do something', 'Import', open_importer))
mb.AddItem(MaxPlus.ActionFactory.Create('Do something', 'Save', open_saver))
mb.AddSeparator()
mb.AddItem(MaxPlus.ActionFactory.Create(
'Do something', 'Version Up', version_up))
mb.AddItem(MaxPlus.ActionFactory.Create(
'Do something', 'Publish', publish))
menu = mb.Create(MaxPlus.MenuManager.GetMainMenu())
print 'menu created', menu.Title
else:
print 'The menu ', menu_name, ' already exists'