export.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
  2. # All rights reserved.
  3. # Code licensed under the BSD License.
  4. # http://www.anki3d.org/LICENSE
  5. # keep methods in alphabetical order
  6. # system imports
  7. import os
  8. import subprocess
  9. from subprocess import Popen
  10. # blender imports
  11. import bpy
  12. from bpy.types import Operator
  13. import mathutils
  14. # local imports
  15. from ..lib import gui as GUI
  16. from ..lib import objects as OBJ
  17. from ..lib import material as MAT
  18. from ..lib import math as MTH
  19. from ..lib import preferences as PREF
  20. from ..lib.images import *
  21. # for k, v in locals().items():
  22. # print(k,v)
  23. bl_info = {"author": "A. A. Kalugin Jr."}
  24. class OBJECT_OT_anki_export_material(Operator):
  25. """Export anki scene textures"""
  26. bl_idname = "scene.anki_export_textures"
  27. bl_label = "Export Textures"
  28. bl_options = {'REGISTER'}
  29. def execute(self, context):
  30. scn = context.scene
  31. exported_log = []
  32. error_log = []
  33. commands = []
  34. addon_prefs = PREF.get_anki_exporter_preferences(context)
  35. py_image_converter = addon_prefs.tool_py_texture
  36. export_path = addon_prefs.export_texture_path
  37. textures = MAT.get_texture_images_nodes()
  38. cmd_string = "{0} -i {1} -o {2}"
  39. # Process the blender texture slot nodes.
  40. for texture in textures:
  41. src_path = texture.filepath
  42. src_file_name = os.path.basename(src_path)
  43. dst_path = ("{0}/{1}.ankitex".format(export_path, os.path.splitext(src_file_name)[0] ))
  44. # Check if the resolution of the image is power of twos
  45. res_x = (texture.size[0])
  46. res_y = (texture.size[1])
  47. if not MTH.is_power_of_two(res_x) or not MTH.is_power_of_two(res_y):
  48. msg = "Image {0} resolution not power of 2. x {1} y {2}"
  49. src_path = d_lib_images["not_powers_of_two"]
  50. cmd = cmd_string.format(py_image_converter, src_path, dst_path)
  51. error = msg.format(texture.filepath, res_x, res_y)
  52. error_log.append(error)
  53. commands.append(cmd)
  54. continue
  55. # Set up the command for texture export.
  56. cmd = cmd_string.format(py_image_converter, texture.filepath, dst_path)
  57. # Popen(cmd, shell=True)
  58. src_time = os.path.getatime(src_path)
  59. if not os.path.exists(dst_path):
  60. commands.append(cmd)
  61. continue
  62. dst_time = os.path.getatime(dst_path)
  63. if src_time > dst_time:
  64. export = "Running command: {0}".format(cmd)
  65. exported_log.append(export)
  66. commands.append(cmd)
  67. # # Process the commands
  68. print (commands)
  69. if len(commands) > 0:
  70. for cmd in commands:
  71. print ("Running command: {0}".format(cmd))
  72. Popen(cmd, shell=True)
  73. else:
  74. print("No commands to to process.")
  75. return {'FINISHED'}
  76. class OBJECT_OT_anki_export_scene(Operator):
  77. """Export anki scene"""
  78. bl_idname = "scene.anki_export_scene"
  79. bl_label = "Export Scene"
  80. bl_options = {'REGISTER'}
  81. def execute(self, context):
  82. scn = context.scene
  83. no_uvs_objs = OBJ.get_objects_wuith_no_uvs()
  84. if no_uvs_objs:
  85. msg = ""
  86. for ob in no_uvs_objs:
  87. msg += "Object: {0},".format(ob.name)
  88. self.report({'INFO'}, "NO UVS on bjects! {0}".format(msg))
  89. return {'FINISHED'}
  90. addon_prefs = PREF.get_anki_exporter_preferences(context)
  91. temp_dea = addon_prefs.temp_dea
  92. view_mtx = None
  93. cam = None
  94. if scn.UseViewport:
  95. # Get camera matrix to viewport projection matrix
  96. cam = OBJ.get_camera()
  97. view_mtx = mathutils.Matrix(cam.matrix_world)
  98. r3d = GUI.get_region3d(context)
  99. cam.matrix_world = r3d.view_matrix.inverted()
  100. print ("Set camera to view projection matrix.")
  101. # Export the collada file to temp.dea
  102. bpy.ops.wm.collada_export(filepath=temp_dea,
  103. check_existing=True,
  104. filter_blender=False,
  105. filter_backup=False,
  106. filter_image=False,
  107. filter_movie=False,
  108. filter_python=False,
  109. filter_font=False,
  110. filter_sound=False,
  111. filter_text=False,
  112. filter_btx=False,
  113. filter_collada=True,
  114. filter_folder=True,
  115. filter_blenlib=False,
  116. filemode=8,
  117. display_type='DEFAULT',
  118. sort_method='FILE_SORT_ALPHA',
  119. apply_modifiers=False,
  120. export_mesh_type=0,
  121. export_mesh_type_selection='view',
  122. selected=False,
  123. include_children=False,
  124. include_armatures=False,
  125. include_shapekeys=True,
  126. deform_bones_only=False,
  127. active_uv_only=False,
  128. include_uv_textures=False,
  129. include_material_textures=True,
  130. use_texture_copies=False,
  131. triangulate=True,
  132. use_object_instantiation=True,
  133. sort_by_name=False,
  134. export_transformation_type=0,
  135. export_transformation_type_selection='matrix',
  136. open_sim=False)
  137. print ("Exported to:", temp_dea)
  138. # Restore Camera
  139. if scn.UseViewport:
  140. cam.matrix_world = view_mtx
  141. print ("Restored camera to original projection matrix.")
  142. # Format the command and convert the exported collada
  143. rel_map_path = (addon_prefs.export_map_path.replace(addon_prefs.anki_project_path, ""))
  144. print(rel_map_path)
  145. rel_texture_path = (addon_prefs.export_texture_path.replace(addon_prefs.anki_project_path, ""))
  146. print(rel_texture_path)
  147. cmd_str = "ankisceneimp {0} {1} -rpath {2} -texrpath {3} -flipyz"
  148. print (cmd_str)
  149. cmd = cmd_str.format(temp_dea,
  150. addon_prefs.export_map_path,
  151. rel_map_path,
  152. rel_texture_path)
  153. print (cmd)
  154. subprocess.call(cmd, shell=True)
  155. # Run the level in the game
  156. bled_app = ("{0}/build/bled/bled".format(addon_prefs.anki_project_path))
  157. print (bled_app)
  158. subprocess.call(bled_app, shell=True)
  159. return {'FINISHED'}