-
Notifications
You must be signed in to change notification settings - Fork 28
/
__init__.py
310 lines (252 loc) · 10.4 KB
/
__init__.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
from mathutils import Matrix, Vector
import bpy
import importlib
import sys
from math import pi
import os
import json
'''
Copyright (C) 2018 Dawid Huczyński
dawid.huczynski@gmail.com
Created by Dawid Huczyński
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 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 <http://www.gnu.org/licenses/>.
'''
bl_info = {
"name": "Meshroom importer",
"description": "Imports from .mg file cameras, images, sparse and obj",
"author": "Dawid Huczyński",
"version": (0, 0, 1),
"blender": (2, 80, 0),
"location": "File > Import > Import Meshroom",
"warning": "This addon is still in development.",
"wiki_url": "",
"category": "Import-Export"}
filepath = r'D:\Koszyk\koszyk.mg'
# module import https://github.com/uhlik/bpy/blob/master/view3d_point_cloud_visualizer.py
# thanks to Jakub Uhlik
vis_mod = 'view3d_point_cloud_visualizer'
if vis_mod in sys.modules.keys() and sys.modules[vis_mod].bl_info['version'] <= (0, 7, 0):
local_visualizer = False
else:
from . import view3d_point_cloud_visualizer as point_cloud
point_cloud.register()
local_visualizer = True
def find_view_layer(coll, lay_coll=None):
if lay_coll is None:
lay_coll = bpy.context.view_layer.layer_collection
if lay_coll.collection == coll:
return lay_coll
else:
for child in lay_coll.children:
a = find_view_layer(coll, child)
if a:
return a
return None
def read_meshlab(filepath):
'Handle meshlab file'
cache = os.path.join(os.path.dirname(filepath), 'MeshroomCache')
data = json.load(open(filepath, 'r'))
try:
nodeSFM = data['graph']['StructureFromMotion_1']
nodeType = nodeSFM['nodeType']
uid0 = nodeSFM['uids']['0']
# sfm = nodeSFM['outputs']['output'].format(
# cache=cache, nodeType=nodeType, uid0=uid0)
cameras_sfm = nodeSFM['outputs']['outputViewsAndPoses'].format(
cache=cache, nodeType=nodeType, uid0=uid0)
cloud = os.path.join(cache, nodeType, uid0, 'cloud_and_poses.ply')
except KeyError:
cameras_sfm = cloud = None
try:
nodeMesh = data['graph']['Meshing_1']
dense_obj = nodeMesh['outputs']['output'].format(
cache=cache, nodeType=nodeMesh['nodeType'], uid0=nodeMesh['uids']['0'])
except KeyError:
dense_obj = None
try:
nodeTex = data['graph']['Texturing_1']
tex_obj = nodeTex['outputs']['outputMesh'].format(
cache=cache, nodeType=nodeTex['nodeType'], uid0=nodeTex['uids']['0'])
except KeyError:
tex_obj = None
return (cameras_sfm, cloud, dense_obj, tex_obj)
def import_cameras(cameras_sfm, img_depth):
'read camera sfm and imports to blender'
data = json.load(open(cameras_sfm, 'r'))
poses = {x['poseId']: x['pose'] for x in data['poses']}
intrinsics = {x['intrinsicId']: x for x in data['intrinsics']}
render = bpy.context.scene.render
render.resolution_x = int(data['views'][0]['width'])
render.resolution_y = int(data['views'][0]['height'])
for view in data['views']:
view_id = view['viewId']
path = view['path']
width, height = int(view['width']), int(view['height'])
focal_length = float(view['metadata']['Exif:FocalLength'])
pose = poses[view['poseId']]['transform']
intrinsic = intrinsics[view['intrinsicId']]
pxFocalLength = float(intrinsic['pxFocalLength'])
principalPoint = [float(x) for x in intrinsic['principalPoint']]
# camera
bcam = bpy.data.cameras.new(f'View {view_id}')
bcam.display_size = .25
bcam.sensor_width = focal_length
bcam.lens_unit = 'MILLIMETERS'
bcam.lens = (pxFocalLength/max((width,height)))*focal_length
bcam.shift_x = (principalPoint[0] - width/2)/width
bcam.shift_y = (principalPoint[1] - height/2)/height
# image
bcam.show_background_images = True
bg = bcam.background_images.new()
bg.image = bpy.data.images.load(path)
bg.display_depth = img_depth
# camera object
ob = bpy.data.objects.new(f'View {view_id}', bcam)
bpy.context.collection.objects.link(ob)
loc = [float(x) for x in pose['center']]
rot = [float(x) for x in pose['rotation']]
rotation = [rot[:3], rot[3:6], rot[6:]]
m = Matrix(rotation)
ob.matrix_world = m.to_4x4() @ Matrix().Rotation(pi, 4, 'X')
ob.location = Vector(loc)
def import_sparse_depricated(cloud):
'''Depricated. Use view3d_point_cloud_visualizer instead.'''
# read .ply file
f = open(cloud, 'r')
ply = f.read()
header = ply[:1000].split('end_header\n')[0].split('\n')
header
assert header[0] == 'ply'
assert header[1].startswith('format ascii')
elements = []
tmp_prop = []
for x in header[2:]:
a = x.split(' ')
if a[0] == 'element':
if tmp_prop:
elements[-1]['props'] = list(tmp_prop)
tmp_prop = []
el = {'name': a[1], 'nr': a[2]}
elements.append(el)
elif a[0] == 'property':
prop = {'name': a[2], 'type': a[1]}
tmp_prop.append(prop)
elements[-1]['props'] = list(tmp_prop)
points = ply.split('end_header\n')[1].split('\n')
if points[-1] == '':
points.pop()
verts = []
for point in points:
verts.append((float(x) for x in point.split()[:3]))
mesh = bpy.data.meshes.new('sparse cloud SFM')
mesh.from_pydata(verts, [], [])
obj = bpy.data.objects.new('sparse cloud SFM', mesh)
bpy.context.collection.objects.link(obj)
def import_object(filepath):
bpy.ops.import_scene.obj(filepath=filepath)
bpy.context.selected_objects[0].matrix_world = Matrix()
class import_meshroom(bpy.types.Operator):
bl_idname = "import_scene.meshroom"
bl_label = "Import Meshroom"
bl_description = "Imports cameras, images, sparse and obj from meshroom .mg file"
# bl_options = {"REGISTER"}
files: bpy.props.CollectionProperty(
type=bpy.types.OperatorFileListElement,
options={'HIDDEN', 'SKIP_SAVE'})
directory: bpy.props.StringProperty(
maxlen=1024, subtype='FILE_PATH', options={'HIDDEN', 'SKIP_SAVE'})
cameras: bpy.props.BoolProperty(default=True, name='Views', description='Import views as cameras and images')
undistorted: bpy.props.BoolProperty(default=True, name='Undistorted', description='Better, but heavy images')
DEPTH = [
('FRONT', 'FRONT', 'Preview semi transparent image in front of the objects', '', 0),
('BACK', 'BACK', 'Preview image behing objects', '', 1)
]
img_front: bpy.props.EnumProperty(items=DEPTH, name='Depth', description='', default='FRONT')
sparse: bpy.props.BoolProperty(default=True, name='Import SFM', description='')
dense: bpy.props.BoolProperty(default=False, name='Import dense mesh', description='')
textured: bpy.props.BoolProperty(default=True, name='Import textured mesh', description='')
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
file = self.files[0].name
directory = self.directory
filepath = os.path.join(directory, file)
# container collection for import
col = bpy.data.collections.new(file)
camera_col = bpy.data.collections.new('Views')
col.children.link(camera_col)
context.scene.collection.children.link(col)
lay_col = find_view_layer(camera_col)
context.view_layer.active_layer_collection = lay_col
# filepath = PATH
cameras_sfm, cloud, dense_obj, tex_obj = read_meshlab(filepath)
if self.cameras:
import_cameras(cameras_sfm, self.img_front)
lay_col = find_view_layer(col)
context.view_layer.active_layer_collection = lay_col
if self.sparse:
empty = bpy.data.objects.new('sparse cloud SFM', None)
col.objects.link(empty)
empty.select_set(True)
context.view_layer.objects.active = empty
bpy.ops.point_cloud_visualizer.load_ply_to_cache(filepath=cloud)
bpy.ops.point_cloud_visualizer.draw()
if self.dense and dense_obj:
import_object(dense_obj)
if self.textured and tex_obj:
import_object(tex_obj)
return {"FINISHED"}
class meshroom_update_focal(bpy.types.Operator):
bl_idname = "view3d.update_focal"
bl_label = "Meshroom: update focal"
bl_description = "Updates Focal Length from active camera to other cameras"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
ob = context.active_object
if ob.type == 'CAMERA':
lens = ob.data.lens
shift_x = ob.data.shift_x
shift_y = ob.data.shift_y
sensor_width = ob.data.sensor_width
for cam in bpy.data.cameras:
if cam.name.startswith('View '):
cam.lens = lens
cam.shift_x = shift_x
cam.shift_y = shift_y
cam.sensor_width = sensor_width
return {"FINISHED"}
def import_meshroom_button(self, context):
self.layout.operator(import_meshroom.bl_idname,
text="Import Meshroom")
classes = (
import_meshroom,
meshroom_update_focal,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.TOPBAR_MT_file_import.append(import_meshroom_button)
def unregister():
bpy.types.TOPBAR_MT_file_import.remove(import_meshroom_button)
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
if local_visualizer:
point_cloud.unregister()
if __name__ == "__main__":
register()