]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/__init__.py
473750a53441ec3abb591d374e25b80c16f830e3
[libs/gl.git] / blender / io_mspgl / __init__.py
1 bl_info = {
2         "name": "Msp GL datafiles",
3         "blender": (2, 80, 0),
4         "author": "Mikko Rasa",
5         "location": "File > Export",
6         "description": "Export Msp GL data",
7         "category": "Import-Export" }
8
9 if "bpy" in locals():
10         import imp
11         for sub in "animation", "armature", "datafile", "export_animation", "export_armature", "export_camera", "export_material", "export_mesh", "export_object", "export_scene", "export_texture", "material", "mesh", "properties", "util":
12                 if sub in locals():
13                         imp.reload(locals()[sub])
14
15 import bpy
16 from bpy_extras.io_utils import ExportHelper
17
18 class ExportMspGLBase(ExportHelper):
19         show_progress: bpy.props.BoolProperty(name="Show progress", description="Display progress indicator while exporting", default=True)
20
21         def set_extension(self, ext):
22                 ext_changed = (ext!=self.filename_ext)
23                 if ext_changed:
24                         if self.filepath.endswith(self.filename_ext):
25                                 self.filepath = self.filepath[:-len(self.filename_ext)]
26                         self.filename_ext = ext
27                 return ext_changed
28
29         def execute(self, context):
30                 exporter = self.create_exporter()
31                 self.prepare_exporter(exporter)
32                 exporter.export_to_file(context, self.filepath)
33                 return {"FINISHED"}
34
35         def create_exporter(self):
36                 raise Exception("create_exporter must be overridden")
37
38         def prepare_exporter(self, exporter):
39                 for k, v in self.as_keywords().items():
40                         setattr(exporter, k, v)
41
42 class ExportMspGLMeshBase(ExportMspGLBase):
43         export_all: bpy.props.BoolProperty(name="Export all selected", description="Export all selected objects (use generated filenames)", default=False)
44
45         def draw(self, context):
46                 self.general_col = self.layout.column()
47
48                 col = self.layout.column()
49                 if len(context.selected_objects)>1:
50                         col.label(text="Object selection")
51                         col.prop(self, "export_all")
52
53 class ExportMspGLMesh(bpy.types.Operator, ExportMspGLMeshBase):
54         bl_idname = "export_mesh.mspgl_mesh"
55         bl_label = "Export Msp GL mesh"
56         bl_description = "Export one or more meshes in Msp GL format"
57
58         filename_ext = ".mesh"
59
60         def create_exporter(self):
61                 from .export_mesh import MeshExporter
62                 return MeshExporter()
63
64 class ExportMspGLObject(bpy.types.Operator, ExportMspGLMeshBase):
65         bl_idname = "export_mesh.mspgl_object"
66         bl_label = "Export Msp GL object"
67         bl_description = "Export one or more objects in Msp GL format"
68
69         filename_ext = ".object"
70
71         collection: bpy.props.BoolProperty(name="As a collection", description="Write all data into a single collection file", default=False)
72         shared_resources: bpy.props.BoolProperty(name="Shared resources", description="Use global names for resource files to enable sharing", default=True)
73
74         def check(self, context):
75                 ext_changed = self.set_extension(".mdc" if self.collection else ".object")
76                 super_result = super().check(context)
77                 return ext_changed or super_result
78
79         def create_exporter(self):
80                 from .export_object import ObjectExporter
81                 return ObjectExporter()
82
83         def draw(self, context):
84                 super().draw(context)
85
86                 col = self.layout.column()
87                 col.label(text="Files")
88                 col.prop(self, "collection")
89                 col.prop(self, "shared_resources")
90
91 class ExportMspGLArmature(bpy.types.Operator, ExportMspGLBase):
92         bl_idname = "export.mspgl_armature"
93         bl_label = "Export Msp GL armature"
94         bl_description = "Export an armature in Msp GL format"
95
96         filename_ext = ".arma"
97
98         def create_exporter(self):
99                 from .export_armature import ArmatureExporter
100                 return ArmatureExporter()
101
102 class ExportMspGLAnimation(bpy.types.Operator, ExportMspGLBase):
103         bl_idname = "export.mspgl_animation"
104         bl_label = "Export Msp GL animation"
105         bl_description = "Export one or more animations in Msp GL format"
106
107         filename_ext = ".anim"
108
109         export_all: bpy.props.BoolProperty(name="Export all animations", description="Export all animations present on the selected objects' NLA tracks")
110         collection: bpy.props.BoolProperty(name="As a collection", description="Export the animations as a single collection file", default=True)
111         looping_threshold: bpy.props.FloatProperty(name="Looping threshold", description="Tolerance for curve beginning and end values for looping", min=0.0, soft_max=1.0, precision=4, default=0.001)
112
113         def check(self, context):
114                 ext_changed = self.set_extension(".mdc" if self.export_all and self.collection else ".anim")
115                 super_result = super().check(context)
116                 return ext_changed or super_result
117
118         def create_exporter(self):
119                 from .export_animation import AnimationExporter
120                 return AnimationExporter()
121
122         def draw(self, context):
123                 col = self.layout.column()
124                 col.prop(self, "export_all")
125                 if self.export_all:
126                         col.prop(self, "collection")
127                 col.prop(self, "looping_threshold")
128
129 class ExportMspGLScene(bpy.types.Operator, ExportMspGLBase):
130         bl_idname = "export_scene.mspgl_scene"
131         bl_label = "Export Msp GL scene"
132         bl_description = "Export the active scene in Msp GL format"
133
134         filename_ext = ".scene"
135
136         selected_only: bpy.props.BoolProperty(name="Selected objects only", description="Only export the selected objects")
137         visible_collections: bpy.props.BoolProperty(name="Visible collections only", description="Only export objects in visible collections", default=True)
138         resource_collection: bpy.props.BoolProperty(name="Resource collection", description="Put resources to a single collection file", default=True)
139         skip_existing: bpy.props.BoolProperty(name="Skip existing files", description="Skip resources that already exist as files", default=True)
140
141         def create_exporter(self):
142                 from .export_scene import SceneExporter
143                 return SceneExporter()
144
145         def draw(self, context):
146                 col = self.layout.column()
147                 col.prop(self, "selected_only")
148                 col.prop(self, "visible_collections")
149                 col.prop(self, "resource_collection")
150                 if self.resource_collection:
151                         col.prop(self, "skip_existing")
152
153 class ExportMspGLCamera(bpy.types.Operator, ExportMspGLBase):
154         bl_idname = "export.mspgl_camera"
155         bl_label = "Export Msp GL camera"
156         bl_description = "Export a camera in Msp GL format"
157
158         filename_ext = ".camera"
159
160         def create_exporter(self):
161                 from .export_camera import CameraExporter
162                 return CameraExporter()
163
164 class AddUniform(bpy.types.Operator):
165         bl_idname = "material.add_uniform"
166         bl_label = "Add Uniform"
167         bl_description = "Add a new uniform value to the material"
168
169         def execute(self, context):
170                 mat = context.active_object.active_material
171                 mat.uniforms.add()
172                 mat.active_uniform_index = len(mat.uniforms)-1
173
174                 return {"FINISHED"}
175
176 class RemoveUniform(bpy.types.Operator):
177         bl_idname = "material.remove_uniform"
178         bl_label = "Remove Uniform"
179         bl_description = "Remove the selected uniform from the material"
180
181         def execute(self, context):
182                 mat = context.active_object.active_material
183                 mat.uniforms.remove(mat.active_uniform_index)
184                 mat.active_uniform_index = min(mat.active_uniform_index, len(mat.uniforms)-1)
185
186                 return {"FINISHED"}
187
188 def menu_func_export(self, context):
189         self.layout.operator(ExportMspGLMesh.bl_idname, text="Msp GL mesh")
190         self.layout.operator(ExportMspGLObject.bl_idname, text="Msp GL object")
191         self.layout.operator(ExportMspGLArmature.bl_idname, text="Msp GL armature")
192         self.layout.operator(ExportMspGLAnimation.bl_idname, text="Msp GL animation")
193         self.layout.operator(ExportMspGLScene.bl_idname, text="Msp GL scene")
194         self.layout.operator(ExportMspGLCamera.bl_idname, text="Msp GL camera")
195
196 classes = [ExportMspGLMesh, ExportMspGLObject, ExportMspGLArmature, ExportMspGLAnimation, ExportMspGLScene, ExportMspGLCamera, AddUniform, RemoveUniform]
197
198 def register():
199         for c in classes:
200                 bpy.utils.register_class(c)
201
202         bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
203
204         from .properties import register_properties
205         register_properties()
206
207 def unregister():
208         for c in classes:
209                 bpy.utils.unregister_class(c)
210
211         bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
212
213         from .properties import unregister_properties
214         unregister_properties()