Repository: isaac-sim/IsaacLab (Python)

Issue:
[Bug Report] local_variable referenced before assignment in IsaacLab's articulation wrapper
If you are submitting a bug report, please fill in the following details and use the tag [bug].

### Describe the bug

When using the articulation object from omni.isaac.lab, the functions `write_root_com_pose_to_sim` and `write_root_link_to_sim` both result in an error where a variable is called before being assigned.

### Steps to reproduce

Please try to provide a minimal example to reproduce the bug. Error messages and stack traces are also helpful.

One way to reproduce this issue is to modify the task Isaac-Cartpol-Direct-v0, inside `source/extensions/omni.isaac.lab_tasks/omni/isaac/lab_tasks/direct/cartpole/cartpole_env.py`.

Inside this file, modify the function `_reset_idx` (line 126 to 148).
In this function replace the line 146 and 147 by:
```python
        self.cartpole.write_root_com_pose_to_sim(default_root_state[:, :7], env_ids)
        self.cartpole.write_root_link_velocity_to_sim(default_root_state[:, 7:], env_ids)
```

This gives the following error:
```
Error executing job with overrides: []
Traceback (most recent call last):
  File "/workspace/isaaclab/source/extensions/omni.isaac.lab_tasks/omni/isaac/lab_tasks/utils/hydra.py", line 99, in hydra_main
    func(env_cfg, agent_cfg, *args, **kwargs)
  File "/workspace/isaaclab/source/standalone/workflows/rl_games/train.py", line 171, in main
    runner.run({"train": True, "play": False, "sigma": train_sigma})
  File "/workspace/isaaclab/_isaac_sim/kit/python/lib/python3.10/site-packages/rl_games/torch_runner.py", line 133, in run
    self.run_train(args)
  File "/workspace/isaaclab/_isaac_sim/kit/python/lib/python3.10/site-packages/rl_games/torch_runner.py", line 116, in run_train
    agent.train()
  File "/workspace/isaaclab/_isaac_sim/kit/python/lib/python3.10/site-packages/rl_games/common/a2c_common.py", line 1307, in train
    self.obs = self.env_reset()
  File "/workspace/isaaclab/_isaac_sim/kit/python/lib/python3.10/site-packages/rl_games/common/a2c_common.py", line 531, in env_reset
    obs = self.vec_env.reset()
  File "/workspace/isaaclab/source/extensions/omni.isaac.lab_tasks/omni/isaac/lab_tasks/utils/wrappers/rl_games.py", line 334, in reset
    return self.env.reset()
  File "/workspace/isaaclab/source/extensions/omni.isaac.lab_tasks/omni/isaac/lab_tasks/utils/wrappers/rl_games.py", line 234, in reset
    obs_dict, _ = self.env.reset()
  File "/isaac-sim/exts/omni.isaac.ml_archive/pip_prebundle/gymnasium/wrappers/order_enforcing.py", line 61, in reset
    return self.env.reset(**kwargs)
  File "/workspace/isaaclab/source/extensions/omni.isaac.lab/omni/isaac/lab/envs/direct_rl_env.py", line 274, in reset
    self._reset_idx(indices)
  File "/workspace/isaaclab/source/extensions/omni.isaac.lab_tasks/omni/isaac/lab_tasks/direct/cartpole/cartpole_env.py", line 147, in _reset_idx
    self.cartpole.write_root_link_velocity_to_sim(default_root_state[:, 7:], env_ids)
  File "/workspace/isaaclab/source/extensions/omni.isaac.lab/omni/isaac/lab/assets/articulation/articulation.py", line 471, in write_root_link_velocity_to_sim
    quat = self.data.root_link_state_w[local_env_ids, 3:7]
UnboundLocalError: local variable 'local_env_ids' referenced before assignment
```

### System Info

Describe the characteristic of your environment:

<!-- Please complete the following description. -->
- Commit: de76c2e9d193ec42b834b920f00ebc7c2bff622a
- Isaac Sim Version: 4.2
- OS: 22.04
- GPU: A2000
- CUDA: 12.1
- GPU Driver: 535.183.01

### Checklist

- [x] I have checked that there is no similar issue in the repo (**required**)
- [x] I have checked that the issue is not in running Isaac Sim itself and is related to the repo

### Acceptance Criteria



Discussion: Thanks for submitting this. We will review. 



Fix (patch):
diff --git a/source/extensions/omni.isaac.lab/omni/isaac/lab/assets/articulation/articulation.py b/source/extensions/omni.isaac.lab/omni/isaac/lab/assets/articulation/articulation.py
index 444a2ad4db4..593a8c88a60 100644
--- a/source/extensions/omni.isaac.lab/omni/isaac/lab/assets/articulation/articulation.py
+++ b/source/extensions/omni.isaac.lab/omni/isaac/lab/assets/articulation/articulation.py
@@ -385,11 +385,13 @@ def write_root_com_pose_to_sim(self, root_pose: torch.Tensor, env_ids: Sequence[
             env_ids: Environment indices. If None, then all indices are used.
         """
         # resolve all indices
+        physx_env_ids = env_ids
         if env_ids is None:
-            local_env_ids = slice(None)
+            env_ids = slice(None)
+            physx_env_ids = self._ALL_INDICES
 
-        com_pos = self.data.com_pos_b[local_env_ids, 0, :]
-        com_quat = self.data.com_quat_b[local_env_ids, 0, :]
+        com_pos = self.data.com_pos_b[env_ids, 0, :]
+        com_quat = self.data.com_quat_b[env_ids, 0, :]
 
         root_link_pos, root_link_quat = math_utils.combine_frame_transforms(
             root_pose[..., :3],
@@ -399,7 +401,7 @@ def write_root_com_pose_to_sim(self, root_pose: torch.Tensor, env_ids: Sequence[
         )
 
         root_link_pose = torch.cat((root_link_pos, root_link_quat), dim=-1)
-        self.write_root_link_pose_to_sim(root_pose=root_link_pose, env_ids=env_ids)
+        self.write_root_link_pose_to_sim(root_pose=root_link_pose, env_ids=physx_env_ids)
 
     def write_root_velocity_to_sim(self, root_velocity: torch.Tensor, env_ids: Sequence[int] | None = None):
         """Set the root center of mass velocity over selected environment indices into the simulation.
@@ -458,18 +460,20 @@ def write_root_link_velocity_to_sim(self, root_velocity: torch.Tensor, env_ids:
             env_ids: Environment indices. If None, then all indices are used.
         """
         # resolve all indices
+        physx_env_ids = env_ids
         if env_ids is None:
-            local_env_ids = slice(None)
+            env_ids = slice(None)
+            physx_env_ids = self._ALL_INDICES
 
         root_com_velocity = root_velocity.clone()
-        quat = self.data.root_link_state_w[local_env_ids, 3:7]
-        com_pos_b = self.data.com_pos_b[local_env_ids, 0, :]
+        quat = self.data.root_link_state_w[env_ids, 3:7]
+        com_pos_b = self.data.com_pos_b[env_ids, 0, :]
         # transform given velocity to center of mass
         root_com_velocity[:, :3] += torch.linalg.cross(
             root_com_velocity[:, 3:], math_utils.quat_rotate(quat, com_pos_b), dim=-1
         )
         # write center of mass velocity to sim
-        self.write_root_com_velocity_to_sim(root_velocity=root_com_velocity, env_ids=env_ids)
+        self.write_root_com_velocity_to_sim(root_velocity=root_com_velocity, env_ids=physx_env_ids)
 
     def write_joint_state_to_sim(
         self,