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