-
Notifications
You must be signed in to change notification settings - Fork 1
/
google_maps.py
247 lines (202 loc) · 9.4 KB
/
google_maps.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
# Copyright (c) 2019 Elie Michel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# The Software is provided “as is”, without warranty of any kind, express or
# implied, including but not limited to the warranties of merchantability,
# fitness for a particular purpose and noninfringement. In no event shall
# the authors or copyright holders be liable for any claim, damages or other
# liability, whether in an action of contract, tort or otherwise, arising from,
# out of or in connection with the software or the use or other dealings in the
# Software.
#
# This file is part of MapsModelsImporter, a set of addons to import 3D models
# from Maps services
import sys
import os
import subprocess
from .utils import getBinaryDir, makeTmpDir
from .preferences import getPreferences
SCRIPT_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "google_maps_rd.py")
MSG_INCORRECT_RDC = """Invalid RDC capture file. Please make sure that:
1. You are importing from Google Maps (NOT Google Earth)
2. You were MOVING in the 3D view while taking the capture (you can use the Capture after delay button in RenderDoc).
Please report to MapsModelsImporter developers providing the .rdc file as well as the full console log.
Console log is accessible in Windows > Toggle System Console (right click to copy)."""
class MapsModelsImportError(Exception):
pass
def captureToFiles(context, filepath, prefix, max_blocks):
"""Extract binary files and textures from a RenderDoc capture file.
This spawns a standalone Python interpreter because renderdoc module cannot be loaded in embedded Python"""
pref = getPreferences(context)
blender_dir = os.path.dirname(sys.executable)
blender_version = ("{0}.{1}").format(*bpy.app.version)
python_home = os.path.join(blender_dir, blender_version, "python")
os.environ["PYTHONHOME"] = python_home
os.environ["PYTHONPATH"] = os.environ.get("PYTHONPATH", "")
os.environ["PYTHONPATH"] += os.pathsep + os.path.abspath(getBinaryDir())
python = os.path.join(python_home, "bin", "python.exe" if sys.platform == "win32" else "python3.7m") # warning: hardcoded python version for non-windows might fail with Blender update
try:
out = subprocess.check_output([python, SCRIPT_PATH, filepath, prefix, str(max_blocks)], stderr=subprocess.STDOUT)
if pref.debug_info:
print("google_maps_rd returned:")
print(out.decode())
success = True
except subprocess.CalledProcessError as err:
if pref.debug_info:
print("google_maps_rd failed and returned:")
print(err.output.decode())
success = False
if not success:
raise MapsModelsImportError(MSG_INCORRECT_RDC)
# -----------------------------------------------------------------------------
import bpy
import bmesh
import pickle
from bpy_extras import object_utils
from math import floor, pi
from mathutils import Matrix, Vector
import os
def makeMatrix(mdata):
return Matrix([
mdata[0:4],
mdata[4:8],
mdata[8:12],
mdata[12:16]
]).transposed()
def extractUniforms(constants, refMatrix):
"""Extract from constant buffer the model matrix and uv offset
The reference matrix is used to cancel the view part of teh modelview matrix
"""
# Extract constants, which have different names depending on the browser/GPU driver
globUniforms = constants['$Globals']
postMatrix = None
if '_i' in globUniforms:
# Google Chrome 85.0.4183.121 (64bit), RendorDoc 1.9, RTX 3090, https://smap.seoul.go.kr/
uvOffsetScale = [0, 0, 1, 1]
matrix = makeMatrix(globUniforms['_i'])
postMatrix = Matrix.Scale(-1, 4, Vector((1, 0., 0.)))
elif '_f' in globUniforms:
# Google Chrome 85.0.4183.121 (64bit), RendorDoc 1.9, RTX 3090, https://smap.seoul.go.kr/
uvOffsetScale = [0, 0, 1, 1]
matrix = makeMatrix(globUniforms['_f'])
postMatrix = Matrix.Scale(1/300, 4, Vector((0., 0., 1))) @ Matrix.Scale(-1, 4, Vector((1, 0., 0.)))
else:
if refMatrix is None:
print("globUniforms:")
for k, v in globUniforms.items():
print(" {}: {}".format(k, v))
raise MapsModelsImportError(MSG_INCORRECT_RDC)
else:
return None, None, None
if refMatrix is None:
# Rotate around Z, upside down for SMAP
refMatrix = Matrix.Rotation(-pi, 4, 'Z') @ matrix.inverted()
matrix = refMatrix @ matrix
if postMatrix is not None:
matrix = postMatrix @ matrix
return uvOffsetScale, matrix, refMatrix
def addMesh(context, name, verts, tris, uvs):
mesh = bpy.data.meshes.new(name)
mesh.from_pydata(verts, [], tris)
mesh.update()
bm = bmesh.new()
bm.from_mesh(mesh)
uv_layer = bm.loops.layers.uv.verify()
for f in bm.faces:
for l in f.loops:
luv = l[uv_layer]
luv.uv = tuple(uvs[l.vert.index])
bm.to_mesh(mesh)
obj = object_utils.object_data_add(context, mesh, operator=None)
return obj
def addImageMaterial(name, obj, img):
bpy.ops.material.new()
mat = bpy.data.materials.new(name=name)
mat.use_nodes = True
obj.data.materials.append(mat)
nodes = mat.node_tree.nodes
principled = nodes["Principled BSDF"]
principled.inputs["Specular"].default_value = 0.0
principled.inputs["Roughness"].default_value = 1.0
texture_node = nodes.new(type="ShaderNodeTexImage")
texture_node.image = img
links = mat.node_tree.links
link = links.new(texture_node.outputs[0], principled.inputs[0])
def loadData(prefix, drawcall_id):
with open("{}{:05d}-indices.bin".format(prefix, drawcall_id), 'rb') as file:
indices = pickle.load(file)
with open("{}{:05d}-positions.bin".format(prefix, drawcall_id), 'rb') as file:
positions = pickle.load(file)
with open("{}{:05d}-uv.bin".format(prefix, drawcall_id), 'rb') as file:
uvs = pickle.load(file)
img = bpy.data.images.load("{}{:05d}-texture.png".format(prefix, drawcall_id))
with open("{}{:05d}-constants.bin".format(prefix, drawcall_id), 'rb') as file:
constants = pickle.load(file)
return indices, positions, uvs, img, constants
# -----------------------------------------------------------------------------
def filesToBlender(context, prefix, max_blocks=200):
"""Import data from the files extracted by captureToFiles"""
# Get reference matrix
refMatrix = None
if max_blocks <= 0:
# If no specific bound, max block is the number of .bin files in the directory
max_blocks = len([file for file in os.listdir(os.path.dirname(prefix)) if file.endswith(".bin")])
drawcall_id = 0
while drawcall_id < max_blocks:
if not os.path.isfile("{}{:05d}-indices.bin".format(prefix, drawcall_id)):
drawcall_id += 1
continue
try:
indices, positions, uvs, img, constants = loadData(prefix, drawcall_id)
except FileNotFoundError as err:
print("Skipping ({})".format(err))
drawcall_id += 1
continue
uvOffsetScale, matrix, refMatrix = extractUniforms(constants, refMatrix)
if uvOffsetScale is None:
drawcall_id += 1
continue
# Make triangles from triangle strip index buffer
n = len(indices)
if constants["DrawCall"]["topology"] == 'TRIANGLE_STRIP':
tris = [ [ indices[i+j] for j in [[0,1,2],[0,2,1]][i%2] ] for i in range(n - 3)]
tris = [ t for t in tris if t[0] != t[1] and t[0] != t[2] and t[1] != t[2] ]
else:
tris = [ [ indices[3*i+j] for j in range(3) ] for i in range(n//3) ]
verts = [ [ p[0], p[1], p[2] ] for p in positions ]
[ou, ov, su, sv] = uvOffsetScale
if uvs and len(uvs[0]) > 2:
print(f"uvs[0][2] = {uvs[0][2]}")
uvs = [u[:2] for u in uvs]
uvs = [ [ (u + ou) * su, (v + ov) * sv ] for u, v in uvs ]
if len(indices) == 0:
continue
mesh_name = "BuildingMesh-{:05d}".format(drawcall_id)
obj = addMesh(context, mesh_name, verts, tris, uvs)
globalScale=1.0/256.0
if constants["DrawCall"]["type"] == 'SeoulMap':
globalScale=1.0
obj.matrix_world = matrix * globalScale
mat_name = "BuildingMat-{:05d}".format(drawcall_id)
addImageMaterial(mat_name, obj, img)
drawcall_id += 1
# Save reference matrix
if refMatrix:
values = sum([list(v) for v in refMatrix], [])
context.scene.maps_models_importer_ref_matrix = values
context.scene.maps_models_importer_is_ref_matrix_valid = True
return None # no error
# -----------------------------------------------------------------------------
def importCapture(context, filepath, max_blocks, pref):
prefix = makeTmpDir(pref, filepath)
captureToFiles(context, filepath, prefix, max_blocks)
filesToBlender(context, prefix, max_blocks)