Repository: NVIDIA-Omniverse/USD-Tutorials-And-Examples Branch: main Commit: c1495c75009e Files: 6 Total size: 40.7 KB Directory structure: gitextract_098kbkei/ ├── ColaboratoryNotebooks/ │ ├── hierarchy_and_traversal.ipynb │ ├── opening_stages.ipynb │ ├── prims_attributes_and_metadata.ipynb │ └── usd_introduction.ipynb ├── LICENSE.md └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: ColaboratoryNotebooks/hierarchy_and_traversal.ipynb ================================================ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "hierarchy-and-traversal.ipynb", "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "IftoR-r0D6R0" }, "source": [ "# USD Tutorial: Hierarchy and Traversal\n", "\n", "**To run this sample:** click _Runtime_ > _Run all_ from the top menu, or use the /CTRL+F9 keyboard shortcut." ] }, { "cell_type": "markdown", "metadata": { "id": "nyWofqfjEmNk" }, "source": [ "## Install the `usd-core` Python package\n", "\n", "Install the [`usd-core`](https://pypi.org/project/usd-core/) Python package providing the core USD libraries. Note that it does not provide any of the optional plugins or imaging features from the complete USD distribution." ] }, { "cell_type": "code", "metadata": { "id": "RjqwbEkREoUb" }, "source": [ "! pip install usd-core\n", "\n", "# See https://pypi.org/project/usd-core/#history for a list of supported USD\n", "# versions." ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "PkN9Gs6NIBIj" }, "source": [ "## Tutorial\n", "\n", "USD Stages are organized in a hierarchy of Prims: there is a special root prim at `/` and it may have N-number of direct Prim descendants, each of which can have their own tree of Prim descendants.\n", "\n", "The path to a Prim is described by a string which starts with the root prim `/` and contains the Prim name separated by the path separator `/` until the last component is the desired Prim's name.\n", "\n", "For example `/Car/Wheel/Tire` refers to the `Tire` prim which has parent `Wheel` and grandparent `Car`. `Car`'s parent is the special root prim `/`.\n", "\n", "In the Tutorial section on Stages there is information on how to retrieve a Prim at a given path using `stage_ref.GetPrimAtPath()`.\n", "\n", "Here is a refresher, we'll assume *car.usda* has the `/Car/Wheel/Tire` path:" ] }, { "cell_type": "code", "metadata": { "id": "oJrApcfjIYcM" }, "source": [ "%%file car.usda\n", "#usda 1.0\n", "def \"Car\" {\n", " def \"Wheel\" {\n", " def \"Tire\" {\n", " \n", " }\n", " }\n", "}" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "Rq-2a6pjIjgx" }, "source": [ "from pxr import Usd\n", "\n", "stage_ref = Usd.Stage.Open('car.usda')\n", "prim_ref = stage_ref.GetPrimAtPath('/Car')" ], "execution_count": 3, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "JirNBzx8IoGD" }, "source": [ "Now, if you want to get a specific child of a Prim, and you know the name, you can use `prim_ref.GetChild(child_name)`:" ] }, { "cell_type": "code", "metadata": { "id": "vurZazQCIlHc" }, "source": [ "stage_ref = Usd.Stage.Open('car.usda')\n", "\n", "prim_ref = stage_ref.GetPrimAtPath('/Car')\n", "child_prim_ref = prim_ref.GetChild('Wheel')\n", "\n", "# Prims can be cast as bool, so you can check if the prim exists by comparing\n", "# its bool() overload\n", "if child_prim_ref:\n", " print(\"/Car/Wheel exists\") # This will execute\n", "\n", "print(child_prim_ref.GetPath()) # Prints \"\"/Car/Wheel\"" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "yvCu6v_nIvgD" }, "source": [ "If you want to get all the children of a Prim, you can use `prim_ref.GetChildren()` which returns a list of prim references:" ] }, { "cell_type": "code", "metadata": { "id": "UVYAXLftIyY0" }, "source": [ "stage_ref = Usd.Stage.Open('car.usda')\n", "\n", "prim_ref = stage_ref.GetPrimAtPath('/Car')\n", "\n", "# Will return [Usd.Prim()]\n", "children_refs = prim_ref.GetChildren()" ], "execution_count": 5, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "QVgskWPGI0mr" }, "source": [ "If you want to traverse the entire stage, the `stage_ref.Traverse()` function is perfect for that, it returns an iterator:" ] }, { "cell_type": "code", "metadata": { "id": "FkRIiEmgI3a8" }, "source": [ "stage_ref = Usd.Stage.Open('car.usda')\n", "\n", "for prim_ref in stage_ref.Traverse():\n", " print(prim_ref.GetPath())" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "qPyn-gVtI5UL" }, "source": [ "There are more advanced traversal methods described in the [UsdStage](https://graphics.pixar.com/usd/docs/api/class_usd_stage.html#adba675b55f41cc1b305bed414fc4f178) documentation." ] } ] } ================================================ FILE: ColaboratoryNotebooks/opening_stages.ipynb ================================================ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "opening-stages.ipynb", "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "IftoR-r0D6R0" }, "source": [ "# USD Tutorial: Opening Stages\n", "\n", "**To run this sample:** click _Runtime_ > _Run all_ from the top menu, or use the /CTRL+F9 keyboard shortcut." ] }, { "cell_type": "markdown", "metadata": { "id": "nyWofqfjEmNk" }, "source": [ "## Install the `usd-core` Python package\n", "\n", "Install the [`usd-core`](https://pypi.org/project/usd-core/) Python package providing the core USD libraries. Note that it does not provide any of the optional plugins or imaging features from the complete USD distribution." ] }, { "cell_type": "code", "metadata": { "id": "RjqwbEkREoUb" }, "source": [ "! pip install usd-core\n", "\n", "# See https://pypi.org/project/usd-core/#history for a list of supported USD\n", "# versions." ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "ZNXYTKBzELR7" }, "source": [ "## Tutorial\n", "\n", "Working with USD Stages is pretty straight forward, as most times everything is one function call away.\n", "\n", "To load a USD file as a USD Stage you use `Usd.Stage.Open(path)`:" ] }, { "cell_type": "code", "metadata": { "id": "2-Z3qbXsE4Qs" }, "source": [ "%%file sphere_sample.usda\n", "#usda 1.0\n", "def Sphere \"sphere\"\n", "{\n", "}" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "8A730ygjEyZ0" }, "source": [ "from pxr import Usd\n", "\n", "stage = Usd.Stage.Open('sphere_sample.usda')" ], "execution_count": 3, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "b55FFtgcFMQs" }, "source": [ "To create a new Stage use `Usd.Stage.CreateNew(path)`:" ] }, { "cell_type": "code", "metadata": { "id": "OlgYaHQBFP67" }, "source": [ "stage = Usd.Stage.CreateNew('a_new_stage.usd') " ], "execution_count": 4, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "BRANR9wMFUO3" }, "source": [ "To save a loaded Stage use `Usd.Stage.Save(path)`:" ] }, { "cell_type": "code", "metadata": { "id": "uNk0nfjNFW3U" }, "source": [ "stage = Usd.Stage.Open('sphere_sample.usda')\n", "# Do something to the stage\n", "stage.Save() " ], "execution_count": 5, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "lEKbxlWXFbhW" }, "source": [ "To export a stage to a new file, you can use `Usd.Stage.Export()`. This function allows you to transition between serialization formats (*usda* or *usdc*) as well, based on the file extension provided." ] }, { "cell_type": "code", "metadata": { "id": "3XsEIAAZFi8b" }, "source": [ "stage = Usd.Stage.Open('sphere_sample.usda')\n", "# Do something to the stage\n", "stage.Export('sphere_sample.usdc')" ], "execution_count": null, "outputs": [] } ] } ================================================ FILE: ColaboratoryNotebooks/prims_attributes_and_metadata.ipynb ================================================ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "prims-attributes-and-metadata.ipynb", "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "IftoR-r0D6R0" }, "source": [ "# USD Tutorial: Prims, Attributes and Metadata\n", "\n", "**To run this sample:** click _Runtime_ > _Run all_ from the top menu, or use the /CTRL+F9 keyboard shortcut." ] }, { "cell_type": "markdown", "metadata": { "id": "nyWofqfjEmNk" }, "source": [ "## Install the `usd-core` Python package\n", "\n", "Install the [`usd-core`](https://pypi.org/project/usd-core/) Python package providing the core USD libraries. Note that it does not provide any of the optional plugins or imaging features from the complete USD distribution." ] }, { "cell_type": "code", "metadata": { "id": "RjqwbEkREoUb" }, "source": [ "! pip install usd-core\n", "\n", "# See https://pypi.org/project/usd-core/#history for a list of supported USD\n", "# versions." ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "gr8anrjrLkJU" }, "source": [ "%%file sphere_sample.usda\n", "#usda 1.0\n", "def Sphere \"sphere\"\n", "{\n", "}" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "Sl7Ua6G6FpKz" }, "source": [ "## Tutorial\n", "\n", "### Prims\n", "Working with Prims is a more complicated since Prims are extremely powerful objects in USD. Prims are referenced by their path in the stage, which is a string in the form of `/Prim/ChildPrim`. `/` is a special prim known as the root prim in a stage.\n", "\n", "To get a reference to a prim at a path use `stage_ref.GetPrimAtPath(path)`:" ] }, { "cell_type": "code", "metadata": { "id": "9fJ8AuN1Fzwj" }, "source": [ "from pxr import Sdf, Usd, UsdGeom\n", "\n", "stage_ref = Usd.Stage.Open('sphere_sample.usda')\n", "\n", "prim = stage_ref.GetPrimAtPath('/sphere')\n", "print(prim.GetName()) # Prints \"sphere\"\n", "print(prim.GetPrimPath()) # Prints \"/sphere\"" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "1clJziPQFzOb" }, "source": [ "To define a new prim use `stage_ref.DefinePrim(path)`:" ] }, { "cell_type": "code", "metadata": { "id": "HCCvIRfmGDEb" }, "source": [ "stage_ref = Usd.Stage.Open('sphere_sample.usda')\n", "\n", "prim = stage_ref.DefinePrim('/UnTypedPrim')\n", "print(prim.GetName()) # Prints \"UnTypedPrim\"" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "E82AGFcYGGaL" }, "source": [ "To define a new prim with a type use `stage_ref.DefinePrim(path, type_name)` or you can use your Type's `SomeType.Define(stage_ref, path)` method:" ] }, { "cell_type": "code", "metadata": { "id": "j-W-VHJ6GLY8" }, "source": [ "stage_ref = Usd.Stage.Open('sphere_sample.usda')\n", "\n", "prim = stage_ref.DefinePrim('/XformPrim', 'Xform')\n", "# Above we have a Usd.Prim, if we want to access all the Xform's types natively,\n", "# we need to get an Xform instance of our prim\n", "xform = UsdGeom.Xform(prim)\n", "\n", "print(xform.GetPath()) # Prints \"/XformPrim\"\n", "\n", "# It is often better to use the Define() method of your type right away, since\n", "# it returns your typed instance rather than a Usd.Prim instance\n", "\n", "xform_faster = UsdGeom.Xform.Define(stage_ref, '/AnotherXformPrim')" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "r8zq9u2bGXU0" }, "source": [ "To delete a prim from the current edit layer (please refer to the [documentation about RemovePrim](https://graphics.pixar.com/usd/docs/api/class_usd_stage.html#ac605faad8fc2673263775b1eecad2955) for details) you can use `stage_ref.RemovePrim(path)`:" ] }, { "cell_type": "code", "metadata": { "id": "Ld7Zz88YGf3b" }, "source": [ "stage_ref = Usd.Stage.Open('sphere_sample.usda')\n", "prim = stage_ref.DefinePrim('/UnTypedPrim')\n", "\n", "if stage_ref.RemovePrim('/UnTypedPrim'):\n", " print('/UnTypedPrim removed')\n", "\n", "# If you try to access the prim object, it will still reference path but it is\n", "# expired\n", "if (prim.IsValid()):\n", " print('{} is valid'.format(prim.GetName()))\n", "else:\n", " print('{} is not valid'.format(prim.GetName()))\n", " \n", "# The above will print \"Accessed invalid expired prim \"" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "we85c_IeGppD" }, "source": [ "## Attributes\n", "Attributes are the workhorse of storing actual data inside a Prim. Attributes are often defined as part of [Schemas](https://graphics.pixar.com/usd/docs/USD-Glossary.html#USDGlossary-Schema) to make it easier to access context-relevant data from within an instance of that Type.\n", "\n", "For example, `Xform` typed Prims have an attribute called `Purpose` which is used to specify the purpose of an imageable prim. It contains one of the following values: `[default, render, proxy, guide]`\n", "\n", "Now, you could get this attribute's value in two ways. One, as a generic `prim_ref.GetAttribute(name)` call, but you would have to know that the exact name of the attribute you want is \"purpose\", and you wouldn't be able to get any code completion in an IDE that way.\n", "\n", "The other way is to use the Xform Schema's exposed function for getting the purpose, which is `xform_ref.GetPurposeAttr()`, which returns the same object, but will be typed in an IDE and does not depend on the underlying string name of the attribute.\n", "\n", "Most often after you get an Attribute object, you will want to get the attribute's actual value or set it. That can be done with *attribute_ref.Get()* to retrieve the value, and `attribute_ref.Set(value)` to set the value.\n", "\n", "Let's see the code for getting an Attribute reference and getting its value:\n" ] }, { "cell_type": "code", "metadata": { "id": "Z5qLyZ-NHEtV" }, "source": [ "stage_ref = Usd.Stage.Open('sphere_sample.usda')\n", "\n", "# Get a reference to the Xform instance as well as a generic Prim instance\n", "xform_ref = UsdGeom.Xform.Define(stage_ref, '/XformPrim')\n", "prim_ref = xform_ref.GetPrim()\n", "\n", "# Get an attribute reference (not its value!)\n", "purpose_from_xform_ref = xform_ref.GetPurposeAttr()\n", "purpose_from_prim_ref = prim_ref.GetAttribute('purpose')\n", "\n", "print(purpose_from_xform_ref == purpose_from_prim_ref) # Prints \"True\"\n", "\n", "# Prints the actual attribute's value, in this case, one of [default, render,\n", "# proxy, guide], since it is the Xform's actual Purpose attribute\n", "print(purpose_from_xform_ref.Get())" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "4QEhnGQXHNFz" }, "source": [ "To create an attribute that isn't part of a Type's namespace (or it is, but you want to create the attribute \"manually\"), you must pass the attribute name and its type to `prim_ref.CreateAttribute(name, type)`.\n", "\n", "Otherwise, most Types expose a `Set`-style command, for example `xform_ref.SetPurposeAttr(value)`.\n", "\n", "### Working with Attributes\n", "\n", "You can call the `prim_ref.CreateAttribute(name, type)` function to create the attribute, and we can use the information above to select a valid `type`. It returns a reference to the attribute created, which we can set with `attribute_ref.Set(value)`, and again we can construct a valid value by looking up the constructor above." ] }, { "cell_type": "code", "metadata": { "id": "gJU_efttH2tU" }, "source": [ "stage_ref = Usd.Stage.CreateInMemory()\n", "\n", "# Create a Usd.Prim\n", "prim_ref = stage_ref.DefinePrim('/Prim')\n", "\n", "# Create an attribute reference, using an explicit reference to the type\n", "weight_attr = prim_ref.CreateAttribute('weight', Sdf.ValueTypeNames.Float)\n", "\n", "print(weight_attr.Get()) # Prints empty string for default Float values, not 0!\n", "\n", "print(weight_attr.Get() == None) # Prints \"True\"\n", "print(weight_attr.Get() == 0) # Prints \"False\"\n", "\n", "# To set an attribute we use the `attribute_ref.Set(value)` function\n", "weight_attr.Set(42.3)\n", "\n", "print(weight_attr.Get()) # Prints \"42.3\"\n", "\n", "# Also, you can chain calls like so\n", "print(prim_ref.GetPrim().GetAttribute('weight').Get()) # Prints \"42.3\"" ], "execution_count": null, "outputs": [] } ] } ================================================ FILE: ColaboratoryNotebooks/usd_introduction.ipynb ================================================ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "GTC_usd_introduction.ipynb", "provenance": [], "collapsed_sections": [], "toc_visible": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "d7SGPe5YpUm3" }, "source": [ "# An introduction to USD basics\n", "\n", "The goal of this Colab notebook is to introduce some basics of USD including variants, references, definitions, and schemas. \n", "\n", "While it does not cover the full extent of USD composition, it shows the practical foundations of some of the most useful features to get started with. \n", "\n", "**To run this sample:** click _Runtime_ > _Run all_ from the top menu, or use the /CTRL+F9 keyboard shortcut." ] }, { "cell_type": "markdown", "metadata": { "id": "-9aGK7fXmghS" }, "source": [ "## Install the `usd-core` Python package\n", "\n", "Install the [`usd-core`](https://pypi.org/project/usd-core/) Python package providing the core USD libraries. Note that it does not provide any of the optional plugins or imaging features from the complete USD distribution." ] }, { "cell_type": "code", "metadata": { "id": "epRX-Z0OmCkp", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "3f0afbdd-0c6a-4fb6-9402-b54fd88fad4a" }, "source": [ "! pip install usd-core\n", "\n", "# See https://pypi.org/project/usd-core/#history for a list of supported USD\n", "# versions." ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "Tgskn4nFmcJ1" }, "source": [ "## Creating a Layer in USD\n", "\n", "USD files represent a layer is the USD lexicon. From the [Pixar Glossary](https://graphics.pixar.com/usd/docs/USD-Glossary.html#USDGlossary-Layer):\n", "> A *Layer* is the atomic persistent container of scene description for USD. A layer contains zero or more `PrimSpecs`, that in turn describe `Property` and `Metadata` values. Each layer possesses an *identifier* that can be used to contruct references to the layer from other layers. Although it may be possible to someday remove this restriction, layers must currently correspond to files on a filesystem accessible via POSIX filesystem interfaces.\n", "\n", "The following section will demonstrate how to create a basic Layer using Python.\n", "\n", "See Pixar's [_USD Tutorials_](https://graphics.pixar.com/usd/docs/USD-Tutorials.html) for more details." ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LQtAdRvxRVOs", "outputId": "6fff73a4-8fb5-4bb2-d2f1-120d54708359" }, "source": [ "from pxr import Usd, UsdGeom\n", "\n", "# Create a tempory stage in memory\n", "stage = Usd.Stage.CreateInMemory('SampleLayer.usda')\n", "\n", "# Create a transform and add a sphere as mesh data\n", "xformPrim = UsdGeom.Xform.Define(stage, '/MySphere')\n", "\n", "# Set a translation\n", "UsdGeom.XformCommonAPI(xformPrim).SetTranslate((7,8,9))\n", "\n", "spherePrim = UsdGeom.Sphere.Define(stage, '/MySphere/MeshData')\n", "\n", "# Get the sphere as a generic prim\n", "sphere = stage.GetPrimAtPath('/MySphere/MeshData')\n", "\n", "# Get the extent and radius parameters for the prim\n", "radiusAttr = sphere.GetAttribute('radius')\n", "extentAttr = sphere.GetAttribute('extent')\n", "\n", "# Access the sphere schema to set the color\n", "colorAttr = spherePrim.GetDisplayColorAttr()\n", "\n", "# Set the radius to 2\n", "radiusAttr.Set(2)\n", "\n", "# Expand the extents to match the new radius \n", "extentAttr.Set(extentAttr.Get()*2)\n", "\n", "# Make the sphere blue\n", "colorAttr.Set([(0,0,1)])\n", "\n", "# Print out the stage\n", "print(stage.GetRootLayer().ExportToString())\n", "\n", "# Save the resulting layer\n", "stage.GetRootLayer().Export('SampleLayer.usda')\n" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "#usda 1.0\n", "\n", "def Xform \"MySphere\"\n", "{\n", " double3 xformOp:translate = (7, 8, 9)\n", " uniform token[] xformOpOrder = [\"xformOp:translate\"]\n", "\n", " def Sphere \"MeshData\"\n", " {\n", " float3[] extent = [(-2, -2, -2), (2, 2, 2)]\n", " color3f[] primvars:displayColor = [(0, 0, 1)]\n", " double radius = 2\n", " }\n", "}\n", "\n", "\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "markdown", "metadata": { "id": "Mxi_-wW3vr9K" }, "source": [ "## Understanding how References work\n", "\n", "One of the most basic and useful tools for composing scenes in USD is the reference. \n", "\n", "From the [USD Glossary](https://graphics.pixar.com/usd/docs/USD-Glossary.html#USDGlossary-References): \n", "\n", "> The primary use for References is to compose smaller units of scene description into larger *aggregates*, building up a namespace that includes the \"encapsulated\" result of composing the scene description targeted by a reference.\n", "\n", "The following example demonstrates how to make a reference, then override properties of the referenced data. " ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "oejtRITBXNk8", "outputId": "3017b468-d650-40ce-9b45-4d65bad61ca5" }, "source": [ "from pxr import Usd, UsdGeom\n", "\n", "# Create a tempory stage in memory\n", "stage = Usd.Stage.CreateInMemory('ReferenceExample.usda')\n", "\n", "# Create a place for the reference to live\n", "refSphere = stage.OverridePrim('/refSphere')\n", "\n", "# Create the reference\n", "refSphere.GetReferences().AddReference('./SampleLayer.usda', '/MySphere')\n", "\n", "# Remove the translation operation applied to the base sphere's transform\n", "refXform = UsdGeom.Xformable(refSphere)\n", "refXform.SetXformOpOrder([])\n", "\n", "# Print out the stage\n", "print(\"The Layer\\n\\n\")\n", "print(stage.GetRootLayer().ExportToString())\n", "print(\"\\n\\nThe result of Composition \\n\\n\")\n", "print(stage.Flatten().ExportToString())\n", "print(\"\\n\\n\")\n", "# Override the color of the sphere to be red\n", "overMeshData = UsdGeom.Sphere.Get(stage, '/refSphere/MeshData')\n", "overMeshData.GetDisplayColorAttr().Set([(1,0,0)])\n", "\n", "# Print out the stage\n", "print(\"The Layer\\n\\n\")\n", "print(stage.GetRootLayer().ExportToString())\n", "print(\"\\n\\nThe result of Composition \\n\\n\")\n", "print(stage.Flatten().ExportToString())\n", "\n", "# Save the resulting layer\n", "stage.GetRootLayer().Export('RefExample.usda')\n" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "The Layer\n", "\n", "\n", "#usda 1.0\n", "\n", "over \"refSphere\" (\n", " prepend references = @./SampleLayer.usda@\n", ")\n", "{\n", " uniform token[] xformOpOrder = []\n", "}\n", "\n", "\n", "\n", "\n", "The result of Composition \n", "\n", "\n", "#usda 1.0\n", "(\n", " doc = \"\"\"Generated from Composed Stage of root layer \n", "\"\"\"\n", ")\n", "\n", "def Xform \"refSphere\"\n", "{\n", " double3 xformOp:translate = (7, 8, 9)\n", " uniform token[] xformOpOrder = []\n", "\n", " def Sphere \"MeshData\"\n", " {\n", " float3[] extent = [(-2, -2, -2), (2, 2, 2)]\n", " color3f[] primvars:displayColor = [(0, 0, 1)]\n", " double radius = 2\n", " }\n", "}\n", "\n", "\n", "\n", "\n", "\n", "The Layer\n", "\n", "\n", "#usda 1.0\n", "\n", "over \"refSphere\" (\n", " prepend references = @./SampleLayer.usda@\n", ")\n", "{\n", " uniform token[] xformOpOrder = []\n", "\n", " over \"MeshData\"\n", " {\n", " color3f[] primvars:displayColor = [(1, 0, 0)]\n", " }\n", "}\n", "\n", "\n", "\n", "\n", "The result of Composition \n", "\n", "\n", "#usda 1.0\n", "(\n", " doc = \"\"\"Generated from Composed Stage of root layer \n", "\"\"\"\n", ")\n", "\n", "def Xform \"refSphere\"\n", "{\n", " double3 xformOp:translate = (7, 8, 9)\n", " uniform token[] xformOpOrder = []\n", "\n", " def Sphere \"MeshData\"\n", " {\n", " float3[] extent = [(-2, -2, -2), (2, 2, 2)]\n", " color3f[] primvars:displayColor = [(1, 0, 0)]\n", " double radius = 2\n", " }\n", "}\n", "\n", "\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "cell_type": "markdown", "metadata": { "id": "lZ-dCTRkHNhL" }, "source": [ "## Understanding Variants\n", "\n", "Variants and VariantSets allow a content author to provide a prim with multiple looks or forms with relative ease. \n", "\n", "From the [USD Glossary](https://graphics.pixar.com/usd/docs/USD-Glossary.html#USDGlossary-VariantSet):\n", "> A *VariantSet* is a composition arc that allows a content creator to package a discrete set of alternatives, between which a downstream consumer is able to non-destructively switch, or augment. A reasonable way to think about VariantSets is as a \"switchable reference\". Each Variant of a VariantSet encapsulates a tree of scene description that will be composed onto the prim on which the VariantSet is defined, when the Variant is selected. VariantSet names must be legal USD identifiers.\n", "\n", "In the following example we will show how to add a VariantSet that lets the content creator select different colors for their sphere." ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "yC52K_ktcfSS", "outputId": "0d4d9b18-7d1b-4e3f-98b4-0932517a315f" }, "source": [ "from pxr import Usd, UsdGeom\n", "\n", "# Create a tempory stage in memory\n", "stage = Usd.Stage.CreateInMemory('VariantExample.usda')\n", "\n", "# Create a place for the reference to live\n", "variantSphere = stage.OverridePrim('/variantSphere')\n", "\n", "# Create the reference\n", "variantSphere.GetReferences().AddReference('./SampleLayer.usda', '/MySphere')\n", "\n", "# Remove the translation operation applied to the base sphere's transform\n", "variantXform = stage.GetPrimAtPath('/variantSphere')\n", "\n", "# Clear any color on the base sphere\n", "overMeshData = UsdGeom.Sphere.Get(stage, '/variantSphere/MeshData')\n", "colorAttr = overMeshData.GetDisplayColorAttr()\n", "colorAttr.Clear()\n", "\n", "# Add the VariantSet\n", "colorVariants = variantXform.GetVariantSets().AddVariantSet('ColorsRGB')\n", "\n", "# Add variants to the VariantSet\n", "colorVariants.AddVariant('red')\n", "colorVariants.AddVariant('green')\n", "colorVariants.AddVariant('blue')\n", "\n", "# Set the variant values\n", "colorVariants.SetVariantSelection('red')\n", "with colorVariants.GetVariantEditContext():\n", " colorAttr.Set([(1,0,0)])\n", "\n", "colorVariants.SetVariantSelection('green')\n", "with colorVariants.GetVariantEditContext():\n", " colorAttr.Set([(0,1,0)])\n", "\n", "colorVariants.SetVariantSelection('blue')\n", "with colorVariants.GetVariantEditContext():\n", " colorAttr.Set([(0,0,1)])\n", "\n", "# Set the color to be green\n", "colorVariants.SetVariantSelection('green')\n", "\n", "print(\"\\n\\n The Layer\\n\\n\")\n", "print(stage.GetRootLayer().ExportToString())\n", "print(\"\\n\\n The Result of Composition\\n\\n\")\n", "print(stage.Flatten().ExportToString())\n", "\n", "# Save the resulting layer\n", "stage.GetRootLayer().Export('VariantExample.usda')\n" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "\n", "\n", " The Layer\n", "\n", "\n", "#usda 1.0\n", "\n", "over \"variantSphere\" (\n", " prepend references = @./SampleLayer.usda@\n", " variants = {\n", " string ColorsRGB = \"green\"\n", " }\n", " prepend variantSets = \"ColorsRGB\"\n", ")\n", "{\n", " variantSet \"ColorsRGB\" = {\n", " \"blue\" {\n", " over \"MeshData\"\n", " {\n", " color3f[] primvars:displayColor = [(0, 0, 1)]\n", " }\n", "\n", " }\n", " \"green\" {\n", " over \"MeshData\"\n", " {\n", " color3f[] primvars:displayColor = [(0, 1, 0)]\n", " }\n", "\n", " }\n", " \"red\" {\n", " over \"MeshData\"\n", " {\n", " color3f[] primvars:displayColor = [(1, 0, 0)]\n", " }\n", "\n", " }\n", " }\n", "}\n", "\n", "\n", "\n", "\n", " The Result of Composition\n", "\n", "\n", "#usda 1.0\n", "(\n", " doc = \"\"\"Generated from Composed Stage of root layer \n", "\"\"\"\n", ")\n", "\n", "def Xform \"variantSphere\"\n", "{\n", " double3 xformOp:translate = (7, 8, 9)\n", " uniform token[] xformOpOrder = [\"xformOp:translate\"]\n", "\n", " def Sphere \"MeshData\"\n", " {\n", " float3[] extent = [(-2, -2, -2), (2, 2, 2)]\n", " color3f[] primvars:displayColor = [(0, 1, 0)]\n", " double radius = 2\n", " }\n", "}\n", "\n", "\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] } ] } ================================================ FILE: LICENSE.md ================================================ MIT License Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. ================================================ FILE: README.md ================================================ # OpenUSD Tutorials and Examples ## :information_source: Content Moved The content previously hosted in this repository has been migrated to the [NVIDIA Learning Path for OpenUSD](https://www.nvidia.com/en-us/learn/learning-path/openusd/). This repository remains available in read-only mode for historical reference. For the most up-to-date tutorials, examples, and learning materials, we encourage you to explore the Learning Path. Designed for developers and 3D practitioners, these free online courses cover the fundamentals of OpenUSD as well as practical applications. The curriculum offers a structured path from introductory concepts and best practices to advanced topics, equipping participants with the skills needed to confidently apply OpenUSD in real-world workflows. ---
Click here for details about historical Jupyter Notebooks ## About This project showcases educational material for [Pixar's Universal Scene Description](https://graphics.pixar.com/usd/docs/index.html) (OpenUSD). For convenience, the Jupyter Notebook from the GTC 2021 session is available on Google Colaboratory, which offers an interactive environment from the comfort of your web browser. Colaboratory makes it possible to: * Try code snippets without building or installing OpenUSD on your machine * Run the samples on any device * Share code experiments with others Should you prefer to run the notebook on your local machine instead, simply download and execute it after [installing Jupyter](https://jupyter.org). ## Sample Notebook Follow along the tutorial using the sample notebook from the GTC session, or get acquainted with OpenUSD using other examples: |Notebook|Google Colab link| |--------|:----------------:| |Introduction to OpenUSD|[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NVIDIA-Omniverse/USD-Tutorials-And-Examples/blob/main/ColaboratoryNotebooks/usd_introduction.ipynb)| |Opening USD Stages|[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NVIDIA-Omniverse/USD-Tutorials-And-Examples/blob/main/ColaboratoryNotebooks/opening_stages.ipynb)| |Prims, Attributes and Metadata|[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NVIDIA-Omniverse/USD-Tutorials-And-Examples/blob/main/ColaboratoryNotebooks/prims_attributes_and_metadata.ipynb)| |Hierarchy and Traversal|[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NVIDIA-Omniverse/USD-Tutorials-And-Examples/blob/main/ColaboratoryNotebooks/hierarchy_and_traversal.ipynb)| ## Additional Resources * [Pixar's OpenUSD](https://openusd.org/release/index.html) * [OpenUSD at NVIDIA](https://www.nvidia.com/en-us/omniverse/usd/) * [OpenUSD training content on _NVIDIA On-Demand_](https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s33132/?playlistId=playList-911c5614-4b7f-4668-b9eb-37f627ac8d17)