forked from solbaver/blender_3mf_import_export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.py
138 lines (112 loc) · 4.26 KB
/
export.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program 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 2
# 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
import zipfile
import sys
import os
import shutil
#just type in console:
#blender --background --python export.py 'YOUR_FILE_PATH' 'YOUR_FILE_NAME'
unit = 'millimeter'
name = 'test'
filepath = sys.argv[4]
filename = sys.argv[5]
print (filename)
print (filepath)
bpy.ops.wm.open_mainfile(filepath=filepath+filename)
def create_types():
t = open("[Content_Types].xml", 'w')
t.write('''<?xml version="1.0" encoding="UTF-8"?>''')
t.write('''<Types>''')
t.write('''<Default ContentType="application/vnd.openxmlformats-package.relationships+xml" Extension="rels"/>''')
t.write('''<Default ContentType="application/vnd.ms-package.3dmanufacturing-3dmodel+xml" Extension="model"/>''')
t.write('''</Types>''')
t.close()
def find_transformation(ob):
if ob.type == 'MESH':
old_matrix = list(ob.matrix_world)
print (ob.name)
new_matrix = ''
for line in old_matrix:
new_matrix = new_matrix + str(line[0]) + ' ' + str(line[1]) + ' ' + str(line[2]) + ' '
print (new_matrix)
return new_matrix
def create_object(ob, i, t):
t.write('''<object id="''' + str(i) + '''" name="''' + str(ob.name) + '''" type="model">''' + '\n')
t.write('''<mesh>''' + '\n')
t.write('''<vertices>''' + '\n')
vert_coords = [(ob.matrix_world * v.co) for v in ob.data.vertices]
for c in vert_coords:
t.write('''<vertex x="''' + str(c[0]) + '''" y="''' + str(c[1]) + '''" z="''' + str(c[2]) + '''"/>''' + '\n')
t.write('''</vertices>''' + '\n')
t.write('''<triangles>''' + '\n')
poly_coords = [(p.vertices[:]) for p in ob.data.polygons]
for c in poly_coords:
t.write('''<triangle v1="''' + str(c[0]) + '''" v2="''' + str(c[1]) + '''" v3="''' + str(c[2]) + '''"/>''' + '\n')
t.write('''</triangles>''' + '\n')
t.write('''</mesh>''' + '\n')
t.write('''</object>''' + '\n')
def create_model():
scene = bpy.context.scene
t = open(filepath + "3D" "/3dmodel.model", 'w')
t.write('''<?xml version="1.0" encoding="UTF-8"?>''' + '\n')
t.write('''<model unit="''' + str(unit) + '''" xml:lang="en-US" xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02">''' + '\n')
t.write('''<metadata name="Title">''' + name + '''</metadata>''' + '\n')
t.write('''<metadata name="Designer">''' + name + '''</metadata>''' + '\n')
t.write('''<metadata name="LicenseTerms">''' + name + '''</metadata>''' + '\n')
t.write('''<metadata name="CreationDate">''' + name + '''</metadata>''' + '\n')
t.write('''<metadata name="Description">''' + name + '''</metadata>''' + '\n')
t.write('''<resources>''' + '\n')
i = 1
for ob in scene.objects:
if ob.type == 'MESH':
create_object(ob, i, t)
i = i + 1
#
t.write('''</resources>''' + '\n')
t.write('''<build>''' + '\n')
i = 1
for ob in scene.objects:
if ob.type == 'MESH':
new_matrix = find_transformation(ob)
line = '''<item objectid="''' + str(i) + '''" transform="''' + str(new_matrix) + '''"/>''' + '\n'
t.write(line)
i = i + 1
t.write('''</build>''' + '\n')
t.write('''</model>''' + '\n')
t.close
for param in sys.argv:
print (param)
create_types()
os.mkdir(filepath+'/_rels')
os.mkdir(filepath+'/3D')
os.chdir(filepath+'/3D')
create_model()
#creating .3dm archive
os.chdir(filepath)
zip_name = filename + '.3mf'
zip_exp = zipfile.ZipFile(filepath + zip_name, mode='w')
zip_exp.write('[Content_Types].xml')
zip_exp.write('_rels')
zip_exp.write('3D/3dmodel.model')
zip_exp.close()
#deleting leftovers
os.remove(filepath+'[Content_Types].xml')
os.remove(filepath+'3D/3dmodel.model')
os.rmdir(filepath+'3D')
os.rmdir(filepath+'_rels')