From 2f20c5941aef604c5a49eecebec0fb7f2d2f6dbd Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 26 May 2019 21:18:01 +0300 Subject: [PATCH] Add an exporter for Blender cameras --- blender/io_mspgl/__init__.py | 13 ++++++++++++- blender/io_mspgl/export_camera.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 blender/io_mspgl/export_camera.py diff --git a/blender/io_mspgl/__init__.py b/blender/io_mspgl/__init__.py index 905bfe73..87aedae5 100644 --- a/blender/io_mspgl/__init__.py +++ b/blender/io_mspgl/__init__.py @@ -7,7 +7,7 @@ bl_info = { if "bpy" in locals(): import imp - for sub in "armature", "datafile", "export_armature", "export_material", "export_mesh", "export_object", "export_scene", "export_texture", "material", "mesh", "properties", "util": + for sub in "armature", "datafile", "export_armature", "export_camera", "export_material", "export_mesh", "export_object", "export_scene", "export_texture", "material", "mesh", "properties", "util": if sub in locals(): imp.reload(locals()[sub]) @@ -106,11 +106,22 @@ class ExportMspGLScene(bpy.types.Operator, ExportMspGLBase): col = self.layout.column() col.prop(self, "resource_collection") +class ExportMspGLCamera(bpy.types.Operator, ExportMspGLBase): + bl_idname = "export.mspgl_camera" + bl_label = "Export Msp GL camera" + + filename_ext = ".camera" + + def create_exporter(self): + from .export_camera import CameraExporter + return CameraExporter() + def menu_func_export(self, context): self.layout.operator(ExportMspGLMesh.bl_idname, text="Msp GL mesh") self.layout.operator(ExportMspGLObject.bl_idname, text="Msp GL object") self.layout.operator(ExportMspGLArmature.bl_idname, text="Msp GL armature") self.layout.operator(ExportMspGLScene.bl_idname, text="Msp GL scene") + self.layout.operator(ExportMspGLCamera.bl_idname, text="Msp GL camera") from .properties import MspGLMeshProperties, MspGLObjectProperties diff --git a/blender/io_mspgl/export_camera.py b/blender/io_mspgl/export_camera.py new file mode 100644 index 00000000..1594423a --- /dev/null +++ b/blender/io_mspgl/export_camera.py @@ -0,0 +1,29 @@ +import math +import mathutils + +class CameraExporter: + def export_to_file(self, context, out_fn): + obj = context.active_object + + resource = self.export_camera(obj) + + with open(out_fn, "w") as out_file: + for s in resource.statements: + s.write_to_file(out_file) + + def export_camera(self, obj): + if obj.type!='CAMERA': + raise Exception("Object is not a camera") + + from .datafile import Resource, Statement + resource = Resource(obj.name+".camera") + + position = obj.matrix_world*mathutils.Vector((0, 0, 0)) + resource.statements.append(Statement("position", position[0], position[1], position[2])) + look_dir = obj.matrix_world*mathutils.Vector((0, 0, -1, 0)) + resource.statements.append(Statement("look_direction", look_dir[0], look_dir[1], look_dir[2])) + resource.statements.append(Statement("up_direction", 0.0, 0.0, 1.0)) + resource.statements.append(Statement("field_of_view", obj.data.angle_y*180/math.pi)) + resource.statements.append(Statement("depth_clip", obj.data.clip_start, obj.data.clip_end)) + + return resource -- 2.45.2