file_path
stringlengths
21
224
content
stringlengths
0
80.8M
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/visibility/show-hide-prim/usda.md
This is an example USDA result from creating a Cube and setting the visibility property to `inherited`. You can edit the value to `invisible` to hide the prim. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/visibility/show-hide-prim/config.toml
[core] # The title for this code sample. Used to name the page. title = "Show or Hide a Prim" [metadata] #A concise description of the code sample for SEO. description = "Universal Scene Description (OpenUSD) code samples that demonstrates how to a show or hide a prim." # Put in SEO keywords relevant to this code sample. keywords = ["OpenUSD", "USD", "Python", "visibility", "show prim", "hide prim"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/visibility/show-hide-prim/header.md
See [USD: Visibility](https://openusd.org/release/glossary.html#usdglossary-visibility). You can show or hide a prim by setting the `visibility` attribute to either `inherited` or `invisible`. If the value is set to `inherited`, then prim will either be visible or invisible depending on the prim's parent visibility value. If the value is set to `invisible`, then the prim and all children prims will be invisible.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/layers/add-sublayer/py_kit_cmds.md
``` {literalinclude} py_kit_cmds.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/layers/add-sublayer/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Sdf def add_sub_layer(sub_layer_path: str, root_layer) -> Sdf.Layer: sub_layer: Sdf.Layer = Sdf.Layer.CreateNew(sub_layer_path) # You can use standard python list.insert to add the subLayer to any position in the list root_layer.subLayerPaths.append(sub_layer.identifier) return sub_layer ############# # Full Usage ############# from pxr import Usd # Get the root layer stage: Usd.Stage = Usd.Stage.CreateInMemory() root_layer: Sdf.Layer = stage.GetRootLayer() # Add the sub layer to the root layer sub_layer = add_sub_layer(r"C:/path/to/sublayer.usd", root_layer) usda = stage.GetRootLayer().ExportToString() print(usda) # Check to see if the sublayer is loaded loaded_layers = root_layer.GetLoadedLayers() assert sub_layer in loaded_layers
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/layers/add-sublayer/py_usd.md
``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/layers/add-sublayer/py_kit_cmds.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import omni.kit.commands omni.kit.commands.execute("CreateSublayer", layer_identifier=stage.GetRootLayer().identifier, # This example prepends to the subLayers list sublayer_position=0, new_layer_path=r"C:/path/to/sublayer.usd", transfer_root_content=False, # When True, it will create the layer file for you too. create_or_insert=True )
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/layers/add-sublayer/config.toml
[core] title = "Add a SubLayer" [metadata] description = "Universal Scene Description (OpenUSD) code samples for adding an Inherit composition arc to a prim." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "layer", "SubLayer", "composition", "composition arc"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/layers/add-sublayer/header.md
A [SubLayer](https://openusd.org/release/glossary.html#usdglossary-sublayers) is a composition arc used to build [LayerStacks](https://openusd.org/release/glossary.html#usdglossary-layerstack). In a LayerStack, the Layer that includes SubLayers has the strongest opinion. From there, the SubLayers are ordered from strongest to weakest starting from top to bottom (or first to last) in the `subLayers` list. This snippet shows how to create a new Layer and add it as a SubLayer.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-payload/py_kit_cmds.md
The `CreatePayload` command is a convenient wrapper that creates an Xform prim and adds a Payload to it all at once. If you don't need the two steps batched together, you may want to [add a Payload](add-payload) to an existing prim via Kit Commands or USD Python API. ``` {literalinclude} py_kit_cmds.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-payload/usda.usda
#usda 1.0 ( endTimeCode = 100 metersPerUnit = 0.01 startTimeCode = 0 timeCodesPerSecond = 60 upAxis = "Y" ) def "World" { def "payload_prim" ( prepend payload = @file:/C:/path/to/file.usd@</World/some/target> ) { } }
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-payload/py_kit_cmds.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import omni.kit.commands import omni.usd from pxr import Usd, Sdf def create_payload(usd_context: omni.usd.UsdContext, path_to: Sdf.Path, asset_path: str, prim_path: Sdf.Path) -> Usd.Prim: omni.kit.commands.execute("CreatePayload", usd_context=usd_context, path_to=path_to, # Prim path for where to create the prim with the payload asset_path=asset_path, # The file path to the payload USD. Relative paths are accepted too. prim_path=prim_path # OPTIONAL: Prim path to a prim in the payloaded USD, if not provided the default prim is used ) return usd_context.get_stage().GetPrimAtPath(path_to) ############# # Full Usage ############# # Get the USD context from kit context: omni.usd.UsdContext = omni.usd.get_context() # Create and add external payload to specific prim payload_prim: Usd.Prim = create_payload(context, Sdf.Path("/World/payload_prim"), "C:/path/to/file.usd", Sdf.Path("/World/some/target")) # Get the existing USD stage from kit stage: Usd.Stage = context.get_stage() usda = stage.GetRootLayer().ExportToString() print(usda) # Check that the payload prims were created assert payload_prim.IsValid() assert payload_prim.GetPrimStack()[0].payloadList.prependedItems[0] == Sdf.Payload(assetPath="file:/C:/path/to/file.usd", primPath=Sdf.Path("/World/some/target"))
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-payload/usda.md
This is an example USDA result from creating a reference with the `CreateReference` command. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-payload/config.toml
[core] title = "Create a Payload" [metadata] description = "Universal Scene Description (OpenUSD) code samples for creating an Xform prim and adding a Payload in Omniverse Kit in one step." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "prim", "payload", "CreatePayload"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-payload/header.md
A [Payload](https://openusd.org/release/glossary.html#usdglossary-payload) is a composition arc that functions similar to a Reference to enable users to aggregate layers or assets onto a Stage. The difference is that a users can choose to not load a Payload. This can help users see the full hierarchy of a Stage, but only load the heavy parts ( i.e. Payloads) that they need. A Payload targets a prim from a layer and loads it and all of its descendants into a new namespace within the referencing layer. This snippet shows how to add a Payload to a prim. A single prim can have multiple Payloads.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-payload/py_kit_cmds.md
The `AddPayload` command in Kit can add payloads to a prim. ``` {literalinclude} py_kit_cmds.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-payload/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Usd, Sdf def add_payload(prim: Usd.Prim, payload_asset_path: str, payload_target_path: Sdf.Path) -> None: payloads: Usd.Payloads = prim.GetPayloads() payloads.AddPayload( assetPath=payload_asset_path, primPath=payload_target_path # OPTIONAL: Payload a specific target prim. Otherwise, uses the payloadd layer's defaultPrim. ) ############# # Full Usage ############# from pxr import UsdGeom # Create new USD stage for this sample stage: Usd.Stage = Usd.Stage.CreateInMemory() # Create and define default prim default_prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")) stage.SetDefaultPrim(default_prim.GetPrim()) # Create an xform which should hold all payloads in this sample payload_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World/payload_prim")).GetPrim() # Add an external payload add_payload(payload_prim, "C:/path/to/file.usd", Sdf.Path("/World/some/target")) # Add other external payload to default prim add_payload(payload_prim, "C:/path/to/other/file.usd", Sdf.Path.emptyPath) usda = stage.GetRootLayer().ExportToString() print(usda) # Get a list of all prepended payloads payloads = [] for prim_spec in payload_prim.GetPrimStack(): payloads.extend(prim_spec.payloadList.prependedItems) # Check that the payload prim was created and that the payloads are correct assert payload_prim.IsValid() assert payloads[0] == Sdf.Payload(assetPath="C:/path/to/file.usd", primPath=Sdf.Path("/World/some/target")) assert payloads[1] == Sdf.Payload(assetPath="C:/path/to/other/file.usd")
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-payload/usda.usda
#usda 1.0 ( defaultPrim = "World" ) def Xform "World" { def Xform "ref_prim" ( prepend payload = [ @C:/path/to/file.usd@</World/some/target>, @C:/path/to/other/file.usd@ ] ) { } }
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-payload/py_usd.md
With the USD API, you can use `Usd.Prim.GetPayloads()` to receive the payloads and add a new one with `Usd.Payloads.AddPayload()`. ``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-payload/py_kit_cmds.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import omni.kit.commands from pxr import Usd, Sdf def add_payload(prim: Usd.Prim, payload_asset_path: str, payload_target_path: Sdf.Path) -> None: omni.kit.commands.execute("AddPayload", stage=prim.GetStage(), prim_path = prim.GetPath(), # an existing prim to add the payload to. payload=Sdf.Payload( assetPath = payload_asset_path, primPath = payload_target_path ) ) ############# # Full Usage ############# from pxr import UsdGeom import omni.usd # Create new USD stage for this sample in OV context: omni.usd.UsdContext = omni.usd.get_context() success: bool = context.new_stage() stage: Usd.Stage = context.get_stage() # Create and define default prim, so this file can be easily payloaderenced again default_prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")) stage.SetDefaultPrim(default_prim.GetPrim()) # Create a xform which should hold all payloads in this sample payload_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World/payload_prim")).GetPrim() # Add an payload specific prim add_payload(payload_prim, "C:/path/to/file.usd", Sdf.Path("/World/some/target")) # Add other payload to default prim add_payload(payload_prim, "C:/path/to/other/file.usd", Sdf.Path.emptyPath) usda = stage.GetRootLayer().ExportToString() print(usda) # Get a list of all prepended payloads payloads = [] for prim_spec in payload_prim.GetPrimStack(): payloads.extend(prim_spec.payloadList.prependedItems) # Check that the payload prim was created and that the payloads are correct assert payload_prim.IsValid() assert payloads[0] == Sdf.Payload(assetPath="C:/path/to/file.usd", primPath=Sdf.Path("/World/some/target")) assert payloads[1] == Sdf.Payload(assetPath="C:/path/to/other/file.usd")
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-payload/usda.md
This is an example USDA result from creating an `Xform` and adding two `Payloads` to it. The first payload target prim in this case is in the file `C:/path/to/file.usd` with the prim path `/World/some/target` and the second is the default prim in the file `C:/path/to/other/file.usd`. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-payload/config.toml
[core] title = "Add a Payload" [metadata] description = "Universal Scene Description (OpenUSD) code samples for adding a Payload to a prim." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "prim", "payload", "AddPayload"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-payload/header.md
A [Payload](https://openusd.org/release/glossary.html#usdglossary-payload) is a composition arc that functions similar to a Reference to enable users to aggregate layers or assets onto a Stage. The difference is that a users can choose to not load a Payload. This can help users see the full hierarchy of a Stage, but only load the heavy parts ( i.e. Payloads) that they need. A Payload targets a prim from a layer and loads it and all of its descendants into a new namespace within the referencing layer. This snippet shows how to add a Payload to a prim. A single prim can have multiple Payloads.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-reference/py_kit_cmds.md
The `CreateReference` command is a convenient wrapper that creates an Xform prim and adds a Reference to it all at once. If you don't need the two steps batched together, you may want to [add a Reference](add-reference) to an existing prim via Kit Commands or USD API. ``` {literalinclude} py_kit_cmds.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-reference/usda.usda
#usda 1.0 ( endTimeCode = 100 metersPerUnit = 0.01 startTimeCode = 0 timeCodesPerSecond = 60 upAxis = "Y" ) def "World" { def "ref_prim" ( prepend references = @file:/C:/path/to/file.usd@</World/some/target> ) { } }
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-reference/py_kit_cmds.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import omni.kit.commands import omni.usd from pxr import Usd, Sdf def create_reference(usd_context: omni.usd.UsdContext, path_to: Sdf.Path, asset_path: str, prim_path: Sdf.Path) -> Usd.Prim: omni.kit.commands.execute("CreateReference", usd_context=usd_context, path_to=path_to, # Prim path for where to create the prim with the reference asset_path=asset_path, # The file path to reference. Relative paths are accepted too. prim_path=prim_path # OPTIONAL: Prim path to a prim in the referenced USD, if not provided the default prim is used ) return usd_context.get_stage().GetPrimAtPath(path_to) ############# # Full Usage ############# # Get the USD context from kit context: omni.usd.UsdContext = omni.usd.get_context() # Create and add external reference to specific prim ref_prim: Usd.Prim = create_reference(context, Sdf.Path("/World/ref_prim"), "C:/path/to/file.usd", Sdf.Path("/World/some/target")) # Get the existing USD stage from kit stage: Usd.Stage = context.get_stage() usda = stage.GetRootLayer().ExportToString() print(usda) # Check that the reference prims were created assert ref_prim.IsValid() assert ref_prim.GetPrimStack()[0].referenceList.prependedItems[0] == Sdf.Reference(assetPath="file:/C:/path/to/file.usd", primPath=Sdf.Path("/World/some/target"))
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-reference/usda.md
This is an example USDA result from creating a reference with the `CreateReference` command. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-reference/config.toml
[core] title = "Create a Reference" [metadata] description = "Universal Scene Description (OpenUSD) code samples for creating an Xform prim and adding a Reference in Omniverse Kit in one step." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "prim", "reference", "CreateReference"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/create-reference/header.md
A [Reference](https://openusd.org/release/glossary.html#usdglossary-references) is a composition arc that enables users to aggregate layers or assets onto a Stage. A Reference targets a prim from a layer and loads it and all of its descendants into a new namespace within the referencing layer. This snippet shows how to create and add a Reference to a prim. A single prim can have multiple References.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-reference/py_kit_cmds.md
The `AddReference` command in Kit can add internal and external references to a prim. ``` {literalinclude} py_kit_cmds.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-reference/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Usd, Sdf def add_int_reference(prim: Usd.Prim, ref_target_path: Sdf.Path) -> None: references: Usd.References = prim.GetReferences() references.AddInternalReference(ref_target_path) def add_ext_reference(prim: Usd.Prim, ref_asset_path: str, ref_target_path: Sdf.Path) -> None: references: Usd.References = prim.GetReferences() references.AddReference( assetPath=ref_asset_path, primPath=ref_target_path # OPTIONAL: Reference a specific target prim. Otherwise, uses the referenced layer's defaultPrim. ) ############# # Full Usage ############# from pxr import UsdGeom # Create new USD stage for this sample stage: Usd.Stage = Usd.Stage.CreateInMemory() # Create and define default prim, so this file can be easily referenced again default_prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")) stage.SetDefaultPrim(default_prim.GetPrim()) # Create an xform which should hold all references in this sample ref_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World/ref_prim")).GetPrim() # Add an internal reference intern_target_path: Sdf.Path = Sdf.Path("/World/intern_target") target_prim: Usd.Prim = UsdGeom.Xform.Define(stage, intern_target_path).GetPrim() add_int_reference(ref_prim, intern_target_path) # Add an external reference to specific prim add_ext_reference(ref_prim, "C:/path/to/file.usd", Sdf.Path("/World/some/target")) # Add other external reference to default prim add_ext_reference(ref_prim, "C:/path/to/other/file.usd", Sdf.Path.emptyPath) usda = stage.GetRootLayer().ExportToString() print(usda) # Get a list of all prepended references references = [] for prim_spec in ref_prim.GetPrimStack(): references.extend(prim_spec.referenceList.prependedItems) # Check that the reference prim was created and that the references are correct assert ref_prim.IsValid() assert references[0] == Sdf.Reference(primPath=intern_target_path) assert references[1] == Sdf.Reference(assetPath="C:/path/to/file.usd", primPath=Sdf.Path("/World/some/target")) assert references[2] == Sdf.Reference(assetPath="C:/path/to/other/file.usd")
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-reference/usda.usda
#usda 1.0 ( defaultPrim = "World" ) def Xform "World" { def Xform "ref_prim" ( prepend references = [ </World/intern_target>, @C:/path/to/file.usd@</World/some/target>, @C:/path/to/other/file.usd@ ] ) { } def Xform "intern_target" { } }
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-reference/py_usd.md
With the USD API, you can use `Usd.Prim.GetReferences()` to receive the references and add a new one with `Usd.References.AddReference()`. ``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-reference/py_kit_cmds.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import omni.kit.commands from pxr import Usd, Sdf def add_int_reference(prim: Usd.Prim, ref_target_path: Sdf.Path) -> None: omni.kit.commands.execute("AddReference", stage=prim.GetStage(), prim_path = prim.GetPath(), # an existing prim to add the reference to. reference=Sdf.Reference( primPath = ref_target_path ) ) def add_ext_reference(prim: Usd.Prim, ref_asset_path: str, ref_target_path: Sdf.Path) -> None: omni.kit.commands.execute("AddReference", stage=prim.GetStage(), prim_path = prim.GetPath(), # an existing prim to add the reference to. reference=Sdf.Reference( assetPath = ref_asset_path, primPath = ref_target_path ) ) ############# # Full Usage ############# import omni.usd from pxr import UsdGeom # Create new USD stage for this sample in OV context: omni.usd.UsdContext = omni.usd.get_context() success: bool = context.new_stage() stage: Usd.Stage = context.get_stage() # Create and define default prim, so this file can be easily referenced again default_prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")) stage.SetDefaultPrim(default_prim.GetPrim()) # Create a xform which should hold all references in this sample ref_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World/ref_prim")).GetPrim() # Add an internal reference intern_target_path: Sdf.Path = Sdf.Path("/World/intern_target") target_prim: Usd.Prim = UsdGeom.Xform.Define(stage, intern_target_path).GetPrim() add_int_reference(ref_prim, intern_target_path) # Add an external reference to specific prim add_ext_reference(ref_prim, "C:/path/to/file.usd", Sdf.Path("/World/some/target")) # Add other external reference to default prim add_ext_reference(ref_prim, "C:/path/to/other/file.usd", Sdf.Path.emptyPath) usda = stage.GetRootLayer().ExportToString() print(usda) # Get a list of all prepended references references = [] for prim_spec in ref_prim.GetPrimStack(): references.extend(prim_spec.referenceList.prependedItems) # Check that the reference prim was created and that the references are correct assert ref_prim.IsValid() assert references[0] == Sdf.Reference(primPath=intern_target_path) assert references[1] == Sdf.Reference(assetPath="C:/path/to/file.usd", primPath=Sdf.Path("/World/some/target")) assert references[2] == Sdf.Reference(assetPath="C:/path/to/other/file.usd")
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-reference/usda.md
This is an example USDA result from creating an `Xform` and adding three `References` to it. The first referenced target prim is inside the current file with the prim path `/World/intern_target`. The second referenced target prim is in the file `C:/path/to/file.usd` with the prim path `/World/some/target` and the third is the default prim in the file `C:/path/to/other/file.usd`. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-reference/config.toml
[core] title = "Add a Reference" [metadata] description = "Universal Scene Description (OpenUSD) code samples for adding a Reference to a prim." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "prim", "reference", "AddReference"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/references-payloads/add-reference/header.md
A [Reference](https://openusd.org/release/glossary.html#usdglossary-references) is a composition arc that enables users to aggregate layers or assets onto a Stage. A Reference targets a prim from a layer and loads it and all of its descendants into a new namespace within the referencing layer. This snippet shows how to create and add a Reference to a prim. A single prim can have multiple References.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/author-variant-data/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 shading_varset = prim.GetVariantSets().GetVariantSet("shading") selected_variant = shading_varset.GetVariantSelection() shading_varset.SetVariantSelection(variant_name) with shading_varset.GetVariantEditContext(): # Specs authored within this context are authored just for the variant. ... # Set the variant selection back to the previously selected variant. # Alternatively, you can use Usd.VariantSet.ClearVariantSelection() # if you know that there isn't a variant selection in the current EditTarget. if selected_variant: shading_varset.SetVariantSelection(selected_variant)
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/author-variant-data/py_usd.md
``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/author-variant-data/config.toml
[core] # The title for this code sample. Used to name the page. title = "Author Data for a Particular Variant" [metadata] #A concise description of the code sample for SEO. description = "Universal Scene Description (OpenUSD) code samples for authoring data for a particular variant set." # Put in SEO keywords relevant to this code sample. keywords = ["OpenUSD", "USD", "code sample", "Python", "C++", "variant set", "composition", "variant"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/author-variant-data/header.md
Opinions (i.e. data) for a particular variant can be authored on different layers. This shows how you can author opinions for an existing variant that might have been authored on a different layer.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/select-variant/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Usd def select_variant_from_varaint_set(prim: Usd.Prim, variant_set_name: str, variant_name: str) -> None: variant_set = prim.GetVariantSets().GetVariantSet(variant_set_name) variant_set.SetVariantSelection(variant_name) ############# # Full Usage ############# from pxr import Sdf, UsdGeom # Create an in-memory Stage with /World Xform prim as the default prim stage: Usd.Stage = Usd.Stage.CreateInMemory() default_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")).GetPrim() stage.SetDefaultPrim(default_prim) # Create the Variant Set shading_varset: Usd.VariantSet = default_prim.GetVariantSets().AddVariantSet("shading") # Add Variants to the Variant Set shading_varset.AddVariant("cell_shading") shading_varset.AddVariant("realistic") select_variant_from_varaint_set(default_prim, "shading", "realistic") usda = stage.GetRootLayer().ExportToString() print(usda) assert default_prim.GetVariantSets().GetVariantSet("shading").GetVariantSelection() == "realistic"
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/select-variant/usda.usda
#usda 1.0 ( defaultPrim = "World" ) def Xform "World" ( variants = { string shading = "realistic" } prepend variantSets = "shading" ) { variantSet "shading" = { "cell_shading" { } "realistic" { } } }
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/select-variant/py_usd.md
``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/select-variant/usda.md
This is an example USDA result from creating a Variant Set, adding two Variants to the set, and selecting the current Variant to `realistic`. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/select-variant/config.toml
[core] # The title for this code sample. Used to name the page. title = "Select a Variant for a Variant Set" [metadata] #A concise description of the code sample for SEO. description = "Universal Scene Description (OpenUSD) code samples for selecting a variant belonging to a variant set." # Put in SEO keywords relevant to this code sample. keywords = ["OpenUSD", "USD", "code sample", "Python", "C++", "variant set", "composition", "variant"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/select-variant/header.md
Apart from selecting a default Variant when you create a Variant Set, you may want to change the selection in other USD layers. For example, a model could have a shading Variant Set defined, but when you Reference that model a few times in a Stage, you may want to select a different shading Variant for each Reference.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/create-variant-set/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Usd def create_variant_set(prim: Usd.Prim, variant_set_name: str, variants: list) -> Usd.VariantSet: variant_set = prim.GetVariantSets().AddVariantSet(variant_set_name) for variant in variants: variant_set.AddVariant(variant) return variant_set ############# # Full Usage ############# from pxr import Sdf, UsdGeom # Create an in-memory Stage with /World Xform prim as the default prim stage: Usd.Stage = Usd.Stage.CreateInMemory() default_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")).GetPrim() stage.SetDefaultPrim(default_prim) # Create the variant set and add your variants to it. variants = ["red", "blue", "green"] shading_varset: Usd.VariantSet = create_variant_set(default_prim, "shading", variants) usda = stage.GetRootLayer().ExportToString() print(usda) assert default_prim.GetVariantSets().HasVariantSet("shading")
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/create-variant-set/usda.usda
#usda 1.0 ( defaultPrim = "World" ) def Xform "World" ( prepend variantSets = "shading" ) { variantSet "shading" = { "blue" { } "green" { } "red" { } } }
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/create-variant-set/py_usd.md
``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/create-variant-set/usda.md
This is an example USDA result from creating a Variant Set and adding Variants to the Set. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/create-variant-set/config.toml
[core] title = "Create a Variant Set" [metadata] description = "Universal Scene Description (OpenUSD) code samples showing how to create a variant set." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "variant set", "composition", "create variant set", "variant"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/variant-sets/create-variant-set/header.md
```{note} If this is your first time authoring a variant set, we recommend that you follow [Authoring Variants USD tutorial](https://openusd.org/docs/Authoring-Variants.html) first. ``` A [Variant Set](https://openusd.org/release/glossary.html#usdglossary-variantset) is a composition arc that serves as a sort of "switchable Reference" allowing you to provide alternate property opinions or entire prim hierarchies. In this snippet, you'll find how to create a Variant Set, add variants to the new Variant Set and author opinions for each variant.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/specializes/add-specialize/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Usd def add_specialize_to(base_prim: Usd.Prim, specializes: Usd.Specializes) -> bool: return specializes.AddSpecialize(base_prim.GetPath()) ############# # Full Usage ############# from pxr import Sdf, UsdGeom # Create an in-memory Stage with /World Xform prim as the default prim stage: Usd.Stage = Usd.Stage.CreateInMemory() default_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")).GetPrim() stage.SetDefaultPrim(default_prim) prim: Usd.Prim = UsdGeom.Xform.Define(stage, default_prim.GetPath().AppendPath("prim")).GetPrim() base: Usd.Prim = UsdGeom.Xform.Define(stage, default_prim.GetPath().AppendPath("base")).GetPrim() specializes: Usd.Specializes = prim.GetSpecializes() added_successfully = add_specialize_to(base, specializes) usda = stage.GetRootLayer().ExportToString() print(usda) assert added_successfully
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/specializes/add-specialize/usda.usda
#usda 1.0 ( defaultPrim = "World" ) def Xform "World" { def Xform "prim" ( prepend specializes = </World/base> ) { } def Xform "base" { } }
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/specializes/add-specialize/py_usd.md
``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/specializes/add-specialize/usda.md
This is an example USDA result adding an Specialize Arc to a prim. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/specializes/add-specialize/config.toml
[core] title = "Add a Specialize" [metadata] description = "Universal Scene Description (OpenUSD) code samples for adding a Specialize composition arc to a prim." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "specialize", "composition", "composition arc"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/specializes/add-specialize/header.md
A [Specialize](https://openusd.org/release/glossary.html#usdglossary-specializes) is a composition arc that enables a prim to contain all of the scene description contained in the base prim it specializes. The difference between Specialize and Inherit is that opinions authored on the prim with the specialize arc will always be stronger than the base prim. This snippet shows how to add a Specialize arc to a prim. A single prim can have multiple Specializes.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/select-prim-by-path/py_kit_cmds.md
``` {literalinclude} py_kit_cmds.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/select-prim-by-path/py_omni_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import omni.usd prim_path = "/World/My/Prim" ctx = omni.usd.get_context() # The second arg is unused. Any boolean can be used. ctx.get_selection().set_selected_prim_paths([prim_path], True)
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/select-prim-by-path/py_kit_cmds.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import omni.kit.commands import omni.usd prim_path = "/World/My/Prim" ctx = omni.usd.get_context() old_selection = ctx.get_selection().get_selected_prim_paths() omni.kit.commands.execute('SelectPrimsCommand', old_selected_paths=old_selection, new_selected_paths=[prim_path], expand_in_stage=True) #DEPRECATED: Used only for backwards compatibility.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/select-prim-by-path/config.toml
[core] title = "Select a Prim by Prim Path" [metadata] description = "Universal Scene Description (OpenUSD) code samples showing how to select a prim using its prim path." keywords = ["OpenUSD", "USD", "Python", "code sample", "prim", "selection", "by path", "path", "prim path"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/select-prim-by-path/py_omni_usd.md
``` {literalinclude} py_omni_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/select-prim-by-path/header.md
USD does not have a concept of a selection, but in Kit, you can select prims enabling users and extensions to then use manipulators and tools on the selection or perform actions upon the selection. These snippets shows you how you can select one or more prims by their prim paths.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/check-prim-exists/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Usd def check_prim_exists(prim: Usd.Prim) -> bool: if prim.IsValid(): return True return False ############# # Full Usage ############# from pxr import Sdf, UsdGeom # Create an in-memory Stage with /World Xform prim as the default prim stage: Usd.Stage = Usd.Stage.CreateInMemory() default_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")).GetPrim() stage.SetDefaultPrim(default_prim) # Create one prim and cube: Usd.Prim = UsdGeom.Cube.Define(stage, Sdf.Path("/World/Cube")).GetPrim() empty_prim = Usd.Prim() usda = stage.GetRootLayer().ExportToString() print(usda) # Check if prims exist assert check_prim_exists(default_prim) assert check_prim_exists(cube) assert not check_prim_exists(empty_prim)
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/check-prim-exists/py_usd.md
``` {literalinclude} py_usd.py :language: py ``` Alternatively, `Usd.Object` overrides the boolean operator so you can check with a simple boolean expression. ``` {literalinclude} py_usd_var1.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/check-prim-exists/config.toml
[core] title = "Check if a Prim Exists" [metadata] description = "Universal Scene Description (OpenUSD) code samples for checking if a Prim exists on a Stage." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "prim", "exists", "IsValid", "valid"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/check-prim-exists/py_usd_var1.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Usd def check_prim_exists(prim: Usd.Prim) -> bool: if prim: return True return False ############# # Full Usage ############# from pxr import Sdf, UsdGeom # Create an in-memory Stage with /World Xform prim as the default prim stage: Usd.Stage = Usd.Stage.CreateInMemory() default_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")).GetPrim() stage.SetDefaultPrim(default_prim) # Create one prim and cube: Usd.Prim = UsdGeom.Cube.Define(stage, Sdf.Path("/World/Cube")).GetPrim() empty_prim = Usd.Prim() usda = stage.GetRootLayer().ExportToString() print(usda) # Check if prims exist assert check_prim_exists(default_prim) assert check_prim_exists(cube) assert not check_prim_exists(None)
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/check-prim-exists/header.md
Certain functions may return a `Usd.Prim` object, but the Prim may not exist due to an incorrect path or because of changes on the Stage. You can use [Usd.Object.IsValid()](https://openusd.org/release/api/class_usd_object.html#ac532c4b500b1a85ea22217f2c65a70ed) to check if the Prim is valid or exists.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-prim/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from typing import Union from pxr import Usd, Sdf def get_prim_by_path(stage: Usd.Stage, prim_path: Union[str, Sdf.Path]) -> Usd.Prim: return stage.GetPrimAtPath(prim_path) ############## # Full Usage ############## from pxr import UsdGeom # Create an in-memory Stage with /World Xform prim as the default prim stage: Usd.Stage = Usd.Stage.CreateInMemory() default_prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")) stage.SetDefaultPrim(default_prim.GetPrim()) # Create some prims UsdGeom.Xform.Define(stage, "/World/Group") UsdGeom.Cube.Define(stage, "/World/Group/Foo") # Get a prim using a str object group_prim_path = "/World/Group" group_prim = get_prim_by_path(stage, group_prim_path) # Get a prim using an Sdf.Path object foo_prim_path = Sdf.Path("/World/Group/Foo") foo_prim = get_prim_by_path(stage, foo_prim_path) # Print the prim objects that were retrieved print(group_prim) print(foo_prim) # Check that the prims retrieve match the paths provided assert group_prim.GetPath() == Sdf.Path(group_prim_path) assert foo_prim.GetPath() == foo_prim_path
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-prim/py_usd.md
Get a prim by using the prim’s path: ``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-prim/config.toml
[core] title = "Get a Prim" [metadata] description = "Universal Scene Description (OpenUSD) code samples for accessing or getting a Prim on the Stage." keywords = ["OpenUSD", "USD", "Python", "code sample", "prim", "get prim", "accessing", "IsValid", "valid"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-prim/header.md
Getting a prim on the Stage lets you read and write to their attributes and lets you pass them to APIs.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-current-selected-prims/py_usdview.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from typing import List from pxr import Usd prims: List[Usd.Prim] = usdviewApi.selectedPrims
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-current-selected-prims/py_omni_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import omni.usd prim_path = "/World/My/Prim" ctx = omni.usd.get_context() # returns a list of prim path strings selection = ctx.get_selection().get_selected_prim_paths()
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-current-selected-prims/py_usdview.md
*usdview* Python interpreter has a built-in object called `usdviewApi` that gives you access to the currently selected prims. ``` {literalinclude} py_usdview.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-current-selected-prims/config.toml
[core] title = "Get the Currently Selected Prims" [metadata] description = "Universal Scene Description (OpenUSD) code samples for getting the currently selected prim(s)." keywords = ["OpenUSD", "USD", "Python", "code sample", "prim", "selection", "selected", "prims", "usdview"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-current-selected-prims/py_omni_usd.md
``` {literalinclude} py_omni_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/prims/get-current-selected-prims/header.md
USD does not have a concept of a selection, but in Kit, you can select prims enabling users and extensions to then use manipulators and tools on the selection or perform actions upon the selection. This snippet shows you how you can get the list of currently selected prim paths on the stage.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/inherits/add-inherit/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Usd def add_inherit(stage: Usd.Stage, prim: Usd.Prim, class_prim: Usd.Prim): inherits: Usd.Inherits = prim.GetInherits() inherits.AddInherit(class_prim.GetPath()) ############# # Full Usage ############# from pxr import Sdf, UsdGeom # Create an in-memory Stage with /World Xform prim as the default prim stage: Usd.Stage = Usd.Stage.CreateInMemory() default_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")).GetPrim() stage.SetDefaultPrim(default_prim) # The base prim typically uses the "class" Specifier to designate that it # is meant to be inherited and skipped in standard stage traversals tree_class: Usd.Prim = stage.CreateClassPrim("/_class_Tree") tree_prim: Usd.Prim = UsdGeom.Mesh.Define(stage, default_prim.GetPath().AppendPath("TreeA")).GetPrim() add_inherit(stage, tree_prim, tree_class) usda = stage.GetRootLayer().ExportToString() print(usda) # Check to see if the inherit was added inherits_list = tree_prim.GetInherits().GetAllDirectInherits() assert tree_class.GetPath() in inherits_list
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/inherits/add-inherit/usda.usda
#usda 1.0 ( defaultPrim = "World" ) def Xform "World" { def Mesh "TreeA" ( prepend inherits = </_class_Tree> ) { } } class "_class_Tree" { }
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/inherits/add-inherit/py_usd.md
This code sample shows how to add an Inherit arc to a prim. A single prim can have multiple Inherits. ``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/inherits/add-inherit/usda.md
This is an example USDA result adding an Inherit Arc to a prim. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/inherits/add-inherit/config.toml
[core] title = "Add an Inherit" [metadata] description = "Universal Scene Description (OpenUSD) code samples for adding an Inherit composition arc to a prim." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "inherit", "AddInherit", "composition", "composition arc"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/inherits/add-inherit/header.md
An [Inherit](https://openusd.org/release/glossary.html#usdglossary-inherits) is a composition arc that enables a prim to contain all of the scene description contained in the base prim it inherits. This enables users to author opinions on the base prim that are broadcast to all the prims that inherit it. The [Inherit USD Glossary entry](https://openusd.org/release/glossary.html#usdglossary-inherits) explains the nuances of the composition arc in more detail.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/data-types/convert-vtarray-numpy/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import numpy from pxr import Vt def convert_vt_to_np(my_array: Vt.Vec3fArray) -> numpy.ndarray: return numpy.array(my_vec3_array) ############# # Full Usage ############# # Create a Vt.Vec3fArray and convert it to a numpy array my_vec3_array = Vt.Vec3fArray([(1,2,3),(4,5,6),(7,8,9)]) np_array: numpy.ndarray = convert_vt_to_np(my_vec3_array) # print the numpy array to check the values print(np_array) # check the size and length of the numpy array assert np_array.size == 9 assert len(np_array) == 3
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/data-types/convert-vtarray-numpy/py_usd.md
**Convert to Numpy Array** To convert a VtArray to a Numpy Array, simply pass the VtArray object to `numpy.array` constructor. ``` {literalinclude} py_usd.py :language: py ``` **Convert from Numpy Array** To convert a Numpy Array to a VtArray, you can use `FromNumpy()` from the VtArray class you want to convert to. ``` {literalinclude} py_usd_var1.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/data-types/convert-vtarray-numpy/config.toml
[core] title = "Convert Between VtArray and Numpy Array" [metadata] description = "Universal Scene Description (OpenUSD) code samples for converting between VtArray classes and Numpy." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "types", "array", "numpy", "VtArray"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/data-types/convert-vtarray-numpy/py_usd_var1.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import numpy from pxr import Vt def convert_np_to_vt(my_array: numpy.ndarray) -> Vt.Vec3fArray: return Vt.Vec3fArray.FromNumpy(my_array) ############# # Full Usage ############# # Create a numpy array and convert it into a Vt.Vec3fArray np_array = numpy.array([(1,2,3),(4,5,6),(7,8,9)]) from_numpy: Vt.Vec3fArray = convert_np_to_vt(np_array) # Print the Vt.Vec3fArray to check the values print(from_numpy) # Check the length of the numpy array assert len(np_array) == 3
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/data-types/convert-vtarray-numpy/header.md
Some Attributes store array type data which are accessed using the VtArray classes. You can find a list of the VtArray classes in our [USD Data Types documentation](https://docs.omniverse.nvidia.com/dev-guide/latest/dev_usd/quick-start/usd-types.html) If you need to manipulate the arrays using Python, it is advantageous to use Numpy to benefit from it's speed and efficiency. These code samples show how you can convert between the VtArray objects and Numpy Array objects. ```{note} These examples show how to convert using only the Vt.Vec3fArray class, but the same can be applied to any VtArray class. See what other VtArray classes exist in the [USD Data Types documentation](https://docs.omniverse.nvidia.com/dev-guide/latest/dev_usd/quick-start/usd-types.html). ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/paths/concatenate-property-with-prim-path/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Sdf def concat_property_with_prim_path(prim_path: Sdf.Path, prop) -> Sdf.Path: prop_path = prim_path.AppendProperty(prop) return prop_path ############# # Full Usage ############# # e.g., get path to "points" attribute on a mesh prim from pxr import UsdGeom, Usd stage: Usd.Stage = Usd.Stage.CreateInMemory() default_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")).GetPrim() stage.SetDefaultPrim(default_prim) mesh_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World/Mesh")).GetPrim() prop_path: Sdf.Path = concat_property_with_prim_path(mesh_prim.GetPrimPath(), UsdGeom.Tokens.points) #nothing happend so did it get added? usda = stage.GetRootLayer().ExportToString() print(usda) assert Sdf.Path.IsValidPathString(prop_path.pathString)
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/paths/concatenate-property-with-prim-path/py_usd.md
``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/paths/concatenate-property-with-prim-path/config.toml
[core] title = "Concatenate a Property Name with a Prim Path" [metadata] description = "Universal Scene Description (OpenUSD) code samples showing how to concatenate a property name with a prim path." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "prim", "property", "path", "AppendProperty"]
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/paths/concatenate-property-with-prim-path/header.md
If you want to concatenate a property name with a Prim path, you can use [Sdf.Path.AppendProperty](https://openusd.org/release/api/class_sdf_path.html#a94b67bfea8a8295271a32014b563d913). In this example, given `Sdf.Path("/World/MyMesh")` in `mesh_prim_path`, `points_attr_path` will contain `Sdf.Path("/World/MyMesh.points")`.
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/paths/get-parent-path/py_usd.py
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 from pxr import Sdf def get_parent_path(prim_path: Sdf.Path) -> Sdf.Path: parent_path = prim_path.GetParentPath() return parent_path ############# # Full Usage ############# from pxr import Usd, UsdGeom # Create an in-memory Stage with /World Xform prim as the default prim stage: Usd.Stage = Usd.Stage.CreateInMemory() default_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")).GetPrim() stage.SetDefaultPrim(default_prim) cone_prim = UsdGeom.Cone.Define(stage, Sdf.Path("/World/Cone")).GetPrim() # Given Sdf.Path('/World/Cone') for my_prim_path, parent_path will contain Sdf.Path('/World') parent_path = get_parent_path(cone_prim.GetPrimPath()) usda = stage.GetRootLayer().ExportToString() print(usda) assert parent_path == default_prim.GetPrimPath()
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/paths/get-parent-path/usda.usda
#usda 1.0 ( defaultPrim = "World" ) def Xform "World" { def Cone "Cone" { } }
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/paths/get-parent-path/py_usd.md
``` {literalinclude} py_usd.py :language: py ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/paths/get-parent-path/usda.md
This is an example USDA result from adding a Cone to the a World Prim. ``` {literalinclude} usda.usda :language: usd ```
NVIDIA-Omniverse/OpenUSD-Code-Samples/source/paths/get-parent-path/config.toml
[core] title = "Get the Parent Path for a Prim Path" [metadata] description = "Universal Scene Description (OpenUSD) code samples for getting the parent path from a prim path." keywords = ["OpenUSD", "USD", "Python", "snippet", "code sample", "prim", "path", "GetParentPath"]