-
Notifications
You must be signed in to change notification settings - Fork 234
/
convert.py
343 lines (269 loc) · 13.2 KB
/
convert.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
# Copyright 2024 The Kubric Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=logging-fstring-interpolation
# see: https://docs.python.org/3/library/subprocess.html
import argparse
import json
import logging
from pathlib import Path
import shutil
import subprocess
import sys
import tarfile
from typing import Tuple
import trimesh
from shapenet_synsets import CATEGORY_NAMES
from trimesh_utils import get_object_properties
import trimesh_utils
from urdf_template import URDF_TEMPLATE
_DEFAULT_LOGGER = logging.getLogger(__name__)
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
def stage0(object_folder: Path, logger=_DEFAULT_LOGGER):
source_path = object_folder / 'models' / 'model_normalized.obj'
target_path = object_folder / 'kubric' / 'visual_geometry_pre.glb'
if target_path.is_file():
logger.debug(f'skipping stage0 on "{object_folder}"')
return # stage already completed; skipping
# --- pre-condition
if not source_path.is_file():
logger.error(f'stage0 pre-condition failed, file does not exist "{source_path}"')
# --- body
logger.debug(f'stage0 running on "{object_folder}"')
cmd = f'obj2gltf -i {source_path} -o {target_path}'
retobj = subprocess.run(cmd, capture_output=True, shell=True, text=True)
if 'ENOENT' in retobj.stdout:
logger.error(f'{retobj.stdout}')
if retobj.returncode != 0:
logger.error(f'obj2gltf failed on "f{object_folder}"')
if retobj.stdout != '': logger.error(f'{retobj.stdout}')
if retobj.stderr != '': logger.error(f'{retobj.stderr}')
# --- post-condition
if not target_path.is_file():
logger.error(f'Post-condition failed, file does not exist "{target_path}"')
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
def stage1(object_folder: Path, logger=_DEFAULT_LOGGER):
source_path = object_folder / 'models' / 'model_normalized.obj'
target_path = object_folder / 'kubric' / 'model_watertight.obj'
if target_path.is_file():
logger.debug(f'skipping stage1 on "{object_folder}"')
return # stage already completed; skipping
# --- pre-condition
if not source_path.is_file():
logger.error(f'stage1 pre-condition failed, file does not exist "{source_path}"')
# --- body
logger.debug(f'stage1 running on "{object_folder}"')
cmd = f'manifold --input {source_path} --output {target_path}'
retobj = subprocess.run(cmd, capture_output=True, shell=True, text=True)
if retobj.returncode != 0:
logger.error(f'manifold failed on "f{object_folder}"')
if retobj.stdout != '': logger.error(f'{retobj.stdout}')
if retobj.stderr != '': logger.error(f'{retobj.stderr}')
# --- post-condition
if not target_path.is_file():
logger.error(f'stage1 post-condition failed, file does not exist "{target_path}"')
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
def stage2(object_folder: Path, logger=_DEFAULT_LOGGER):
source_path = object_folder / 'kubric' / 'model_watertight.obj'
target_path = object_folder / 'kubric' / 'collision_geometry.obj'
log_path = object_folder / 'kubric' / 'stage2_logs.txt'
stdout_path = str(object_folder / 'kubric' / 'stage2_stdout.txt')
if target_path.is_file():
logger.debug(f'skipping stage2 on "{object_folder}"')
return # stage already completed; skipping
# --- pre-condition
if not source_path.is_file():
logger.error(f'stage2 pre-condition failed, file does not exist "{source_path}"')
# --- body
logger.debug(f'stage2 running on "{object_folder}"')
# TODO: how to monitor errors? should we move to "raw" VHCD?
command_string = f"python pybullet_vhacd.py " \
f"--source_path={source_path} --target_path={target_path} " \
f"--stdout_path={stdout_path} > {log_path}"
retobj = subprocess.run(command_string, shell=True, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if retobj.returncode != 0:
logger.error(f'stage2 failed with return code {retobj.returncode}')
# --- post-condition
if not target_path.is_file():
logger.error(f'stage2 post-condition failed, file does not exist "{target_path}"')
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
def stage3(object_folder: Path, logger=_DEFAULT_LOGGER):
source_path = object_folder / 'kubric' / 'visual_geometry_pre.glb'
log_path = object_folder / 'kubric' / 'stage3_logs.txt'
target_path = object_folder / 'kubric' / 'visual_geometry.glb'
if target_path.is_file():
logger.debug(f'skipping stage3 on "{object_folder}"')
return # stage already completed; skipping
logger.debug(f'stage3 running on "{object_folder}"')
asset_id = str(object_folder.relative_to(object_folder.parent.parent))
command_string = f"python bpy_clean_mesh.py " \
f"--source_path={source_path} --target_path={target_path} " \
f"--asset_id={asset_id} > {log_path}"
retobj = subprocess.run(command_string, shell=True, check=True)
if retobj.returncode != 0:
logger.error(f'stage3 failed with return code {retobj.returncode}')
if not target_path.is_file():
logger.error(f'stage3 post-condition failed, file does not exist "{target_path}"')
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
def get_asset_id_and_category(object_folder: Path) -> Tuple[str, str, str]:
category_id = str(object_folder.parent.relative_to(object_folder.parent.parent))
asset_id = str(object_folder.relative_to(object_folder.parent))
category_name = CATEGORY_NAMES[category_id]
return asset_id, category_id, category_name
def get_object_volume(obj_path:Path, logger=_DEFAULT_LOGGER, density=1.0):
# --- override the trimesh logger
trimesh.util.log = logger
tmesh = trimesh_utils.get_tmesh(str(obj_path))
properties = {
"volume": tmesh.volume,
"surface_area": tmesh.area,
"mass": tmesh.volume * density,
}
return properties
def get_visual_properties(obj_path:Path, logger=_DEFAULT_LOGGER):
# --- override the trimesh logger
trimesh.util.log = logger
tmesh = trimesh_utils.get_tmesh(str(obj_path))
properties = {
"nr_vertices": len(tmesh.vertices),
"nr_faces": len(tmesh.faces),
}
return properties
def stage4(object_folder: Path, logger=_DEFAULT_LOGGER):
# TODO: we should probably use a mixture of model_normalized and model_wateright here?
source_path = object_folder / 'kubric' / 'collision_geometry.obj'
watertight_mesh_path = object_folder / 'kubric' / 'model_watertight.obj'
vis_mesh_path = object_folder / 'kubric' / 'visual_geometry.glb'
target_urdf_path = object_folder / 'kubric' / 'object.urdf'
target_json_path = object_folder / 'kubric' / 'data.json'
if target_urdf_path.is_file() and target_json_path.is_file():
return
# --- pre-condition
if not source_path.is_file():
logger.error(f'stage4 pre-condition failed, file does not exist "{source_path}"')
if not watertight_mesh_path.is_file():
logger.error(f'stage4 pre-condition failed, file does not exist "{watertight_mesh_path}"')
if not vis_mesh_path.is_file():
logger.error(f'stage4 pre-condition failed, file does not exist "{vis_mesh_path}"')
logger.debug(f'stage4 running on "{object_folder}"')
# --- body1: object.urdf file
properties = get_object_properties(source_path, logger)
properties.update(get_object_volume(watertight_mesh_path))
properties.update(get_visual_properties(vis_mesh_path))
asset_id, category_id, category_name = get_asset_id_and_category(object_folder)
properties["id"] = asset_id
urdf_str = URDF_TEMPLATE.format(**properties)
with open(target_urdf_path, 'w') as fd:
fd.write(urdf_str)
# --- body2: data.json file
asset_entry = {
"id": asset_id,
"asset_type": "FileBasedObject",
"kwargs": {
"bounds": properties["bounds"],
"mass": properties["mass"],
"render_filename": "visual_geometry.glb",
"simulation_filename": "object.urdf",
},
"license": "https://shapenet.org/terms",
"metadata": {
"category": category_name,
"category_id": category_id,
"watertight_mesh_filename": "model_watertight.obj",
"nr_faces": properties["nr_faces"],
"nr_vertices": properties["nr_vertices"],
"surface_area": properties["surface_area"],
"volume": properties["volume"],
}
}
with open(target_json_path, "w") as fd:
json.dump(asset_entry, fd, indent=4, sort_keys=True)
# --- post-condition
if not target_urdf_path.is_file():
logger.error(f'stage4 post-condition failed, file does not exist "{target_urdf_path}"')
if not target_json_path.is_file():
logger.error(f'stage4 post-condition failed, file does not exist "{target_json_path}"')
return properties
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
def stage5(object_folder: Path, logger=_DEFAULT_LOGGER):
target_path = object_folder / 'kubric.tar.gz'
if target_path.is_file():
logger.debug(f'skipping stage5 on "{object_folder}"')
return # stage already completed; skipping
logger.debug(f'stage5 running on "{object_folder}"')
# --- dumps file into tar (pre-conditions auto-verified by exceptions)
with tarfile.open(target_path, 'w:gz') as tar:
tar.add(object_folder / 'kubric' / 'visual_geometry.glb',
arcname='visual_geometry.glb')
tar.add(object_folder / 'kubric' / 'collision_geometry.obj',
arcname='collision_geometry.obj')
tar.add(object_folder / 'kubric' / 'model_watertight.obj',
arcname='model_watertight.obj')
tar.add(object_folder / 'kubric' / 'object.urdf',
arcname='object.urdf')
tar.add(object_folder / 'kubric' / 'data.json',
arcname='data.json')
if not target_path.is_file():
logger.error(f'stage5 post-condition failed, file does not exist "{target_path}"')
def stage6(object_folder: Path, logger=_DEFAULT_LOGGER):
asset_id, category_id, category_name = get_asset_id_and_category(object_folder)
source_path = object_folder / 'kubric.tar.gz'
target_path = object_folder.parent.parent / 'kubric' / f'{category_id}_{asset_id}.tar.gz'
if target_path.is_file():
logger.debug(f'skipping stage6 on "{object_folder}"')
return
if not source_path.is_file():
logger.error(f'stage6 pre-condition failed, file does not exist "{source_path}"')
return
logger.debug(f'stage6 running on "{object_folder}"')
target_path.parent.mkdir(exist_ok=True)
shutil.move(str(source_path), str(target_path))
#shutil.rmtree(str(object_folder / 'kubric'))
if not target_path.is_file():
logger.error(f'stage6 post-condition failed, file does not exist "{target_path}"')
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--datadir', default='/ShapeNetCore.v2')
parser.add_argument('--model', default='02933112/718f8fe82bc186a086d53ab0fe94e911')
parser.add_argument('--stages', nargs='+')
args = parser.parse_args()
# --- setup logger (→stdout)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)
# --- execute functors
object_folder = Path(args.datadir)/args.model
stages = [int(stage) for stage in args.stages]
if 0 in stages: stage0(object_folder, logger)
if 1 in stages: stage1(object_folder, logger)
if 2 in stages: stage2(object_folder, logger)
if 3 in stages: stage3(object_folder, logger)
if 4 in stages: properties = stage4(object_folder, logger)
if 5 in stages: stage5(object_folder, logger)
if 6 in stages: stage6(object_folder, logger)