Skip to content

Commit f4087f1

Browse files
authored
Merge pull request #4 from maurock/shapenet
Fix bug on object path
2 parents 4c8eae5 + 700b149 commit f4087f1

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ pybullet_object_models
215215

216216
- Some applications require watertight meshes. An example is DeepSDF. If a watertight mesh is required, please run `python build_shapenet_urdfs.py --watertight`. This first generates a watertight mesh and then extract its urdf. The generated dataset has the same structure as described above, but `model.obj` and `model.urdf` are watertight.
217217

218-
If you use watertight meshes, please make sure to citethe original contributor:
219-
220-
221218
### Usage ###
222219

223220
Example scripts for importing the objects into pybullet are provided in the examples folder. To run these cd into the `examples` directory and run `python demo_load_object.py`. Use the `-object_set=` argument to load from a given object set, currently this can be selected from `primitive`, `random`, `ycb`, `superquadric` or `google` if setup correctly.

pybullet_object_models/build_shapenet_urdfs.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import point_cloud_utils as pcu
99
import numpy as np
1010
import trimesh
11+
import re
12+
from tempfile import mkstemp
1113
# Build entire libraries of URDFs
1214
# Can take a while
1315

@@ -23,12 +25,37 @@ def as_mesh(scene_or_mesh):
2325
return mesh
2426

2527

28+
# Update file
29+
def replace_in_file(filepath, original, replacement):
30+
"""Replace original string with replacement string in file at filepath.
31+
These can be single strings or list of strings."""
32+
with open(filepath, "r") as file:
33+
filedata = file.read()
34+
35+
original = [original] if not isinstance(original, list) else original
36+
replacement = [replacement] if not isinstance(replacement, list) else replacement
37+
assert len(original) == len(replacement)
38+
39+
for idx in range(len(original)):
40+
filedata = filedata.replace(original[idx], replacement[idx])
41+
42+
with open(filepath, "w") as file:
43+
file.write(filedata)
44+
45+
2646
def main(args):
2747
# Create new directory to place processed files
2848
new_folder = os.path.join(os.path.dirname(shapenet.__file__), 'ShapeNetCoreV2urdf')
2949
if not os.path.exists(new_folder):
3050
os.makedirs(new_folder)
3151

52+
# Create __init__.py file
53+
initfile = os.path.join(new_folder, '__init__.py')
54+
try:
55+
open(initfile, 'x')
56+
except FileExistsError:
57+
pass
58+
3259
shapenet_folder = os.path.join( os.path.dirname(shapenet.__file__), 'ShapeNetCoreV2')
3360

3461
subdirs = get_immediate_subdirectories(shapenet_folder)
@@ -65,7 +92,7 @@ def main(args):
6592
shutil.copy2(obj_path, os.path.join(new_object_folder, 'model.obj'))
6693

6794
else:
68-
vm, fm = pcu.make_mesh_watertight(mesh.vertices, mesh.faces, 10000)
95+
vm, fm = pcu.make_mesh_watertight(mesh.vertices, mesh.faces, 50000)
6996
watertight_path = os.path.join(new_object_folder, 'model.obj')
7097
pcu.save_mesh_vf(watertight_path, vm, fm, dtype=np.float32)
7198

@@ -83,7 +110,16 @@ def main(args):
83110
# rename urdf with their .obj name
84111
src_urdf_path = glob(os.path.join(new_category_folder, '[!_]*.urdf'))[0]
85112
dst_urdf_path = os.path.join(new_object_folder, 'model.urdf')
86-
shutil.move(src_urdf_path, dst_urdf_path)
113+
shutil.move(src_urdf_path, dst_urdf_path)
114+
115+
# Add flag 'concave=yes' to allow concave meshes in simulators,
116+
# edit the new urdf with the updated mesh path
117+
obj_index = dst_urdf_path.split(os.sep)[-2]
118+
original = [f'filename=\"{obj_index}\"',
119+
'collision']
120+
replacement = ['filename=\"model.obj\"',
121+
'collision concave=\"yes\"']
122+
replace_in_file(dst_urdf_path, original, replacement)
87123

88124

89125
if __name__=='__main__':

0 commit comments

Comments
 (0)