Exercise: Experimenting With Specializes#
Starting off with a simple example, we will be using a cube.usd
and cube_pyramid.usd
similar to our inherits exercise. These files can be found in composition_arcs/specializes/simple_example
.
One of the key differences is in cube.usd
, _cube_asset
contains an authored value for diffuseColor
as (1,0,0)
.
Let’s open up our example file.
In the Visual Studio Code terminal, run the code below to open the file in usdview.
Windows:
.\scripts\usdview.bat .\composition_arcs\specializes\simple_example\specializes_simple.usd
Linux:
./scripts/usdview.sh ./composition_arcs/specializes/simple_example/specializes_simple.usd
What are some things you notice about the scene? How does it differ from the simple example in the inherits exercise?
In the tree view, select Shader under
_cube_asset
and take notice of thediffuseColor
.
Only the top cube is magenta. Why is this? If you remember, our cube.usd
is a red cube, where the specializes arc contributes the red color. In cube_pyramid.usd
we authored opinions on the bottom two cubes to be blue and green. When we change the red fallback value to a magenta fallback, the other two are unaffected. The other two cubes have an authored opinion apart from the specializes arc on what color they want.
Again, if we were to open the unaffected_scenario.usd
, we would see the same concept as inherits and the result would be the same. This is because the magenta color was only applied in the context of specializes_simple.usd
.
For this scenario, we’re creating a digital twin of the roadways. By default the max speed for each road piece is 30. However, side roads should have a max speed of 20. This needs to be applied to the whole city, so we can do this using a specializes arc.
In Visual Studio Code, open the following file:
composition_arcs/specializes/exercise/specializes_exercise_1.py
We will be creating the class in which our roads will specialize from. It will contain a maxspeed attribute that is set to 30 by default.
Add the code below into the Part 1 code block.
1class_prim = asset_stage.CreateClassPrim("/_osm_street_data")
2max_speed_attr = class_prim.CreateAttribute("osm:street:maxspeed", Sdf.ValueTypeNames.Int, custom=True)
3max_speed_attr.Set(30)
Then, we’ll traverse to stage and add this specializes arc to each road mesh. If the road mesh we are looking at is in our listed side_streets
, we will set its max speed to 20.
Add the code below into Part 2 block.
1for prim in asset_stage.Traverse():
2 if prim.IsA(UsdGeom.Mesh) and prim.GetName().startswith("road_") and not "Barrier" in prim.GetName():
3 prim.GetSpecializes().AddSpecialize(class_prim.GetPath())
4 if prim.GetName() in side_streets:
5 prim.GetAttribute("osm:street:maxspeed").Set(20)
Run the script by running the following code in the terminal:
Windows:
python .\composition_arcs\specializes\exercise\specializes_exercise_1.py
Linux:
python ./composition_arcs/specializes/exercise/specializes_exercise_1.py
Now we can view the change in usdview. In the Visual Studio Code terminal, run the following code:
Windows:
.\scripts\usdview.bat .\composition_arcs\specializes\exercise\main_street.usd
Linux:
./scripts/usdview.sh ./composition_arcs/specializes/exercise/main_street.usd
If you select the road pieces and look at the Property panel, you’ll see in that our roads have a new attribute osm:street:maxspeed
which is coming from the specializes arc. Our side roads also have been set to 20 while our main road is still 30.
Let’s imagine we’ve been told that in composition_arcs/specializes/exercisescenario_02.usd
we should try setting a new default maximum speed limit of 40. Since we have already created our specializes arc, we just need to override the maxspeed value on the source prim of the specializes arc to broadcast a fallback.
In Visual Studio Code, open the following file:
composition_arcs/specializes/exercise/specializes_exercise_2.py
Similar to what we did previously, we are going to override the class prim, add our maxspeed attribute to it, and set the value to 40.
Add the code below to the Python file:
1class_prim = scenario2.OverridePrim("/_osm_street_data")
2max_speed_attr = class_prim.CreateAttribute("osm:street:maxspeed", Sdf.ValueTypeNames.Int, custom=True)
3max_speed_attr.Set(40)
Save the file.
In the terminal, run the following code to run the script
Windows:
python .\composition_arcs\specializes\exercise\specializes_exercise_2.py
Linux:
python ./composition_arcs/specializes/exercise/specializes_exercise_2.py
In the terminal, run the following code to open the file in usdview.
Windows:
.\scripts\usdview.bat .\composition_arcs\specializes\exercise\scenario_02.usd
Linux:
./scripts/usdview.sh ./composition_arcs/specializes/exercise/scenario_02.usd
We can see here that our main roads all have maxspeed of 40 however, our side roads still have a maxspeed of 20 from when we authored it in main_street.usd
. The side roads were previous refined or made more specialized so they can ignore this fallback broadcast from the specializes arc.