Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymel.core as pm
- from PySide2 import QtCore
- from PySide2 import QtWidgets
- def export():
- if (inputField.toPlainText()):
- vOffset = 0
- vTOffset = 0
- vNOffset = 0
- startVOffset = 0
- startVTOffset = 0
- startVNOffset = 0
- selection = optExpSelect.isChecked()
- triangulate = optTriangulate.isChecked()
- if(optLoc.isChecked()):
- space = 'object'
- else:
- space = 'world'
- dupList = []
- nrOfObjs = 0
- file1 = open(inputField.toPlainText(), "w")
- if selection == True:
- nodeList = pm.ls(sl=True)
- else:
- nodeList = pm.ls(type=pm.nodetypes.Mesh)
- for myNode in nodeList:
- if(optExpMat.isChecked()):
- file2 = open(inputField.toPlainText().replace('.obj', '.mtl'),"w")
- print s
- file2.write("newmtl " + s + "\n")
- file2.write("illum 4\n")
- sGroup = s.listConnections(type=('lambert', 'blinn'))
- matColor = sGroup.getColor() # Kd
- matAmbient = sGroup.getAmbientColor() # Ka
- file2.write("Kd " + str(matColor[0]) + " "
- + str(matColor[1]) + " " + str(matColor[2]) + "\r\n")
- file2.write("Ka " + str(matAmbient[0]) + " "
- + str(matAmbient[1]) + " " + str(matAmbient[2]) + "\r\n")
- if (u'blinn' in sGroup):
- matSpec = sGroup.getSpecularColor() # Ks
- file2.write("Ks " + str(matSpec[0]) + " "
- + str(matSpec[1]) + " " + str(matSpec[2]) + "\r\n")
- file2.close()
- #fileNode = sInfo[0].connections(type='file')
- #textureFile = pm.getAttr(fileNode[0].fileTextureName)
- #print "This is the file ", str(textureFile)
- if triangulate == True:
- pm.select(myNode)
- pm.duplicate()
- myNode = pm.ls(sl=True)[0]
- pm.polyTriangulate()
- VTU, VTV = myNode.getUVs() # Listor med U och V
- VTUV = zip(VTU, VTV) # Lista med UV
- for point in myNode.vtx:
- sv = ('v ' + str(point.getPosition(space)[0]) + ' ' + str(point.getPosition(space)[1]) + ' ' + str(point.getPosition(space)[2]) + "\n") # Skapa vertex-strängen som ska skrivas ut
- file1.write(sv)
- vOffset+=1
- for uv in VTUV:
- file1.write('vt ')
- for element in uv:
- file1.write(str(element) + ' ') # Skapa UV-strängen som ska skrivas ut
- file1.write("\n")
- vTOffset+=1
- for nm in myNode.getNormals(space):
- sn = 'vn '
- sn += (str(nm[0]) + ' ' + str(nm[1]) + ' ' + str(nm[2]) + "\n") # Skapa Normal-strängen som ska skrivas ut
- file1.write(sn)
- vNOffset+=1
- file1.write('g cube'+ str(nrOfObjs) + '\n')
- for face in myNode.faces:
- sf = 'f '
- for nrOf, verts in enumerate(face.getVertices()): # Enumerate genererar en automatisk räknare, den kan användas som index
- sf += str(verts+1+startVOffset) + '/' + str(face.getUVIndex(nrOf)+1+startVTOffset) + '/' + str(face.normalIndex(nrOf)+1+startVNOffset) + ' '# Skapa face-strängen som ska skrivas ut
- sf += '\n'
- file1.write(sf)
- startVOffset = vOffset
- startVTOffset = vTOffset
- startVNOffset = vNOffset
- nrOfObjs+=1
- if triangulate == True:
- pm.delete()
- else:
- print "No file directory"
- def saveFile():
- filePath = pm.fileDialog2(fileFilter="OBJ Files (*.obj)")[0]
- inputField.setText(filePath)
- print filePath
- return filePath
- # UI
- width = 400
- height = 350
- wid = QtWidgets.QWidget()
- wid.resize(width, height)
- wid.setWindowTitle("Dave's OBJ Exporter")
- basOpt = QtWidgets.QGroupBox("Basic Options", wid)
- basOpt.resize(width/2, height/2)
- basOpt.move(width/4, height/4)
- filePathLabel = QtWidgets.QLabel("Filepath", wid)
- filePathLabel.move(50, 20)
- inputField = QtWidgets.QTextEdit()
- inputField.resize(200, 25)
- inputField.setParent(wid)
- inputField.move(100, 20)
- browseBtn = QtWidgets.QPushButton("Browse", wid)
- browseBtn.resize(50, 25)
- browseBtn.move(310, 20)
- browseBtn.clicked.connect(saveFile)
- optTriangulate = QtWidgets.QCheckBox("Triangulate", basOpt)
- optTriangulate.move(30, 30)
- optExpSelect = QtWidgets.QCheckBox("Export Selection", basOpt)
- optExpSelect.move(30, 50)
- optExpMat = QtWidgets.QCheckBox("Export Material", basOpt)
- optExpMat.move(30, 70)
- spaceOpt = QtWidgets.QLabel("Space : ", basOpt)
- spaceOpt.move(30, 100)
- optLoc = QtWidgets.QRadioButton("Local", basOpt)
- optLoc.setChecked(True)
- optLoc.move(80, 100)
- optGlob = QtWidgets.QRadioButton("Global", basOpt)
- optGlob.move(140, 100)
- expBtn = QtWidgets.QPushButton("Export",wid)
- expBtn.resize(width, height/6)
- expBtn.move(0, height-height/6)
- expBtn.clicked.connect(export)
- wid.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement