-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3dm_exporter.py
403 lines (320 loc) · 12.6 KB
/
3dm_exporter.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#
# Blender-OpenComputers-3DM-Exporter - Blender Minecraft OpenComputers 3DM (print3d) Exporter
#
# Copyright (c) 2016 Kevin Velickovic
#
#
# Author(s):
#
# Kevin Velickovic <[email protected]>
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# Additional Terms:
#
# You are required to preserve legal notices and author attributions in
# that material or in the Appropriate Legal Notices displayed by works
# containing it.
#
# Imports
import bpy
from bpy.props import ( StringProperty, BoolProperty, IntProperty )
# Plugin details
bl_info = {
'name': 'OpenComputers 3DM Exporter',
'author': 'Kevin Velickovic (F0x06)',
'version': (0, 1, 0),
'blender': (2, 76, 0),
'location': 'Scene properties > Minecarft OpenComputers 3D exporter',
'description': 'Export voxel model to OpenComputers 3DM format',
'warning': '',
'wiki_url': '',
'tracker_url': '',
'support': 'COMMUNITY',
'category': 'Exporter'}
# Function to initialize properties
def initProps():
# Object export settings
bpy.types.Scene.f0x_3dm_tint_param = BoolProperty(
name="Export tints ( diffuse color )",
description="",
default = True )
# 3DM model export settings
bpy.types.Scene.f0x_3dm_label_param = StringProperty(
name="Label",
default = "" )
bpy.types.Scene.f0x_3dm_tooltip_param = StringProperty(
name="Tooltip",
default = "" )
bpy.types.Scene.f0x_3dm_lightlevel_param = IntProperty(
name="Light level",
default = 0,
min = 0,
max = 8 )
bpy.types.Scene.f0x_3dm_emitredstone_param = BoolProperty(
name="Emit redstone",
default = False )
bpy.types.Scene.f0x_3dm_buttonmode_param = BoolProperty(
name="Button mode",
default = False )
bpy.types.Scene.f0x_3dm_collidable_param = BoolProperty(
name="Collidable",
default = True )
# Function to unload properties
def unloadProps():
# Object export settings
del bpy.types.Scene.f0x_3dm_tint_param
# 3DM model export settings
del bpy.types.Scene.f0x_3dm_label_param
del bpy.types.Scene.f0x_3dm_tooltip_param
del bpy.types.Scene.f0x_3dm_lightlevel_param
del bpy.types.Scene.f0x_3dm_emitredstone_param
del bpy.types.Scene.f0x_3dm_buttonmode_param
del bpy.types.Scene.f0x_3dm_collidable_param
# Function to calculate bottom vertex coordinates
def get_bottom_vertex( _obj ):
# Compute arrays
x_values = []
y_values = []
z_values = []
# Iterate over object verticles
for v in _obj.data.vertices:
# Get verticle world coordinates
coords = ( _obj.matrix_world * v.co )
# Append values to compute arrays
x_values.append( coords[ 0 ] )
y_values.append( coords[ 1 ] )
z_values.append( coords[ 2 ] )
# Build result
return [
round( min( x_values ) + 8 ),
round( min( z_values ) + 0 ),
round( min( y_values ) + 8 )
]
# Function to calculate top vertex coordinates
def get_top_vertex( _obj ):
# Compute arrays
x_values = []
y_values = []
z_values = []
# Iterate over object verticles
for v in _obj.data.vertices:
# Get verticle world coordinates
coords = ( _obj.matrix_world * v.co )
# Append values to compute arrays
x_values.append( coords[ 0 ] )
y_values.append( coords[ 1 ] )
z_values.append( coords[ 2 ] )
# Build result
return [
round( max( x_values ) + 8 ),
round( max( z_values ) + 0 ),
round( max( y_values ) + 8 )
]
# Function to generate 3DM file
def generate_3dm_file( file_path ):
# Scene container
scene = bpy.context.scene
# Open output file
with open( file_path, "w" ) as output_file:
# Write 3DM header
output_file.write("{\n")
output_file.write(" label = \"%s\",\n" % ( scene.f0x_3dm_label_param ) )
output_file.write(" tooltip = \"%s\",\n" % ( scene.f0x_3dm_tooltip_param ) )
output_file.write(" lightLevel = %d,\n" % ( scene.f0x_3dm_lightlevel_param ) )
output_file.write(" emitRedstone = %s,\n" % ( "True" if scene.f0x_3dm_emitredstone_param else "False" ) )
output_file.write(" buttonMode = %s,\n" % ( "True" if scene.f0x_3dm_buttonmode_param else "False" ) )
output_file.write(" collidable = %s,\n" % ( "True" if scene.f0x_3dm_collidable_param else "False" ) )
output_file.write(" shapes={\n")
# Iterate over scene objects
for _obj in bpy.data.objects:
# Check if object is a cube
if _obj and not _obj.hide and _obj.type == 'MESH' and _obj.data.name.startswith("Cube"):
# Check if object is in the active layer
if _obj.layers[ bpy.context.scene.active_layer ]:
# +------+ top_corner_pos
# / /|
# +------+ |
# | | +
# | |/
# bot_corner_pos +------+
# Compute world coordinates of corners and align them to 3DM start position
bot_corner_pos = get_bottom_vertex( _obj )
top_corner_pos = get_top_vertex( _obj )
# Tint variable
object_tint = '0x000000'
# Check if object have material
if _obj.active_material:
# Check for default diffuse color
if _obj.active_material.diffuse_color.hsv != (0.0, 0.0, 0.800000011920929):
# Get object tint ( diffuse color )
object_tint = '0x%02x%02x%02x' % ( round( _obj.active_material.diffuse_color.r * 255.0 ),
round( _obj.active_material.diffuse_color.g * 255.0 ),
round( _obj.active_material.diffuse_color.b * 255.0 ) )
# Write block data to 3DM file
output_file.write(" {%02d, %02d, %02d, %02d, %02d, %02d, texture=\"%s\", tint = %s},\n" % (
bot_corner_pos[0], bot_corner_pos[1], bot_corner_pos[2],
top_corner_pos[0], top_corner_pos[1], top_corner_pos[2],
_obj.active_material.name if _obj.active_material else "planks_oak",
object_tint if bpy.types.Scene.f0x_3dm_tint_param else '0x000000'
))
# Write footer
output_file.write(" }\n")
output_file.write("}\n")
# Class to handle file save dialog and 3DM file generation
class f0x_export_3dm(bpy.types.Operator):
# Properties
bl_idname = "f0x.export_3dm"
bl_label = "Export 3DM file"
filepath = bpy.props.StringProperty(subtype="FILE_PATH")
filter_glob = StringProperty(
default="*.3dm",
options={'HIDDEN'},
)
@classmethod
def poll(cls, context):
return ( len( bpy.context.scene.objects ) > 0 )
def execute(self, context):
generate_3dm_file( self.filepath )
return {'FINISHED'}
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
# Function to get selected object
def get_selected():
# Iterate over scene objects
for _obj in bpy.data.objects:
# Check if object is valid and selected
if _obj and _obj.select:
# Return result
return _obj
# Function to deselect all object
def deselect_all():
# Iterate over scene objects
for _obj in bpy.data.objects:
# Check if object is valid
if _obj:
# Deselect object
_obj.select = False
# Function to find bounding box
def boundingbox_find():
# Iterate over scene objects
for _obj in bpy.data.objects:
# Check if object is a bounding box
if _obj and "3dm_boundingbox_vtx" in _obj.data.name:
# Check if object is in the active layer
if _obj.layers[ bpy.context.scene.active_layer ]:
# Return object
return _obj
# Function to add a bounding box
class f0x_boundingbox_add(bpy.types.Operator):
# Properties
bl_idname = "f0x.boundingbox_add"
bl_label = "Add a bounding box"
@classmethod
def poll(cls, context):
return ( boundingbox_find() == None ) and ( bpy.context.active_object.mode == 'OBJECT' )
def execute(self, context):
# Deselect all objects
deselect_all()
# Create a cube
bpy.ops.mesh.primitive_cube_add(location=(0,0,8))
added_cube = get_selected()
# Cube settings
added_cube.dimensions = [ 16, 16, 16 ]
added_cube.draw_type = 'WIRE'
added_cube.hide_select = True
added_cube.hide_render = True
added_cube.name = '3dm_boundingbox'
added_cube.data.name = '3dm_boundingbox_vtx'
# Move cube origin
saved_location = bpy.context.scene.cursor_location.copy()
bpy.context.scene.cursor_location = ( -8, -8, 0 )
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.context.scene.cursor_location = saved_location
# Deselect cube
added_cube.select = False
return {'FINISHED'}
# Function to remove a bounding box
class f0x_boundingbox_remove(bpy.types.Operator):
# Properties
bl_idname = "f0x.boundingbox_remove"
bl_label = "Remove a bounding box"
@classmethod
def poll(cls, context):
return ( boundingbox_find() != None ) and ( bpy.context.active_object.mode == 'OBJECT' )
def execute(self, context):
# Deselect all objects
deselect_all()
# Find bounding box
bbox = boundingbox_find()
# If there is a bounding box
if bbox:
# Remove bounding box
bbox.hide_select = False
bbox.select = True
bpy.ops.object.delete()
return {'FINISHED'}
# Class to handle panel drawing
class MainPanel(bpy.types.Panel):
# Properties
bl_label = "OpenComputers 3DM Exporter"
bl_idname = "SCENE_PT_layout"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def draw(self, context):
layout = self.layout
layout.label(text="Object export settings")
row = layout.row()
row.prop(context.scene, "f0x_3dm_tint_param")
layout.label(text="3DM model settings")
row = layout.row()
row.prop(context.scene, "f0x_3dm_label_param")
row = layout.row()
row.prop(context.scene, "f0x_3dm_tooltip_param")
row = layout.row()
row.prop(context.scene, "f0x_3dm_lightlevel_param")
row = layout.row()
row.prop(context.scene, "f0x_3dm_emitredstone_param")
row.prop(context.scene, "f0x_3dm_buttonmode_param")
row.prop(context.scene, "f0x_3dm_collidable_param")
layout.label(text="Bounding box")
row = layout.row()
row.scale_y = 1.0
row.operator("f0x.boundingbox_add", text = "Add")
row.operator("f0x.boundingbox_remove", text = "Remove")
layout.label(text="Export")
row = layout.row()
row.scale_y = 2.0
row.operator("f0x.export_3dm", text = "Export")
# Function to register module
def register():
initProps()
bpy.utils.register_class(f0x_export_3dm)
bpy.utils.register_class(f0x_boundingbox_add)
bpy.utils.register_class(f0x_boundingbox_remove)
bpy.utils.register_class(MainPanel)
# Function to unregister module
def unregister():
unloadProps()
bpy.utils.unregister_class(f0x_export_3dm)
bpy.utils.unregister_class(f0x_boundingbox_add)
bpy.utils.unregister_class(f0x_boundingbox_remove)
bpy.utils.unregister_class(MainPanel)
# Entry point
if __name__ == "__main__":
register()