### Install Newton with Examples Source: https://github.com/nvidiagameworks/kaolin/blob/master/kaolin/experimental/newton/README.md Install the Newton physics engine with optional dependencies for example scripts. This command is useful for setting up the environment to run coupling examples. ```bash pip install "newton[examples]" ``` -------------------------------- ### Setup Environment and Load Libraries Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/nerfstudio_gsplat_interactive_visualizer.ipynb Imports necessary libraries for 3D rendering, visualization, and data handling. Sets up logging and specifies the CUDA device for computations. Ensure gsplat is installed. ```python import copy import gsplat.rendering import ipywidgets import ipyevents import logging import math import os import torch import kaolin from kaolin.render.camera import kaolin_camera_to_gsplat_nerfstudio from kaolin.utils.bundled_data import SCANNED_TOYS_PATH, SCANNED_TOYS_NAMES, download_scanned_toys_dataset from kaolin.utils.log import log_tensor from kaolin.visualize.ipython import quick_viz %load_ext autoreload %autoreload 2 %matplotlib inline kaolin.utils.log.default_log_setup(logging.DEBUG) device = 'cuda' ``` -------------------------------- ### Example Pip Install for Specific Versions Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/installation.md Example of installing Kaolin for PyTorch 2.8.0 and CUDA 12.9. ```bash $ pip install kaolin==0.18.0 -f https://nvidia-kaolin.s3.us-east-2.amazonaws.com/torch-2.8.0_cu129.html ``` -------------------------------- ### Install Kaolin from Source Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/installation.md Installs Kaolin in development mode using the setup.py script after cloning the repository and installing dependencies. ```bash $ python setup.py develop ``` -------------------------------- ### Install Dependencies and Run Tests Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/installation.md Install necessary testing packages and execute the test suite. Note that tests rely on CUDA operations and may fail on CPU-only installations. ```bash pip install -r tools/ci_requirements.txt ``` ```bash export CI='true' # on Linux ``` ```bash set CI='true' # on Windows ``` ```bash pytest --import-mode=importlib -s tests/python/ ``` -------------------------------- ### Simplicits Soft-Body Simulation Quick Start Source: https://github.com/nvidiagameworks/kaolin/blob/master/kaolin/experimental/newton/README.md This snippet demonstrates the basic workflow for setting up and running a Simplicits soft-body simulation. It covers object creation, model building, solver initialization, and the simulation loop. Ensure you have the necessary Kaolin and Warp libraries installed. ```python import torch import warp as wp from kaolin.experimental.newton.builder import SimplicitsModelBuilder from kaolin.experimental.newton.solver import SimplicitsSolver from kaolin.physics.simplicits import SimplicitsObject # 1. Create a Simplicits soft-body object (see kaolin.physics.simplicits docs) sim_obj = SimplicitsObject.create_rigid(pts, yms, prs, rhos, approx_volume) # 2. Build the coupled model builder = SimplicitsModelBuilder(up_axis="y", gravity=-9.81) builder.add_object(sim_obj, num_qp=1024) builder.add_shape_plane(plane=(*builder.up_vector, -1.0), width=0.0, length=0.0) model = builder.finalize(device="cuda") # 3. Create solver and states solver = SimplicitsSolver(model) state_in = model.state() state_out = model.state() # 4. Simulation loop dt = 0.033 for frame in range(300): contacts = model.collide(state_in) solver.step(state_in, state_out, control=None, contacts=contacts, dt=dt) state_in, state_out = state_out, state_in ``` -------------------------------- ### Install k3d Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simplicits_mesh.ipynb Installs the k3d library, which is required for visualization in this demo. ```python !pip install -q k3d ``` -------------------------------- ### Install Required Libraries Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/understanding_spcs_tutorial.ipynb Installs necessary Python libraries for the tutorial, including matplotlib, termcolor, and ipywidgets. It's recommended to use a virtual environment for installation. ```python !pip install -q matplotlib !pip install -q termcolor !pip install -q ipywidgets ``` -------------------------------- ### Test Kaolin Installation Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/installation.md Runs a Python command to import Kaolin and print its installed version to verify the installation. ```python python -c "import kaolin; print(kaolin.__version__)" ``` -------------------------------- ### Install Build and Viz Dependencies Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/installation.md Installs dependencies required for building and visualization from the respective requirement files. ```bash $ pip install -r tools/build_requirements.txt -r tools/viz_requirements.txt -r tools/requirements.txt ``` -------------------------------- ### Install and Import Libraries Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/camera_and_rasterization.ipynb Installs necessary libraries like matplotlib and nvdiffrast, then imports Kaolin and other required modules. Sets up the CUDA context for rasterization. ```python !pip install matplotlib --quiet import glob import math from matplotlib import pyplot as plt import nvdiffrast import os import torch from tutorial_common import COMMON_DATA_DIR import kaolin as kal nvctx = nvdiffrast.torch.RasterizeCudaContext(device='cuda') ``` -------------------------------- ### Install Dependencies and Run Tests Source: https://github.com/nvidiagameworks/kaolin/blob/master/CONTRIBUTING.md Installs necessary dependencies and executes all tests locally. Ensure you are on a Unix-based system for this script. ```bash pip install -r tools/ci_requirements.txt pip install -r tools/doc_requirements.txt bash tools/linux/run_tests.sh all ``` -------------------------------- ### Setup Kaolin Environment Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/quaternion_tutorial.ipynb Import necessary PyTorch and Kaolin modules. Ensure your device is set, typically 'cuda' for acceleration. ```python # set up the environment import torch from kaolin.math import quat device = 'cuda' ``` -------------------------------- ### Install Additional Requirements Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simulatable_3dgrut.ipynb Installs necessary Python packages for the tutorial, including matplotlib, ipywidgets, and k3d. Use this to set up your environment. ```python !pip install matplotlib ipywidgets k3d --quiet ``` -------------------------------- ### Start Kaolin Dash3D Visualizer Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/checkpoints.md Launches a web server to stream geometry to web clients. The `logdir` should match the directory configured with `kaolin.visualize.Timelapse`. Connect via `http://ip.of.machine:8080`. ```bash kaolin-dash3d --logdir=$TIMELAPSE_DIR --port=8080 ``` -------------------------------- ### Install Matplotlib Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/bbox_tutorial.ipynb Installs the matplotlib library, which is used for plotting and visualization in the tutorial. ```python !pip install -q matplotlib ``` -------------------------------- ### MPM Simulation Setup and Initialization Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/newton_mpm_coupling_oneway.ipynb Initializes the MPM solver, adds ground plane, spawns sand particles, and mirrors Simplicits particles as static obstacles. ```python sim_substeps = 10 frame_dt = FRAME_DT sim_dt = frame_dt / sim_substeps mpm_builder = ModelBuilder() SolverImplicitMPM.register_custom_attributes(mpm_builder) mpm_builder.add_ground_plane() sand_particles_ids = emit_particles(mpm_builder, voxel_size=MPM_VOXEL_SIZE) # Mirror Simplicits particles as static obstacles in MPM sim_pts = simplicits_model.sim_z_to_full(simplicits_state_0.sim_z).numpy() mpm_static_start_idx = len(mpm_builder.particle_q) mpm_builder.add_particles( pos=[p for p in sim_pts], vel=[(0.0, 0.0, 0.0)] * sim_pts.shape[0], mass=[1.0] * sim_pts.shape[0], radius=[0.1] * sim_pts.shape[0], flags=[0] * sim_pts.shape[0], ) mpm_static_idx_end = len(mpm_builder.particle_q) mpm_static_idx = wp.array( np.arange(mpm_static_start_idx, mpm_static_idx_end, dtype=int), dtype=wp.int32, device='cuda' ) mpm_model = mpm_builder.finalize() mpm_options = SolverImplicitMPM.Config() mpm_options.voxel_size = MPM_VOXEL_SIZE mpm_options.tolerance = MPM_TOLERANCE mpm_options.max_iterations = MPM_MAX_ITERATIONS mpm_state_0 = mpm_model.state() mpm_state_1 = mpm_model.state() mpm_solver = SolverImplicitMPM(mpm_model, mpm_options) mpm_model.particle_ke = MPM_PARTICLE_KE mpm_model.particle_kd = MPM_PARTICLE_KD mpm_model.particle_mu = MPM_PARTICLE_MU print(f"MPM model: {mpm_model.particle_count} particles " f"({mpm_static_start_idx} sand + {mpm_static_idx_end - mpm_static_start_idx} static)") ``` -------------------------------- ### Install Necessary Packages Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simplicits_gaussians_splatting.ipynb Installs required Python packages for Gaussian splat rendering and simulation, including plyfile, k3d, matplotlib, and gsplat. ```python ### Install necessary packages !pip install -q plyfile k3d matplotlib gsplat ``` -------------------------------- ### Install k3d Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/newton_rigidbody_coupling.ipynb Installs the k3d library quietly. This is required for interactive 3D visualization within the notebook. ```python !pip install --quiet k3d ``` -------------------------------- ### Setup Gaussian Splat Renderer Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simplicits_gaussians_splatting.ipynb Initializes a Kaolin camera and a GaussianRenderer for interactive visualization of splats in a notebook. Requires PyTorch and Kaolin libraries. ```python resolution = 512 default_cam = kaolin.render.camera.Camera.from_args( eye=torch.ones((3,)) * 2, at=torch.zeros((3,)), up=torch.tensor([0., 0., 1.]), fov=torch.pi * 45 / 180, height=resolution, width=resolution) class GaussianRenderer: """ Define a rendering closure. """ def __init__(self, gaussians, downscale_factor=1): self.gaussians = gaussians self.downscale_factor = downscale_factor def downscale_camera(self, in_cam): lowres_cam = copy.deepcopy(in_cam) lowres_cam.width = in_cam.width // self.downscale_factor lowres_cam.height = in_cam.height // self.downscale_factor return lowres_cam def fast_render(self, camera): if self.downscale_factor > 1: camera = self.downscale_camera(camera) return self(camera) def __call__(self, camera): # Convert kaolin camera to nerfstudio gsplat camera cam_params = kaolin.render.camera.kaolin_camera_to_gsplat_nerfstudio(camera.cuda()) # Render gaussians using the gsplat rendering function render_colors, render_alphas, info = gsplat.rendering.rasterization( self.gaussians.positions, self.gaussians.orientations, self.gaussians.scales, self.gaussians.opacities, self.gaussians.sh_coeff, sh_degree=self.gaussians.sh_degree, **cam_params ) return (torch.clamp(render_colors[0], 0, 1) * 255).to(torch.uint8).detach().cpu() static_scene_viz = kaolin.visualize.IpyTurntableVisualizer( resolution, resolution, copy.deepcopy(default_cam), GaussianRenderer(gaussians), focus_at=None, world_up_axis=2, max_fps=12, img_quality=75, img_format='JPEG') static_scene_viz.show() ``` -------------------------------- ### Install Kaolin with Wheels Source: https://github.com/nvidiagameworks/kaolin/blob/master/README.md Install Kaolin using pip, specifying the desired version and compatible PyTorch and CUDA versions. Replace placeholders with your specific versions. ```bash pip install kaolin==0.18.0 -f https://nvidia-kaolin.s3.us-east-2.amazonaws.com/torch-{TORCH_VERSION}_cu{CUDA_VERSION}.html ``` ```bash pip install kaolin==0.18.0 -f https://nvidia-kaolin.s3.us-east-2.amazonaws.com/torch-2.8.0_cu128.html ``` -------------------------------- ### Install Matplotlib and Import Libraries Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/diffuse_lighting.ipynb Installs the matplotlib library and imports necessary modules from kaolin, torch, and math. Includes a helper function for displaying images. ```python !pip install matplotlib import kaolin as kal import torch import math from matplotlib import pyplot as plt def disp_imgs(imgs, title=None): """scatter images plotting""" nb_scenes, nb_views, _, _, _ = imgs.shape f, axes = plt.subplots(nb_scenes, nb_views, figsize=(1.95 * nb_views, 2. * nb_scenes)) f.suptitle(title, fontsize=16) plt.subplots_adjust(wspace=0, hspace=0) for i in range(nb_views): for j in range(nb_scenes): axes[j, i].imshow(imgs[j, i].cpu().detach()) axes[j, i].set_xticks([]) axes[j, i].set_yticks([]) ``` -------------------------------- ### Run Kaolin Dash3D Server Source: https://github.com/nvidiagameworks/kaolin/blob/master/kaolin/experimental/dash3d/README.md Execute this command on the machine with your training checkpoints to start the Dash3D server. View checkpoints in a browser at localhost:8080. ```bash $ kaolin-dash3d --logdir=$MY_EXPERIMENTS/test0/checkpts --port=8080 ``` -------------------------------- ### Install Kaolin with Pip (Linux/Windows) Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/installation.md Installs the latest version of Kaolin using pip. Replace TORCH_VER and CUDA_VER with compatible versions from the provided table. ```bash $ pip install kaolin==0.18.0 -f https://nvidia-kaolin.s3.us-east-2.amazonaws.com/torch-{TORCH_VER}_cu{CUDA_VER}.html ``` -------------------------------- ### Setup Plotting for Training Progress Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/bbox_tutorial.ipynb Initializes a matplotlib figure and axes for visualizing the progress of bounding box fitting during training. This setup is used to display canonical test render views. ```python # set up for plotting progress during training test_batch_ids = [2, 5, 10] # pick canonical test render views num_subplots = len(test_batch_ids) fig, ax = plt.subplots(ncols=num_subplots) ``` -------------------------------- ### Setting up States and Solvers for Simulation Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/newton_rigidbody_coupling.ipynb Initializes the simulation states and solvers for both soft-body Simplicits and rigid bodies. This setup is crucial for running the physics simulation. ```python state_0 = model.state() state_0.particle_q = model.sim_z_to_full(state_0.sim_z) state_1 = model.state() simplicits_solver = SimplicitsSolver(model) rigid_solver = SolverSemiImplicit(model) print("States and solvers ready") ``` -------------------------------- ### Install Matplotlib and Import Libraries Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/dibr_tutorial.ipynb Installs the matplotlib library and imports necessary modules for data manipulation, rendering, and visualization. Initializes a Timelapse object for visualization. ```python !pip install -q matplotlib import json import os import glob import time from PIL import Image import torch import numpy as np from matplotlib import pyplot as plt import kaolin as kal # path to the rendered image (using the data synthesizer) rendered_path = "../samples/rendered_clock/" # path to the output logs (readable with the training visualizer in the omniverse app) logs_path = './logs/' # We initialize the timelapse that will store USD for the visualization apps timelapse = kal.visualize.Timelapse(logs_path) ``` -------------------------------- ### Install Matplotlib and Import Libraries Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/sg_specular_lighting.ipynb Installs the matplotlib library and imports necessary modules from kaolin, torch, math, and matplotlib for rendering and visualization. ```python !pip install matplotlib import kaolin as kal import torch import math from matplotlib import pyplot as plt from kaolin.render.camera import generate_pinhole_rays ``` -------------------------------- ### Import Libraries and Setup Logging Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simulatable_3dgrut.ipynb Imports essential Python libraries for the tutorial, such as torch, numpy, and kaolin, and configures logging. It also loads the autoreload extension for interactive development. ```python import copy import logging import numpy as np import os import sys import time import torch from pathlib import Path import torchvision.transforms.functional as F import kaolin from matplotlib import pyplot as plt logging.basicConfig(level=logging.INFO, stream=sys.stdout, format="%(asctime)s|%(levelname)8s| %(message)s") logger = logging.getLogger(__name__) def log_tensor(t, name, **kwargs): """ Debugging util, e.g. call: log_tensor(t, 'my tensor', print_stats=True) """ logger.info(kaolin.utils.testing.tensor_info(t, name=name, **kwargs)) %load_ext autoreload %autoreload 2 sys.path.append(str(Path(".." ))) from tutorial_common import FILE_DIR ``` -------------------------------- ### Import and Prepare Mesh Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/easy_mesh_render.ipynb Import a mesh from a file (e.g., USD), triangulate it for rasterization, move it to the GPU, and normalize its vertices for consistent camera setup. ```python # Import and triangulate to enable rasterization; move to GPU mesh = kal.io.import_mesh(sample_mesh_path('armchair.usdc'), triangulate=True).cuda() # Normalize so it is easy to set up default camera mesh.vertices = kal.ops.pointcloud.center_points(mesh.vertices.unsqueeze(0), normalize=True).squeeze(0) # Inspect print(mesh) ``` -------------------------------- ### Camera Setup and Transformation Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/sg_specular_lighting.ipynb Sets up multiple camera views and transforms mesh vertices into camera and normalized device coordinates for rendering. It also computes rays originating from the camera. ```python cam_pos = torch.tensor([ [0., 0., 1.], [0., -0.3, 0.9], [0., -1., 1.], [0., -0.999, 0.111], [0., 0.999, 0.111], [0.5, 0., 0.5] ], device='cuda') nb_views = cam_pos.shape[0] cam_pos = cam_pos / cam_pos.norm(dim=-1, keepdim=True) cams = kal.render.camera.Camera.from_args( eye=cam_pos, at=torch.tensor([[0., 0., 0.]], device='cuda').repeat(nb_views, 1), up=torch.tensor([[0., 1., 0.]], device='cuda').repeat(nb_views, 1), fov=70. * 2. * math.pi / 360, width=256, height=256, device='cuda' ) vertices_camera = cams.extrinsics.transform(mesh.vertices) vertices_ndc = cams.intrinsics.transform(vertices_camera) face_vertices_camera = kal.ops.mesh.index_vertices_by_faces(vertices_camera, mesh.faces) face_vertices_image = kal.ops.mesh.index_vertices_by_faces(vertices_ndc[..., :2], mesh.faces) face_vertices_z = face_vertices_camera[..., -1] # Compute the rays rays_d = [] for cam in cams: _, per_cam_to_ray_d = generate_pinhole_rays(cam) per_cam_to_ray_d = per_cam_to_ray_d.reshape(cam.height, cam.width, 3) rays_d.append(per_cam_to_ray_d) # Rays must be toward the camera rays_d = -torch.stack(rays_d, dim=0) ``` -------------------------------- ### Setup IK Control Buffers and Kernel Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/newton_franka_coupling.ipynb Allocates buffers for Jacobian computation and defines the kernel for extracting end-effector velocity. This is essential for setting up the inverse kinematics solver. ```python def set_up_control(): """Allocate IK control buffers and define the end-effector velocity kernel.""" out_dim = 6 in_dim = model.joint_dof_count def _onehot(i, n): return wp.array([1.0 if j == i else 0.0 for j in range(n)], dtype=float) Jacobian_one_hots = [_onehot(i, out_dim) for i in range(out_dim)] @wp.kernel def _compute_body_out( body_qd: wp.array(dtype=wp.spatial_vector), body_out_arr: wp.array(dtype=float) ): mv = transform_twist(wp.static(endeffector_offset), body_qd[wp.static(endeffector_id)]) for i in range(6): body_out_arr[i] = mv[i] compute_body_out_kernel = _compute_body_out temp_state_for_jacobian = model.state(requires_grad=True) body_out = wp.empty(out_dim, dtype=float, requires_grad=True) J_flat = wp.empty(out_dim * in_dim, dtype=float) ee_delta = wp.empty(1, dtype=wp.spatial_vector) initial_pose = model.joint_q.numpy() return SimpleNamespace( one_hots=Jacobian_one_hots, kernel=compute_body_out_kernel, temp_state=temp_state_for_jacobian, body_out=body_out, J_flat=J_flat, ee_delta=ee_delta, initial_pose=initial_pose, ) ik = set_up_control() ``` -------------------------------- ### Optimizer and Learning Rate Scheduler Setup Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/dmtet_tutorial.ipynb Initializes an Adam optimizer and a learning rate scheduler with exponential decay. The scheduler reduces the learning rate over time. ```python vars = [p for _, p in model.named_parameters()] optimizer = torch.optim.Adam(vars, lr=lr) scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda x: max(0.0, 10**(-x*0.0002))) # LR decay over time ``` -------------------------------- ### Initialize Camera and Visualizer Source: https://github.com/nvidiagameworks/kaolin/blob/master/tests/python/kaolin/physics/simplicits/test_simplicits_drop.ipynb Sets up the rendering camera and an interactive visualizer for displaying the simulation results in a notebook. The visualizer uses the defined render and fast_render functions. ```python resolution = 512 camera = kal.render.easy_render.default_camera(resolution).cuda() rest_state_viz = kal.visualize.IpyTurntableVisualizer( resolution, resolution, copy.deepcopy(camera), render, fast_render=fast_render, max_fps=24, world_up_axis=1) rest_state_viz.show() ``` -------------------------------- ### Set Up Default Camera and Lighting Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/easy_mesh_render.ipynb Initialize default camera parameters and Spherical Gaussian lighting for rendering. Ensure these are moved to the GPU. ```python # Default rendering settings camera = kal.render.easy_render.default_camera(512).cuda() lighting = kal.render.easy_render.default_lighting().cuda() ``` -------------------------------- ### Install Dependencies Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/nerfstudio_gsplat_interactive_visualizer.ipynb Installs the 'matplotlib' and 'gsplat' Python packages. The '-q' flag for matplotlib ensures a quiet installation. ```bash !pip install -q matplotlib !pip install gsplat ``` -------------------------------- ### Run Kaolin Dash3D with Test Samples Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/checkpoints.md Starts the Dash3D visualizer using provided test samples. This is useful for trying out the tool's functionality. Connect locally via `http://localhost:8080`. ```bash kaolin-dash3d --logdir=$KAOLIN_ROOT/tests/samples/timelapse/notexture/ --port=8080 ``` -------------------------------- ### Install Matplotlib Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/gltf_viz.ipynb Installs the matplotlib library, which may be required for visualization. ```bash pip install matplotlib --quiet ``` -------------------------------- ### Initialize DMTet Tutorial Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/dmtet_tutorial.ipynb Sets up necessary imports and paths for the DMTet reconstruction tutorial. Initializes a Timelapse object for visualization. ```python import torch import kaolin import numpy as np from dmtet_network import Decoder # path to the point cloud to be reconstructed pcd_path = "../samples/bear_pointcloud.usd" # path to the output logs (readable with the training visualizer in the omniverse app) logs_path = './logs/' # We initialize the timelapse that will store USD for the visualization apps timelapse = kaolin.visualize.Timelapse(logs_path) ``` -------------------------------- ### Install Dependencies for Simplicit Source: https://github.com/nvidiagameworks/kaolin/blob/master/tests/python/kaolin/physics/simplicits/test_simplicits_drop.ipynb Installs the k3d and gdown libraries required for the simulation and data downloading. ```python !pip install -q k3d !pip install gdown ``` -------------------------------- ### Install Front-end Dependencies with npm Source: https://github.com/nvidiagameworks/kaolin/blob/master/tests/integration/experimental/dash3d/README.md Installs front-end dependencies managed by npm from the root of the Kaolin project. ```bash npm install ``` -------------------------------- ### Configure and Run Full Simulation Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simulatable_3dgrut.ipynb Sets up initial object placements and runs the simulation for a specified number of steps. Allows for different initial configurations of the mesh and doll. ```python def run_simulation(b=None): if scene.current_sim_step == 0: # Uncomment lines below to try few different placements for the mesh and doll # Doll placements # scene.set_object_initial_transform(doll_obj_idx, init_transform=torch.tensor([[1,0,0,-0.3],[0,1,0,-0.2],[0,0,1,0.5]], dtype=torch.float32, device='cuda')) # doll falls on blue speakers scene.set_object_initial_transform(doll_obj_idx, init_transform=torch.tensor([[1,0,0,0.0],[0,1,0,0.8],[0,0,1,0.5]], dtype=torch.float32, device='cuda')) # doll falls in bag # Mesh placements # scene.set_object_initial_transform(mesh_obj_idx, init_transform=torch.tensor([[1,0,0,-1.8],[0,1,0,0.6],[0,0,1,1]], dtype=torch.float32, device='cuda')) # mesh on drill # scene.set_object_initial_transform(mesh_obj_idx, init_transform=torch.tensor([[1,0,0,-1.5],[0,1,0,1.5],[0,0,1,2]], dtype=torch.float32, device='cuda')) # mesh falls on boots # scene.set_object_initial_transform(mesh_obj_idx, init_transform=torch.tensor([[1,0,0,0.0],[0,1,0,-0.2],[0,0,1,1]], dtype=torch.float32, device='cuda')) # mesh falls on pliers # Use sliders to control mesh position scene.set_object_initial_transform( mesh_obj_idx, init_transform=engine.primitives.objects[mesh_name].transform.model_matrix() ) # Scene already takes care of transformation for us, so we can keep the transform at identity. # This will stop the engine from applying the transform twice.. engine.primitives.objects[mesh_name].transform.reset() reset_transform_widgets_to_engine_values() num_simulation_steps = num_sim_steps_slider.value for s in range(num_simulation_steps): with visualizer.out: run_sim_step() ``` -------------------------------- ### Install Node.js with Conda Source: https://github.com/nvidiagameworks/kaolin/blob/master/tests/integration/experimental/dash3d/README.md Installs Node.js using Conda, a necessary dependency for front-end package management. ```bash conda install -c conda-forge nodejs ``` -------------------------------- ### Setup Camera and Render Mesh Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/nerfstudio_gsplat_interactive_visualizer.ipynb Sets up a Kaolin camera and renders a mesh using Kaolin's easy_render module. The camera is configured with specific eye, at, up vectors, and field of view. The rendered mesh's albedo is then concatenated with the Gaussian splat rendering for visualization. ```python kaolin_cam = kaolin.render.camera.Camera.from_args( eye=mesh.vertices.mean(dim=0) + torch.tensor([0, -1.3, 0], device=device), at=mesh.vertices.mean(dim=0), up=torch.tensor([0., 0.0, 1.0], device=device), fov=math.pi * 50 / 180, height=512, width=512).to(device) colors, alphas, info = render_with_gsplat(kaolin_cam, gsmodel) log_tensor(colors, 'colors', print_stats=True) log_tensor(alphas, 'alphas', print_stats=True) mesh_render = kaolin.render.easy_render.render_mesh(kaolin_cam, mesh) log_tensor(mesh_render, 'mesh_render', print_stats=True) kaolin.visualize.ipython.quick_viz( torch.cat([colors, mesh_render[kaolin.render.easy_render.RenderPass.albedo]], dim=0).permute(0, 3, 1, 2), inches=20).set_title('3D Gaussian Splat Rendering vs. Aligned Low-Poly Mesh') ``` -------------------------------- ### Import necessary libraries and set up device Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/working_with_gaussians.ipynb Imports essential libraries for Gaussian Splatting and Kaolin, and sets the computation device to CUDA if available. ```python import copy import gsplat.rendering import math import os import torch import kaolin from kaolin.rep import GaussianSplatModel from kaolin.render.camera import kaolin_camera_to_gsplat_nerfstudio from kaolin.utils.bundled_data import SCANNED_TOYS_PATH, SCANNED_TOYS_NAMES, download_scanned_toys_dataset from kaolin.utils.log import log_tensor from kaolin.visualize.ipython import quick_viz %matplotlib inline device = 'cuda' ``` -------------------------------- ### Check CUDA Installation Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/installation.md Verifies that CUDA is installed and accessible by checking the NVIDIA SMI and NVCC versions. ```bash $ nvidia-smi $ nvcc --version ``` -------------------------------- ### Initialize and Display Visualizer Source: https://github.com/nvidiagameworks/kaolin/blob/master/tests/python/kaolin/physics/simplicits/test_simplicits_drop.ipynb Sets up the interactive visualizer, including the canvas, buttons, and output area, then renders the initial frame. ```python scene.reset_object(obj_idx) button = Button(description='Run Sim') button.on_click(start_simulation) visualizer = kal.visualize.IpyTurntableVisualizer( resolution, resolution, copy.deepcopy(camera), render, fast_render=fast_render, max_fps=24, world_up_axis=1) visualizer.render_update() # render first frame display(HBox([visualizer.canvas, button]), visualizer.out) ``` -------------------------------- ### Start Physics Simulation Source: https://github.com/nvidiagameworks/kaolin/blob/master/tests/python/kaolin/physics/simplicits/test_simplicits_drop.ipynb Manages the simulation thread, starting a new simulation or joining an existing one if already running. ```python def start_simulation(b): global sim_thread_open, sim_thread with visualizer.out: if(sim_thread_open): sim_thread.join() sim_thread_open = False sim_thread_open = True sim_thread = threading.Thread(target=run_sim, daemon=True) sim_thread.start() ``` -------------------------------- ### Install PyTorch for Specific CUDA Version Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/installation.md Installs PyTorch version 2.8.0 with support for CUDA 12.8 using the extra index URL. ```bash $ pip install torch==2.8.0 --extra-index-url https://download.pytorch.org/whl/cu128 ``` -------------------------------- ### Example SPC Point Hierarchies Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/spc_summary.md Illustrates the structure of point hierarchies for a 2D SPC example. This tensor represents sparse coordinates at all levels. ```python >>> torch.ShortTensor([[0, 0], [1, 1], [1, 0], [2, 2], [2, 1], [3, 1], [5, 5] ]) ``` -------------------------------- ### Initialize Visualizer Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simulatable_3dgrut.ipynb Initialize Kaolin's `IpyTurntableVisualizer` with camera properties, rendering functions, and visualizer settings. ```python visualizer = kaolin.visualize.IpyTurntableVisualizer( height=camera.height, width=camera.width, camera=camera, render=render, fast_render=fast_render, max_fps=8, world_up_axis=2 ) ``` -------------------------------- ### Set up Interactive Simulation Visualizer Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simplicits_gaussians_splatting.ipynb Configures an interactive visualizer for the simulation. This includes setting up the camera, renderer, and UI elements like buttons for controlling the simulation. ```python num_sim_iterations = 100 resolution = 512 sim_visualizer = kaolin.visualize.IpyTurntableVisualizer( resolution, resolution, copy.deepcopy(default_cam), renderer, fast_render=renderer.fast_render, focus_at=torch.tensor([0, 0, 0.0]), world_up_axis=2, max_fps=12, img_quality=75, img_format='JPEG') buttons = [Button(description=x) for x in ['Run Sim', 'Reset']] buttons[0].on_click(lambda e: start_simulation(sim_visualizer, num_sim_iterations)) buttons[1].on_click(lambda e: reset_simulation(renderer.reset, sim_visualizer)) sim_visualizer.render_update() display(VBox([HBox([sim_visualizer.canvas, VBox(buttons)]), sim_visualizer.out])) ``` -------------------------------- ### Initialize and Configure Interactive Visualizer Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/gltf_viz.ipynb Sets up an interactive visualizer with sliders for controlling lighting parameters like elevation, azimuth, amplitude, and sharpness. It uses ipywidgets for the UI elements and Kaolin's visualizer for rendering. ```python from ipywidgets import interactive, HBox, FloatSlider visualizer = kal.visualize.IpyTurntableVisualizer( 512, 512, copy.deepcopy(camera), render, fast_render=lowres_render, max_fps=5, world_up_axis=1, img_format='jpeg', img_quality=75 ) def sliders_callback(new_elevation, new_azimuth, new_amplitude, new_sharpness): """ipywidgets sliders callback""" with visualizer.out: # This is in case of bug elevation[:] = new_elevation azimuth[:] = new_azimuth amplitude[:] = new_amplitude sharpness[:] = new_sharpness # this is how we request a new update visualizer.render_update() elevation_slider = FloatSlider( value=elevation.item(), min=-math.pi / 2., max=math.pi / 2., step=0.1, description='Elevation:', continuous_update=True, readout=True, readout_format='.1f', ) azimuth_slider = FloatSlider( value=azimuth.item(), min=-math.pi, max=math.pi, step=0.1, description='Azimuth:', continuous_update=True, readout=True, readout_format='.1f', ) amplitude_slider = FloatSlider( value=amplitude[0,0].item(), min=0.1, max=40., step=0.1, description='Amplitude:\n', continuous_update=True, readout=True, readout_format='.1f', ) sharpness_slider = FloatSlider( value=sharpness.item(), min=0.1, max=20., step=0.1, description='Sharpness:\n', continuous_update=True, readout=True, readout_format='.1f', ) interactive_slider = interactive( sliders_callback, new_elevation=elevation_slider, new_azimuth=azimuth_slider, new_amplitude=amplitude_slider, new_sharpness=sharpness_slider ) # We combine all the widgets and the visualizer canvas and output in a single display full_output = HBox([visualizer.canvas, interactive_slider]) display(full_output, visualizer.out) ``` -------------------------------- ### Reset Scene Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simulatable_3dgrut.ipynb Resets the scene to its initial state. This is a fundamental step before starting or restarting a simulation. ```python scene.reset_scene() ``` -------------------------------- ### Create and Activate Conda Environment Source: https://github.com/nvidiagameworks/kaolin/blob/master/docs/notes/installation.md Creates a new Conda environment named 'kaolin' with Python 3.9 and activates it. Recommended for installing Kaolin. ```bash $ conda create --name kaolin python=3.9 $ conda activate kaolin ``` -------------------------------- ### Modify SurfaceMesh Vertices Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/working_with_meshes.ipynb Demonstrates how to directly set mesh attributes, such as vertices. This example centers and normalizes the vertices using kaolin operations. ```python # We can also directly set mesh attributes, for example: print(mesh.describe_attribute('vertices', print_stats=True)) mesh.vertices = kal.ops.pointcloud.center_points(mesh.vertices, normalize=True) print(mesh.describe_attribute('vertices', print_stats=True)) ``` -------------------------------- ### Mesh Material Type Check Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/sg_specular_lighting.ipynb This code snippet shows an example of a mesh material type that is not PBRMaterial, indicating no support for device conversions. ```python specular_texture = mesh.materials[0][0]['Ks'].cuda().float().reshape(1, 1, 3) ``` -------------------------------- ### Reset Simulation State Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/simulatable_3dgrut.ipynb Resets the simulation environment and object transforms to their initial conditions. This is useful for starting a new simulation run or debugging. ```python def reset_simulation(): # Reset simulator state back to initial conditions scene.reset_scene() # Set object to original coordinates engine.primitives.objects[mesh_name].transform.reset() scene.set_object_initial_transform( mesh_obj_idx, init_transform=engine.primitives.objects[mesh_name].transform.model_matrix() ) # Reset object coords in engine to frame 0 deform_rendered_scene() ``` -------------------------------- ### Visualization Setup with k3d Source: https://github.com/nvidiagameworks/kaolin/blob/master/examples/tutorial/physics/newton_mpm_coupling_oneway.ipynb Initializes k3d plot, defines and adds meshes for soft bodies and a floor, and points for sand particles. Requires k3d, numpy, and pre-defined variables like orig_vertices, mesh, mpm_state_0, and FLOOR_PLANE. ```python orig_vertices_np = orig_vertices.cpu().numpy().astype(np.float32) orig_faces_np = mesh.faces.cpu().numpy().astype(np.uint32) num_verts = orig_vertices_np.shape[0] combined_faces_np = np.concatenate( [orig_faces_np, orig_faces_np + num_verts] ).astype(np.uint32) plot = k3d.plot(camera_auto_fit=False) plot.camera = [3, 3, 3, 0, 0, 0, 0, 0, 1] mesh_soft = k3d.mesh( np.concatenate([orig_vertices_np, orig_vertices_np]).astype(np.float32), combined_faces_np, color=0x3399ff ) sand_pts_np = mpm_state_0.particle_q.numpy()[:mpm_static_start_idx].astype(np.float32) points_sand = k3d.points(sand_pts_np, color=0xffcc00, point_size=0.05) plot += mesh_soft plot += points_sand # Floor plane at FLOOR_PLANE height _s = 3.0 # half-extent floor_verts = np.array([ [-_s, -_s, FLOOR_PLANE], [ _s, -_s, FLOOR_PLANE], [ _s, _s, FLOOR_PLANE], [-_s, _s, FLOOR_PLANE], ], dtype=np.float32) floor_faces = np.array([[0, 1, 2], [0, 2, 3]], dtype=np.uint32) mesh_floor = k3d.mesh(floor_verts, floor_faces, color=0xcccccc, opacity=0.5) plot += mesh_floor plot.display() ```