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