Skip to content

asset_utils

change_data_path()

Changes the data paths for this repo

Source code in utils/asset_utils.py
def change_data_path():
    """
    Changes the data paths for this repo
    """
    with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "global_config.yaml")) as f:
        global_config = yaml.load(f, Loader=yaml.FullLoader)
    print("Current dataset path:")
    for k, v in global_config.items():
        print("{}: {}".format(k, v))
    for k, v in global_config.items():
        new_path = input("Change {} from {} to: ".format(k, v))
        global_config[k] = new_path

    print("New dataset path:")
    for k, v in global_config.items():
        print("{}: {}".format(k, v))
    response = input("Save? [y/n]")
    if response == "y":
        with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "global_config.yaml"), "w") as f:
            yaml.dump(global_config, f)

download_assets()

Download OmniGibson assets

Source code in utils/asset_utils.py
def download_assets():
    """
    Download OmniGibson assets
    """
    if os.path.exists(og.assets_path):
        print("Assets already downloaded.")
    else:
        tmp_file = os.path.join(tempfile.gettempdir(), "og_assets.tar.gz")
        os.makedirs(og.assets_path, exist_ok=True)
        path = "https://storage.googleapis.com/gibson_scenes/og_assets.tar.gz"
        logging.info(f"Downloading and decompressing demo OmniGibson assets from {path}")
        assert subprocess.call(["wget", "-c", "--no-check-certificate", "--retry-connrefused", "--tries=5", "--timeout=5", path, "-O", tmp_file]) == 0, "Assets download failed."
        assert subprocess.call(["tar", "-zxf", tmp_file, "--strip-components=1", "--directory", og.assets_path]) == 0, "Assets extraction failed."

download_demo_data()

Download OmniGibson demo dataset

Source code in utils/asset_utils.py
def download_demo_data():
    """
    Download OmniGibson demo dataset
    """
    # TODO: Update. Right now, OG just downloads beta release
    download_og_dataset()

download_og_dataset()

Download OmniGibson dataset

Source code in utils/asset_utils.py
def download_og_dataset():
    """
    Download OmniGibson dataset
    """
    # Print user agreement
    if os.path.exists(og.key_path):
        print("OmniGibson dataset encryption key already installed.")
    else:
        print("\n")
        print_user_agreement()
        while (
            input(
                "Do you agree to the above terms for using OmniGibson dataset? [y/n]"
            )
            != "y"
        ):
            print("You need to agree to the terms for using OmniGibson dataset.")

        download_key()

    if os.path.exists(og.og_dataset_path):
        print("OmniGibson dataset already installed.")
    else:
        tmp_file = os.path.join(tempfile.gettempdir(), "og_dataset.tar.gz")
        os.makedirs(og.og_dataset_path, exist_ok=True)
        path = "https://storage.googleapis.com/gibson_scenes/og_dataset.tar.gz"
        logging.info(f"Downloading and decompressing demo OmniGibson dataset from {path}")
        assert subprocess.call(["wget", "-c", "--no-check-certificate", "--retry-connrefused", "--tries=5", "--timeout=5", path, "-O", tmp_file]) == 0, "Dataset download failed."
        assert subprocess.call(["tar", "-zxf", tmp_file, "--strip-components=1", "--directory", og.og_dataset_path]) == 0, "Dataset extraction failed."

folder_is_hidden(p)

Removes hidden folders from a list. Works on Linux, Mac and Windows

Returns:

Name Type Description
bool

true if a folder is hidden in the OS

Source code in utils/asset_utils.py
def folder_is_hidden(p):
    """
    Removes hidden folders from a list. Works on Linux, Mac and Windows

    Returns:
        bool: true if a folder is hidden in the OS
    """
    if os.name == "nt":
        attribute = win32api.GetFileAttributes(p)
        return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM)
    else:
        return p.startswith(".")  # linux-osx

get_all_object_categories()

Get OmniGibson all object categories

Returns:

Name Type Description
list

all object categories

Source code in utils/asset_utils.py
def get_all_object_categories():
    """
    Get OmniGibson all object categories

    Returns:
        list: all object categories
    """
    og_dataset_path = og.og_dataset_path
    og_categories_path = os.path.join(og_dataset_path, "objects")

    categories = sorted([f for f in os.listdir(og_categories_path) if not folder_is_hidden(f)])
    return categories

get_all_object_models()

Get OmniGibson all object models

Returns:

Name Type Description
list

all object model paths

Source code in utils/asset_utils.py
def get_all_object_models():
    """
    Get OmniGibson all object models

    Returns:
        list: all object model paths
    """
    og_dataset_path = og.og_dataset_path
    og_categories_path = os.path.join(og_dataset_path, "objects")

    categories = os.listdir(og_categories_path)
    categories = [item for item in categories if os.path.isdir(os.path.join(og_categories_path, item))]
    models = []
    for category in categories:
        category_models = os.listdir(os.path.join(og_categories_path, category))
        category_models = [
            item for item in category_models if os.path.isdir(os.path.join(og_categories_path, category, item))
        ]
        models.extend([os.path.join(og_categories_path, category, item) for item in category_models])
    return models

get_assisted_grasping_categories()

Generate a list of categories that can be grasped using assisted grasping, using labels provided in average category specs file.

Returns:

Type Description

list of str: Object category allowlist for assisted grasping

Source code in utils/asset_utils.py
def get_assisted_grasping_categories():
    """
    Generate a list of categories that can be grasped using assisted grasping,
    using labels provided in average category specs file.

    Returns:
        list of str: Object category allowlist for assisted grasping
    """
    assisted_grasp_category_allow_list = set()
    avg_category_spec = get_og_avg_category_specs()
    for k, v in avg_category_spec.items():
        if v["enable_ag"]:
            assisted_grasp_category_allow_list.add(k)
    return assisted_grasp_category_allow_list

get_available_g_scenes()

Returns:

Name Type Description
list

available Gibson scenes

Source code in utils/asset_utils.py
def get_available_g_scenes():
    """
    Returns:
        list: available Gibson scenes
    """
    data_path = og.g_dataset_path
    available_g_scenes = sorted([f for f in os.listdir(data_path) if not folder_is_hidden(f)])
    return available_g_scenes

get_available_og_scenes()

OmniGibson interactive scenes

Returns:

Name Type Description
list

Available OmniGibson interactive scenes

Source code in utils/asset_utils.py
def get_available_og_scenes():
    """
    OmniGibson interactive scenes

    Returns:
        list: Available OmniGibson interactive scenes
    """
    og_dataset_path = og.og_dataset_path
    og_scenes_path = os.path.join(og_dataset_path, "scenes")
    available_og_scenes = sorted(
        [f for f in os.listdir(og_scenes_path) if (not folder_is_hidden(f) and f != "background")]
    )
    return available_og_scenes

get_object_models_of_category(category_name, filter_method=None)

Get OmniGibson all object models of a given category

TODO: Make this less ugly -- filter_method is a single hard-coded check

Parameters:

Name Type Description Default
category_name str

object category

required
filter_method str

Method to use for filtering object models

None

Returns:

Name Type Description
list

all object models of a given category

Source code in utils/asset_utils.py
def get_object_models_of_category(category_name, filter_method=None):
    """
    Get OmniGibson all object models of a given category

    # TODO: Make this less ugly -- filter_method is a single hard-coded check

    Args:
        category_name (str): object category
        filter_method (str): Method to use for filtering object models

    Returns:
        list: all object models of a given category
    """
    models = []
    og_category_path = get_og_category_path(category_name)
    for model_name in os.listdir(og_category_path):
        if filter_method is None:
            models.append(model_name)
        elif filter_method in ["sliceable_part", "sliceable_whole"]:
            model_path = get_og_model_path(category_name, model_name)
            metadata_json = os.path.join(model_path, "misc", "metadata.json")
            with open(metadata_json) as f:
                metadata = json.load(f)
            if (filter_method == "sliceable_part" and "object_parts" not in metadata) or (
                filter_method == "sliceable_whole" and "object_parts" in metadata
            ):
                models.append(model_name)
        else:
            raise Exception("Unknown filter method: {}".format(filter_method))
    return models

get_og_assets_version()

Returns:

Name Type Description
str

OmniGibson asset version

Source code in utils/asset_utils.py
def get_og_assets_version():
    """
    Returns:
        str: OmniGibson asset version
    """
    process = subprocess.Popen(
        ["git", "-C", og.og_dataset_path, "rev-parse", "HEAD"], shell=False, stdout=subprocess.PIPE
    )
    git_head_hash = str(process.communicate()[0].strip())
    return "{}".format(git_head_hash)

get_og_avg_category_specs()

Load average object specs (dimension and mass) for objects

Returns:

Name Type Description
dict

Average category specifications for all object categories

Source code in utils/asset_utils.py
def get_og_avg_category_specs():
    """
    Load average object specs (dimension and mass) for objects

    Returns:
        dict: Average category specifications for all object categories
    """
    avg_obj_dim_file = os.path.join(og.og_dataset_path, "metadata", "avg_category_specs.json")
    if os.path.exists(avg_obj_dim_file):
        with open(avg_obj_dim_file) as f:
            return json.load(f)
    else:
        logging.warning(
            "Requested average specs of the object categories in the OmniGibson Dataset of objects, but the "
            "file cannot be found. Did you download the dataset? Returning an empty dictionary"
        )
        return dict()

get_og_category_ids()

Get OmniGibson object categories

Returns:

Name Type Description
str

file path to the scene name

Source code in utils/asset_utils.py
def get_og_category_ids():
    """
    Get OmniGibson object categories

    Returns:
        str: file path to the scene name
    """
    og_dataset_path = og.og_dataset_path
    og_categories_files = os.path.join(og_dataset_path, "metadata", "categories.txt")
    name_to_id = {}
    with open(og_categories_files, "r") as fp:
        for i, l in enumerate(fp.readlines()):
            name_to_id[l.rstrip()] = i
    return defaultdict(lambda: 255, name_to_id)

get_og_category_path(category_name)

Get OmniGibson object category path

Parameters:

Name Type Description Default
category_name str

object category

required

Returns:

Name Type Description
str

file path to the object category

Source code in utils/asset_utils.py
def get_og_category_path(category_name):
    """
    Get OmniGibson object category path

    Args:
        category_name (str): object category

    Returns:
        str: file path to the object category
    """
    og_dataset_path = og.og_dataset_path
    og_categories_path = os.path.join(og_dataset_path, "objects")
    assert category_name in os.listdir(og_categories_path), "Category {} does not exist".format(category_name)
    return os.path.join(og_categories_path, category_name)

get_og_model_path(category_name, model_name)

Get OmniGibson object model path

Parameters:

Name Type Description Default
category_name str

object category

required
model_name str

object model

required

Returns:

Name Type Description
str

file path to the object model

Source code in utils/asset_utils.py
def get_og_model_path(category_name, model_name):
    """
    Get OmniGibson object model path

    Args:
        category_name (str): object category
        model_name (str): object model

    Returns:
        str: file path to the object model
    """
    og_category_path = get_og_category_path(category_name)
    assert model_name in os.listdir(og_category_path), "Model {} from category {} does not exist".format(
        model_name, category_name
    )
    return os.path.join(og_category_path, model_name)

get_og_scene_path(scene_name)

Get OmniGibson scene path

Parameters:

Name Type Description Default
scene_name str

scene name, e.g., "Rs_int"

required

Returns:

Name Type Description
str

file path to the scene name

Source code in utils/asset_utils.py
def get_og_scene_path(scene_name):
    """
    Get OmniGibson scene path

    Args:
        scene_name (str): scene name, e.g., "Rs_int"

    Returns:
        str: file path to the scene name
    """
    og_dataset_path = og.og_dataset_path
    og_scenes_path = os.path.join(og_dataset_path, "scenes")
    logging.info("Scene name: {}".format(scene_name))
    assert scene_name in os.listdir(og_scenes_path), "Scene {} does not exist".format(scene_name)
    return os.path.join(og_scenes_path, scene_name)

get_scene_path(scene_id)

Parameters:

Name Type Description Default
scene_id str

scene id, e.g., "Rs_int"

required

Returns:

Name Type Description
str

scene path for this scene_id

Source code in utils/asset_utils.py
def get_scene_path(scene_id):
    """
    Args:
        scene_id (str): scene id, e.g., "Rs_int"

    Returns:
        str: scene path for this scene_id
    """
    data_path = og.g_dataset_path
    assert scene_id in os.listdir(data_path), "Scene {} does not exist".format(scene_id)
    return os.path.join(data_path, scene_id)

get_texture_file(mesh_file)

Get texture file

Parameters:

Name Type Description Default
mesh_file str

path to mesh obj file

required

Returns:

Name Type Description
str

texture file path

Source code in utils/asset_utils.py
def get_texture_file(mesh_file):
    """
    Get texture file

    Args:
        mesh_file (str): path to mesh obj file

    Returns:
        str: texture file path
    """
    model_dir = os.path.dirname(mesh_file)
    with open(mesh_file, "r") as f:
        lines = [line.strip() for line in f.readlines() if "mtllib" in line]
        if len(lines) == 0:
            return
        mtl_file = lines[0].split()[1]
        mtl_file = os.path.join(model_dir, mtl_file)

    with open(mtl_file, "r") as f:
        lines = [line.strip() for line in f.readlines() if "map_Kd" in line]
        if len(lines) == 0:
            return
        texture_file = lines[0].split()[1]
        texture_file = os.path.join(model_dir, texture_file)

    return texture_file