-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport_images.py
122 lines (92 loc) · 3.1 KB
/
export_images.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
# ------------------------------------------------------------------------------
# Export Images from image manager
# ------------------------------------------------------------------------------
# export selected images from the image manager
# ** Mari >= 2.6 compatible **
#
# If the image has .format, it exports in the same format.
# otherwise it exports in .tga format.
# overwrites the file if it already exits.
#
# copy the script to the same location as your log folder in
# windows: C:\Users\[user_name]\Documents\Mari\Scripts
# linux: /home/[user_name]/Mari/Scripts
# Mac: /home/[Username]/Mari/Scripts
#
# Creates a menue item in Tools > Export Images
#
# @uthor sreenivas alapati (cg-cnu)
# ------------------------------------------------------------------------------
import mari
from PythonQt import QtGui, QtCore
import os
class ProgressDialog(QtGui.QDialog):
def __init__(self, maxStep):
super(ProgressDialog, self).__init__()
self.setWindowTitle('Exporting Images...')
self.cancelCpy = False
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
self.pbar = QtGui.QProgressBar(self)
self.pbar.setRange(0, maxStep)
self.pbar.setGeometry(30, 40, 200, 25)
self.pbar.connect("valueChanged (int)", self.status)
layout.addWidget(self.pbar)
self.cBtn = QtGui.QPushButton("cancel")
self.cBtn.connect('clicked()', lambda: self.cancelCopy())
layout.addWidget(self.cBtn)
def status(self):
if self.pbar.value == self.pbar.maximum:
self.close()
def cancelCopy(self):
self.pbar.value = self.pbar.maximum
self.cancelCpy = True
def exportSelImgs():
''' export the selected images to the given path '''
if not mari.projects.current():
mari.utils.message("No project currently open")
return
path = mari.utils.getExistingDirectory()
if not os.path.exists(path):
mari.utils.message("Not a valid path")
return
else:
path = path + '/'
images = mari.images.selected()
if len(images) == 0:
mari.utils.message("No images currently selected")
return
formats = [ str(i) for i in mari.images.supportedWriteFormats() ]
maxStep = len( images )
progressDiag = ProgressDialog(maxStep)
progressDiag.show()
progStep = 0
for image in images:
if progressDiag.cancelCpy == True:
progressDiag.close()
return
imageName = ( image.filePath() ).split("/")[-1]
try:
format = imageName.split(".")[-1]
# check if the format is valid...
if format in formats:
image.saveAs( path + imageName )
else:
format = ".tif"
image.saveAs( path + imageName + format)
except:
pass
progStep += 1
progressDiag.pbar.setValue(progStep)
# UI Integration
UI_path = 'MriImageManager/ItemContext'
script_menu_path = 'MainWindow/Scripts/Image Manager'
exportUVMask = mari.actions.create ('Export Selection', 'exportSelImgs()')
mari.menus.addAction(exportUVMask, UI_path)
mari.menus.addAction(exportUVMask, script_menu_path)
icon_filename = 'ExtractImage.png'
icon_path = mari.resources.path(mari.resources.ICONS) + '/' + icon_filename
exportUVMask.setIconPath(icon_path)
exportUVMask.setShortcut('')
### Menu Separators ###
mari.menus.addSeparator(UI_path,'Export Selection')