Newton Viewer¶
KangEngine can be used as a viewer for Newton simulations. Newton
owns the simulation while NewtonViewer handles rendering and interaction.

Install¶
uv pip install "kangengine[newton]"
From a source checkout, use uv pip install -e "./python[newton]" instead.
Basic example¶
import newton
from kangengine.adapters.newton import NewtonViewer
builder = newton.ModelBuilder()
body = builder.add_body()
builder.add_shape_box(body, hx=0.5, hy=0.5, hz=0.5)
model = builder.finalize(device="cpu") # or "cuda:0"
state = model.state()
viewer: NewtonViewer = NewtonViewer()
viewer.set_model(model=model)
try:
while viewer.is_running():
# Step Newton and update state here.
viewer.begin_frame(0.0)
viewer.log_state(state=state)
viewer.end_frame()
finally:
viewer.close()
Use an existing App¶
Use NewtonVisualizer with an initialized App. Your application controls
simulation, drawing, and App cleanup.
from kangengine.adapters.newton import NewtonVisualizer
visualizer: NewtonVisualizer = NewtonVisualizer(app)
visualizer.set_model(model=model)
try:
# Repeat with the current state after each simulation step.
visualizer.log_state(state=state)
app.process_events()
app.draw_frame()
finally:
visualizer.release() # The App remains open.
Keep the model and state alive until drawing finishes. On CUDA, log the state on the solver’s Warp stream. Use one visualizer per App.
Both classes support the viewport eye menu for colliders, centers of mass,
body/joint axes, and contacts.
After log_state(), call log_contacts(contacts, state) or
log_mujoco_contacts(solver, state) to display contacts. Forces and estimated
contact states require solver-provided forces; otherwise only normals are shown.
Examples¶
Run from the repository root:
python python/examples/adapters/newton/newton_basic_shapes.py --device cpu
python python/examples/adapters/newton/newton_mjcf_articulation.py --device cpu
python python/examples/adapters/newton/newton_franka_ik.py --device cpu
Use --device cpu or --device cuda to select the simulation device.
Complete source: newton_basic_shapes.py
1"""Run a Newton rigid-body simulation with KangEngine as its viewer.
2
3Newton owns the model, contacts, solver, and state. KangEngine receives the
4Newton ViewerBase calls and only handles rendering, input, and the window.
5Use Shift + Left drag to apply a Newton-owned picking force.
6"""
7
8from __future__ import annotations
9
10import numpy as np
11
12from kangengine.utils._example_args import render_example_parser
13from kangengine.adapters.newton import NewtonViewer
14
15
16def main():
17 parser = render_example_parser()
18 parser.add_argument(
19 "--device", help="Newton device: cpu or cuda:0 (default: Warp auto-selection)."
20 )
21 parser.add_argument("--width", type=int, default=1920)
22 parser.add_argument("--height", type=int, default=1080)
23 parser.add_argument("--headless", action="store_true")
24 parser.add_argument(
25 "--frames",
26 type=int,
27 default=0,
28 help="Stop after this many rendered frames; zero runs until closed.",
29 )
30 args = parser.parse_args()
31
32 import newton
33 import warp as wp
34
35 if args.device is not None:
36 wp.set_device(args.device)
37
38 newton.use_coord_layout_targets = True
39 builder = newton.ModelBuilder()
40 builder.add_ground_plane()
41
42 sphere = builder.add_body(
43 xform=wp.transform(p=wp.vec3(-2.0, -2.0, 2.0), q=wp.quat_identity()),
44 label="sphere",
45 )
46 builder.add_shape_sphere(sphere, radius=0.5)
47
48 capsule = builder.add_body(
49 xform=wp.transform(p=wp.vec3(0.0, -2.0, 2.0), q=wp.quat_identity()),
50 label="capsule",
51 )
52 builder.add_shape_capsule(capsule, radius=0.3, half_height=0.7)
53
54 box = builder.add_body(
55 xform=wp.transform(p=wp.vec3(2.0, -2.0, 2.0), q=wp.quat_identity()),
56 label="box",
57 )
58 builder.add_shape_box(box, hx=0.5, hy=0.35, hz=0.25)
59
60 cylinder = builder.add_body(
61 xform=wp.transform(p=wp.vec3(-2.0, 0.0, 2.0), q=wp.quat_identity()),
62 label="cylinder",
63 )
64 builder.add_shape_cylinder(cylinder, radius=0.35, half_height=0.65)
65
66 cone = builder.add_body(
67 xform=wp.transform(p=wp.vec3(0.0, 0.0, 2.0), q=wp.quat_identity()),
68 label="cone",
69 )
70 builder.add_shape_cone(cone, radius=0.5, half_height=0.65)
71
72 tetra_mesh = newton.Mesh(
73 vertices=np.array(
74 [
75 [-0.6, -0.5, -0.4],
76 [0.6, -0.5, -0.4],
77 [0.0, 0.6, -0.4],
78 [0.0, 0.0, 0.7],
79 ],
80 dtype=np.float32,
81 ),
82 indices=np.array([0, 2, 1, 0, 1, 3, 1, 2, 3, 2, 0, 3], dtype=np.int32),
83 )
84 mesh_body = builder.add_body(
85 xform=wp.transform(p=wp.vec3(2.0, 0.0, 2.0), q=wp.quat_identity()),
86 label="mesh",
87 )
88 builder.add_shape_mesh(mesh_body, mesh=tetra_mesh)
89
90 model = builder.finalize()
91 state_0 = model.state()
92 state_1 = model.state()
93 control = model.control()
94 collision_pipeline = newton.CollisionPipeline(model)
95 contacts = collision_pipeline.contacts()
96 solver = newton.solvers.SolverXPBD(model, iterations=10)
97
98 viewer = NewtonViewer(
99 width=args.width,
100 height=args.height,
101 headless=args.headless,
102 renderer=args.renderer,
103 )
104 viewer.show_ground = False
105 viewer.app.scene.add_ground("/Ground", scale=20.0)
106 viewer.set_model(model)
107 viewer.set_camera(wp.vec3(8.0, -8.0, 4.0), pitch=-10.0, yaw=135.0)
108
109 frame_dt = 1.0 / 60.0
110 substeps = 4
111 sim_dt = frame_dt / substeps
112 sim_time = 0.0
113 rendered_frames = 0
114
115 try:
116 while viewer.is_running():
117 if viewer.should_step():
118 for _ in range(substeps):
119 state_0.clear_forces()
120 viewer.apply_forces(state_0)
121 collision_pipeline.collide(state_0, contacts)
122 solver.step(
123 state_0,
124 state_1,
125 control,
126 contacts,
127 sim_dt,
128 )
129 state_0, state_1 = state_1, state_0
130 sim_time += frame_dt
131
132 viewer.begin_frame(sim_time)
133 viewer.log_state(state_0)
134 viewer.log_contacts(contacts, state_0)
135 viewer.end_frame()
136 rendered_frames += 1
137 if args.frames > 0 and rendered_frames >= args.frames:
138 break
139 finally:
140 viewer.close()
141
142
143if __name__ == "__main__":
144 main()
The basic-shapes example shows spheres, capsules, boxes, cylinders, cones, and meshes. Shift + left drag applies a force.
