]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/__init__.py
cba8178f66b06c2b36d775a5c37a125cf534d3cd
[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", "export_animation", "export_armature", "export_camera", "export_material", "export_mesh", "export_object", "export_scene", "export_texture", "material", "mesh", "properties", "scene", "util":
12                 if sub in locals():
13                         imp.reload(locals()[sub])
14
15 import os
16 import bpy
17 import bpy_extras
18
19 class ExportHelper(bpy_extras.io_utils.ExportHelper):
20         def set_extension(self, ext):
21                 ext_changed = (ext!=self.filename_ext)
22                 if ext_changed:
23                         if self.filepath.endswith(self.filename_ext):
24                                 self.filepath = self.filepath[:-len(self.filename_ext)]
25                         self.filename_ext = ext
26                 return ext_changed
27
28 class ExportMspGLData(bpy.types.Operator):
29         bl_idname = "export.mspgl_data"
30         bl_label = "Export Msp GL data"
31         bl_description = "Export object data in Msp GL format"
32
33         filepath: bpy.props.StringProperty(name="File path", description="File path for exporting the data", subtype='FILE_PATH')
34         collection: bpy.props.BoolProperty(name="As a collection", description="Export all data as a single collection file", default=False)
35         shared_resources: bpy.props.BoolProperty(name="Shared resources", description="Use global names for resource files to enable sharing", default=True)
36
37         @classmethod
38         def poll(cls, context):
39                 return len(context.selected_objects)>0
40
41         def invoke(self, context, event):
42                 blend_filepath = context.blend_data.filepath
43                 if blend_filepath:
44                         self.filepath = os.path.splitext(blend_filepath)[0]+".mdc"
45                 else:
46                         self.filepath = "data.mdc"
47                 context.window_manager.fileselect_add(self)
48                 return {'RUNNING_MODAL'}
49
50         def execute(self, context):
51                 from .export import DataExporter
52                 exporter = DataExporter()
53                 exporter.collection = self.collection
54                 exporter.shared_resources = self.shared_resources
55                 exporter.export_to_file(context, self.filepath)
56                 return {'FINISHED'}
57
58         def draw(self, context):
59                 col = self.layout.column()
60                 col.label(text="Files")
61                 col.prop(self, "collection")
62                 col.prop(self, "shared_resources")
63
64 class ExportMspGLAnimation(bpy.types.Operator, ExportHelper):
65         bl_idname = "export.mspgl_animation"
66         bl_label = "Export Msp GL animation"
67         bl_description = "Export one or more animations in Msp GL format"
68
69         filename_ext = ".anim"
70
71         export_all: bpy.props.BoolProperty(name="Export all animations", description="Export all animations present on the selected objects' NLA tracks")
72         collection: bpy.props.BoolProperty(name="As a collection", description="Export the animations as a single collection file", default=True)
73         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)
74
75         def check(self, context):
76                 ext_changed = self.set_extension(".mdc" if self.export_all and self.collection else ".anim")
77                 super_result = super().check(context)
78                 return ext_changed or super_result
79
80         def execute(self, context):
81                 from .export_animation import AnimationExporter
82                 exporter = AnimationExporter()
83                 exporter.export_all = self.export_all
84                 exporter.collection = self.collection
85                 exporter.looping_threshold = self.looping_threshold
86                 exporter.export_to_file(context, self.filepath)
87                 return {'FINISHED'}
88
89         def draw(self, context):
90                 col = self.layout.column()
91                 col.prop(self, "export_all")
92                 if self.export_all:
93                         col.prop(self, "collection")
94                 col.prop(self, "looping_threshold")
95
96 class ExportMspGLScene(bpy.types.Operator, ExportHelper):
97         bl_idname = "export_scene.mspgl_scene"
98         bl_label = "Export Msp GL scene"
99         bl_description = "Export the active scene in Msp GL format"
100
101         filename_ext = ".scene"
102
103         selected_only: bpy.props.BoolProperty(name="Selected objects only", description="Only export the selected objects", default=False)
104         visible_only: bpy.props.BoolProperty(name="Visible only", description="Only export objects in visible collections", default=True)
105         collection: bpy.props.BoolProperty(name="As a collection", description="Export the scene and all resources as a single collection file", default=False)
106         skip_existing: bpy.props.BoolProperty(name="Skip existing files", description="Skip resources that already exist as files", default=True)
107
108         def invoke(self, context, event):
109                 self.filepath = context.scene.name+".scene"
110                 return super().invoke(context, event)
111
112         def check(self, context):
113                 ext_changed = self.set_extension(".mdc" if self.collection else ".scene")
114                 super_result = super().check(context)
115                 return ext_changed or super_result
116
117         def execute(self, context):
118                 from .export_scene import SceneExporter
119                 exporter = SceneExporter()
120                 exporter.selected_only = self.selected_only
121                 exporter.visible_only = self.visible_only
122                 exporter.collection = self.collection
123                 exporter.skip_existing = self.skip_existing
124                 exporter.export_to_file(context, self.filepath)
125                 return {'FINISHED'}
126
127         def draw(self, context):
128                 col = self.layout.column()
129                 col.prop(self, "selected_only")
130                 col.prop(self, "visible_only")
131                 col.prop(self, "collection")
132                 if self.collection:
133                         col.prop(self, "skip_existing")
134
135 class ExportMspGLProject(bpy.types.Operator):
136         bl_idname = "export.mspgl_project"
137         bl_label = "Export Msp GL project"
138         bl_description = "Export the entire project in Msp GL format"
139
140         directory: bpy.props.StringProperty(name="Directory", description="Directory for exporting the data", subtype='FILE_PATH')
141
142         def invoke(self, context, event):
143                 blend_filepath = context.blend_data.filepath
144                 if blend_filepath:
145                         self.directory = os.path.split(blend_filepath)[0]
146                 context.window_manager.fileselect_add(self)
147                 return {'RUNNING_MODAL'}
148
149         def execute(self, context):
150                 from .export import ProjectExporter
151                 exporter = ProjectExporter()
152                 exporter.export_to_directory(context, self.directory)
153                 return {'FINISHED'}
154
155 class AddUniform(bpy.types.Operator):
156         bl_idname = "material.add_uniform"
157         bl_label = "Add Uniform"
158         bl_description = "Add a new uniform value to the material"
159
160         def execute(self, context):
161                 mat = context.active_object.active_material
162                 mat.uniforms.add()
163                 mat.active_uniform_index = len(mat.uniforms)-1
164
165                 return {"FINISHED"}
166
167 class RemoveUniform(bpy.types.Operator):
168         bl_idname = "material.remove_uniform"
169         bl_label = "Remove Uniform"
170         bl_description = "Remove the selected uniform from the material"
171
172         def execute(self, context):
173                 mat = context.active_object.active_material
174                 mat.uniforms.remove(mat.active_uniform_index)
175                 mat.active_uniform_index = min(mat.active_uniform_index, len(mat.uniforms)-1)
176
177                 return {"FINISHED"}
178
179 def menu_func_export(self, context):
180         self.layout.operator(ExportMspGLData.bl_idname, text="Msp GL data")
181         self.layout.operator(ExportMspGLAnimation.bl_idname, text="Msp GL animation")
182         self.layout.operator(ExportMspGLScene.bl_idname, text="Msp GL scene")
183         self.layout.operator(ExportMspGLProject.bl_idname, text="Msp GL project")
184
185 classes = [ExportMspGLData, ExportMspGLAnimation, ExportMspGLScene, ExportMspGLProject, AddUniform, RemoveUniform]
186
187 def register():
188         for c in classes:
189                 bpy.utils.register_class(c)
190
191         bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
192
193         from .properties import register_properties
194         register_properties()
195
196 def unregister():
197         for c in classes:
198                 bpy.utils.unregister_class(c)
199
200         bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
201
202         from .properties import unregister_properties
203         unregister_properties()