Skip to content

Adds USD-level randomization to event manager - #1165

Closed
Mayankm96 wants to merge 13 commits into
mainfrom
feature/spawn-events
Closed

Mayankm96 wants to merge 13 commits into
mainfrom
feature/spawn-events

Conversation

@Mayankm96

@Mayankm96 Mayankm96 commented Oct 6, 2024 •

Copy link
Copy Markdown
Contributor

Description

Certain scene-level randomizations (such as randomizing the scale) must happen before the simulation starts playing. To this end, the MR adds a new event mode called "scene," which gets called right after the scene design is complete.

Since the event manager CANNOT be initialized before the simulation starts playing, the current implementation hacks the event mode into the environment constructor directly. This is not ideal but at this point, there is no other way around it.

Type of change

  • New feature (non-breaking change which adds functionality)

Screenshots

If you execute:

./isaaclab.sh -p source/standalone/tutorials/03_envs/create_cube_base_env.py --num_envs 32

Output:

random

Checklist

  • I have run the pre-commit checks with ./isaaclab.sh --format
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the changelog and the corresponding version in the extension's config/extension.toml file
  • I have added my name to the CONTRIBUTORS.md or my name already exists there

@Mayankm96 Mayankm96 changed the title Adds scene-level randomization to event manager Adds USD-level randomization to event manager Oct 6, 2024
@Mayankm96
Mayankm96 force-pushed the feature/spawn-events branch from 32db208 to d3b0605 Compare October 7, 2024 09:03
@Mayankm96
Mayankm96 marked this pull request as ready for review October 7, 2024 12:12
@Mayankm96

Copy link
Copy Markdown
Contributor Author

I already spent a day trying to figure out multiple ways to initialize the event manager before the sim starts playing. The scene entity config resolution is impossible until physics parses the scene (since we need the PhysX body and joint ordering). It is rather unfortunate, but that's how it is right now.

The functionality still works, so if it makes sense, we can move forward with it.

@Mayankm96 Mayankm96 self-assigned this Oct 7, 2024
@Mayankm96 Mayankm96 added the enhancement New feature or request label Oct 7, 2024
@taochenshh

Copy link
Copy Markdown

@Mayankm96 thanks for the great work. wonder if you have plans to merge these functions into the main branch?


.. attention::
Since this function modifies USD properties, it should only be used before the simulation starts
playing. This corresponds to the event mode named "spawn". Using it at simulation time, may lead to

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a check for simulation running and throw an error or warning if it's used while simulation is running?

prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[env_id])

# get the attribute to randomize
scale_spec = prim_spec.GetAttributeAtPath(prim_paths[env_id] + ".xformOp:scale")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if xformOp:scale is not on the prim?

prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[env_id])

# get the attribute to randomize
color_spec = prim_spec.GetAttributeAtPath(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to also support DisplayColor attribute? primitive shapes might not always have materials assigned.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kellyguo11 Quick question about the DisplayColor Attribute. In case the material was not assigned to the mesh, I tried to default to randomizing the color of the DisplayColor Attribute, however I was unable to access it through code or even manually through the GUI. It was grayed out even if no material was bounded. Any ideas about how to get around this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh interesting, I was thinking of this attribute https://openusd.org/dev/api/class_usd_geom_gprim.html#a7fd86a311b27d9b4fdd82736bb423056, but maybe that has changed? do you see any other relevant attributes for updating color without using a material?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked through all the available properties and can't seem to find one that's relevant outside the material. So my current code doesn't account for this, but if I come across anything I will be sure to update it. Thanks for the reply : )

color_spec = prim_spec.GetAttributeAtPath(
prim_paths[env_id] + "/geometry/material/Shader.inputs:diffuseColor"
)
color_spec.default = Gf.Vec3f(*rand_samples[i])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can handle missing attributes more gracefully than just erroring out. I feel like printing a message and continuing might be more user friendly

@jsmith-bdai jsmith-bdai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, LGTM, but I think we can consolidate the apply_randomization method as it looks identical.

Also, would be good to add a unit test for this functionality

self.scene = InteractiveScene(self.cfg.scene)
print("[INFO]: Scene manager: ", self.scene)

# randomization at scene level

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a note here why this is needed here for future reference?

applied_scene_randomization = False
# iterate over all event terms
for term_name, term_cfg in self.cfg.events.__dict__.items():
# check for non config

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# check for non config
# check for none config

Helper functions.
"""

def _apply_scene_randomization(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a static helper method so that all 3 implementations can share it? It would need to take in an env of any of the three types and do the same operation on each

unpredictable behaviors.

.. note::
When randomizing the scale of individual assets, please make sure to set

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just throw an exception if replicate_physics is True here?


- "startup": Event is applied once at the beginning of the training.
- "scene": Event is applied once after designing the scene.
- "startup": Event is applied once after the simulation starts playing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double check that this doesn't need to be updated in any tutorials or how-tos

Comment on lines +234 to +236
# check if sim is playing and resolve the entity
# joint and body names are only available once the sim is playing
# hence, we do not resolve the entity if the sim is not playing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do the entities get resolved if the sim is not playing?

print("[INFO]: Scene manager: ", self.scene)

# randomization at scene level
if not self.cfg.events:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mayankm96 can you explain the logic behind this statement. You have it present in direct_marl_env.py and direct_rl_env.py. but not in manager_based_env.py. The fucntion call to _apply_scene_randomization() is causing some tests because there is an attempted iteration on self.cfg.events of type None.


This function randomizes the color of USD shapes created using :class:`omni.isaac.lab.sim.spawn.ShapeCfg`
class. It modifies the attribute: "geometry/material/Shader.inputs:diffuseColor" under the prims
corresponding to the asset. If the attribute does not exist, the function errors out.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I'm still concerned that the assumption of "geometry/material/Shader.inputs:diffuseColor" will cause issues for users. It might not be obvious that they need to make sure there's a material defined for the prim and it has to be under this particular path. Would it make sense to automatically create a material if one doesn't exist instead of just erroring out?

kellyguo11 added a commit that referenced this pull request Mar 13, 2025
# Description

Certain scene-level randomizations (such as randomizing the scale) must
happen before the simulation starts playing. To this end, the MR adds a
new event mode called "prestartup," which gets called right after the
scene design is complete and before the simulation is played.

Since the scene entities cannot be resolved before the simulation starts
playing (as we currently rely on PhysX to provide us with the joint/body
ordering), the MR adds a callback to resolve the scene entity
configurations separately once the simulation plays.

This MR replaces the prior implementation in #1165

## Type of change

- New feature (non-breaking change which adds functionality)
- This change requires a documentation update

## Screenshots

If you execute:

```bash
./isaaclab.sh -p scripts/tutorials/03_envs/create_cube_base_env.py --num_envs 32
```

Output:


![image](https://github.com/user-attachments/assets/a4b3a5e7-ff5c-4593-97ce-3b5af19bfde2)

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [x] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

---------

Signed-off-by: Mayank Mittal <[email protected]>
Co-authored-by: James Smith <[email protected]>
Co-authored-by: Kelly Guo <[email protected]>
@Mayankm96 Mayankm96 closed this Mar 19, 2025
@Mayankm96
Mayankm96 deleted the feature/spawn-events branch March 19, 2025 21:04
jtigue-bdai pushed a commit that referenced this pull request Apr 14, 2025
# Description

Certain scene-level randomizations (such as randomizing the scale) must
happen before the simulation starts playing. To this end, the MR adds a
new event mode called "prestartup," which gets called right after the
scene design is complete and before the simulation is played.

Since the scene entities cannot be resolved before the simulation starts
playing (as we currently rely on PhysX to provide us with the joint/body
ordering), the MR adds a callback to resolve the scene entity
configurations separately once the simulation plays.

This MR replaces the prior implementation in #1165

## Type of change

- New feature (non-breaking change which adds functionality)
- This change requires a documentation update

## Screenshots

If you execute:

```bash
./isaaclab.sh -p scripts/tutorials/03_envs/create_cube_base_env.py --num_envs 32
```

Output:


![image](https://github.com/user-attachments/assets/a4b3a5e7-ff5c-4593-97ce-3b5af19bfde2)

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [x] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

---------

Signed-off-by: Mayank Mittal <[email protected]>
Co-authored-by: James Smith <[email protected]>
Co-authored-by: Kelly Guo <[email protected]>
ToxicNS pushed a commit to ToxicNS/IsaacLab that referenced this pull request Apr 24, 2025
# Description

Certain scene-level randomizations (such as randomizing the scale) must
happen before the simulation starts playing. To this end, the MR adds a
new event mode called "prestartup," which gets called right after the
scene design is complete and before the simulation is played.

Since the scene entities cannot be resolved before the simulation starts
playing (as we currently rely on PhysX to provide us with the joint/body
ordering), the MR adds a callback to resolve the scene entity
configurations separately once the simulation plays.

This MR replaces the prior implementation in isaac-sim#1165

## Type of change

- New feature (non-breaking change which adds functionality)
- This change requires a documentation update

## Screenshots

If you execute:

```bash
./isaaclab.sh -p scripts/tutorials/03_envs/create_cube_base_env.py --num_envs 32
```

Output:


![image](https://github.com/user-attachments/assets/a4b3a5e7-ff5c-4593-97ce-3b5af19bfde2)

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [x] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

---------

Signed-off-by: Mayank Mittal <[email protected]>
Co-authored-by: James Smith <[email protected]>
Co-authored-by: Kelly Guo <[email protected]>
LJones-alt pushed a commit to LJones-alt/IsaacLab-Laura that referenced this pull request Jul 1, 2026
# Description

Certain scene-level randomizations (such as randomizing the scale) must
happen before the simulation starts playing. To this end, the MR adds a
new event mode called "prestartup," which gets called right after the
scene design is complete and before the simulation is played.

Since the scene entities cannot be resolved before the simulation starts
playing (as we currently rely on PhysX to provide us with the joint/body
ordering), the MR adds a callback to resolve the scene entity
configurations separately once the simulation plays.

This MR replaces the prior implementation in isaac-sim#1165

## Type of change

- New feature (non-breaking change which adds functionality)
- This change requires a documentation update

## Screenshots

If you execute:

```bash
./isaaclab.sh -p scripts/tutorials/03_envs/create_cube_base_env.py --num_envs 32
```

Output:


![image](https://github.com/user-attachments/assets/a4b3a5e7-ff5c-4593-97ce-3b5af19bfde2)

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [x] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

---------

Signed-off-by: Mayank Mittal <[email protected]>
Co-authored-by: James Smith <[email protected]>
Co-authored-by: Kelly Guo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants