forked from solbaver/blender_3mf_import_export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
171 lines (126 loc) · 4.25 KB
/
import.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
# ##### 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
from mathutils import Matrix
from bs4 import BeautifulSoup
import sys
import re
import os
import shutil
#just type in console:
#blender --background --python import.py 'YOUR_FILE_PATH' 'YOUR_FILE_NAME'
for i in sys.argv:
print (i)
filepath = sys.argv[4]
filename = sys.argv[5]
modelpath = '/model'
zip_ref = zipfile.ZipFile(filepath + filename, 'r')
zip_ref.extractall(filepath + modelpath)
zip_ref.close()
model_file = open(filepath + modelpath + '/3D/' + '3dmodel.model', 'r')
soup = BeautifulSoup(model_file, features="xml")
#number of objects in scene
objects_number = len(soup.resources.findAll("object"))
num = 1
unit = soup.model['unit']
print (unit)
if unit == 'micron':
bpy.context.scene.unit_settings.system = 'METRIC'
bpy.context.scene.scale_length = 0.001
if unit == 'millimeter':
bpy.context.scene.unit_settings.system = 'METRIC'
bpy.context.scene.unit_settings.scale_length = 0.01
if unit == 'centimeter':
bpy.context.scene.unit_settings.system= 'METRIC'
bpy.context.scene.scale_length = 0.1
if unit == 'meter':
bpy.context.scene.unit_settings.system= 'METRIC'
bpy.context.scene.scale_length = 1
if unit == 'foot':
bpy.context.scene.unit_settings.system = 'IMPERIAL'
bpy.context.scene.scale_length = 1
if unit == 'inch':
bpy.context.scene.unit_settings.system = 'IMPERIAL'
bpy.context.scene.scale_length = 0.1
while num < objects_number:
#id - number of current object
ob = soup.resources.findAll(id=num)
for n in ob:
name = n.get('name')
print (name)
vetexes = []
triangles = []
ob = BeautifulSoup(str(ob)[1:-1], features="xml")
vertex = ob.findAll('vertex')
triangle = ob.findAll('triangle')
ob = soup.resources.findAll(id=num)
#part for matrix calculations
ob_transform = soup.build.findAll(objectid=num)
ob_transform = BeautifulSoup(str(ob_transform)[1:-1])
transform = ob_transform.item['transform']
N = map(float,transform.split())
N = list(N)
matrix = [(N[0], N[1], N[2], 0), (N[3], N[4], N[5], 0), (N[6], N[7], N[8], 0), (N[9], N[10], N[11], 1.0)]
matrix = Matrix(matrix)
vertexes_x = []
vertexes_y = []
vertexes_z = []
triangles_1 = []
triangles_2 = []
triangles_3 = []
verts = []
faces = []
for v in vertex:
v = BeautifulSoup(str(v), features="xml")
vertexes_x.append(v.vertex['x'])
vertexes_y.append(v.vertex['y'])
vertexes_z.append(v.vertex['z'])
for t in triangle:
t = BeautifulSoup(str(t), features="xml")
triangles_1.append(t.triangle['v1'])
triangles_2.append(t.triangle['v2'])
triangles_3.append(t.triangle['v3'])
i = 0
while i < len(vertexes_x):
vertex_tuple = (float(vertexes_x[i]), float(vertexes_y[i]), float(vertexes_z[i]))
verts.insert(i, vertex_tuple)
i = i + 1
verts = tuple(verts)
i = 0
while i < len(triangles_1):
triangle_tuple = (int(triangles_1[i]), int(triangles_2[i]), int(triangles_3[i]))
faces.insert(i, triangle_tuple )
i = i + 1
faces = tuple(faces)
mesh_name = str(num)
#creating object in blender
blender_me = bpy.data.meshes.new(name)
blender_ob = bpy.data.objects.new(name, blender_me)
scn = bpy.context.scene
scn.objects.link(blender_ob)
scn.objects.active = blender_ob
blender_ob.select = True
blender_me.from_pydata(verts, [], faces)
blender_me.update(calc_edges=True)
num = num + 1
blender_ob.matrix_world = matrix * blender_ob.matrix_world
model_file.close()
bpy.ops.wm.save_as_mainfile(filepath=filepath+filename+'.blend')
rempath = os.path.join(os.path.abspath(os.path.dirname(filepath)), 'model')
shutil.rmtree(rempath)