├── .gitignore ├── LICENSE ├── README.md ├── create_ycb_sdf.py ├── download_ycb_dataset.py ├── images └── ycb_gazebo_inertia.png └── templates └── ycb ├── model.config ├── template.material └── template.sdf /.gitignore: -------------------------------------------------------------------------------- 1 | **/models/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020-2021 Sebastian Castro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YCB Tools 2 | Tools to download models from the [YCB dataset](https://www.ycbbenchmarks.com/) and use them with the Gazebo simulator. 3 | 4 | By Sebastian Castro, 2020-2021 5 | 6 | ![YCB Models in Gazebo](images/ycb_gazebo_inertia.png) 7 | 8 | ## Python Setup 9 | 10 | You only need to install a few Python packages to get these tools running. 11 | 12 | ``` 13 | pip install numpy pillow scipy shapely trimesh 14 | ``` 15 | 16 | ## Downloading YCB objects 17 | 18 | You can download the models using a variant of the download script provided on the [YCB web page](http://ycb-benchmarks.s3-website-us-east-1.amazonaws.com/). 19 | However, this script has been modified to work with Python 3. 20 | 21 | ``` 22 | python download_ycb_dataset.py 23 | ``` 24 | 25 | You can configure a few options in the script, including choosing which objects and model types to download. 26 | However, the default options will get all of the YCB object models and may take a few minutes to download. 27 | 28 | ## Using YCB Object Models in Gazebo 29 | 30 | After you have downloaded the YCB models, you can run the following script. 31 | 32 | ``` 33 | python3 create_ycb_sdf.py 34 | ``` 35 | 36 | This by default uses the `./models/ycb` and `./templates/ycb` folder, assuming you are running the script from the top-level folder of this repo. 37 | You can always modify this if you want: 38 | 39 | ``` 40 | python3 create_ycb_sdf.py --template-folder /path/to/templates/ycb --ycb-folder /path/to/models.ycb 41 | ``` 42 | 43 | There is also a `--downsample_ratio` argument set in this script which uses rejection sampling to randomly remove faces from the mesh to get a subset of faces. 44 | This downsampled mesh is used only for collision detection, so it will not affect the visuals, but it should make simulation faster if you don't need perfect collision fidelity (i.e., if you're not trying to do manipulation). 45 | The Google 16k meshes are fairly high-resolution, so this should not have a huge impact, but if you want the full meshes used for collision, you can leave this parameter at its default value of `1`. 46 | 47 | ``` 48 | python3 create_ycb_sdf.py --downsample-ratio 0.5 49 | ``` 50 | 51 | **NOTE:** A few of the models in the dataset do not have either of the `google_16k` or `tsdf` meshes available, so these will not work with Gazebo. 52 | 53 | Finally, you need to make sure the folder containing your models is included in your `GAZEBO_MODEL_PATH` environment variable. We recommend setting this in your `~/.bashrc` file as follows. 54 | 55 | ```bash 56 | export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:$REPO_BASE_FOLDER/models/ycb 57 | ``` 58 | 59 | where `$REPO_BASE_FOLDER` is the folder containing this README file. 60 | -------------------------------------------------------------------------------- /create_ycb_sdf.py: -------------------------------------------------------------------------------- 1 | import os 2 | import trimesh 3 | import argparse 4 | import numpy as np 5 | 6 | """ 7 | Creates Gazebo compatible SDF files from downloaded YCB data. 8 | 9 | This looks through all the YCB objects you have downloaded in a particular 10 | folder, and creates Gazebo compatible SDF files from a set of templates. 11 | 12 | If the object has google_16k meshes downloaded, it will use those; else, it 13 | will use the tsdf meshes which are of lower quality. 14 | 15 | We recommend ensuring that you've enabled `google_16k` as one of the file 16 | types to download in the `download_ycb_dataset.py` script. 17 | 18 | Sebastian Castro 2020-2021 19 | """ 20 | 21 | # Define folders 22 | default_ycb_folder = os.path.join("models", "ycb") 23 | default_template_folder = os.path.join("templates", "ycb") 24 | 25 | if __name__=="__main__": 26 | 27 | print("Creating files to use YCB objects in Gazebo...") 28 | 29 | # Parse arguments 30 | parser = argparse.ArgumentParser(description="YCB Model Importer") 31 | parser.add_argument("--downsample-ratio", type=float, default=1, 32 | help="Mesh vertex downsample ratio (set to 1 to leave meshes as they are)") 33 | parser.add_argument("--template-folder", type=str, default=default_template_folder, 34 | help="Location of YCB models (defaults to ./templates/ycb)") 35 | parser.add_argument("--ycb-folder", type=str, default=default_ycb_folder, 36 | help="Location of YCB models (defaults to ./models/ycb)") 37 | 38 | args = parser.parse_args() 39 | 40 | # Get the list of all downloaded mesh folders 41 | folder_names = os.listdir(args.ycb_folder) 42 | 43 | # Get the template files to copy over 44 | config_template_file = os.path.join(args.template_folder, "model.config") 45 | model_template_file = os.path.join(args.template_folder, "template.sdf") 46 | material_template_file = os.path.join(args.template_folder, "template.material") 47 | with open(config_template_file,"r") as f: 48 | config_template_text = f.read() 49 | with open(model_template_file,"r") as f: 50 | model_template_text = f.read() 51 | with open(material_template_file,"r") as f: 52 | material_template_text = f.read() 53 | 54 | # Now loop through all the folders 55 | for folder in folder_names: 56 | if folder != "template": 57 | try: 58 | print("Creating Gazebo files for {} ...".format(folder)) 59 | 60 | # Extract model name and folder 61 | model_long = folder 62 | model_short = folder[4:] 63 | model_folder = os.path.join(args.ycb_folder, model_long) 64 | 65 | # Check if there are Google meshes; else use the TSDF folder 66 | if "google_16k" in os.listdir(model_folder): 67 | mesh_type = "google_16k" 68 | else: 69 | mesh_type = "tsdf" 70 | 71 | # Extract key data from the mesh 72 | if mesh_type == "google_16k": 73 | mesh_file = os.path.join(model_folder, "google_16k", "textured.obj") 74 | elif mesh_type == "tsdf": 75 | mesh_file = os.path.join(model_folder, "tsdf", "textured.obj") 76 | mesh = trimesh.load(mesh_file) 77 | # Mass and moments of inertia 78 | mass_text = str(mesh.mass) 79 | tf = mesh.principal_inertia_transform 80 | inertia = trimesh.inertia.transform_inertia(tf, mesh.moment_inertia) 81 | # Center of mass 82 | com_vec = mesh.center_mass.tolist() 83 | eul = trimesh.transformations.euler_from_matrix(np.linalg.inv(tf), axes="sxyz") 84 | com_vec.extend(list(eul)) 85 | com_text = str(com_vec) 86 | com_text = com_text.replace("[", "") 87 | com_text = com_text.replace("]", "") 88 | com_text = com_text.replace(",", "") 89 | 90 | # Create a downsampled mesh file with a subset of vertices and faces 91 | if args.downsample_ratio < 1: 92 | mesh_pts = mesh.vertices.shape[0] 93 | num_pts = int(mesh_pts * args.downsample_ratio) 94 | (_, face_idx) = mesh.sample(num_pts, True) 95 | downsampled_mesh = mesh.submesh((face_idx,), append=True) 96 | with open(os.path.join(model_folder, "downsampled.obj"), "w") as f: 97 | downsampled_mesh.export(f, "obj") 98 | collision_mesh_text = model_long + "/downsampled.obj" 99 | else: 100 | collision_mesh_text = model_long + "/" + mesh_type + "/textured.obj" 101 | 102 | # Copy and modify the model configuration file template 103 | config_text = config_template_text.replace("$MODEL_SHORT", model_short) 104 | with open(os.path.join(model_folder, "model.config"), "w") as f: 105 | f.write(config_text) 106 | 107 | # Copy and modify the model file template 108 | model_text = model_template_text.replace("$MODEL_SHORT", model_short) 109 | model_text = model_text.replace("$MODEL_LONG", model_long) 110 | model_text = model_text.replace("$MESH_TYPE", mesh_type) 111 | model_text = model_text.replace("$COLLISION_MESH", collision_mesh_text) 112 | model_text = model_text.replace("$MASS", mass_text) 113 | model_text = model_text.replace("$COM_POSE", com_text) 114 | model_text = model_text.replace("$IXX", str(inertia[0][0])) 115 | model_text = model_text.replace("$IYY", str(inertia[1][1])) 116 | model_text = model_text.replace("$IZZ", str(inertia[2][2])) 117 | model_text = model_text.replace("$IXY", str(inertia[0][1])) 118 | model_text = model_text.replace("$IXZ", str(inertia[0][2])) 119 | model_text = model_text.replace("$IYZ", str(inertia[1][2])) 120 | with open(os.path.join(model_folder, model_short + ".sdf"), "w") as f: 121 | f.write(model_text) 122 | 123 | # Copy and modify the material file template 124 | if mesh_type == "google_16k": 125 | texture_file = "texture_map.png" 126 | elif mesh_type == "tsdf": 127 | texture_file = "textured.png" 128 | material_text = material_template_text.replace("$MODEL_SHORT", model_short) 129 | material_text = material_text.replace("$MODEL_LONG", model_long) 130 | material_text = material_text.replace("$MESH_TYPE", mesh_type) 131 | material_text = material_text.replace("$TEXTURE_FILE", texture_file) 132 | with open(os.path.join(model_folder, model_short + ".material"), "w") as f: 133 | f.write(material_text) 134 | 135 | except: 136 | print("Error processing {}. Textured mesh likely does not exist for this object.".format(folder)) 137 | 138 | print("Done.") 139 | -------------------------------------------------------------------------------- /download_ycb_dataset.py: -------------------------------------------------------------------------------- 1 | #Copyright 2015 Yale University - Grablab 2 | #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:\ 3 | #The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 4 | #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. 5 | 6 | # Modified to work with Python 3 by Sebastian Castro, 2020 7 | 8 | import os 9 | import sys 10 | import json 11 | import urllib 12 | from urllib.request import Request, urlopen 13 | 14 | 15 | # Define an output folder 16 | output_directory = os.path.join("models", "ycb") 17 | 18 | # Define a list of objects to download from 19 | # http://ycb-benchmarks.s3-website-us-east-1.amazonaws.com/ 20 | objects_to_download = "all" 21 | # objects_to_download = ["001_chips_can", 22 | # "002_master_chef_can", 23 | # "003_cracker_box", 24 | # "004_sugar_box"] 25 | 26 | # You can edit this list to only download certain kinds of files. 27 | # 'berkeley_rgbd' contains all of the depth maps and images from the Carmines. 28 | # 'berkeley_rgb_highres' contains all of the high-res images from the Canon cameras. 29 | # 'berkeley_processed' contains all of the segmented point clouds and textured meshes. 30 | # 'google_16k' contains google meshes with 16k vertices. 31 | # 'google_64k' contains google meshes with 64k vertices. 32 | # 'google_512k' contains google meshes with 512k vertices. 33 | # See the website for more details. 34 | #files_to_download = ["berkeley_rgbd", "berkeley_rgb_highres", "berkeley_processed", "google_16k", "google_64k", "google_512k"] 35 | files_to_download = ["berkeley_processed", "google_16k"] 36 | 37 | # Extract all files from the downloaded .tgz, and remove .tgz files. 38 | # If false, will just download all .tgz files to output_directory 39 | extract = True 40 | 41 | base_url = "http://ycb-benchmarks.s3-website-us-east-1.amazonaws.com/data/" 42 | objects_url = "https://ycb-benchmarks.s3.amazonaws.com/data/objects.json" 43 | 44 | if not os.path.exists(output_directory): 45 | os.makedirs(output_directory) 46 | 47 | 48 | def fetch_objects(url): 49 | """ Fetches the object information before download """ 50 | response = urlopen(url) 51 | html = response.read() 52 | objects = json.loads(html) 53 | return objects["objects"] 54 | 55 | 56 | def download_file(url, filename): 57 | """ Downloads files from a given URL """ 58 | u = urlopen(url) 59 | f = open(filename,"wb") 60 | file_size = int(u.getheader("Content-Length")) 61 | print("Downloading: {} ({} MB)".format(filename, file_size/1000000.0)) 62 | 63 | file_size_dl = 0 64 | block_sz = 65536 65 | while True: 66 | buffer = u.read(block_sz) 67 | if not buffer: 68 | break 69 | 70 | file_size_dl += len(buffer) 71 | f.write(buffer) 72 | status = r"%10d [%3.2f%%]" % (file_size_dl/1000000.0, file_size_dl * 100. / file_size) 73 | status = status + chr(8)*(len(status)+1) 74 | print(status) 75 | f.close() 76 | 77 | 78 | def tgz_url(object, type): 79 | """ Get the TGZ file URL for a particular object and dataset type """ 80 | if type in ["berkeley_rgbd", "berkeley_rgb_highres"]: 81 | return base_url + "berkeley/{object}/{object}_{type}.tgz".format(object=object,type=type) 82 | elif type in ["berkeley_processed"]: 83 | return base_url + "berkeley/{object}/{object}_berkeley_meshes.tgz".format(object=object,type=type) 84 | else: 85 | return base_url + "google/{object}_{type}.tgz".format(object=object,type=type) 86 | 87 | 88 | def extract_tgz(filename, dir): 89 | """ Extract a TGZ file """ 90 | tar_command = "tar -xzf {filename} -C {dir}".format(filename=filename,dir=dir) 91 | os.system(tar_command) 92 | os.remove(filename) 93 | 94 | def check_url(url): 95 | """ Check the validity of a URL """ 96 | try: 97 | request = Request(url) 98 | request.get_method = lambda : 'HEAD' 99 | response = urlopen(request) 100 | return True 101 | except Exception as e: 102 | return False 103 | 104 | 105 | if __name__ == "__main__": 106 | 107 | # Grab all the object information 108 | objects = fetch_objects(objects_url) 109 | 110 | # Download each object for all objects and types specified 111 | for object in objects: 112 | if objects_to_download == "all" or object in objects_to_download: 113 | for file_type in files_to_download: 114 | url = tgz_url(object, file_type) 115 | if not check_url(url): 116 | continue 117 | filename = "{path}/{object}_{file_type}.tgz".format( 118 | path=output_directory, 119 | object=object, 120 | file_type=file_type) 121 | download_file(url, filename) 122 | if extract: 123 | extract_tgz(filename, output_directory) -------------------------------------------------------------------------------- /images/ycb_gazebo_inertia.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sea-bass/ycb-tools/129ddd6c48a77164f0945b3297ff4e92c676d72d/images/ycb_gazebo_inertia.png -------------------------------------------------------------------------------- /templates/ycb/model.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | $MODEL_SHORT 4 | 1.0 5 | $MODEL_SHORT.sdf 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /templates/ycb/template.material: -------------------------------------------------------------------------------- 1 | material $MODEL_SHORT 2 | { 3 | technique 4 | { 5 | pass 6 | { 7 | texture_unit 8 | { 9 | // Relative to the location of the material script 10 | texture ../$MODEL_LONG/$MESH_TYPE/$TEXTURE_FILE 11 | } 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /templates/ycb/template.sdf: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | false 5 | 6 | 7 | $COM_POSE 8 | $MASS 9 | 10 | $IXX 11 | $IXY 12 | $IXZ 13 | $IYY 14 | $IYZ 15 | $IZZ 16 | 17 | 18 | 19 | 20 | 21 | 22 | model://$COLLISION_MESH 23 | 24 | 25 | 26 | 32 | 33 | 34 | 35 | 36 | 37 | model://$MODEL_LONG/$MESH_TYPE/textured.obj 38 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 | --------------------------------------------------------------------------------