2727def add_labels (prim : Usd .Prim , labels : list [str ], instance_name : str = "class" , overwrite : bool = True ) -> None :
2828 """Apply semantic labels to a prim using the :class:`UsdSemantics.LabelsAPI`.
2929
30- This function is a wrapper around the :func:`omni.replicator.core.functional.modify.semantics` function .
31- It applies the labels to the prim using the :class:`UsdSemantics.LabelsAPI `.
30+ For Isaac Sim 5.0+, this uses :class:`UsdSemantics.LabelsAPI` directly on the prim .
31+ For earlier versions, it falls back to the deprecated :class:`Semantics.SemanticsAPI `.
3232
3333 .. versionadded:: 2.3.0
3434 This function is available in Isaac Sim 5.0 and later, which introduces the :class:`UsdSemantics.LabelsAPI`.
35- For previous versions, the function falls back to use the deprecated :class:`UsdSemantics .SemanticsAPI` instead.
35+ For previous versions, the function falls back to use the deprecated :class:`Semantics .SemanticsAPI` instead.
3636
3737 Example:
3838 >>> prim = sim_utils.create_prim("/World/Test/Sphere", "Sphere", stage=stage, attributes={"radius": 10.0})
@@ -45,44 +45,37 @@ def add_labels(prim: Usd.Prim, labels: list[str], instance_name: str = "class",
4545 overwrite: Whether to overwrite existing labels for this instance. If False,
4646 the new labels are appended to existing ones (if any). Defaults to True.
4747 """
48- # Try modern approach (Isaac Sim >= 5.0 )
49- try :
50- import omni . replicator . core . functional as rep_functional
51-
52- mode = "replace" if overwrite else "add"
53- rep_functional . modify . semantics ( prim , { instance_name : labels }, mode = mode )
54-
55- return
56- except ( ModuleNotFoundError , ImportError ) as e :
57- # check if we are using isaac sim 5.0
58- if get_isaac_sim_version (). major >= 5 :
59- logger . warning (
60- f"Failed to add labels to prim { prim . GetPath () } using Replicator API: { e } . "
61- " \n Please ensure Replicator API is enabled by passing '--enable_cameras' to the AppLauncher."
62- " \n Falling back to legacy approach."
63- )
64-
65- # Try legacy approach (Isaac Sim < 5.0)
66- try :
48+ isaac_sim_version = get_isaac_sim_version ( )
49+
50+ if isaac_sim_version . major >= 5 :
51+ # Isaac Sim 5.0+: Use UsdSemantics.LabelsAPI directly
52+ # This uses the prim's own stage reference, avoiding issues with stale global stage references
53+ # that can occur with the Replicator API when using stage-in-memory mode.
54+ labels_api = UsdSemantics . LabelsAPI . Apply ( prim , instance_name )
55+ labels_attr = labels_api . CreateLabelsAttr ()
56+ if overwrite :
57+ labels_attr . Set ( labels )
58+ else :
59+ existing = labels_attr . Get ()
60+ if existing :
61+ combined = list ( existing ) + [ lbl for lbl in labels if lbl not in existing ]
62+ labels_attr . Set ( combined )
63+ else :
64+ labels_attr . Set ( labels )
65+ else :
66+ # Isaac Sim < 5.0: Use legacy Semantics.SemanticsAPI
6767 import Semantics
6868
69- # check we have only one label
7069 if len (labels ) != 1 :
71- raise ValueError (f"Only one label can be applied to a prim. Received: { labels } " )
72- # set the semantic API for the instance
73- instance_name = f" { instance_name } _ { labels [ 0 ] } "
74- sem = Semantics . SemanticsAPI . Apply ( prim , instance_name )
75- # create semantic type and data attributes
70+ raise ValueError (
71+ f"Legacy SemanticsAPI only supports one label per instance. Received { len ( labels ) } : { labels } "
72+ )
73+ sem_instance_name = f" { instance_name } _ { labels [ 0 ] } "
74+ sem = Semantics . SemanticsAPI . Apply ( prim , sem_instance_name )
7675 sem .CreateSemanticTypeAttr ()
7776 sem .CreateSemanticDataAttr ()
78- sem .GetSemanticTypeAttr ().Set (instance_name )
77+ sem .GetSemanticTypeAttr ().Set (sem_instance_name )
7978 sem .GetSemanticDataAttr ().Set (labels [0 ])
80- except Exception as e :
81- logger .warning (
82- f"Failed to add labels to prim { prim .GetPath ()} using legacy API: { e } . "
83- "\n Semantics functionality may not be available in this Isaac Sim version."
84- " Please open an issue at https://github.com/isaac-sim/IsaacLab/issues if you believe this is a bug."
85- )
8679
8780
8881def get_labels (prim : Usd .Prim ) -> dict [str , list [str ]]:
0 commit comments