from maya import cmds, mel
'''
OBJ Exporter
Instructions: select object(s) to export and click EXPORT OBJ
[Contact Info]
'''
#UI
#This forces a single instance of window
if cmds.window('w_obj_exp', ex=True):
cmds.deleteUI('w_obj_exp', window=True)
#This creates a new window
cmds.window('w_obj_exp', t='OBJ Exporter', s=False)
#Add a layout for window
cmds.columnLayout(rs=10, adj=True)
#Add a separator to help organize the window (optional)
cmds.separator()
#Add instructions with Text command
cmds.text('Instructions: select object(s) to export and click EXPORT OBJ')
cmds.separator()
#Create a Text Field with a Button that will be used to browse to the desired export path
cmds.textFieldButtonGrp('tfbg_obj_browse', l='Export Path:', pht='Load Directory', bl='Load Path', bc='go_obj_path()')
cmds.separator()
#Create a button that will export our selected objects as .OBJs
cmds.button(l='EXPORT OBJ', h=40, bgc=[0.2, 0.5, 0.3], c='go_obj_export()')
#Show the window
cmds.showWindow('w_obj_exp')
#Edit the window's Width and Height
cmds.window('w_obj_exp', e=True, wh=[500, 140])
#Functions
#Define a custom function
def go_obj_path():
#Create a variable to store the desired directory
path = cmds.fileDialog2(fm=2, cc='Cancel', okc='GO')
if path:
#"path[0]" stores the directory alone
path = path[0]
#Edit the Text Field to include the path
cmds.textFieldButtonGrp('tfbg_obj_browse', e=True, tx=path)
#If a path is not chosen, print "Cancelled"
else:
print('Cancelled')
def endProcess():
#Check if preogress of export is cancelled
interrupt = cmds.progressWindow(q=True, isCancelled=True)
#If cancelled, make sure "endProgress()" will return the command to interrupt the exporting progress
if interrupt:
cmds.progressWindow(endProgress=True)
return interrupt
def go_obj_export():
#Grab export directory from the Text Field
path = cmds.textFieldButtonGrp('tfbg_obj_browse', q=True, tx=True)
#Store objects in selection
sel = cmds.ls(sl=True, type='transform')
#Tell the user the export process has started
print('Export started!')
#Create the Progress Bar that can be cancelled
cmds.progressWindow(step=1, status='Exporting...', isInterruptable=True)
#Store amount of objects in selection
size = len(sel)
#Create a variable that will be used to increment the Progress Bar
iter = 100/size
#Iterate through each object in selection with For Loop
for each in sel:
#This ends the export process using the Escape key
if endProcess():
return
#Increment the Progress Bar using the amount of objects in selection
cmds.progressWindow(e=True, step=iter, status='Exporting...')
#Select the objects to export
cmds.select(each, r=True)
#Export selected objects as OBJ files
mel.eval('file -force -options "groups=1;ptgroups=1;materials=1;smoothing=1;normals=1" -typ "OBJexport" -pr -es "' + path + '/' + each + '.obj";')
#Close the Progress Window
cmds.progressWindow(endProgress=True)
#Tell the user the export process has ended
print('Export completed!')
#Added a Heads Up Message to remind the user the export process as ended
cmds.headsUpMessage('Export completed!')