# Copyright (c) 2022-2025, The Isaac Lab Project Developers.# All rights reserved.## SPDX-License-Identifier: BSD-3-Clausefrom__future__importannotationsfromtypingimportTYPE_CHECKINGimportomni.isaac.core.utils.primsasprim_utilsimportomni.isaac.core.utils.stageasstage_utilsimportomni.kit.commandsimportomni.logfrompxrimportGf,Sdf,Usdfromomni.isaac.lab.simimportconverters,schemasfromomni.isaac.lab.sim.utilsimportbind_physics_material,bind_visual_material,clone,select_usd_variantsifTYPE_CHECKING:from.importfrom_files_cfg
[文档]@clonedefspawn_from_usd(prim_path:str,cfg:from_files_cfg.UsdFileCfg,translation:tuple[float,float,float]|None=None,orientation:tuple[float,float,float,float]|None=None,)->Usd.Prim:"""Spawn an asset from a USD file and override the settings with the given config. In the case of a USD file, the asset is spawned at the default prim specified in the USD file. If a default prim is not specified, then the asset is spawned at the root prim. In case a prim already exists at the given prim path, then the function does not create a new prim or throw an error that the prim already exists. Instead, it just takes the existing prim and overrides the settings with the given config. .. note:: This function is decorated with :func:`clone` that resolves prim path into list of paths if the input prim path is a regex pattern. This is done to support spawning multiple assets from a single and cloning the USD prim at the given path expression. Args: prim_path: The prim path or pattern to spawn the asset at. If the prim path is a regex pattern, then the asset is spawned at all the matching prim paths. cfg: The configuration instance. translation: The translation to apply to the prim w.r.t. its parent prim. Defaults to None, in which case the translation specified in the USD file is used. orientation: The orientation in (w, x, y, z) to apply to the prim w.r.t. its parent prim. Defaults to None, in which case the orientation specified in the USD file is used. Returns: The prim of the spawned asset. Raises: FileNotFoundError: If the USD file does not exist at the given path. """# spawn asset from the given usd filereturn_spawn_from_usd_file(prim_path,cfg.usd_path,cfg,translation,orientation)
[文档]@clonedefspawn_from_urdf(prim_path:str,cfg:from_files_cfg.UrdfFileCfg,translation:tuple[float,float,float]|None=None,orientation:tuple[float,float,float,float]|None=None,)->Usd.Prim:"""Spawn an asset from a URDF file and override the settings with the given config. It uses the :class:`UrdfConverter` class to create a USD file from URDF. This file is then imported at the specified prim path. In case a prim already exists at the given prim path, then the function does not create a new prim or throw an error that the prim already exists. Instead, it just takes the existing prim and overrides the settings with the given config. .. note:: This function is decorated with :func:`clone` that resolves prim path into list of paths if the input prim path is a regex pattern. This is done to support spawning multiple assets from a single and cloning the USD prim at the given path expression. Args: prim_path: The prim path or pattern to spawn the asset at. If the prim path is a regex pattern, then the asset is spawned at all the matching prim paths. cfg: The configuration instance. translation: The translation to apply to the prim w.r.t. its parent prim. Defaults to None, in which case the translation specified in the generated USD file is used. orientation: The orientation in (w, x, y, z) to apply to the prim w.r.t. its parent prim. Defaults to None, in which case the orientation specified in the generated USD file is used. Returns: The prim of the spawned asset. Raises: FileNotFoundError: If the URDF file does not exist at the given path. """# urdf loader to convert urdf to usdurdf_loader=converters.UrdfConverter(cfg)# spawn asset from the generated usd filereturn_spawn_from_usd_file(prim_path,urdf_loader.usd_path,cfg,translation,orientation)
[文档]defspawn_ground_plane(prim_path:str,cfg:from_files_cfg.GroundPlaneCfg,translation:tuple[float,float,float]|None=None,orientation:tuple[float,float,float,float]|None=None,)->Usd.Prim:"""Spawns a ground plane into the scene. This function loads the USD file containing the grid plane asset from Isaac Sim. It may not work with other assets for ground planes. In those cases, please use the `spawn_from_usd` function. Note: This function takes keyword arguments to be compatible with other spawners. However, it does not use any of the kwargs. Args: prim_path: The path to spawn the asset at. cfg: The configuration instance. translation: The translation to apply to the prim w.r.t. its parent prim. Defaults to None, in which case the translation specified in the USD file is used. orientation: The orientation in (w, x, y, z) to apply to the prim w.r.t. its parent prim. Defaults to None, in which case the orientation specified in the USD file is used. Returns: The prim of the spawned asset. Raises: ValueError: If the prim path already exists. """# Spawn Ground-planeifnotprim_utils.is_prim_path_valid(prim_path):prim_utils.create_prim(prim_path,usd_path=cfg.usd_path,translation=translation,orientation=orientation)else:raiseValueError(f"A prim already exists at path: '{prim_path}'.")# Create physics materialifcfg.physics_materialisnotNone:cfg.physics_material.func(f"{prim_path}/physicsMaterial",cfg.physics_material)# Apply physics material to ground planecollision_prim_path=prim_utils.get_prim_path(prim_utils.get_first_matching_child_prim(prim_path,predicate=lambdax:prim_utils.get_prim_type_name(x)=="Plane"))bind_physics_material(collision_prim_path,f"{prim_path}/physicsMaterial")# Scale only the mesh# Warning: This is specific to the default grid plane asset.ifprim_utils.is_prim_path_valid(f"{prim_path}/Environment"):# compute scale from sizescale=(cfg.size[0]/100.0,cfg.size[1]/100.0,1.0)# apply scale to the meshprim_utils.set_prim_property(f"{prim_path}/Environment","xformOp:scale",scale)# Change the color of the plane# Warning: This is specific to the default grid plane asset.ifcfg.colorisnotNone:prop_path=f"{prim_path}/Looks/theGrid/Shader.inputs:diffuse_tint"# change the coloromni.kit.commands.execute("ChangePropertyCommand",prop_path=Sdf.Path(prop_path),value=Gf.Vec3f(*cfg.color),prev=None,type_to_create_if_not_exist=Sdf.ValueTypeNames.Color3f,)# Remove the light from the ground plane# It isn't bright enough and messes up with the user's lighting settingsomni.kit.commands.execute("ToggleVisibilitySelectedPrims",selected_paths=[f"{prim_path}/SphereLight"])# return the primreturnprim_utils.get_prim_at_path(prim_path)
"""Helper functions."""def_spawn_from_usd_file(prim_path:str,usd_path:str,cfg:from_files_cfg.FileCfg,translation:tuple[float,float,float]|None=None,orientation:tuple[float,float,float,float]|None=None,)->Usd.Prim:"""Spawn an asset from a USD file and override the settings with the given config. In case a prim already exists at the given prim path, then the function does not create a new prim or throw an error that the prim already exists. Instead, it just takes the existing prim and overrides the settings with the given config. Args: prim_path: The prim path or pattern to spawn the asset at. If the prim path is a regex pattern, then the asset is spawned at all the matching prim paths. usd_path: The path to the USD file to spawn the asset from. cfg: The configuration instance. translation: The translation to apply to the prim w.r.t. its parent prim. Defaults to None, in which case the translation specified in the generated USD file is used. orientation: The orientation in (w, x, y, z) to apply to the prim w.r.t. its parent prim. Defaults to None, in which case the orientation specified in the generated USD file is used. Returns: The prim of the spawned asset. Raises: FileNotFoundError: If the USD file does not exist at the given path. """# check file path existsstage:Usd.Stage=stage_utils.get_current_stage()ifnotstage.ResolveIdentifierToEditTarget(usd_path):raiseFileNotFoundError(f"USD file not found at path: '{usd_path}'.")# spawn asset if it doesn't exist.ifnotprim_utils.is_prim_path_valid(prim_path):# add prim as reference to stageprim_utils.create_prim(prim_path,usd_path=usd_path,translation=translation,orientation=orientation,scale=cfg.scale,)else:omni.log.warn(f"A prim already exists at prim path: '{prim_path}'.")# modify variantsifhasattr(cfg,"variants")andcfg.variantsisnotNone:select_usd_variants(prim_path,cfg.variants)# modify rigid body propertiesifcfg.rigid_propsisnotNone:schemas.modify_rigid_body_properties(prim_path,cfg.rigid_props)# modify collision propertiesifcfg.collision_propsisnotNone:schemas.modify_collision_properties(prim_path,cfg.collision_props)# modify mass propertiesifcfg.mass_propsisnotNone:schemas.modify_mass_properties(prim_path,cfg.mass_props)# modify articulation root propertiesifcfg.articulation_propsisnotNone:schemas.modify_articulation_root_properties(prim_path,cfg.articulation_props)# modify tendon propertiesifcfg.fixed_tendons_propsisnotNone:schemas.modify_fixed_tendon_properties(prim_path,cfg.fixed_tendons_props)# define drive API on the joints# note: these are only for setting low-level simulation properties. all others should be set or are# and overridden by the articulation/actuator properties.ifcfg.joint_drive_propsisnotNone:schemas.modify_joint_drive_properties(prim_path,cfg.joint_drive_props)# modify deformable body propertiesifcfg.deformable_propsisnotNone:schemas.modify_deformable_body_properties(prim_path,cfg.deformable_props)# apply visual materialifcfg.visual_materialisnotNone:ifnotcfg.visual_material_path.startswith("/"):material_path=f"{prim_path}/{cfg.visual_material_path}"else:material_path=cfg.visual_material_path# create materialcfg.visual_material.func(material_path,cfg.visual_material)# apply materialbind_visual_material(prim_path,material_path)# return the primreturnprim_utils.get_prim_at_path(prim_path)