-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.py
More file actions
459 lines (336 loc) · 14.8 KB
/
Copy pathinstall.py
File metadata and controls
459 lines (336 loc) · 14.8 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#-*- coding: utf-8 -*-
import os,sys
import inspect
import maya.cmds as cmds
import maya.mel as mel
def reload_pkg(pkg):
if (sys.version > "3"):
import importlib
importlib.reload(pkg)
else:
reload(pkg)
class Commands():
def __init__(self):
self.commands = []
def add_commands(self,new_command):
self.commands.append(new_command)
cmds_list = Commands()
class Shelf(object):
'''A simple class to build shelves in maya. Since the build method is empty,
it should be extended by the derived class to build the necessary shelf elements.
By default it creates an empty shelf called "customShelf".'''
def __init__(self, name="", iconPath=""):
self.name = name
self.iconPath = iconPath
self.labelBackground = (0, 0, 0, 0)
self.labelColour = (.9, .9, .9)
# Clean old shelf if it exists
self._cleanOldShelf()
# Delay shelf creation until Maya's UI is fully initialized
self._initializeShelf()
def _initializeShelf(self):
"""Initialize the shelf by creating a new tab and setting its parent."""
try:
self._addNewShelf()
# Set parent to the new shelf for further customization
cmds.setParent(self.name)
# Build the shelf contents
self.build()
except RuntimeError as e:
pass
def _addNewShelf(self):
"""Add a new shelf tab with the specified name."""
try:
main_shelf = mel.eval('global string $gShelfTopLevel;$tempMelVar=$gShelfTopLevel;')
cmds.shelfLayout(self.name, parent=main_shelf)
except RuntimeError as e:
print("Error: failed to add new shelf{}:{}".format(main_shelf,e))
def _null(self):
pass
def build(self):
'''This method should be overwritten in derived classes to actually build the shelf
elements. Otherwise, nothing is added to the shelf.'''
pass
def addButon(self, parent, label, icon="commandButton.png", command=_null, doubleCommand=_null, toolTip = ""):
'''Adds a shelf button with the specified label, command, double click command and image.'''
cmds.setParent(parent)
if icon:
icon = self.iconPath + '/' + icon
cmds.shelfButton(label, annotation=toolTip, width=32, height=32, image=icon, l=label, command=command, style = "iconOnly", dcc=doubleCommand, olb=self.labelBackground, olc=self.labelColour)
def addMenuItem(self, parent, label, command=_null, icon=""):
'''Adds a shelf button with the specified label, command, double click command and image.'''
if icon:
icon = self.iconPath + icon
return cmds.menuItem(p=parent, l=label, c=command, i="")
def addSubMenu(self, parent, label, icon=None):
'''Adds a sub menu item with the specified label and icon to the specified parent popup menu.'''
if icon:
icon = self.iconPath + icon
return cmds.menuItem(p=parent, l=label, i=icon, subMenu=1)
def _cleanOldShelf(self):
try:
'''Checks if the shelf exists and empties it if it does or creates it if it does not.'''
if cmds.shelfLayout(self.name, ex=1):
cmds.deleteUI(self.name, layout=True)
except Exception as e:
# 捕获所有异常,但不提示用户
print("Error cleaning old shelf: {}".format(e))
###################################################################################
'''This is an example shelf.'''
class MX_ToolPackShelf(Shelf):
def __init__(self, name="", iconPath=""):
super(MX_ToolPackShelf,self).__init__(name, iconPath)
def build(self):
self.addButon(parent=self.name, label="common tool", command = cmds_list.commands[0], icon="common.png", toolTip="common tool")
self.addButon(parent=self.name, label="asset tool", command = cmds_list.commands[1], icon="asset.png", toolTip="asset tool")
self.addButon(parent=self.name, label="rigging tool", command = cmds_list.commands[2], icon="rigging.png", toolTip="rigging tool")
self.addButon(parent=self.name, label="animation tool", command = cmds_list.commands[3], icon="animation.png", toolTip="animation tool")
self.addButon(parent=self.name, label="lighting tool", command = cmds_list.commands[4], icon="lighting.png", toolTip="lighting tool")
self.addButon(parent=self.name, label="render tool", command = cmds_list.commands[5], icon="render.png", toolTip="render tool")
self.addButon(parent=self.name, label="ai", command = cmds_list.commands[6], icon="ai.png", toolTip="ai tool")
self.addButon(parent=self.name, label="settings", command = cmds_list.commands[7], icon="settings.png", toolTip="settings")
'''
self.addButon("popup")
p = cmds.popupMenu(b=1)
self.addMenuItem(p, "popupMenuItem1")
self.addMenuItem(p, "popupMenuItem2")
sub = self.addSubMenu(p, "subMenuLevel1")
self.addMenuItem(sub, "subMenuLevel1Item1")
sub2 = self.addSubMenu(sub, "subMenuLevel2")
self.addMenuItem(sub2, "subMenuLevel2Item1")
self.addMenuItem(sub2, "subMenuLevel2Item2")
self.addMenuItem(sub, "subMenuLevel1Item2")
self.addMenuItem(p, "popupMenuItem3")
self.addButon("button3")
'''
def create_dock_shelf(self):
dock_name = "MXToolPackDock"
# 删除已存在的UI
if cmds.dockControl(dock_name, ex=True):
cmds.deleteUI(dock_name, control=True)
# 1. 创建 `paneLayout` 来限制宽度
main_pane = cmds.paneLayout(configuration='single', width=40, parent="ShelfLayout")
# 2. 在 `paneLayout` 里面创建 `columnLayout`
main_shelf = cmds.columnLayout(parent=main_pane, adjustableColumn=True)
# 3. 创建 `dockControl`,并附加 `paneLayout`
cmds.dockControl(
dock_name,
area='right',
content=main_pane,
allowedArea='all',
floating=False,
label="",
width=40, # 这里的宽度不会直接生效
fixedWidth=True, #必须加上fixwidth参数
ret=True
)
def add_Button():
self.addButon(parent=main_shelf, label="common tool", command = cmds_list.commands[0],icon="common.png", toolTip="common tool")
self.addButon(parent=main_shelf, label="asset tool", command = cmds_list.commands[1],icon="asset.png", toolTip="asset tool")
self.addButon(parent=main_shelf, label="rigging tool", command = cmds_list.commands[2],icon="rigging.png", toolTip="rigging tool")
self.addButon(parent=main_shelf, label="animation tool", command = cmds_list.commands[3],icon="animation.png", toolTip="animation tool")
self.addButon(parent=main_shelf, label="lighting tool", command = cmds_list.commands[4],icon="lighting.png", toolTip="lighting tool")
self.addButon(parent=main_shelf, label="render tool", command = cmds_list.commands[5],icon="render.png", toolTip="render tool")
self.addButon(parent=main_shelf, label="ai", command = cmds_list.commands[6],icon="ai.png", toolTip="ai tool")
cmds.evalDeferred(add_Button)
##################################################################################
def create_mx_shelf(root_path,icon_path):
#========== button1 ==========#
#the common button command
command1 = "root_path = r\"%s\"\n"%root_path
command1 += """
import os,sys
sys.path.append("%s"%root_path)
common_path = os.path.join(root_path,'common')
import unload_pkgs
unload_pkgs.unload_packages(True, ['common'])
import common
import common.mx_common_tools
common_tools = common.mx_common_tools.MX_CommonTools("%s"%common_path)
common_tools.initUI()
"""
#remove the indent
command1 = inspect.cleandoc(command1)
cmds_list.add_commands(command1)
#========== button2 ==========#
#the asset button command
command2 = "root_path = r\"%s\"\n"%root_path
command2 += """
import os,sys
sys.path.append("%s"%root_path)
asset_path = os.path.join(root_path,'asset')
import unload_pkgs
unload_pkgs.unload_packages(True, ['asset'])
import asset
import asset.mx_asset_tools
asset_tools = asset.mx_asset_tools.MX_AssetTools("%s"%asset_path)
asset_tools.initUI()
"""
#remove the indent
command2 = inspect.cleandoc(command2)
cmds_list.add_commands(command2)
#========== button3 ==========#
#the rig button command
command3 = "root_path = r\"%s\"\n"%root_path
command3 += """
import os,sys
sys.path.append("%s"%root_path)
rig_path = os.path.join(root_path,'rig')
import unload_pkgs
unload_pkgs.unload_packages(True, ['rig'])
import rig
import rig.mx_rig_tools
rig_tools = rig.mx_rig_tools.MX_RigTools("%s"%rig_path)
rig_tools.initUI()
"""
#remove the indent
command3 = inspect.cleandoc(command3)
cmds_list.add_commands(command3)
#========== button4 ==========#
#the anim button command
command4 = "root_path = r\"%s\"\n"%root_path
command4 += """
import os,sys
sys.path.append("%s"%root_path)
anim_path = os.path.join(root_path,'anim')
import unload_pkgs
unload_pkgs.unload_packages(True, ['anim'])
import anim
import anim.mx_anim_tools
anim_tools = anim.mx_anim_tools.MX_AnimTools("%s"%anim_path)
anim_tools.initUI()
"""
#remove the indent
command4 = inspect.cleandoc(command4)
cmds_list.add_commands(command4)
#========== button5 ==========#
#the light button command
command5 = "root_path = r\"%s\"\n"%root_path
command5 += """
import os,sys
sys.path.append("%s"%root_path)
light_path = os.path.join(root_path,'light')
import unload_pkgs
unload_pkgs.unload_packages(True, ['light'])
import light
import light.mx_light_tools
light_tools = light.mx_light_tools.MX_LightTools("%s"%light_path)
light_tools.initUI()
"""
#remove the indent
command5 = inspect.cleandoc(command5)
cmds_list.add_commands(command5)
#========== button6 ==========#
#the render button command
command6 = "root_path = r\"%s\"\n"%root_path
command6 += """
import os,sys
sys.path.append("%s"%root_path)
render_path = os.path.join(root_path,'render')
import unload_pkgs
unload_pkgs.unload_packages(True, ['render'])
import render
import render.mx_render_tools
render_tools = render.mx_render_tools.MX_RenderTools("%s"%render_path)
render_tools.initUI()
"""
#remove the indent
command6 = inspect.cleandoc(command6)
cmds_list.add_commands(command6)
#========== button7 ==========#
#the settings button command
command7 = "root_path = r\"%s\"\n"%root_path
command7 += """
import os,sys
sys.path.append("%s"%root_path)
ai_path = os.path.join(root_path,'ai')
import unload_pkgs
unload_pkgs.unload_packages(True, ['ai'])
import ai
import ai.mx_ai_tools
ai_tools = ai.mx_ai_tools.MX_AITools("%s"%ai_path)
ai_tools.initUI()
"""
#remove the indent
command7 = inspect.cleandoc(command7)
cmds_list.add_commands(command7)
#========== button7 ==========#
#the settings button command
command8 = "root_path = r\"%s\"\n"%root_path
command8 += """
import os,sys
sys.path.append("%s"%root_path)
settings_path = os.path.join(root_path,'settings')
import unload_pkgs
unload_pkgs.unload_packages(True, ['settings'])
import settings
import settings.mx_settings
settings = settings.mx_settings.MX_Settings("%s"%settings_path)
settings.initUI()
"""
#remove the indent
command8 = inspect.cleandoc(command8)
cmds_list.add_commands(command8)
global mx_toolpack_instance
mx_toolpack_instance = MX_ToolPackShelf(name="mx_toolpack", iconPath=icon_path)
# 为“dock”按钮绑定实例方法
cmds.shelfButton(parent=mx_toolpack_instance.name,
label="dock",
command=lambda *args: mx_toolpack_instance.create_dock_shelf(),
sourceType="Python",
image=os.path.join(icon_path, "dock.png"),
annotation="dock")
#启动自动添加
mx_toolpack_instance.create_dock_shelf()
return mx_toolpack_instance
def onMayaDroppedPythonFile(*args):
import unload_pkgs
unload_pkgs.unload_packages(packages=["install"])
import install
reload_pkg(install)
root_path = ''
icon_path = ''
if (sys.version > "3"):
import pathlib
root_path = pathlib.Path(__file__).parent
sys.path.append(root_path) #install this mod to maya
else:
root_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(root_path) #install this mod to maya
icon_path = os.path.join(root_path,"icons")
# write bifrost to maya.env
# 使用inspect.cleandoc去掉多余的缩进和不必要的换行
bifrost_conf = inspect.cleandoc("""
BIFROST_LIB_CONFIG_FILES={}/third-party/Bifrost/rebel_pack_0.4.1/bifrost_lib_config.json;{}/third-party/Bifrost/mx_pack/bifrost_lib_config.json
""".format(root_path, root_path, root_path))
ver = cmds.about(version=True)
# Get the Maya preferences directory
prefs_dir = os.path.expanduser("~/maya")
# Look for the maya.env file in the preferences directory
env_file = os.path.join(prefs_dir + "/" + ver + "/maya.env")
print(env_file)
# 以读写模式打开文件
with open(env_file,'r+') as file:
conf = file.read()
# 检查是否已经包含了需要添加的配置
if bifrost_conf in conf:
print("Bifrost configuration already exists in the file. No changes made.")
else:
# 配置不存在,追加到文件末尾
file.seek(0, 2) # 移动文件指针到文件末尾
file.write("\n" + bifrost_conf) # 在文件末尾添加配置,并确保前面有换行
print("Bifrost configuration added to the file.")
#creating mod file
mod_file = os.path.join(prefs_dir, 'modules', 'mx_toolpack.mod')
# 检查文件是否存在
if os.path.exists(mod_file):
os.remove(mod_file)
# 创建新的 mx_toolpack.mod 文件
mod_content = """+ Mx Toolpack 1.0 {}\r\nscripts: {}""".format(root_path,root_path)
try:
# 写入 .mod 文件
with open(mod_file, "w") as file:
file.write(mod_content)
except Exception as e:
print("error creating mod file")
create_mx_shelf(root_path,icon_path)