File size: 10,292 Bytes
d6b3397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause

"""This script demonstrates how to spawn deformable prims into the scene.

.. code-block:: bash

    # Usage
    ./isaaclab.sh -p scripts/demos/deformables.py

"""

"""Launch Isaac Sim Simulator first."""


import argparse

from isaaclab.app import AppLauncher

# create argparser
parser = argparse.ArgumentParser(description="This script demonstrates how to spawn deformable prims into the scene.")
parser.add_argument("--backend", type=str, default="physx", choices=["physx", "newton"], help="Physics backend.")
# append AppLauncher cli args
AppLauncher.add_app_launcher_args(parser)
# demos should open Kit visualizer by default
parser.set_defaults(visualizer=["kit"])
# parse the arguments
args_cli = parser.parse_args()
# launch omniverse app
app_launcher = AppLauncher(args_cli)
simulation_app = app_launcher.app

"""Rest everything follows."""

import random

import numpy as np
import torch
import tqdm

import isaaclab.sim as sim_utils
from isaaclab.assets import DeformableObject, DeformableObjectCfg
from isaaclab.utils.assets import ISAACLAB_NUCLEUS_DIR

if args_cli.backend == "newton":
    from isaaclab_newton.sim.schemas import NewtonDeformableBodyPropertiesCfg as DeformableBodyPropertiesCfg
    from isaaclab_newton.sim.spawners.materials import (
        NewtonDeformableBodyMaterialCfg as VolumeDeformableMaterialCfg,
    )
    from isaaclab_newton.sim.spawners.materials import (
        NewtonSurfaceDeformableBodyMaterialCfg as SurfaceDeformableMaterialCfg,
    )
else:
    from isaaclab_physx.sim.schemas import PhysxDeformableBodyPropertiesCfg as DeformableBodyPropertiesCfg
    from isaaclab_physx.sim.spawners.materials import (
        PhysxDeformableBodyMaterialCfg as VolumeDeformableMaterialCfg,
    )
    from isaaclab_physx.sim.spawners.materials import (
        PhysxSurfaceDeformableBodyMaterialCfg as SurfaceDeformableMaterialCfg,
    )


def define_origins(num_origins: int, radius: float = 2.0, center_height: float = 3.0) -> list[list[float]]:
    """Defines origins distributed on the surface of a sphere, sampled according to a Fibonacci lattice.

    Args:
        num_origins: Number of points to place.
        radius: Radius of the sphere [m].
        center_height: Height of the sphere center above ground [m].
    """
    golden_ratio = (1 + np.sqrt(5)) / 2
    env_origins = torch.zeros(num_origins, 3)
    for i in range(num_origins):
        theta = 2 * np.pi * i / golden_ratio
        phi = np.arccos(1 - 2 * (i + 0.5) / num_origins)
        env_origins[i, 0] = radius * np.cos(theta) * np.sin(phi)
        env_origins[i, 1] = radius * np.sin(theta) * np.sin(phi)
        env_origins[i, 2] = radius * np.cos(phi) + center_height
    return env_origins.tolist()


def design_scene() -> tuple[dict, list[list[float]]]:
    """Designs the scene."""
    # Ground-plane
    cfg_ground = sim_utils.GroundPlaneCfg()
    cfg_ground.func("/World/defaultGroundPlane", cfg_ground)

    # spawn distant light
    cfg_light = sim_utils.DomeLightCfg(
        intensity=3000.0,
        color=(0.75, 0.75, 0.75),
    )
    cfg_light.func("/World/light", cfg_light)

    # spawn a red cone
    cfg_sphere = sim_utils.MeshSphereCfg(
        radius=0.4,
        deformable_props=DeformableBodyPropertiesCfg(),
        visual_material=sim_utils.PreviewSurfaceCfg(),
        physics_material=VolumeDeformableMaterialCfg(),
    )
    cfg_cuboid = sim_utils.MeshCuboidCfg(
        size=(0.6, 0.6, 0.6),
        deformable_props=DeformableBodyPropertiesCfg(),
        visual_material=sim_utils.PreviewSurfaceCfg(),
        physics_material=VolumeDeformableMaterialCfg(),
    )
    cfg_cylinder = sim_utils.MeshCylinderCfg(
        radius=0.25,
        height=0.5,
        deformable_props=DeformableBodyPropertiesCfg(),
        visual_material=sim_utils.PreviewSurfaceCfg(),
        physics_material=VolumeDeformableMaterialCfg(),
    )
    cfg_capsule = sim_utils.MeshCapsuleCfg(
        radius=0.35,
        height=0.5,
        deformable_props=DeformableBodyPropertiesCfg(),
        visual_material=sim_utils.PreviewSurfaceCfg(),
        physics_material=VolumeDeformableMaterialCfg(),
    )
    cfg_cone = sim_utils.MeshConeCfg(
        radius=0.35,
        height=0.75,
        deformable_props=DeformableBodyPropertiesCfg(),
        visual_material=sim_utils.PreviewSurfaceCfg(),
        physics_material=VolumeDeformableMaterialCfg(),
    )
    cfg_cloth = sim_utils.MeshRectangleCfg(
        size=(1.5, 1.0),
        resolution=(21, 21),
        deformable_props=DeformableBodyPropertiesCfg(),
        visual_material=sim_utils.PreviewSurfaceCfg(),
        physics_material=SurfaceDeformableMaterialCfg(),
    )
    cfg_usd = sim_utils.UsdFileCfg(
        usd_path=f"{ISAACLAB_NUCLEUS_DIR}/Objects/Teddy_Bear/teddy_bear.usd",
        deformable_props=DeformableBodyPropertiesCfg(),
        visual_material=sim_utils.PreviewSurfaceCfg(),
        physics_material=VolumeDeformableMaterialCfg(),
        scale=[0.05, 0.05, 0.05],
    )
    # create a dictionary of all the objects to be spawned
    objects_cfg = {
        "sphere": cfg_sphere,
        "cuboid": cfg_cuboid,
        "cylinder": cfg_cylinder,
        "capsule": cfg_capsule,
        "cone": cfg_cone,
        "cloth": cfg_cloth,
        "usd": cfg_usd,
    }

    # Create separate groups of deformable objects
    origins = define_origins(num_origins=12, radius=1.5, center_height=2.0)
    print("[INFO]: Spawning objects...")
    # Iterate over all the origins, spawn objects, and create a view for all the deformables
    # note: since we manually spawned random deformable meshes above, we don't need to
    #   specify the spawn configuration for the deformable object
    scene_entities = {}
    for idx, origin in tqdm.tqdm(enumerate(origins), total=len(origins)):
        # randomly select an object to spawn
        obj_name = random.choice(list(objects_cfg.keys()))
        obj_cfg = objects_cfg[obj_name]
        # randomize the deformable material stiffness
        if args_cli.backend == "newton" and obj_name == "cloth":
            obj_cfg.physics_material.tri_ke = random.uniform(5e3, 5e4)
            obj_cfg.physics_material.tri_ka = random.uniform(5e3, 5e4)
        else:
            youngs_modulus = random.uniform(5e5, 1e8)
            poissons_ratio = random.uniform(0.25, 0.45)
            if args_cli.backend == "newton":
                obj_cfg.physics_material.k_mu = youngs_modulus / (2.0 * (1.0 + poissons_ratio))
                obj_cfg.physics_material.k_lambda = (
                    youngs_modulus * poissons_ratio / ((1.0 + poissons_ratio) * (1.0 - 2.0 * poissons_ratio))
                )
            else:
                obj_cfg.physics_material.youngs_modulus = youngs_modulus
                obj_cfg.physics_material.poissons_ratio = poissons_ratio
        # randomize the color
        obj_cfg.visual_material.diffuse_color = (random.random(), random.random(), random.random())
        # spawn the object, separate groups for surface and volume deformables
        if obj_name in ["cloth"]:
            prim_path = f"/World/Origin/Surface{idx:02d}"
            cfg = DeformableObjectCfg(
                prim_path=prim_path,
                spawn=obj_cfg,
                init_state=DeformableObjectCfg.InitialStateCfg(pos=origin),
            )
            scene_entities[f"Surface{idx:02d}"] = DeformableObject(cfg=cfg)
        else:
            prim_path = f"/World/Origin/Volume{idx:02d}"
            cfg = DeformableObjectCfg(
                prim_path=prim_path,
                spawn=obj_cfg,
                init_state=DeformableObjectCfg.InitialStateCfg(pos=origin),
            )
            scene_entities[f"Volume{idx:02d}"] = DeformableObject(cfg=cfg)

    # return the scene information
    return scene_entities, origins


def run_simulator(sim: sim_utils.SimulationContext, entities: dict[str, DeformableObject]):
    """Runs the simulation loop."""
    # Define simulation stepping
    sim_dt = sim.get_physics_dt()
    sim_time = 0.0
    count = 0

    # Simulate physics
    while simulation_app.is_running():
        # reset
        if count % int(3.0 / sim_dt) == 0:
            # reset counters
            count = 0
            # reset deformable object state
            for _, deform_body in enumerate(entities.values()):
                # root state
                nodal_state = deform_body.data.default_nodal_state_w.torch.clone()
                deform_body.write_nodal_state_to_sim_index(nodal_state)
                # reset the internal state
                deform_body.reset()
            print("[INFO]: Resetting deformable object state...")
        # perform step
        sim.step()
        # update sim-time
        sim_time += sim_dt
        count += 1
        # update buffers
        for deform_body in entities.values():
            deform_body.update(sim_dt)


def main():
    """Main function."""
    # Initialize the simulation context
    if args_cli.backend == "newton":
        from isaaclab_newton.physics import NewtonCfg

        from isaaclab_contrib.deformable.newton_manager_cfg import VBDSolverCfg

        physics_cfg = NewtonCfg(
            solver_cfg=VBDSolverCfg(
                iterations=5,
                particle_enable_self_contact=True,
                particle_self_contact_radius=0.0001,
                particle_self_contact_margin=0.1,
            ),
            num_substeps=4,
        )
    else:
        from isaaclab_physx.physics import PhysxCfg

        physics_cfg = PhysxCfg()
    sim_cfg = sim_utils.SimulationCfg(dt=0.01, device=args_cli.device, physics=physics_cfg)
    sim = sim_utils.SimulationContext(sim_cfg)
    # Set main camera
    sim.set_camera_view([4.0, 4.0, 3.0], [0.5, 0.5, 0.0])

    # Design scene by adding assets to it
    scene_entities, _ = design_scene()
    # Play the simulator
    sim.reset()
    # Now we are ready!
    print("[INFO]: Setup complete...")
    run_simulator(sim, scene_entities)
    print("[INFO]: Simulation complete...")


if __name__ == "__main__":
    # run the main function
    main()
    # close sim app
    simulation_app.close()