First Scene¶
This example creates geometry data, inserts it into the scene, and demonstrates materials, hierarchy, local/world transforms, quaternion input, and debug axes.
import kangengine as ke
import numpy as np
class MyApp(ke.App):
def setup(self):
self.standard_materials = self.create_standard_materials()
self.scene.add_ground(path="/ground", scale=30.0)
box: ke.RenderablePrimView = self.scene.add_mesh(
path="/box",
mesh_data=ke.geometry.create_cube_data(scale=1.0),
material=self.standard_materials.common,
color=ke.Vec4(0.8, 0.3, 0.02, 1.0),
)
box.set_local_translation(translation=ke.Vec3(0.0, 2.0, 0.0))
box.set_local_rotation_axis_angle(
axis=ke.Vec3(0.0, 1.0, 0.0),
angle_radians=np.deg2rad(25.0),
)
The important separation is:
ke.geometry.create_cube_data(...)creates mesh data only.app.scene.add_mesh(...)creates a scene prim and renderable view.The returned
boxview controls common rendering and transform operations.box.primremains available for lower-level scene graph operations.
Parent and child transforms¶
An absolute prim path establishes hierarchy. /box/box2 is a child of
/box, so its local transform is relative to the first box.
box2: ke.RenderablePrimView = self.scene.add_mesh(
path="/box/box2",
mesh_data=ke.geometry.create_cube_data(scale=1.0),
material=self.standard_materials.common,
)
box2.set_local_translation(translation=ke.Vec3(0.0, 1.5, 0.0))
box2.set_local_scale(scale=ke.Vec3(0.5, 0.5, 0.5))
local_position = box2.get_local_translation()
world_position = box2.get_world_translation()
Use set_world_translation(...) when a value is already expressed in scene
coordinates. Otherwise, prefer local transforms so children follow their
parent naturally.
Quaternion ordering¶
ke.Quat and set_local_quat_wxyz() / set_world_quat_wxyz() inputs use WXYZ
ordering. Setters accept a four-element array or a ke.Quat object; getters
get_local_quat() / get_world_quat() return ke.Quat objects. Local means
relative to the parent; world means relative to the scene origin.
# Approximately 45 degrees around Z, in wxyz order.
box2.set_local_quat_wxyz(
quat_wxyz=np.array([0.924, 0.0, 0.0, 0.383], dtype=np.float32)
)
rotation: ke.Quat = box2.get_world_quat()
quat_wxyz = rotation.to_wxyz()
quat_xyzw = rotation.to_xyzw()
Physics and simulation state arrays use XYZW, for example
get_root_quat_xyzw() and get_body_quat_xyzw(). Convert at the scene boundary:
box2.set_world_quat_wxyz(quat_wxyz=ke.Quat.from_xyzw(quat_xyzw))
For array conversion, use ke.utils.quat_xyzw_to_wxyz() and
ke.utils.quat_wxyz_to_xyzw() (NumPy), or the same functions in
kangengine.utils.batched_rotations (Torch, preserving device and dtype).
Inspect transform axes¶
For a lightweight render overlay that does not create a scene prim:
self.debug.overlay.axes(
"/debug/world_axes",
origin=np.array([0.0, 1.0, 0.0]),
rotation=np.eye(3),
length=1.0,
)
To create axes that appear in the SceneGraph, use the scene-backed helper:
self.debug.geometry.add_axes(
"/debug/box2_axes",
box2.get_world_translation(),
box2.get_world_quat(),
length=0.8,
radius=0.01,
)
debug.overlay draws directly through the graphics debug renderer and does not
create scene prims. debug.geometry creates mesh-based renderables that
appear in the SceneGraph and returns a DebugPrimitiveView.
To run the complete example below, clone the repository and run from its root (the example scripts are not installed by pip):
python ./python/examples/render_prim_scene.py
Complete source: render_prim_scene.py
1"""
2Render Prim Scene — Python equivalent of test_prim_scene.cpp.
3Demonstrates the material-first Prim scene graph.
4"""
5
6import numpy as np
7
8import kangengine as ke
9from kangengine.utils._example_args import render_example_parser
10
11
12class MyApp(ke.App):
13 def setup(self):
14 self.standard_materials = self.create_standard_materials()
15
16 self.debug.overlay.axes(
17 "/debug/box_axes", # is not shown in the scene graph.
18 origin=np.array([0.0, 1.0, 0.0]),
19 rotation=np.eye(3),
20 length=1.0,
21 width=5.0,
22 )
23
24 # Ground plane (Y-up)
25 self.scene.add_ground("/ground", scale=30.0)
26
27 # Box
28 box = self.scene.add_mesh(
29 "/box",
30 ke.geometry.create_cube_data(1.0),
31 self.standard_materials.common,
32 color=ke.Vec4(0.8, 0.3, 0.02, 1.0),
33 )
34 box.set_local_translation(ke.Vec3(0.0, 2.0, 0.0))
35 box.set_local_rotation_axis_angle(ke.Vec3(0.0, 1.0, 0.0), np.deg2rad(25.0))
36
37 # Box2
38 box2 = self.scene.add_mesh(
39 "/box/box2",
40 ke.geometry.create_cube_data(1.0),
41 self.standard_materials.common,
42 color=ke.Vec4(0.3, 0.3, 0.02, 1.0),
43 )
44 box2.set_local_translation(ke.Vec3(0.0, 1.5, 0.0))
45 box2.set_local_quat_wxyz(np.array([0.924, 0, 0, 0.383]))
46 box2.set_local_scale(ke.Vec3(0.5, 0.5, 0.5))
47
48 w_trans = box2.get_world_translation()
49 w_ori = box2.get_world_quat()
50 self.debug.geometry.add_axes(
51 "/debug/box2_axes",
52 w_trans,
53 w_ori,
54 length=0.8,
55 radius=0.01,
56 segments=8,
57 )
58 # self.scene.remove_prim("/debug/box2_axes")
59
60 print(box2.get_local_translation())
61 print(box2.get_local_quat())
62 print(box2.get_local_quat().to_wxyz())
63 print(box2.get_local_quat().to_xyzw())
64 print(box2.get_world_translation())
65 print(np.array(w_ori))
66 # box2.remove()
67
68 # Sphere
69 sphere = self.scene.add_mesh(
70 "/sphere",
71 ke.geometry.create_sphere_data(0.5, 16, 12),
72 self.standard_materials.common,
73 color=ke.Vec4(0.2, 0.4, 0.9, 1.0),
74 )
75 sphere.set_local_translation(ke.Vec3(2.5, 0.5, 0.0))
76
77 self.check_error()
78
79 def pre_render(self):
80 self.check_error()
81
82 def render(self):
83 pass
84
85 def post_render(self):
86 pass
87
88
89if __name__ == "__main__":
90 args = render_example_parser(description=__doc__).parse_args()
91 app = MyApp()
92 app.initialize(1920, 1080, False, ke.UpAxis.Y, renderer=args.renderer)
93 app.start()

Next: First Simulation or Scene and Rendering.