-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexporters.py
336 lines (272 loc) · 10.7 KB
/
exporters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
'''
Copyright (C) 2021-2022 Orange Turbine
https://orangeturbine.com
Created by Jason van Gumster
This file is part of Export to .blend.
Export to .blend is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>.
'''
import bpy
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty
from bpy.types import Operator
# Local imports
from .functions import export_blend_objects, export_blend_nodes
from .utilities import mode_toggle
class ExportBlenderObjects(Operator, ExportHelper):
"""Export some or all of your Blender scene to a .blend file"""
bl_idname = "export_scene.blend"
bl_label = "Export Blender"
filename_ext = ".blend"
filter_glob: StringProperty(
default="*.blend",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
directory: StringProperty(
default="//"
)
filename: StringProperty(
default=""
)
# Operator properties
export_selected: BoolProperty(
name="Export Selected",
description="Export selected objects to .blend file",
default=True
)
export_as_collection: BoolProperty(
name="Export as Collection",
description="Bundle selected objects in a collection before exporting",
default=False
)
collection_name: StringProperty(
name="Collection Name",
description="Name of exported collection",
default="export_collection"
)
mark_asset: BoolProperty(
name="Mark as Asset",
description="Mark selected objects as assets for visibility in the Asset Browser",
default=False
)
backlink: BoolProperty(
name="Backlink",
description="Replace selection with a link to the exported object",
default=False
)
def invoke(self, context, event):
preferences = context.preferences.addons[__package__].preferences
self.directory = preferences.filepath
self.filename = context.active_object.name
self.export_as_collection = preferences.export_as_collection
self.backlink = preferences.backlink
self.collection_name = context.active_object.name
if bpy.app.version > (2, 93, 0):
self.mark_asset = preferences.mark_asset
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def invoke(self, context, event):
preferences = context.preferences.addons[__package__].preferences
self.directory = preferences.filepath
self.filename = context.active_object.name
self.export_as_collection = preferences.export_as_collection
self.backlink = preferences.backlink
self.collection_name = context.active_object.name
if bpy.app.version > (2, 93, 0):
self.mark_asset = preferences.mark_asset
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def draw(self, context):
layout = self.layout
col = layout.column()
col.prop(self, "export_selected")
if self.export_selected:
box = col.box()
box.prop(self, "export_as_collection")
if self.export_as_collection:
box.prop(self, "collection_name", icon="COLLECTION_NEW", icon_only=True)
if bpy.app.version > (2, 93, 0):
box.prop(self, "mark_asset")
box.prop(self, "backlink")
def execute(self, context):
export_settings = {
"is_collection": False,
"filepath": self.filepath,
"export_selected": self.export_selected,
"export_as_collection": self.export_as_collection,
"collection_name": self.collection_name,
"backlink": self.backlink
}
if bpy.app.version > (2, 93, 0):
export_settings["mark_asset"] = self.mark_asset
else:
export_settings["mark_asset"] = False
# switching to object mode prevents unexpected behavior
prev_mode = mode_toggle(context, 'OBJECT')
export_blend_objects(context, export_settings)
mode_toggle(context, prev_mode)
return {'FINISHED'}
class ExportBlenderCollection(Operator, ExportHelper):
"""Export a selected collection to a separate .blend file"""
bl_idname = "export_collection.blend"
bl_label = "Export Blender Collection"
filename_ext = ".blend"
filter_glob: StringProperty(
default="*.blend",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
directory: StringProperty(
default="//"
)
filename: StringProperty(
default=""
)
# Operator properties
mark_asset: BoolProperty(
name="Mark as Asset",
description="Mark selected collection as an asset for visibility in the Asset Browser",
default=False
)
backlink: BoolProperty(
name="Backlink",
description="Replace selection with a link to the exported object",
default=False
)
def invoke(self, context, event):
preferences = context.preferences.addons[__package__].preferences
self.directory = preferences.filepath
self.filename = context.collection.name
self.backlink = preferences.backlink
if bpy.app.version > (2, 93, 0):
self.mark_asset = preferences.mark_asset
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def invoke(self, context, event):
preferences = context.preferences.addons[__package__].preferences
self.directory = preferences.filepath
self.filename = context.collection.name
self.backlink = preferences.backlink
if bpy.app.version > (2, 93, 0):
self.mark_asset = preferences.mark_asset
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
export_settings = {
"is_collection": True,
"filepath": self.filepath,
"export_selected": True,
"export_as_collection": True,
"collection_name": context.selected_ids[0].name, #XXX Assumes only one collection is selected
"backlink": self.backlink
}
if bpy.app.version > (2, 93, 0):
export_settings["mark_asset"] = self.mark_asset
else:
export_settings["mark_asset"] = False
# switching to object mode prevents unexpected behavior
prev_mode = mode_toggle(context, 'OBJECT')
export_blend_objects(context, export_settings)
mode_toggle(context, prev_mode)
return {'FINISHED'}
class ExportBlenderNodes(Operator, ExportHelper):
"""Export selected nodes to a separate .blend file"""
bl_idname = "export_nodes.blend"
bl_label = "Export Blender Nodes"
filename_ext = ".blend"
filter_glob: StringProperty(
default="*.blend",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
directory: StringProperty(
default="//"
)
filename: StringProperty(
default=""
)
# Operator properties
export_selected: BoolProperty(
name="Export Selected",
description="Export selected nodes to .blend file. Otherwise, export the active node tree",
default=True
)
export_as_group: BoolProperty(
name="Export as Node Group",
description="Bundle selected objects in a node group before exporting",
default=False
)
group_name: StringProperty(
name="Group Name",
description="Name of exported node group",
default="export_group"
)
is_compositor: BoolProperty(
name="Is Compositor",
description="Should be True if exporting Compositor nodes",
default=False
)
backlink: BoolProperty(
name="Backlink",
description="Replace selection with a link to the exported node group",
default=False
)
def invoke(self, context, event):
preferences = context.preferences.addons[__package__].preferences
self.directory = preferences.filepath
self.export_as_group = preferences.export_as_group
self.backlink = preferences.backlink
if bpy.app.version > (2, 93, 0):
self.mark_asset = preferences.mark_asset
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def invoke(self, context, event):
preferences = context.preferences.addons[__package__].preferences
self.directory = preferences.filepath
self.export_as_group = preferences.export_as_group
self.backlink = preferences.backlink
if bpy.app.version > (2, 93, 0):
self.mark_asset = preferences.mark_asset
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def draw(self, context):
layout = self.layout
col = layout.column()
if self.is_compositor:
self.export_selected = True
self.export_as_group = True
col.enabled = False
col.prop(self, "export_selected")
if self.export_selected:
box = col.box()
box.prop(self, "export_as_group")
if self.export_as_group and not self.is_compositor:
box.prop(self, "group_name", icon="NODETREE", icon_only=True)
box.prop(self, "backlink")
if self.is_compositor:
col = layout.column()
col.prop(self, "group_name", icon="NODETREE", icon_only=True)
col.prop(self, "backlink")
def execute(self, context):
export_settings = {
"filepath": self.filepath,
"export_selected": self.export_selected,
"export_as_group": self.export_as_group,
"group_name": self.group_name,
"backlink": self.backlink
}
# switching to object mode prevents unexpected behavior
prev_mode = mode_toggle(context, 'OBJECT')
export_blend_nodes(context, export_settings)
mode_toggle(context, prev_mode)
return {'FINISHED'}