Browse Source

Added blender addon for exporting scenes to anki engine

akal 9 years ago
parent
commit
c34e68dbcf

+ 146 - 0
tools/anki_scene_exporter/__init__.py

@@ -0,0 +1,146 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSEimport os
+# keep methods in alphabetical order
+
+bl_info = {
+	"name": "Anki Scene Exporter",
+	"author": "A. A. Kalugin Jr.",
+	"version": (0, 1),
+	"blender": (2, 65, 0),
+	"location": "Anki Preferences",
+	"description": "Anki Scene Exporter",
+	"warning": "",
+	"wiki_url": "",
+	"tracker_url": "",
+	"category": "User"}
+# set up the
+
+if "bpy" in locals():
+	import importlib
+
+	importlib.reload(ops.environment)
+	importlib.reload(ops.export)
+	importlib.reload(props.export_props)
+	importlib.reload(ui.gui)
+
+else:
+	from .ops import environment
+	from .ops import export
+	from .props import export_props
+	from .ui import gui
+
+import bpy, os
+from bpy.types import AddonPreferences
+from bpy.props import StringProperty
+from .lib import environment as ENV
+
+
+################################ CONSTANTS ###############################
+ENV.ENVIRO = "ANKI_DATA_PATH"
+################################ CONSTANTS ###############################
+
+class SCENE_anki_scene_exporter(AddonPreferences):
+	# this must match the addon name, use '__package__'
+	# when defining this in a submodule of a python package.
+	bl_idname = __name__
+
+
+	anki_environment = StringProperty(
+			name="Anki Environment",
+			default=ENV.ENVIRO,
+			)
+	anki_project_path = StringProperty(
+		name="Project Path",
+		subtype='FILE_PATH',
+		)
+	export_path = StringProperty(
+			name="Source Path",
+			default="Source Path:",
+			)
+	im_convert = StringProperty(
+			name="ImageMagick Convert Path",
+			default="/usr/bin/convert",
+			subtype='FILE_PATH',
+			)
+	tool_etcpack_path = StringProperty(
+			name="Third Party etc pack",
+			default="",
+			subtype='FILE_PATH',
+			)
+	tools_path = StringProperty(
+			name="Tools Path",
+			default="Tools Path:",
+			)
+	map_path = StringProperty(
+			name="Map Path",
+			default="Map Path:",
+			)
+	texture_path = StringProperty(
+			name="Texture Path",
+			default="Texture Path:{}",
+			)
+	temp_dea = StringProperty(
+			name="Temp Dea Path",
+			)
+	# Try to get/set the environment variables
+	path_list = os.getenv(ENV.ENVIRO)
+	environment_root = None
+	if path_list:
+		# Environment variable exists get the common path.
+		environment_root = ENV.get_common_environment_path()
+		anki_project_path[1]['default'] = environment_root
+
+		env_dct, export_dct, tools_dct = ENV.get_environment()
+		# ENV.set_environment(env_dct, tools_dct)
+
+		# check converter if does not exit remove default path
+		if not os.path.exists(im_convert[1]['default']):
+			im_convert[1]['default'] = ""
+
+		#check etcpack tool and fill the path
+		tool_etcpack = tools_dct['tool_etcpack_path']
+		if os.path.exists(tool_etcpack):
+			tool_etcpack_path[1]['default'] = tool_etcpack
+
+		# Set the gui
+		tools_path[1]['default'] = tools_dct['tools_path']
+		export_path[1]['default'] = export_dct['export_src_data']
+		map_path[1]['default'] = export_dct['export_map_path']
+		texture_path[1]['default'] = export_dct['export_texture_path']
+		temp_dea[1]['default'] = export_dct['export_temp_dea']
+
+	def draw(self, context):
+		layout = self.layout
+		msg = "Preferences are driven by {} environment variable".format(ENV.ENVIRO)
+		layout.label(text=msg)
+		layout.prop(self, "anki_project_path")
+		layout.prop(self, "im_convert")
+		layout.prop(self, "tool_etcpack_path")
+
+		split = layout.split()
+		col = split.column()
+		col.label(text='Source Path:')
+		col.label(text='Map Path:')
+		col.label(text='Texture Path:')
+		col.label(text='Tools Path:')
+
+		col = split.column()
+		col.label(text=self.export_path)
+		col.label(text=self.map_path)
+		col.label(text=self.texture_path)
+		col.label(text=self.tools_path)
+
+		layout.operator("scene.anki_preference_set", text="Set Preferences")
+
+
+def register():
+	bpy.utils.register_module(__name__)
+
+
+def unregister():
+	bpy.utils.unregister_module(__name__)
+
+
+if __name__ == "__main__":
+	register()
+

+ 436 - 0
tools/anki_scene_exporter/__init__2.py

@@ -0,0 +1,436 @@
+
+bl_info = {
+    "name": "Anki Scene Exporter",
+    "author": "A. A. Kalugin Jr.",
+    "version": (0, 1),
+    "blender": (2, 65, 0),
+    "location": "Anki Preferences",
+    "description": "Anki Scene Exporter",
+    "warning": "",
+    "wiki_url": "",
+    "tracker_url": "",
+    "category": "User"}
+
+import os
+import subprocess
+import bpy
+import mathutils
+from bpy.types import Operator, AddonPreferences,Panel
+from bpy.props import StringProperty, IntProperty, BoolProperty
+from bpy.props import *
+# Constants
+
+ENVIRO = "ANKI_DATA_PATH"
+
+bpy.types.Scene.UseViewport = BoolProperty(
+    name = "View", 
+    description = "True or False?",
+    default= True)
+
+bpy.types.Scene.ExportAllTextures = BoolProperty(
+    name = "Export All Textures", 
+    description = "True or False?",
+    default= False)
+
+bpy.types.Scene.UpdateTextures = BoolProperty(
+    name = "Update Textures", 
+    description = "True or False?",
+    default= True)
+
+# scn['UseViewport'] = True
+# scn['ExportAllTextures'] = False 
+# scn['UpdateTextures'] = True
+
+
+def set_environment(anki_env_dct,tools_dct):
+    environment_path = ':'.join(anki_env_dct.values())
+    os.environ[ENVIRO]=environment_path
+    environment_path = ':'.join(tools_dct.values())
+    os.environ['ANKI_TOOLS']=environment_path
+
+def get_environment(environment_root):
+    environment_root = (os.path.abspath(environment_root)) #clean the path
+    # ANKI Environment
+    anki_build_path       = "{0}/build".format(environment_root)
+    anki_shaders_path     = "{0}/shaders".format(environment_root)
+    anki_engine_data_path = "{0}/engine_data".format(environment_root)
+    # Export Environment
+    export_data_path      = "{0}/data".format(environment_root)
+    export_src_data       = "{0}/src_data".format(export_data_path) 
+    export_map_path       = "{0}/map".format(export_data_path) 
+    export_temp_dea       = "{0}/tmp_scene.dae".format(export_src_data)
+    export_texture_path   = "{0}/texture".format(export_data_path) 
+    # Tools Environment
+    tool_etcpack_path     = "{0}/thirdparty/bin/etcpack".format(environment_root) 
+    tools_path            = "{0}/tools".format(anki_build_path) 
+    tools_scene_path      = "{0}/scene".format(tools_path) 
+    tools_texture_path    = "{0}/texture".format(tools_path) 
+    
+    # Make the Export Paths
+    for _path in [export_src_data, export_map_path, export_texture_path]:
+        if not os.path.exists(_path):
+            print ("Making directory:", _path)
+            os.makedirs(_path)
+    
+    env_dct = {     
+                    'environment_root':environment_root+'/',
+                    'anki_build_path':anki_build_path,
+                    'anki_engine_data_path':anki_engine_data_path,
+                    'anki_shaders_path':anki_shaders_path,
+                    }
+    tools_dct = {     
+                    'tool_etcpack_path':tool_etcpack_path,
+                    'tools_path':tools_path,
+                    'tools_scene_path':tools_scene_path,
+                    'tools_texture_path':tools_texture_path,
+                    }
+
+    export_dct = {
+                    'export_src_data':export_src_data,
+                    'export_map_path':export_map_path,
+                    'export_texture_path':export_texture_path,
+                    'export_temp_dea':export_temp_dea,
+                    }
+
+    return env_dct, export_dct, tools_dct
+
+def get_images():
+    mats = bpy.data.materials
+    for mat in mats:
+        for slot in mat.texture_slots:
+            if slot:
+                if (slot.texture.image != None):
+                    print(slot)
+                    print(slot.texture.image.file_format)
+                    path = slot.texture.image.filepath
+
+class AnkiSceneExporter(AddonPreferences):
+    # this must match the addon name, use '__package__'
+    # when defining this in a submodule of a python package.
+    bl_idname = __name__
+
+
+    anki_environment = StringProperty(
+            name="Anki Environment",
+            default="ANKI_PATH:",
+            )
+    anki_project_path = StringProperty(
+        name="Project Path",
+        subtype='FILE_PATH',
+        )
+    export_path = StringProperty(
+            name="Source Path",
+            default="Source Path:",
+            )
+    im_convert = StringProperty(
+            name="ImageMagick Convert Path",
+            default="/usr/bin/convert",
+            subtype='FILE_PATH',
+            )
+    tool_etcpack_path = StringProperty(
+            name="Third Party etc pack",
+            default="",
+            subtype='FILE_PATH',
+            )
+    tools_path = StringProperty(
+            name="Tools Path",
+            default="Tools Path:",
+            )
+    map_path = StringProperty(
+            name="Map Path",
+            default="Map Path:",
+            )
+    texture_path = StringProperty(
+            name="Texture Path",
+            default="Texture Path:{}",
+            )
+    temp_dea = StringProperty(
+            name="Temp Dea Path",
+            )
+    # Try to get/set the environment variables
+    path_list = os.getenv(ENVIRO)
+    environment_root = None
+    if path_list:
+        # Environment variable exists get the common path.
+        path_list = os.getenv(ENVIRO).split(":")
+        environment_root = os.path.commonprefix(path_list)
+        anki_project_path[1]['default'] = environment_root
+
+        env_dct, export_dct, tools_dct = get_environment(environment_root)
+        set_environment(env_dct, tools_dct)
+        
+        # check converter if does not exit remove default path
+        if not os.path.exists(im_convert[1]['default']):
+            im_convert[1]['default'] = ""
+
+        #check etcpack tool and fill the path
+        tool_etcpack = tools_dct['tool_etcpack_path']
+        if os.path.exists(tool_etcpack):
+            tool_etcpack_path[1]['default'] = tool_etcpack
+        
+        # Set the gui
+        tools_path[1]['default'] = tools_dct['tools_path']
+        export_path[1]['default'] = export_dct['export_src_data']
+        map_path[1]['default'] = export_dct['export_map_path']
+        texture_path[1]['default'] = export_dct['export_texture_path']
+        temp_dea[1]['default'] = export_dct['export_temp_dea']
+
+        
+    def draw(self, context):
+        layout = self.layout
+        msg = "Preferences are driven by {} environment variable".format(ENVIRO)
+        layout.label(text=msg)
+        layout.prop(self, "anki_project_path")
+        layout.prop(self, "im_convert")
+        layout.prop(self, "tool_etcpack_path")
+        
+        split = layout.split()
+        col = split.column()
+        col.label(text='Source Path:')
+        col.label(text='Map Path:')
+        col.label(text='Texture Path:')
+        col.label(text='Tools Path:')
+        
+        col = split.column()
+        col.label(text=self.export_path)
+        col.label(text=self.map_path)
+        col.label(text=self.texture_path)
+        col.label(text=self.tools_path)
+    
+        # layout.label(text=self.anki_environment)
+        layout.operator("scene.anki_preference_set", text="Set Preferences")
+
+class OBJECT_OT_anki_preference_set(Operator):
+    """Set anki preferences"""
+    bl_idname = "scene.anki_preference_set"
+    bl_label = "Anki Preferences Setter"
+    bl_options = {'REGISTER', 'UNDO'}
+    # anki_path_str = "ANKI_PATH"
+    def execute(self, context):
+        user_preferences = context.user_preferences
+        addon_prefs = user_preferences.addons[__name__].preferences
+        
+        # Evironment variables
+        build = "{0}build".format(addon_prefs.anki_project_path)
+        data = "{0}data".format(addon_prefs.anki_project_path)
+        shaders = "{0}shaders".format(addon_prefs.anki_project_path)
+        engine_data = "{0}engine_data".format(addon_prefs.anki_project_path)
+
+        # Set project paths
+        export_path ="{0}/src_data".format(data) 
+        addon_prefs.export_path = export_path
+        
+        map_path ="{0}/map".format(data) 
+        addon_prefs.map_path = map_path
+
+        texture_path ="{0}/texture".format(data) 
+        addon_prefs.texture_path = texture_path
+        
+        tools_path ="{0}/tools".format(build) 
+        addon_prefs.tools_path = tools_path
+
+        tools_scene_path ="{0}/scene".format(tools_path) 
+        tools_texture_path ="{0}/texture".format(tools_path) 
+
+        # Environment variable setter
+        # environment_path = (":".join([build, data, shaders, engine_data, tools_path, tools_scene_path, tools_texture_path]))
+        # os.environ[self.anki_path_str]=environment_path
+        # # os.environ['ANKI_DATA_PATH']=environment_path
+        # addon_prefs.anki_environment = "ANKI_PATH: {}".format(environment_path)
+
+        # # Make the paths
+        # for _path in [export_path,map_path, texture_path]:
+        #     if not os.path.exists(_path):
+        #         print ("Making directory:", _path)
+        #         os.makedirs(_path)
+        # addon_prefs.temp_dea = "{0}/tmp_scene.dae".format(export_path)
+        # print (addon_prefs.temp_dea)
+
+        return {'FINISHED'}
+
+def get_camera():
+    def getter():
+        obs = bpy.data.objects
+        for ob in obs:
+            if ob.type == 'CAMERA':
+                cam = ob
+                return ob
+    cam = getter()        
+    if cam == None:
+        bpy.ops.object.camera_add()
+    return getter()
+
+def get_region3d():
+    for view in bpy.context.window.screen.areas:
+        if view.type == 'VIEW_3D':
+            return view.spaces[0].region_3d
+    return None
+            #print(dir(a))
+            #cam.matrix_world = a.spaces[0].region_3d.view_matrix.inverted()
+
+class OBJECT_OT_anki_export(Operator):
+    """Export anki scene"""
+    bl_idname = "scene.anki_export"
+    bl_label = "Anki Export"
+    bl_options = {'REGISTER'}
+
+    def execute(self, context):
+        scn = context.scene
+        user_preferences = context.user_preferences
+        addon_prefs = user_preferences.addons[__name__].preferences
+
+        temp_dea =  addon_prefs.temp_dea
+        
+        view_mtx = None
+        cam = None
+        if scn['UseViewport']:
+            # Get camera matrix to viewport projection matrix
+            cam = get_camera()
+            view_mtx = mathutils.Matrix(cam.matrix_world)
+            r3d = get_region3d()
+            cam.matrix_world = r3d.view_matrix.inverted()
+            print ("Set camera to view projection matrix.")
+        
+        # Export the collada file to temp.dea    
+        bpy.ops.wm.collada_export(filepath=temp_dea,
+                          check_existing=True,
+                          filter_blender=False,
+                          filter_backup=False,
+                          filter_image=False,
+                          filter_movie=False,
+                          filter_python=False,
+                          filter_font=False,
+                          filter_sound=False,
+                          filter_text=False,
+                          filter_btx=False,
+                          filter_collada=True,
+                          filter_folder=True,
+                          filter_blenlib=False,
+                          filemode=8,
+                          display_type='DEFAULT',
+                          sort_method='FILE_SORT_ALPHA',
+                          apply_modifiers=False,
+                          export_mesh_type=0,
+                          export_mesh_type_selection='view',
+                          selected=False,
+                          include_children=False,
+                          include_armatures=False,
+                          include_shapekeys=True,
+                          deform_bones_only=False,
+                          active_uv_only=False,
+                          include_uv_textures=False,
+                          include_material_textures=False,
+                          use_texture_copies=True,
+                          triangulate=True,
+                          use_object_instantiation=True,
+                          sort_by_name=False,
+                          export_transformation_type=0,
+                          export_transformation_type_selection='matrix',
+                          open_sim=False)
+        print ("Exported to:", temp_dea)
+
+        # Restore Camera
+        if scn['UseViewport']:
+            cam.matrix_world = view_mtx
+            print ("Restored camera to original projection matrix.")
+            
+
+        relative_path = (addon_prefs.map_path.replace(addon_prefs.anki_project_path, ""))
+        cmd  = "ankisceneimp {0} {1} -rpath {2}".format(temp_dea, 
+                                                        addon_prefs.map_path, 
+                                                        relative_path)
+
+        # Run the level in the game
+        subprocess.call(cmd, shell=True)
+        test_app = ("{0}build/testapp/testapp".format(addon_prefs.anki_project_path))
+        subprocess.call(test_app, shell=True)
+
+        return {'FINISHED'}
+
+class AnkiPanel:
+    bl_space_type = 'VIEW_3D'
+    bl_region_type = 'TOOLS'
+
+
+class VIEW3D_PT_prefs_anki(AnkiPanel, Panel):
+    bl_category = "Anki"
+    bl_label = "Preferences"
+
+    def draw(self, context):
+        scn = context.scene
+        layout = self.layout
+        col = layout.column(align=True)
+        col.label(text="Texture Export:")
+        row = col.row(align=True)
+        row.prop(scn, "ExportAllTextures")
+        row.prop(scn, "UpdateTextures")
+
+class VIEW3D_PT_tools_anki(AnkiPanel, Panel):
+    bl_category = "Anki"
+    bl_label = "Exporter"
+
+    def draw(self, context):
+        scn = context.scene
+        layout = self.layout
+        
+        col = layout.column(align=True)
+
+        split = layout.split()
+        col = split.column()
+        col.prop(scn, 'UseViewport')
+        col = split.column()
+        col.operator("scene.anki_export", icon='PLAY')
+
+        # layout.prop(scn, 'MyInt', icon='BLENDER', toggle=True)
+        # col.prop(scn, 'MyFloat')
+        # layout.prop(scn, 'MyEnum')
+        # layout.prop(scn, 'MyString')
+        # layout.operator("idname_must.be_all_lowercase_and_contain_one_dot")
+        #col.prop(cam, "show_limits", text="Limits")
+
+# Registration
+def register():
+    bpy.utils.register_class(OBJECT_OT_anki_preference_set)
+    bpy.utils.register_class(OBJECT_OT_anki_export)
+    bpy.utils.register_class(AnkiSceneExporter)
+    bpy.utils.register_class(VIEW3D_PT_prefs_anki)
+    bpy.utils.register_class(VIEW3D_PT_tools_anki)
+
+
+def unregister():
+    bpy.utils.unregister_class(OBJECT_OT_anki_preference_set)
+    bpy.utils.unregister_class(OBJECT_OT_anki_export)
+    bpy.utils.unregister_class(AnkiSceneExporter)
+    bpy.utils.unregister_class(VIEW3D_PT_prefs_anki)
+    bpy.utils.unregister_class(VIEW3D_PT_tools_anki)
+
+
+
+if __name__ == "__main__":
+    register()
+# import bpy  
+# import mathutils
+# import json
+    
+# cam = bpy.context.scene.camera
+# cam.rotation_mode = 'XYZ'
+
+
+# rot = list()
+# pos = list()
+# print (list(pos))
+# for area in bpy.context.screen.areas:
+#     if area.type == 'VIEW_3D':
+#         region = area
+#         for space in area.spaces:
+#             if space.type == 'VIEW_3D':
+#                 rv3d = space.region_3d
+#                 mat = rv3d.view_matrix
+#                 pos = list(mat.to_translation())
+#                 rot = list(mat.to_euler())
+
+# rot = list(cam.rotation_euler)
+# pos = list(cam.location)
+# d = {"camera_pos":pos, "camera_rot":rot}
+# with open('/home/akal/hacking/anki/data/map/camera.json', 'w') as fp:
+#     json.dump(d, fp, indent=4)

+ 0 - 0
tools/anki_scene_exporter/lib/__init__.py


+ 80 - 0
tools/anki_scene_exporter/lib/environment.py

@@ -0,0 +1,80 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSE Panagiotis Christopoulos Charitos and contributors
+# keep methods in alphabetical order
+
+# system imports
+import os
+
+bl_info = {"author": "A. A. Kalugin Jr."}
+
+################################ CONSTANTS ###############################
+ENVIRO = ""
+################################ CONSTANTS ###############################
+
+def get_common_environment_path():
+	"""
+	Returns a string of the common path of a given environment variable
+	"""
+	path_list = os.getenv(ENVIRO).split(":")
+	return os.path.commonprefix(path_list)
+
+def get_environment():
+	"""
+	Returns three dictionaries of various environment paths.
+	Build, export and tools in the anki path environment.
+	"""
+	environment_root = os.path.abspath(get_common_environment_path())
+	# environment_root = (os.path.abspath(environment_root)) #clean the path
+	# ANKI Environment
+	anki_build_path       = "{0}/build".format(environment_root)
+	anki_shaders_path     = "{0}/shaders".format(environment_root)
+	anki_engine_data_path = "{0}/engine_data".format(environment_root)
+	# Export Environment
+	export_data_path      = "{0}/data".format(environment_root)
+	export_src_data       = "{0}/src_data".format(export_data_path)
+	export_map_path       = "{0}/map".format(export_data_path)
+	export_temp_dea       = "{0}/tmp_scene.dae".format(export_src_data)
+	export_texture_path   = "{0}/texture".format(export_data_path)
+	# Tools Environment
+	tool_etcpack_path     = "{0}/thirdparty/bin".format(environment_root)
+	tools_path            = "{0}/tools".format(anki_build_path)
+	tools_scene_path      = "{0}/scene".format(tools_path)
+	tools_texture_path    = "{0}/texture".format(tools_path)
+
+	# Make the Export Paths
+	for _path in [export_src_data, export_map_path, export_texture_path]:
+		if not os.path.exists(_path):
+			print ("Making directory:", _path)
+			os.makedirs(_path)
+
+	env_dct = {
+					'environment_root':environment_root+'/',
+					'anki_build_path':anki_build_path,
+					'anki_engine_data_path':anki_engine_data_path,
+					'anki_shaders_path':anki_shaders_path,
+					}
+	tools_dct = {
+					'tool_etcpack_path':tool_etcpack_path,
+					'tools_path':tools_path,
+					'tools_scene_path':tools_scene_path,
+					'tools_texture_path':tools_texture_path,
+					}
+
+	export_dct = {
+					'export_src_data':export_src_data,
+					'export_map_path':export_map_path,
+					'export_texture_path':export_texture_path,
+					'export_temp_dea':export_temp_dea,
+					}
+
+	return env_dct, export_dct, tools_dct
+
+def set_environment(anki_env_dct, tools_dct):
+	"""
+	Sets the environment variable.
+	"""
+	environment_path = ':'.join(anki_env_dct.values())
+	os.environ[ENVIRO]=environment_path
+	environment_path = ':'.join(tools_dct.values())
+	os.environ[ENVIRO]=environment_path
+

+ 17 - 0
tools/anki_scene_exporter/lib/gui.py

@@ -0,0 +1,17 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSE Panagiotis Christopoulos Charitos and contributors
+# keep methods in alphabetical order
+
+# blender imports
+import bpy
+
+bl_info = {"author": "A. A. Kalugin Jr."}
+
+def get_region3d(context):
+	"""
+	Gets the first region 3d viewport.
+	"""
+	for view in context.window.screen.areas:
+		if view.type == 'VIEW_3D':
+			return view.spaces[0].region_3d
+	return None

+ 22 - 0
tools/anki_scene_exporter/lib/material.py

@@ -0,0 +1,22 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSE Panagiotis Christopoulos Charitos and contributors
+# keep methods in alphabetical order
+
+# blender imports
+import bpy
+
+bl_info = {"author": "A. A. Kalugin Jr."}
+
+def get_blender_images():
+	"""
+	Gets the blender images using the materials
+	"""
+	bl_images = []
+	mats = bpy.data.materials
+	for mat in mats:
+		for slot in mat.texture_slots:
+			if slot:
+				if (slot.texture.image != None):
+					bl_images.append(slot.texture.image.file_format)
+	return bl_images
+

+ 23 - 0
tools/anki_scene_exporter/lib/objects.py

@@ -0,0 +1,23 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSE Panagiotis Christopoulos Charitos and contributors
+# keep methods in alphabetical order
+
+# blender imports
+import bpy
+
+bl_info = {"author": "A. A. Kalugin Jr."}
+
+def get_camera():
+	"""
+	Gets a camera in the scene.
+	If one doesn't exist creates one.
+	"""
+	def getter():
+		obs = bpy.data.objects
+		for ob in obs:
+			if ob.type == 'CAMERA':
+				return ob
+	cam = getter()
+	if cam == None:
+		bpy.ops.object.camera_add()
+	return getter()

+ 12 - 0
tools/anki_scene_exporter/lib/preferences.py

@@ -0,0 +1,12 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSEimport os
+# keep methods in alphabetical order
+
+bl_info = {"author": "A. A. Kalugin Jr."}
+
+def get_anki_exporter_preferences(context):
+	"""
+	Gets the scene preferences.
+	"""
+	user_preferences = context.user_preferences
+	return user_preferences.addons['anki_scene_exporter'].preferences

+ 0 - 0
tools/anki_scene_exporter/ops/__init__.py


+ 35 - 0
tools/anki_scene_exporter/ops/environment.py

@@ -0,0 +1,35 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSE Panagiotis Christopoulos Charitos and contributors
+# keep methods in alphabetical order
+
+# blender imports
+import bpy
+import mathutils
+from bpy.types import Operator
+from bpy.props import StringProperty
+
+# local imports
+from ..lib import preferences as PREF
+from ..lib import environment as ENV
+
+bl_info = {"author": "A. A. Kalugin Jr."}
+
+class OBJECT_OT_anki_preference_set(Operator):
+	"""Set anki preferences operator """
+	bl_idname = "scene.anki_preference_set"
+	bl_label = "Anki Preferences Setter"
+	bl_options = {'REGISTER', 'UNDO'}
+
+	def execute(self, context):
+		#get the environment variables
+		addon_prefs = PREF.get_anki_exporter_preferences(context)
+		ENV.ENVIRO = addon_prefs.anki_environment
+		env_dct, export_dct, tools_dct = ENV.get_environment()
+
+		# Set project paths
+		addon_prefs.tools_path = tools_dct['tools_path']
+		addon_prefs.export_path = export_dct['export_src_data']
+		addon_prefs.map_path = export_dct['export_map_path']
+		addon_prefs.texture_path = export_dct['export_texture_path']
+
+		return {'FINISHED'}

+ 97 - 0
tools/anki_scene_exporter/ops/export.py

@@ -0,0 +1,97 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSE Panagiotis Christopoulos Charitos and contributors
+# keep methods in alphabetical order
+
+# system imports
+import subprocess
+import os
+
+# blender imports
+import bpy
+from bpy.types import Operator
+import mathutils
+
+# local imports
+from ..lib import gui as GUI
+from ..lib import preferences as PREF
+from ..lib import objects as OBJ
+
+bl_info = {"author": "A. A. Kalugin Jr."}
+
+class OBJECT_OT_anki_export(Operator):
+		"""Export anki scene"""
+		bl_idname = "scene.anki_export"
+		bl_label = "Anki Export"
+		bl_options = {'REGISTER'}
+
+		def execute(self, context):
+				scn = context.scene
+
+				addon_prefs = PREF.get_anki_exporter_preferences(context)
+
+				temp_dea =  addon_prefs.temp_dea
+
+				view_mtx = None
+				cam = None
+				if scn.UseViewport:
+						# Get camera matrix to viewport projection matrix
+						cam = OBJ.get_camera()
+						view_mtx = mathutils.Matrix(cam.matrix_world)
+						r3d = GUI.get_region3d(context)
+						cam.matrix_world = r3d.view_matrix.inverted()
+						print ("Set camera to view projection matrix.")
+
+				# Export the collada file to temp.dea
+				bpy.ops.wm.collada_export(filepath=temp_dea,
+													check_existing=True,
+													filter_blender=False,
+													filter_backup=False,
+													filter_image=False,
+													filter_movie=False,
+													filter_python=False,
+													filter_font=False,
+													filter_sound=False,
+													filter_text=False,
+													filter_btx=False,
+													filter_collada=True,
+													filter_folder=True,
+													filter_blenlib=False,
+													filemode=8,
+													display_type='DEFAULT',
+													sort_method='FILE_SORT_ALPHA',
+													apply_modifiers=False,
+													export_mesh_type=0,
+													export_mesh_type_selection='view',
+													selected=False,
+													include_children=False,
+													include_armatures=False,
+													include_shapekeys=True,
+													deform_bones_only=False,
+													active_uv_only=False,
+													include_uv_textures=False,
+													include_material_textures=False,
+													use_texture_copies=True,
+													triangulate=True,
+													use_object_instantiation=True,
+													sort_by_name=False,
+													export_transformation_type=0,
+													export_transformation_type_selection='matrix',
+													open_sim=False)
+				print ("Exported to:", temp_dea)
+
+				# Restore Camera
+				if scn.UseViewport:
+						cam.matrix_world = view_mtx
+						print ("Restored camera to original projection matrix.")
+
+				# Convert the exported collada
+				relative_path = (addon_prefs.map_path.replace(addon_prefs.anki_project_path + os.sep, ""))
+				cmd  = "ankisceneimp {0} {1} -rpath {2}  -flipyz".format(temp_dea, addon_prefs.map_path, relative_path)
+				subprocess.call(cmd, shell=True)
+				print (cmd)
+				# Run the level in the game
+				test_app = ("{0}/build/bled/bled".format(addon_prefs.anki_project_path))
+
+				subprocess.call(test_app, shell=True)
+
+				return {'FINISHED'}

+ 0 - 0
tools/anki_scene_exporter/props/__init__.py


+ 24 - 0
tools/anki_scene_exporter/props/export_props.py

@@ -0,0 +1,24 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSE Panagiotis Christopoulos Charitos and contributors
+# keep methods in alphabetical order
+
+# blender imports
+import bpy
+from bpy.props import BoolProperty
+
+bl_info = {"author": "A. A. Kalugin Jr."}
+
+bpy.types.Scene.UseViewport = BoolProperty(
+	name = "Use Viewport",
+	description = "True or False?",
+	default= True)
+
+bpy.types.Scene.ExportAllTextures = BoolProperty(
+	name = "Export All Textures",
+	description = "True or False?",
+	default= False)
+
+bpy.types.Scene.UpdateTextures = BoolProperty(
+	name = "Update Textures",
+	description = "True or False?",
+	default= True)

+ 0 - 0
tools/anki_scene_exporter/ui/__init__.py


+ 52 - 0
tools/anki_scene_exporter/ui/gui.py

@@ -0,0 +1,52 @@
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSE Panagiotis Christopoulos Charitos and contributors
+# keep methods in alphabetical order
+
+"""
+Gui Classes
+"""
+# system imports
+import os
+
+# blender imports
+import bpy, os
+from bpy.types import Operator,  Panel, AddonPreferences
+from bpy.props import StringProperty
+
+# local imports
+from ..lib import environment as ENV
+
+bl_info = {"author": "A. A. Kalugin Jr."}
+
+class AnkiPanel:
+	bl_space_type = 'VIEW_3D'
+	bl_region_type = 'TOOLS'
+
+class VIEW3D_PT_prefs_anki(AnkiPanel, Panel):
+	bl_category = "Anki"
+	bl_label = "Export Preferences"
+
+	def draw(self, context):
+		scn = context.scene
+		layout = self.layout
+		col = layout.column(align=True)
+		col.label(text="Texture Export:")
+		row = col.row(align=True)
+		row.prop(scn, "ExportAllTextures")
+		row.prop(scn, "UpdateTextures")
+
+class VIEW3D_PT_tools_anki(AnkiPanel, Panel):
+	bl_category = "Anki"
+	bl_label = "Exporter"
+
+	def draw(self, context):
+		scn = context.scene
+		layout = self.layout
+
+		col = layout.column(align=True)
+
+		split = layout.split()
+		col = split.column()
+		col.prop(scn, 'UseViewport')
+		col = split.column()
+		col.operator("scene.anki_export", icon='PLAY')