]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/__init__.py
Replace single file mode in object exporter with collection
[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         use_strips = bpy.props.BoolProperty(name="Use strips", description="Combine faces into triangle strips", default=True)
44         use_degen_tris = bpy.props.BoolProperty(name="Use degen tris", description="Concatenate triangle strips with degenerate triangles", default=False)
45
46         def draw(self, context):
47                 self.general_col = self.layout.column()
48
49                 col = self.layout.column()
50                 col.label(text="Triangle strips")
51                 col.prop(self, "use_strips")
52                 col.prop(self, "use_degen_tris")
53
54 class ExportMspGLMesh(bpy.types.Operator, ExportMspGLMeshBase):
55         bl_idname = "export_mesh.mspgl_mesh"
56         bl_label = "Export Msp GL mesh"
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
68         filename_ext = ".object"
69
70         collection = bpy.props.BoolProperty(name="As a collection", description="Write all data into a single collection file", default=False)
71         shared_resources = bpy.props.BoolProperty(name="Shared resources", description="Use global names for resource files to enable sharing", default=True)
72
73         export_lods = bpy.props.BoolProperty(name="Export LoDs", description="Export all levels of detail", default=True)
74         use_textures = bpy.props.BoolProperty(name="Use textures", description="Use textures in the exported object", default=True)
75
76         def check(self, context):
77                 ext_changed = self.set_extension(".mdc" if self.collection else ".object")
78                 super_result = super().check(context)
79                 return ext_changed or super_result
80
81         def create_exporter(self):
82                 from .export_object import ObjectExporter
83                 return ObjectExporter()
84
85         def draw(self, context):
86                 super().draw(context)
87
88                 self.general_col.prop(self, "export_lods")
89                 self.general_col.prop(self, "use_textures")
90
91                 col = self.layout.column()
92                 col.label(text="Files")
93                 col.prop(self, "collection")
94                 col.prop(self, "shared_resources")
95
96 class ExportMspGLArmature(bpy.types.Operator, ExportMspGLBase):
97         bl_idname = "export.mspgl_armature"
98         bl_label = "Export Msp GL armature"
99
100         filename_ext = ".arma"
101
102         def create_exporter(self):
103                 from .export_armature import ArmatureExporter
104                 return ArmatureExporter()
105
106 class ExportMspGLAnimation(bpy.types.Operator, ExportMspGLBase):
107         bl_idname = "export.mspgl_animation"
108         bl_label = "Export Msp GL animation"
109
110         filename_ext = ".anim"
111
112         export_all = bpy.props.BoolProperty(name="Export all animations", description="Export all animations present on the selected objects' NLA tracks")
113         collection = bpy.props.BoolProperty(name="As a collection", description="Export the animations as a single collection file", default=True)
114         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)
115
116         def check(self, context):
117                 ext_changed = self.set_extension(".mdc" if self.export_all and self.collection else ".anim")
118                 super_result = super().check(context)
119                 return ext_changed or super_result
120
121         def create_exporter(self):
122                 from .export_animation import AnimationExporter
123                 return AnimationExporter()
124
125         def draw(self, context):
126                 col = self.layout.column()
127                 col.prop(self, "export_all")
128                 if self.export_all:
129                         col.prop(self, "collection")
130                 col.prop(self, "looping_threshold")
131
132 class ExportMspGLScene(bpy.types.Operator, ExportMspGLBase):
133         bl_idname = "export_scene.mspgl_scene"
134         bl_label = "Export Msp GL scene"
135
136         filename_ext = ".scene"
137
138         selected_only = bpy.props.BoolProperty(name="Selected objects only", description="Only export the selected objects")
139         visible_collections = bpy.props.BoolProperty(name="Visible collections only", description="Only export objects in visible collections", default=True)
140         resource_collection = bpy.props.BoolProperty(name="Resource collection", description="Put resources to a single collection file", default=True)
141         skip_existing = bpy.props.BoolProperty(name="Skip existing files", description="Skip resources that already exist as files", default=True)
142
143         def create_exporter(self):
144                 from .export_scene import SceneExporter
145                 return SceneExporter()
146
147         def draw(self, context):
148                 col = self.layout.column()
149                 col.prop(self, "selected_only")
150                 col.prop(self, "visible_collections")
151                 col.prop(self, "resource_collection")
152                 if self.resource_collection:
153                         col.prop(self, "skip_existing")
154
155 class ExportMspGLCamera(bpy.types.Operator, ExportMspGLBase):
156         bl_idname = "export.mspgl_camera"
157         bl_label = "Export Msp GL camera"
158
159         filename_ext = ".camera"
160
161         def create_exporter(self):
162                 from .export_camera import CameraExporter
163                 return CameraExporter()
164
165 def menu_func_export(self, context):
166         self.layout.operator(ExportMspGLMesh.bl_idname, text="Msp GL mesh")
167         self.layout.operator(ExportMspGLObject.bl_idname, text="Msp GL object")
168         self.layout.operator(ExportMspGLArmature.bl_idname, text="Msp GL armature")
169         self.layout.operator(ExportMspGLAnimation.bl_idname, text="Msp GL animation")
170         self.layout.operator(ExportMspGLScene.bl_idname, text="Msp GL scene")
171         self.layout.operator(ExportMspGLCamera.bl_idname, text="Msp GL camera")
172
173 classes = [ExportMspGLMesh, ExportMspGLObject, ExportMspGLArmature, ExportMspGLAnimation, ExportMspGLScene, ExportMspGLCamera]
174
175 def register():
176         for c in classes:
177                 bpy.utils.register_class(c)
178
179         bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
180
181         from .properties import register_properties
182         register_properties()
183
184 def unregister():
185         for c in classes:
186                 bpy.utils.unregister_class(c)
187
188         bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
189
190         from .properties import unregister_properties
191         unregister_properties()