-
Notifications
You must be signed in to change notification settings - Fork 1
/
maya_tools.py
283 lines (225 loc) · 9.37 KB
/
maya_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
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
# -*- coding: utf-8 -*-
# Adam Thompson 2018
import os
import importer
import project_launcher
import saver
import publisher
import environment
import maya_hooks
import software_tools
import maya.cmds as cmds
import maya.mel as mel
import pymel.core as pm
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 = "maya"
WORKSPACE_FILE = "workspace.mel"
from pipeline_config import TEMP_FILE_SUFFIX
def addMenu():
print("Loading Pipeline...")
# Name of the global variable for the Maya window
MainMayaWindow = pm.language.melGlobals['gMainWindow']
# Build a menu and parent underthe Maya Window
customMenu = pm.menu('Carbon Pipeline', parent=MainMayaWindow)
# Build a menu item and parent under the 'customMenu'
pm.menuItem(label="Launch Project",
command="pipeline.maya_tools.MayaProjectLauncher()", parent=customMenu)
pm.menuItem(label="Import",
command="pipeline.maya_tools.MayaImporter()", parent=customMenu)
pm.menuItem(label="Save",
command="pipeline.maya_tools.MayaSaver()", parent=customMenu)
pm.menuItem(divider=True, parent=customMenu)
pm.menuItem(label="Version Up",
command="pipeline.maya_tools.MayaTools().version_up()", parent=customMenu)
pm.menuItem(label="Create Temporary Copy",
command="pipeline.maya_tools.MayaTools().open_temp_file()", parent=customMenu)
pm.menuItem(label="Publish",
command="pipeline.maya_tools.MayaTools().publish()", parent=customMenu)
class MayaTools(software_tools.SoftwareTools):
def __init__(self):
self.software = SOFTWARE
def debug_msg(self, msg):
print(msg)
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)
self.debug_msg("Trying to load this workspace: " + path)
if os.path.isfile(filepath):
self.debug_msg("Loading this workspace: " + path)
cmds.workspace(path, openWorkspace=True)
else:
self.debug_msg("That's not a valid workspace!")
def _save_as(self, path):
try:
cmds.file(rename=path)
cmds.file(save=True)
return True
except:
return False
def _save(self):
try:
cmds.file(save=True)
return True
except:
raise
return False
def get_project_path(self):
return pm.system.sceneName()
def is_project_modified(self):
return cmds.file(q=True, modified=True)
def open_project(self, path, force=True):
try:
cmds.file(path, open=True, force=True)
return True
except:
return False
def open_temp_file(self):
project_path = self.get_project_path()
if self.is_project_modified():
if self.file_not_saved_dlg():
self._save()
else:
return ''
# Get current project name
project_name, ext = os.path.splitext(os.path.basename(project_path))
temp_project_name = project_name + TEMP_FILE_SUFFIX + ext
# Check if it's already a temporary file
if project_name.endswith(TEMP_FILE_SUFFIX):
raise ValueError("This is already a temp file!")
self.save_project_as(
os.path.join(os.path.dirname(project_path), temp_project_name))
class MayaImporter(importer.Importer, MayaTools):
def __init__(self):
self.maya_tools = MayaTools()
super(MayaImporter, self).__init__(
QtGuiWidgets.QApplication.activeWindow(), self.maya_tools)
self.maya_tools.debug_msg("Starting maya importer...")
def import_file(self, file_path):
import_dialog = ImportDialog(QtGuiWidgets.QApplication.activeWindow(), file_path).exec_()
# Returns true or false based on the import dialog
return import_dialog
class ImportDialog(QtGuiWidgets.QDialog):
def __init__(self, activeWindow, file_path):
self.maya_tools = MayaTools()
super(ImportDialog, self).__init__(activeWindow)
self.file_path = file_path
self.file_name, self.file_ext = os.path.splitext(os.path.basename(file_path))
self.initUI()
self.setModal(True)
self.show()
def initUI(self):
self.setWindowTitle("Import Dialog")
self.ok_button = QtGuiWidgets.QPushButton("OK")
self.ok_button.clicked.connect(self.on_ok)
self.cancel_button = QtGuiWidgets.QPushButton("Cancel")
self.cancel_button.clicked.connect(self.reject)
self.ref_checkbox = QtGuiWidgets.QCheckBox()
self.ref_checkbox.setChecked(True)
self.use_namespace_checkbox = QtGuiWidgets.QCheckBox()
self.use_namespace_checkbox.setChecked(True)
self.use_namespace_checkbox.stateChanged.connect(self.on_namespace_checkbox_change)
self.namespace_lineedit = QtGuiWidgets.QLineEdit(self.file_name)
reference_form = QtGuiWidgets.QFormLayout()
reference_form.addRow("Reference", self.ref_checkbox)
reference_group = QtGuiWidgets.QGroupBox("Reference")
reference_group.setLayout(reference_form)
namespace_form = QtGuiWidgets.QFormLayout()
namespace_form.addRow("Use Namespace", self.use_namespace_checkbox)
namespace_form.addRow("Namespace", self.namespace_lineedit)
namespace_group = QtGuiWidgets.QGroupBox("Namespace")
namespace_group.setLayout(namespace_form)
hbox = QtGuiWidgets.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(self.ok_button)
hbox.addWidget(self.cancel_button)
vbox = QtGuiWidgets.QVBoxLayout()
vbox.addWidget(reference_group)
vbox.addWidget(namespace_group)
vbox.addStretch(1)
vbox.addLayout(hbox)
self.setLayout(vbox)
def on_ok(self):
flags = self.get_flags()
print("flags: " + str(flags))
cmds.file(self.file_path, **flags)
self.accept()
def on_namespace_checkbox_change(self, state):
self.namespace_lineedit.setEnabled(state)
def get_flags(self):
""" Returns a dictionary of flags based on what is currently selected in the dialog. """
flags = dict()
if self.ref_checkbox.isChecked():
flags["reference"] = True
else:
flags["i"] = True
if self.use_namespace_checkbox.isChecked():
flags["namespace"] = self.namespace_lineedit.text()
return flags
class MayaProjectLauncher(project_launcher.ProjectLauncher):
def __init__(self):
self.maya_tools = MayaTools()
super(MayaProjectLauncher, self).__init__(
QtGuiWidgets.QApplication.activeWindow(), self.maya_tools)
self.maya_tools.debug_msg("Starting maya project launcher...")
def launchProject(self, filePath):
tokenDict = self.get_token_dict()
try:
cmds.file(filePath, o=True)
self.maya_tools.set_environment(
self.configReader, self.template, self.get_token_dict())
return True
except RuntimeError:
ret = self.fileNotSavedDlg()
if ret == QtGuiWidgets.QMessageBox.Save:
cmds.file(save=True)
cmds.file(filePath, o=True)
self.maya_tools.set_environment(
self.configReader, self.template, self.get_token_dict())
return True
elif ret == QtGuiWidgets.QMessageBox.Discard:
cmds.file(new=True, force=True)
cmds.file(filePath, open=True)
self.maya_tools.set_environment(
self.configReader, self.template, self.get_token_dict())
return True
elif ret == QtGuiWidgets.QMessageBox.Cancel:
self.maya_tools.debug_msg("Nevermind...")
return False
def fileNotSavedDlg(self):
msgBox = QtGuiWidgets.QMessageBox()
msgBox.setText("The document has been modified.")
msgBox.setInformativeText("Do you want to save your changes?")
msgBox.setStandardButtons(QtGuiWidgets.QMessageBox.Save |
QtGuiWidgets.QMessageBox.Discard |
QtGuiWidgets.QMessageBox.Cancel)
msgBox.setDefaultButton(QtGuiWidgets.QMessageBox.Save)
ret = msgBox.exec_()
return ret
class MayaSaver(saver.Saver):
def __init__(self):
self.maya_tools = MayaTools()
super(MayaSaver, self).__init__(QtGuiWidgets.QApplication.activeWindow(), self.maya_tools)
self.maya_tools.debug_msg("Starting maya saver...")
def save_file(self, file_path):
try:
cmds.file( rename=file_path)
cmds.file(save=True)
self.maya_tools.set_environment(
self.configReader, self.template, self.get_token_dict())
return True
except:
raise
return False