mrDIMAS

Exporter

Jun 2nd, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. outputFileName = getSaveFileName caption:"Scene Exporter" types:"Scene Files(*.scene)|*.scene" initialDir:(getDir #export) historyCategory:"ExportSceneFiles"
  2.  
  3. if( outputFileName == undefined ) then
  4. break
  5.  
  6. global Z2YRotation = rotateXMatrix -90
  7. global file = fopen outputFileName "wb"
  8.  
  9. -- cache functions - according to MAXScript Help it must save a lot of time, but it's not :/
  10. global polyop_getVert = getVert
  11. global polyop_getTVert = getTVert
  12. global polyop_getFace = getFace
  13. global polyop_getFaceMatID = getFaceMatID
  14. global polyop_getTVFace = getTVFace
  15. global cacheWriteFloat = WriteFloat
  16. global cacheWriteLong = WriteLong
  17.  
  18. fn FindSkinModifier obj =
  19. (
  20. local skinModifier = undefined
  21.  
  22. for i = 1 to obj.modifiers.count do
  23. (
  24. if obj.modifiers[i].Name == "Skin" then
  25. (
  26. skinModifier = obj.modifiers[i]
  27.  
  28. max modify mode
  29. modPanel.setCurrentObject skinModifier
  30.  
  31. exit
  32. )
  33. )
  34.  
  35. return skinModifier
  36. )
  37.  
  38. fn WriteVector3 vec3 =
  39. (
  40. -- write vertex according to X - right, Y - up, Z - forward
  41. cacheWriteFloat file ( vec3.x)
  42. cacheWriteFloat file ( vec3.z)
  43. cacheWriteFloat file (-vec3.y)
  44. )
  45.  
  46. fn WriteQuaternion quaternion =
  47. (
  48. quaternion = quaternion * Z2YRotation
  49.  
  50. cacheWriteFloat file quaternion.x
  51. cacheWriteFloat file quaternion.y
  52. cacheWriteFloat file quaternion.z
  53. cacheWriteFloat file quaternion.w
  54. )
  55.  
  56. fn WriteFace face =
  57. (
  58. cacheWriteLong file (face.x - 1)
  59. cacheWriteLong file (face.y - 1)
  60. cacheWriteLong file (face.z - 1)
  61. )
  62.  
  63. fn WriteTextureCoordinates texCoord =
  64. (
  65. cacheWriteFloat file texCoord.x
  66. cacheWriteFloat file (1 - texCoord.y)
  67. )
  68.  
  69. fn WriteNodePositionRotation obj =
  70. (
  71. -- write position
  72. WriteVector3 ( obj.position )
  73.  
  74. -- write rotation
  75. WriteQuaternion ( obj.rotation )
  76. )
  77.  
  78. fn GetTextureFileNamesArray obj =
  79. (
  80. -- collect textures
  81. local textures = #()
  82. local currentMaterial = obj.material
  83.  
  84. if( currentMaterial != undefined ) then
  85. (
  86. subMaterials = getNumSubmtls currentMaterial
  87.  
  88. if (subMaterials == 0) then
  89. (
  90. if( currentMaterial.diffuseMap != undefined ) then
  91. append textures ( FileNameFromPath currentMaterial.diffuseMap.filename )
  92. )
  93. else
  94. (
  95. for i = 1 to subMaterials do
  96. (
  97. local subMaterial = getSubmtl currentMaterial i
  98.  
  99. if( subMaterial.diffuseMap != undefined ) then
  100. append textures ( FileNameFromPath subMaterial.diffuseMap.filename )
  101. )
  102. )
  103. )
  104.  
  105. return textures
  106. )
  107.  
  108. global animStart = animationRange.start.frame as integer
  109. global animEnd = animationRange.end.frame as integer
  110. global framesCount = (animEnd - animStart + 1)
  111.  
  112. if outputFileName != undefined then
  113. (
  114. DisableSceneRedraw
  115. undo off
  116. animate off
  117.  
  118. sliderTime = 0
  119.  
  120. writeLong file (objects.count)
  121. writeLong file (framesCount)
  122.  
  123. for currentObject in objects do
  124. (
  125. local hasAnimation = 0
  126.  
  127. if currentObject.position.isAnimated == true then
  128. hasAnimation = 1
  129.  
  130. if currentObject.rotation.isAnimated == true then
  131. hasAnimation = 1
  132.  
  133. local skinModifier = FindSkinModifier currentObject
  134.  
  135. local mesh = currentObject.mesh
  136. local userProp = getUserPropBuffer currentObject
  137.  
  138. -- properties buffer
  139. writeString file userProp
  140.  
  141. -- geometry parameters
  142. writeLong file mesh.numverts
  143. writeLong file mesh.numtverts
  144. writeLong file mesh.numfaces
  145.  
  146. -- animation
  147. if( skinModifier != undefined ) then
  148. writeLong file 1
  149. else
  150. writeLong file 0
  151.  
  152. writeLong file hasAnimation
  153.  
  154. -- write name
  155. writeString file currentObject.name
  156.  
  157. in coordsys parent
  158. (
  159. if hasAnimation == 1 then -- write keyframes
  160. for frame = animStart to animEnd do
  161. at time frame
  162. WriteNodePositionRotation currentObject
  163. else -- write only position and rotation
  164. at time 0
  165. WriteNodePositionRotation currentObject
  166. )
  167.  
  168. -- collect textures
  169. textures = GetTextureFileNamesArray currentObject
  170.  
  171. -- write texture file names
  172. WriteLong file textures.count
  173.  
  174. for texture in textures do
  175. WriteString file texture
  176.  
  177. -- write geometry at start frame
  178. at time 0
  179. for i = 1 to mesh.numverts do
  180. WriteVector3 ((polyop_getVert mesh i) * ( currentObject.scale ))
  181.  
  182. -- export bones
  183. if( skinModifier != undefined ) then
  184. (
  185. -- write bone weights
  186. for i = 1 to mesh.numVerts do
  187. (
  188. local weightCount = skinops.GetVertexWeightCount skinModifier i
  189.  
  190. if( weightCount > 4 ) then
  191. weightCount = 4
  192.  
  193. WriteLong file weightCount
  194.  
  195. for k = 1 to weightCount do
  196. (
  197. local weight = skinops.GetVertexWeight skinModifier i k
  198. local id = skinops.GetVertexWeightBoneID skinModifier i k
  199.  
  200. WriteLong file id
  201. WriteFloat file weight
  202. )
  203. )
  204.  
  205. local boneCount = skinops.GetNumberBones skinModifier
  206.  
  207. WriteLong file boneCount
  208.  
  209. for i = 1 to boneCount do
  210. (
  211. local boneName = skinops.GetBoneName skinModifier i 1
  212.  
  213. WriteString file boneName
  214. )
  215. )
  216.  
  217. local numTVerts = mesh.numTVerts
  218.  
  219. -- write texture coordinates
  220. for v = 1 to numTVerts do
  221. WriteTextureCoordinates ( polyop_getTVert mesh v )
  222.  
  223. -- write faces
  224. for f = 1 to mesh.numfaces do
  225. (
  226. local face = polyop_getFace mesh f
  227.  
  228. WriteFace face
  229.  
  230. if ( numTVerts != 0 ) then
  231. WriteFace ( polyop_getTVFace mesh f )
  232. else
  233. WriteFace face
  234.  
  235. if (textures.count > 1) then
  236. writeLong file ((polyop_getFaceMatID mesh f) - 1)
  237. else
  238. writeLong file 0
  239. )
  240.  
  241. delete mesh
  242. )
  243.  
  244. for currentObject in objects do
  245. (
  246. local parentName = "World";
  247.  
  248. if (currentObject.parent != undefined) then
  249. parentName = currentObject.parent.name
  250.  
  251. writeString file currentObject.name
  252. writeString file parentName
  253. )
  254.  
  255. fclose file
  256.  
  257. undo on
  258. EnableSceneRedraw
  259. )
Advertisement
Add Comment
Please, Sign In to add comment