Guest User

Untitled

a guest
Oct 21st, 2019
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pymel.core as pm
  2. from PySide2 import QtCore
  3. from PySide2 import QtWidgets
  4.  
  5.  
  6. def export():
  7. if (inputField.toPlainText()):
  8. vOffset = 0
  9. vTOffset = 0
  10. vNOffset = 0
  11. startVOffset = 0
  12. startVTOffset = 0
  13. startVNOffset = 0
  14. selection = optExpSelect.isChecked()
  15. triangulate = optTriangulate.isChecked()
  16.  
  17. if(optLoc.isChecked()):
  18. space = 'object'
  19. else:
  20. space = 'world'
  21.  
  22. dupList = []
  23. nrOfObjs = 0
  24. file1 = open(inputField.toPlainText(), "w")
  25.  
  26. if selection == True:
  27. nodeList = pm.ls(sl=True)
  28. else:
  29. nodeList = pm.ls(type=pm.nodetypes.Mesh)
  30.  
  31. for myNode in nodeList:
  32.  
  33. if(optExpMat.isChecked()):
  34. file2 = open(inputField.toPlainText().replace('.obj', '.mtl'),"w")
  35.  
  36. print s
  37. file2.write("newmtl " + s + "\n")
  38. file2.write("illum 4\n")
  39.  
  40. sGroup = s.listConnections(type=('lambert', 'blinn'))
  41. matColor = sGroup.getColor() # Kd
  42. matAmbient = sGroup.getAmbientColor() # Ka
  43.  
  44. file2.write("Kd " + str(matColor[0]) + " "
  45. + str(matColor[1]) + " " + str(matColor[2]) + "\r\n")
  46. file2.write("Ka " + str(matAmbient[0]) + " "
  47. + str(matAmbient[1]) + " " + str(matAmbient[2]) + "\r\n")
  48.  
  49.  
  50. if (u'blinn' in sGroup):
  51. matSpec = sGroup.getSpecularColor() # Ks
  52. file2.write("Ks " + str(matSpec[0]) + " "
  53. + str(matSpec[1]) + " " + str(matSpec[2]) + "\r\n")
  54.  
  55. file2.close()
  56. #fileNode = sInfo[0].connections(type='file')
  57. #textureFile = pm.getAttr(fileNode[0].fileTextureName)
  58. #print "This is the file ", str(textureFile)
  59.  
  60.  
  61. if triangulate == True:
  62. pm.select(myNode)
  63. pm.duplicate()
  64. myNode = pm.ls(sl=True)[0]
  65. pm.polyTriangulate()
  66.  
  67. VTU, VTV = myNode.getUVs() # Listor med U och V
  68. VTUV = zip(VTU, VTV) # Lista med UV
  69. for point in myNode.vtx:
  70. 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
  71. file1.write(sv)
  72. vOffset+=1
  73.  
  74. for uv in VTUV:
  75. file1.write('vt ')
  76. for element in uv:
  77. file1.write(str(element) + ' ') # Skapa UV-strängen som ska skrivas ut
  78. file1.write("\n")
  79. vTOffset+=1
  80.  
  81. for nm in myNode.getNormals(space):
  82. sn = 'vn '
  83. sn += (str(nm[0]) + ' ' + str(nm[1]) + ' ' + str(nm[2]) + "\n") # Skapa Normal-strängen som ska skrivas ut
  84. file1.write(sn)
  85. vNOffset+=1
  86.  
  87. file1.write('g cube'+ str(nrOfObjs) + '\n')
  88.  
  89. for face in myNode.faces:
  90. sf = 'f '
  91. for nrOf, verts in enumerate(face.getVertices()): # Enumerate genererar en automatisk räknare, den kan användas som index
  92. 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
  93. sf += '\n'
  94. file1.write(sf)
  95. startVOffset = vOffset
  96. startVTOffset = vTOffset
  97. startVNOffset = vNOffset
  98. nrOfObjs+=1
  99. if triangulate == True:
  100. pm.delete()
  101. else:
  102. print "No file directory"
  103.  
  104. def saveFile():
  105. filePath = pm.fileDialog2(fileFilter="OBJ Files (*.obj)")[0]
  106. inputField.setText(filePath)
  107. print filePath
  108. return filePath
  109.  
  110. # UI
  111. width = 400
  112. height = 350
  113. wid = QtWidgets.QWidget()
  114. wid.resize(width, height)
  115. wid.setWindowTitle("Dave's OBJ Exporter")
  116.  
  117. basOpt = QtWidgets.QGroupBox("Basic Options", wid)
  118. basOpt.resize(width/2, height/2)
  119. basOpt.move(width/4, height/4)
  120.  
  121. filePathLabel = QtWidgets.QLabel("Filepath", wid)
  122. filePathLabel.move(50, 20)
  123.  
  124. inputField = QtWidgets.QTextEdit()
  125. inputField.resize(200, 25)
  126. inputField.setParent(wid)
  127. inputField.move(100, 20)
  128.  
  129. browseBtn = QtWidgets.QPushButton("Browse", wid)
  130. browseBtn.resize(50, 25)
  131. browseBtn.move(310, 20)
  132. browseBtn.clicked.connect(saveFile)
  133.  
  134. optTriangulate = QtWidgets.QCheckBox("Triangulate", basOpt)
  135. optTriangulate.move(30, 30)
  136.  
  137. optExpSelect = QtWidgets.QCheckBox("Export Selection", basOpt)
  138. optExpSelect.move(30, 50)
  139.  
  140. optExpMat = QtWidgets.QCheckBox("Export Material", basOpt)
  141. optExpMat.move(30, 70)
  142.  
  143. spaceOpt = QtWidgets.QLabel("Space : ", basOpt)
  144. spaceOpt.move(30, 100)
  145.  
  146. optLoc = QtWidgets.QRadioButton("Local", basOpt)
  147. optLoc.setChecked(True)
  148. optLoc.move(80, 100)
  149.  
  150. optGlob = QtWidgets.QRadioButton("Global", basOpt)
  151. optGlob.move(140, 100)
  152.  
  153. expBtn = QtWidgets.QPushButton("Export",wid)
  154. expBtn.resize(width, height/6)
  155. expBtn.move(0, height-height/6)
  156. expBtn.clicked.connect(export)
  157.  
  158. wid.show()
RAW Paste Data