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