Guest User

Untitled

a guest
Nov 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #!BPY
  2.  
  3. """
  4. Name: 'FaceGen TRI (.tri)...'
  5. Blender: 249
  6. Group: 'Import'
  7. Tooltip: 'Load a FaceGen TRI file.'
  8. """
  9.  
  10. __author__= "himika"
  11. __version__= "0.2"
  12.  
  13. __bpydoc__= """\
  14. This script imports a FaceGen TRI file to Blender.
  15.  
  16. Usage:
  17. Run this script from "File->Import" menu and then load the desired TRI file
  18. """
  19.  
  20. # ***** BEGIN GPL LICENSE BLOCK *****
  21. #
  22. # Copyright (C) 2012 himika
  23. #
  24. # This program is free software; you can redistribute it and/or
  25. # modify it under the terms of the GNU General Public License
  26. # as published by the Free Software Foundation; either version 2
  27. # of the License, or (at your option) any later version.
  28. #
  29. # This program is distributed in the hope that it will be useful,
  30. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. # GNU General Public License for more details.
  33. #
  34. # You should have received a copy of the GNU General Public License
  35. # along with this program; if not, write to the Free Software Foundation,
  36. # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  37. #
  38. # ***** END GPL LICENCE BLOCK *****
  39. # --------------------------------------------------------------------------
  40.  
  41. import bpy
  42. import Blender
  43. from Blender import Mesh, Draw, Window, Texture, Material, sys
  44. import BPyMessages
  45. from pyffi.formats.tri import TriFormat
  46.  
  47.  
  48.  
  49. def importMenu():
  50. global ROTATE_X90, INPORT_MORPH_SHAPE, INPORT_MODIFIER_SHAPE
  51. ROTATE_X90 = Blender.Draw.Create(1)
  52. INPORT_MORPH_SHAPE = Blender.Draw.Create(1)
  53. INPORT_MODIFIER_SHAPE = Blender.Draw.Create(1)
  54.  
  55. # Get USER Options
  56. pup_block = [\
  57. ('Rotate X90', ROTATE_X90, 'Rotate X -90'),\
  58. ('TRI Morph', INPORT_MORPH_SHAPE, 'Include TRI Morph'),\
  59. ('TRI Modifier', INPORT_MODIFIER_SHAPE, 'Include TRI Modifier')\
  60. ]
  61.  
  62. if not Blender.Draw.PupBlock('Import TRI...', pup_block):
  63. return
  64.  
  65. Blender.Window.WaitCursor(1)
  66.  
  67. ROTATE_X90 = ROTATE_X90.val
  68. INPORT_MORPH_SHAPE = INPORT_MORPH_SHAPE.val
  69. INPORT_MODIFIER_SHAPE = INPORT_MODIFIER_SHAPE.val
  70.  
  71.  
  72.  
  73. def loadTri(filename):
  74. if BPyMessages.Error_NoFile(filename):
  75. return
  76.  
  77. importMenu()
  78.  
  79. print('\nImporting tri: "%s"' % (Blender.sys.expandpath(filename)))
  80. istrm = open(filename, 'rb')
  81. data = TriFormat.Data()
  82. data.inspect(istrm)
  83. data.read(istrm)
  84. istrm.close()
  85. print("\nTRI version 0x%x | Ve:%d | Fa:%d | UV:%d |" % (data.version, data.num_vertices, data.num_tri_faces, data.num_uvs))
  86. print("Num Morphs:%d | Num Modifiers:%d\n" % (data.num_morphs, data.num_modifiers))
  87.  
  88. name = filename.split('\\')[-1].split('/')[-1]
  89. scn = bpy.data.scenes.active
  90. mesh = Mesh.New()
  91.  
  92. # vertex
  93. verts_list = [(v.x, v.y, v.z) for v in data.vertices]
  94. if ROTATE_X90:
  95. verts_list[:] = [(v[0], -v[2], v[1]) for v in verts_list]
  96. mesh.verts.extend(verts_list)
  97.  
  98. # face
  99. face_list = [(f.v_1, f.v_2, f.v_3) for f in data.tri_faces]
  100. mesh.faces.extend(face_list)
  101. del face_list
  102.  
  103. # UV
  104. if data.num_uvs > 0:
  105. mesh.faceUV = True
  106. for i, face in enumerate(mesh.faces):
  107. utf = data.uv_tri_faces[i]
  108. idx = [utf.v_1, utf.v_2, utf.v_3]
  109. for ii, j in enumerate(face):
  110. uv = data.uvs[idx[ii]]
  111. face.uv[ii].x = uv.u
  112. face.uv[ii].y = uv.v
  113.  
  114. ob = scn.objects.new(mesh, name)
  115.  
  116. # Shape Keys
  117. if (data.num_morphs > 0 and INPORT_MORPH_SHAPE) or (data.num_modifiers > 0 and INPORT_MODIFIERS_SHAPE):
  118. mesh.insertKey(1, 'relative')
  119.  
  120. if INPORT_MORPH_SHAPE:
  121. for morph in data.morphs:
  122. morphName = str(morph.name.decode("ascii"))
  123. scale = morph.scale
  124. print("... Morhph:" + morphName)
  125. for i, nv in enumerate(mesh.verts):
  126. verts_morph = morph.vertices[i]
  127. v3 = [verts_morph.x, verts_morph.y, verts_morph.z]
  128. if ROTATE_X90:
  129. v3 = [v3[0], -v3[2], v3[1]]
  130. nv.co[0] = verts_list[i][0] + v3[0] * scale
  131. nv.co[1] = verts_list[i][1] + v3[1] * scale
  132. nv.co[2] = verts_list[i][2] + v3[2] * scale
  133. mesh.insertKey(1, 'relative')
  134. mesh.key.blocks[-1].name = morphName
  135.  
  136. if INPORT_MODIFIER_SHAPE:
  137. for modifier in data.modifiers:
  138. modifierName = str(modifier.name.decode("ascii"))
  139. print("... Modifier:" + modifierName)
  140. for i, nv in enumerate(mesh.verts):
  141. nv.co[0] = verts_list[i][0]
  142. nv.co[1] = verts_list[i][1]
  143. nv.co[2] = verts_list[i][2]
  144. for i, n in enumerate(modifier.vertices_to_modify):
  145. nv = mesh.verts[n]
  146. mv = modifier.modifier_vertices[i]
  147. v3 = [mv.x, mv.y, mv.z]
  148. if ROTATE_X90:
  149. v3 = [v3[0], -v3[2], v3[1]]
  150. nv.co[0] = v3[0]
  151. nv.co[1] = v3[1]
  152. nv.co[2] = v3[2]
  153. mesh.insertKey(1, 'relative')
  154. mesh.key.blocks[-1].name = modifierName
  155.  
  156. for i, nv in enumerate(mesh.verts):
  157. nv.co[0] = verts_list[i][0]
  158. nv.co[1] = verts_list[i][1]
  159. nv.co[2] = verts_list[i][2]
  160.  
  161. del verts_list
  162. print("done.")
  163.  
  164. #============================
  165. # main
  166. if __name__=='__main__':
  167. Blender.Window.FileSelector(loadTri, 'Import tri', '*.tri')
Add Comment
Please, Sign In to add comment