PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/brlcad/tags/rel-7-14-4/src/adrt/scripts/adrt.py

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
Python | 350 lines | 219 code | 78 blank | 53 comment | 49 complexity | 693786976297743613b55629312a2a20 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. # A D R T . P Y
  2. # BRL-CAD / ADRT
  3. #
  4. # Copyright (c) 2007-2009 United States Government as represented by
  5. # the U.S. Army Research Laboratory.
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public License
  9. # version 2.1 as published by the Free Software Foundation.
  10. #
  11. # This library is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this file; see the file named COPYING for more
  18. # information.
  19. #
  20. ################
  21. ## REQUIRES PYTHON 2.3 RIGHT NOW
  22. ## CHANGE THESE TO WHERE YOUR PYTHON 2.2 IS LOCATED
  23. #import sys
  24. #sys.path.append('/usr/local/lib/python2.2')
  25. #sys.path.append('/usr/local/lib/python2.2/lib-dynload')
  26. ################
  27. import commands
  28. import Blender
  29. import math
  30. import os
  31. import struct
  32. from struct import pack
  33. from Blender import BGL, Draw, NMesh, Object, Camera, Lamp, Scene, Types
  34. from math import log
  35. framework_name = "scene"
  36. layer_mask = 3
  37. export_all_frames = 0
  38. framework_button = 0
  39. def export_meshes():
  40. fh = open(framework_name + ".adrt", "wb")
  41. ## Endian
  42. fh.write(pack('h', 1))
  43. ## Version
  44. fh.write(pack('h', 2))
  45. ## Calculate total number of triangles
  46. obj_list = Blender.Object.Get()
  47. num = 0
  48. for obj in obj_list:
  49. if (obj.Layer & layer_mask) == obj.Layer:
  50. if(type(obj.getData()) == Types.NMeshType):
  51. for f in obj.getData().faces:
  52. if len(f.v) == 4:
  53. num = num + 2
  54. if len(f.v) == 3:
  55. num = num + 1
  56. fh.write(pack('i', num))
  57. print "\nWriting %d triangles..." % num
  58. ## Pack each mesh
  59. for obj in obj_list:
  60. if (obj.Layer & layer_mask) == obj.Layer:
  61. if(type(obj.getData()) == Types.NMeshType):
  62. ## Mesh Name Length
  63. fh.write(pack('B', len(obj.getName())+1))
  64. ## Mesh Name
  65. fh.write(obj.getName())
  66. fh.write(pack('B', 0))
  67. ## Vertice Total
  68. fh.write(pack('I', len(obj.getData().verts)))
  69. ## Write Vertices
  70. for v in obj.getData().verts:
  71. fh.write(pack('fff', v.co[0], v.co[1], v.co[2]))
  72. ## Write Faces
  73. num = 0
  74. for f in obj.getData().faces:
  75. if len(f.v) == 4:
  76. num = num + 2
  77. if len(f.v) == 3:
  78. num = num + 1
  79. if(len(obj.getData().faces) < 1<<16):
  80. fh.write(pack('B', 0))
  81. fh.write(pack('H', num))
  82. for f in obj.getData().faces:
  83. if len(f.v) >= 3:
  84. fh.write(pack('HHH', f.v[0].index, f.v[1].index, f.v[2].index))
  85. if len(f.v) == 4:
  86. fh.write(pack('HHH', f.v[0].index, f.v[2].index, f.v[3].index))
  87. else:
  88. fh.write(pack('B', 1))
  89. fh.write(pack('I', num))
  90. for f in obj.getData().faces:
  91. if len(f.v) >= 3:
  92. fh.write(pack('III', f.v[0].index, f.v[1].index, f.v[2].index))
  93. if len(f.v) == 4:
  94. fh.write(pack('III', f.v[0].index, f.v[2].index, f.v[3].index))
  95. ## close the file handle
  96. fh.close()
  97. def export_properties():
  98. fh = open(framework_name + ".properties", "w")
  99. print "Writing Properties..."
  100. ## default property
  101. fh.write("properties,%s\n" % "default")
  102. fh.write("color,%f,%f,%f\n" % (0.8, 0.8, 0.8))
  103. fh.write("gloss,%f\n" % (0.2))
  104. fh.write("emission,%f\n" % (0.0))
  105. for m in Blender.Material.Get():
  106. fh.write("properties,%s\n" % m.getName())
  107. fh.write("color,%f,%f,%f\n" % (m.rgbCol[0], m.rgbCol[1], m.rgbCol[2]))
  108. if(m.getEmit() > 0.0):
  109. fh.write("emission,%f\n" % (m.getEmit()))
  110. fh.close()
  111. def export_textures():
  112. fh = open(framework_name + ".textures", "w")
  113. print "Writing Textures..."
  114. fh.close()
  115. def export_mesh_map():
  116. fh = open(framework_name + ".map", "wb")
  117. print "Writing Mesh Map..."
  118. obj_list = Blender.Object.Get()
  119. for obj in obj_list:
  120. if (obj.Layer & layer_mask) == obj.Layer:
  121. if(type(obj.getData()) == Types.NMeshType):
  122. if(len(obj.getData().materials)) > 0:
  123. fh.write(pack('B', len(obj.getName())+1))
  124. fh.write(obj.getName());
  125. fh.write(pack('B', 0))
  126. fh.write(pack('B', len(obj.getData().materials[0].getName())+1))
  127. fh.write(obj.getData().materials[0].getName());
  128. fh.write(pack('B', 0))
  129. else:
  130. fh.write(pack('B', len(obj.getName())+1))
  131. fh.write(obj.getName());
  132. fh.write(pack('B', 0))
  133. fh.write(pack('B', len("default")+1))
  134. fh.write("default");
  135. fh.write(pack('B', 0))
  136. fh.close()
  137. def write_camera(fh, obj):
  138. focus = [0, 0, 0]
  139. fh.write("camera")
  140. loc = obj.matrix[3]
  141. look = obj.matrix[2]
  142. upvec = obj.matrix[1]
  143. # Generate Focus/Look Vector
  144. focus[0] = loc[0] - look[0]
  145. focus[1] = loc[1] - look[1]
  146. focus[2] = loc[2] - look[2]
  147. fh.write(",%f,%f,%f" % (loc[0], loc[1], loc[2])) # position
  148. fh.write(",%f,%f,%f" % (focus[0], focus[1], focus[2])) # focus
  149. fh.write(",0.0") # tilt
  150. # Vertical FoV = 2 * atan(height/(2*lens_mm)) * pi / 180
  151. # Horiz FoV = 2 * atan(width/(2*lens_mm)) * pi / 180
  152. #
  153. # simulate a 35mm camera w 24x36mm image plane
  154. # Blender uses the image width for this calcuation
  155. FoV = math.atan(35.0 / (2.0 * obj.getData().getLens())) * 180 / math.pi * 0.75
  156. fh.write(",%f,%f\n" % (FoV, 0.0))
  157. def write_frame(fh, frame):
  158. print "Writing frame: %d" % frame
  159. fh.write("frame,%d\n" % frame)
  160. m_identity = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
  161. obj_list = Blender.Object.Get()
  162. for obj in obj_list:
  163. if(obj.Layer & layer_mask) == obj.Layer:
  164. if type(obj.getData()) == Types.CameraType:
  165. write_camera(fh, obj)
  166. if type(obj.getData()) == Types.NMeshType:
  167. obj_name = obj.getName()
  168. m = obj.matrix
  169. same = 1
  170. for y in range(4):
  171. for x in range(4):
  172. if m[x][y] != m_identity[x][y]:
  173. same = 0
  174. if same == 0:
  175. fh.write("transform_mesh,%s" % obj_name)
  176. fh.write(",%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n" % \
  177. (m[0][0], m[0][1], m[0][2], m[0][3], m[1][0], m[1][1], m[1][2], m[1][3], \
  178. m[2][0], m[2][1], m[2][2], m[2][3], m[3][0], m[3][1], m[3][2], m[3][3]))
  179. def export_frames():
  180. global export_all_frames
  181. fh = open(framework_name + ".frames", "w")
  182. cur = Blender.Get('curframe')
  183. if export_all_frames == 0:
  184. write_frame(fh, cur)
  185. else:
  186. sta = Blender.Get('staframe')
  187. end = Blender.Get('endframe')
  188. for frame in range (sta, end+1):
  189. Blender.Set('curframe', frame)
  190. Blender.Window.RedrawAll()
  191. write_frame(fh, frame)
  192. fh.close()
  193. def export_environment():
  194. fh = open(framework_name + ".env", "w")
  195. fh.write("geometry_file," + framework_name + ".adrt\n")
  196. fh.write("properties_file," + framework_name + ".properties\n")
  197. fh.write("textures_file," + framework_name + ".textures\n")
  198. fh.write("mesh_map_file," + framework_name + ".map\n")
  199. fh.write("frames_file," + framework_name + ".frames\n")
  200. fh.write("image_size,512,384,128,128\n")
  201. fh.write("rendering_method,normal\n")
  202. fh.close()
  203. def event(evt, val):
  204. if evt == Draw.ESCKEY:
  205. Draw.Exit()
  206. return
  207. else:
  208. return
  209. Draw.Register(draw_gui, event, buttonEvent)
  210. def button_event(evt):
  211. global framework_button, framework_name, layer_mask, export_all_frames
  212. ## Export ADRT Framework
  213. if evt == 1:
  214. ## export mesh data
  215. export_meshes()
  216. ## export properties data
  217. export_properties()
  218. ## export textures data
  219. export_textures()
  220. ## export mesh map
  221. export_mesh_map()
  222. ## export frame data
  223. export_frames()
  224. ## export an environment file
  225. export_environment()
  226. print "Complete."
  227. if evt == 2:
  228. framework_name = framework_button.val
  229. if evt == 3:
  230. layer_mask = 0xfffff
  231. if evt == 4:
  232. layer_mask = 0
  233. if evt == 5:
  234. export_all_frames ^= 1
  235. if evt >= 32:
  236. layer_mask ^= 1<<(evt - 32)
  237. Draw.Redraw(1)
  238. def draw_gui():
  239. global framework_button, framework_name, layer_mask, export_all_frames
  240. ##########
  241. Draw.Button("Export ADRT Framework", 1, 0, 0, 160, 20, "Export Scene")
  242. framework_button = Draw.String("Framework Name:", 2, 160, 0, 240, 20, framework_name, 64, "Framework Name")
  243. ##########
  244. ##########
  245. Draw.Button("Select All", 3, 0, 60, 160, 20, "Deselect all Layers")
  246. Draw.Button("Select None", 4, 0, 40, 160, 20, "Select all Layers")
  247. layer_id = 0
  248. for row in range(2):
  249. for col in range(10):
  250. Draw.Toggle(" ", 32+layer_id, 160+col*20, 60-row*20, 20, 20, layer_mask & 1<<layer_id, "")
  251. layer_id += 1
  252. ##########
  253. ##########
  254. Draw.Toggle("Export All Frames", 5, 0, 100, 160, 20, export_all_frames, "")
  255. ##########
  256. ############################################
  257. ## Begin Event Handling
  258. ############################################
  259. Draw.Register(draw_gui, event, button_event)
  260. ############################################
  261. # Local Variables:
  262. # mode: Python
  263. # tab-width: 8
  264. # c-basic-offset: 4
  265. # python-indent-level: 4
  266. # indent-tabs-mode: t
  267. # End:
  268. # ex: shiftwidth=4 tabstop=8