Advertisement
Guest User

Untitled

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