Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds USD-level randomization to event manager #1165

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

Mayankm96
Copy link
Contributor

@Mayankm96 Mayankm96 commented Oct 6, 2024

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
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.

@taochenshh
Copy link

@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
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
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
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.

color_spec = prim_spec.GetAttributeAtPath(
prim_paths[env_id] + "/geometry/material/Shader.inputs:diffuseColor"
)
color_spec.default = Gf.Vec3f(*rand_samples[i])
Copy link
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

Copy link
Collaborator

@jsmith-bdai jsmith-bdai left a comment

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

@@ -116,6 +116,10 @@ def __init__(self, cfg: ManagerBasedEnvCfg):
self.scene = InteractiveScene(self.cfg.scene)
print("[INFO]: Scene manager: ", self.scene)

# randomization at scene level
Copy link
Collaborator

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
Collaborator

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

@@ -563,6 +568,39 @@ def set_debug_vis(self, debug_vis: bool) -> bool:
Helper functions.
"""

def _apply_scene_randomization(self):
Copy link
Collaborator

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
Collaborator

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?

@@ -38,7 +38,8 @@ class EventManager(ManagerBase):

For a typical training process, you may want to apply events in the following modes:

- "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
Collaborator

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
Collaborator

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?

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.

4 participants