/
bdImportObj.py
148 lines (106 loc) · 3.78 KB
/
bdImportObj.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
import pymel.core as pm
import pymel.core.datatypes as dt
import re,os
import maya.OpenMaya as OM
import logging
import maya.OpenMayaUI as mui
import maya.OpenMaya as om
import sip,os, shiboken
import PySide.QtGui as QtGui
import PySide.QtCore as QtCore
import pyside_util
reload(pyside_util)
# #####################
TOOLS_PATH = os.path.dirname( __file__ )
WINDOW_TITLE = 'Import Objs Tool'
WINDOW_VERTION = 1.0
WINDOW_NAME = 'bdImportObjWin'
# ###################
UI_FILE_PATH = os.path.join( TOOLS_PATH, 'UI/bdImportObjUI.ui' )
UI_OBJECT, BASE_CLASS = pyside_util.get_pyside_class( UI_FILE_PATH )
pysideLoggers = ['pysideuic.properties','pysideuic.uiparser']
for log in pysideLoggers:
logger = logging.getLogger(log)
logger.setLevel(logging.ERROR)
def widgetPath(windowName, widgetNames):
"""
@param windowName: Window instance name to search
@param widgetNames: list of names to search for
taken from http://www.chris-g.net/2011/06/24/maya-qt-interfaces-in-a-class/
"""
returnDict = {}
mayaWidgetList = pm.lsUI(dumpWidgets=True)
for widget in widgetNames:
for mayaWidget in mayaWidgetList:
if windowName in mayaWidget:
if mayaWidget.endswith(widget):
returnDict[widget] = mayaWidget
return returnDict
def removeNamespace():
sceneNS = pm.namespaceInfo(lon=True,r=True)
importNS = []
mayaNS = set([u'UI', u'shared'])
for ns in sceneNS:
if ns not in mayaNS:
importNS.append(ns)
importNS.reverse()
for ns in importNS:
pm.namespace( rm = ns,mergeNamespaceWithRoot=True)
class ImportObjUI(BASE_CLASS, UI_OBJECT):
def __init__( self, parent = pyside_util.get_maya_window(), *args ):
super( ImportObjUI, self ).__init__( parent )
self.path = ''
self.setupUi( self )
self.setWindowTitle( '{0} {1}'.format( WINDOW_TITLE, str( WINDOW_VERTION ) ) )
self.objPathBtn.clicked.connect(self.bdGetObjPath)
self.importBtn.clicked.connect(self.bdImportObjs)
self.exportBtn.clicked.connect(self.bdExportObjs)
self.show()
def bdGetObjPath(self):
projectPath = pm.workspace.name
self.path = pm.fileDialog2(dir=projectPath,ds=2,fm=3,okc='Select Folder')[0]
if self.path:
self.objPath.setText(self.path)
self.bdPopulateFiles()
def bdPopulateFiles(self):
fileList = [f for f in sorted(os.listdir(self.path)) if f.endswith('.obj') ]
self.objFilesTableWidget.setColumnCount(1)
self.objFilesTableWidget.setRowCount(len(fileList))
for i in range(len(fileList)):
item = QtGui.QTableWidgetItem(fileList[i])
item.setFlags(~QtCore.Qt.ItemIsEditable)
self.objFilesTableWidget.setItem(i, 0, item)
def bdImportObjs(self):
selected = self.objFilesTableWidget.selectedItems()
print len(selected)
if len(selected) > 0 :
for item in selected:
objFile = item.text()
self.bdImportFile(objFile)
else:
for i in range(self.objFilesTableWidget.rowCount()):
objFile = self.objFilesTableWidget.item(i,0).text()
self.bdImportFile(objFile)
def bdImportFile(self,objFile):
print 'Importing'
objPath = os.path.join(self.path,objFile )
pm.importFile(objPath ,namespace= 'tempObj')
mesh = pm.ls('tempObj:*',type='mesh')[0].getParent()
print mesh
mesh.rename(objFile.split('.')[0])
removeNamespace()
def bdExportObjs(self):
print 'Exporting'
selection = pm.ls(sl=1)
for mesh in selection:
if mesh.getShapes()[0].type() == 'mesh':
print mesh.name()
pm.select(mesh)
command = 'file -force -options "groups=0;ptgroups=1;materials=0;smoothing=1;normals=1" -typ "OBJexport" -pr -es "' + os.path.join(self.path, mesh.name()+'.obj') + '";'
pm.mel.eval(command)
def bdMain():
UI_FILE_PATH = os.path.join( TOOLS_PATH, 'UI/bdImportObjUI.ui' )
UI_OBJECT, BASE_CLASS = pyside_util.get_pyside_class( UI_FILE_PATH )
if pm.window( WINDOW_NAME, exists = True, q = True ):
pm.deleteUI( WINDOW_NAME )
ImportObjUI()