ke.utils¶
Pure Python helpers for batch state machines, joint mapping, math, tensor conversion, colors, and debug drawing.
API overview¶
Resolve semantic and literal joint names against a motion skeleton. |
|
Lowercase a joint name and remove non-alphanumeric characters. |
|
|
Return a tensor on the requested public API device. |
|
Copy or view a value as a CPU NumPy array for CPU-native bindings. |
Return a NumPy-backed CPU or Torch-backed CUDA simulation buffer. |
|
|
Return a ColorLibrary preset as an RGBA list with optional alpha. |
Evaluate at most one state transition per environment per step. |
|
A named edge; higher priority wins, then definition order. |
Batch State Machines¶
See the Batch FSM guide for signals, priorities, and handling state changes.
- class kangengine.utils.BatchFSM(
- states: Mapping[str, str],
- transitions: Sequence[FSMTransition],
- *,
- initial_state: str,
- num_envs: int = 1,
- device: str | device = 'cpu',
Evaluate at most one state transition per environment per step.
states maps stable IDs to display labels. Definitions are immutable and separate from runtime tensors, so tools can inspect nodes and edges. Signals are boolean [N] tensors on device; the caller supplies their meaning. state_ids, elapsed, and transition_ids are borrowed, read-only runtime views. transition_ids indexes definitions, or is -1 when no transition occurred.
- state_idx(state_id: str) int¶
Resolve a stable state ID to its runtime tensor index.
- Return type:
int
- reset(
- env_ids: Tensor | None = None,
- *,
- state: str | None = None,
- Return type:
None
- step(
- dt: float,
- signals: Mapping[str, Tensor],
Advance by nonnegative seconds and return a new [N] changed mask.
All edges read the state at step entry. Newly entered states start at elapsed=0 and cannot transition again until the next call. Every declared condition must have a signal, even for inactive states.
- Return type:
Tensor
- class kangengine.utils.FSMTransition(
- id: str,
- source: str,
- target: str,
- condition: str,
- priority: int = 0,
- min_duration: float = 0.0,
A named edge; higher priority wins, then definition order.
source/target are stable state IDs, condition is a signal name, and min_duration is the minimum time in the source state in seconds.
- id¶
- source¶
- target¶
- condition¶
- priority = 0¶
- min_duration = 0.0¶
Joint Mapping¶
- class kangengine.utils.JointSemantic(*values)[python]¶
Bases:
str,Enum- ROOT = 'root'¶
- HIPS = 'hips'¶
- SPINE = 'spine'¶
- CHEST = 'chest'¶
- NECK = 'neck'¶
- HEAD = 'head'¶
- LEFT_SHOULDER = 'left_shoulder'¶
- RIGHT_SHOULDER = 'right_shoulder'¶
- LEFT_ELBOW = 'left_elbow'¶
- RIGHT_ELBOW = 'right_elbow'¶
- LEFT_WRIST = 'left_wrist'¶
- RIGHT_WRIST = 'right_wrist'¶
- LEFT_HAND = 'left_hand'¶
- RIGHT_HAND = 'right_hand'¶
- LEFT_HIP = 'left_hip'¶
- RIGHT_HIP = 'right_hip'¶
- LEFT_KNEE = 'left_knee'¶
- RIGHT_KNEE = 'right_knee'¶
- LEFT_ANKLE = 'left_ankle'¶
- RIGHT_ANKLE = 'right_ankle'¶
- LEFT_FOOT = 'left_foot'¶
- RIGHT_FOOT = 'right_foot'¶
- LEFT_TOE = 'left_toe'¶
- RIGHT_TOE = 'right_toe'¶
- LEFT_HEEL = 'left_heel'¶
- RIGHT_HEEL = 'right_heel'¶
- class kangengine.utils.JointMapper(
- joint_names: list[str],
- profiles: tuple[str, ...] | list[str] | None = ('geno', 'mixamo', 'kw5', 'kw', 'common'),
Resolve semantic and literal joint names against a motion skeleton.
- classmethod from_motion(
- motion,
- profiles: tuple[str, ...] | list[str] | None = ('geno', 'mixamo', 'kw5', 'kw', 'common'),
- Return type:
- find_name(name: str) int | None¶
- Return type:
int|None
- find_names(names: list[str]) list[int]¶
- Return type:
list[int]
- aliases(
- semantic: JointSemantic | str,
- Return type:
tuple[str,...]
- find(
- semantic: JointSemantic | str,
- Return type:
int|None
- find_many(
- semantics: list[JointSemantic | str],
- Return type:
list[int]
- default_tracking_indices() list[int]¶
- Return type:
list[int]
- default_contact_indices() list[int]¶
- Return type:
list[int]
- kangengine.utils.normalize_joint_name(name: str) str[python]¶
Lowercase a joint name and remove non-alphanumeric characters.
- Return type:
str
Math Helpers¶
- kangengine.utils.math.normalize_vector(value, fallback=None, eps: float = 1e-08) ndarray[python]¶
Return a normalized float32 vector or a fallback for a near-zero input.
- Return type:
ndarray
- kangengine.utils.math.quat_xyzw_normalize(quat, eps: float = 1e-08) ndarray[python]¶
Normalize an xyzw quaternion, falling back to identity.
- Return type:
ndarray
- kangengine.utils.math.quat_xyzw_multiply(a, b) ndarray[python]¶
Multiply two xyzw quaternions.
- Return type:
ndarray
- kangengine.utils.math.quat_xyzw_conjugate(quat) ndarray[python]¶
Return the conjugate of an xyzw quaternion.
- Return type:
ndarray
- kangengine.utils.math.quat_xyzw_rotate(quat, vector) ndarray[python]¶
Rotate a 3D vector by an xyzw quaternion.
- Return type:
ndarray
- kangengine.utils.math.quat_xyzw_from_two_vectors(src, dst) ndarray[python]¶
Return the shortest xyzw rotation from src to dst.
- Return type:
ndarray
- kangengine.utils.math.quat_wxyz_to_xyzw(quat) ndarray[python]¶
Convert arbitrary-shaped wxyz quaternion arrays to xyzw ordering.
- Return type:
ndarray
- kangengine.utils.math.quat_wxyz_to_matrix_numpy(
- quat: ArrayLike,
Convert NumPy wxyz quaternion arrays to float32 rotation matrices.
- Parameters:
quat (
ArrayLike) – Shape (…, 4), in wxyz order. Each quaternion is normalized internally.- Return type:
GenericAlias[float32]- Returns:
Shape (…, 3, 3), preserving batch dimensions. Matrices act on column vectors.
- Raises:
ValueError – The final dimension is not 4, or any float32 squared norm is non-finite or at most 1e-12.
- kangengine.utils.math.quat_wxyz_twist_angle(quat, axis) float[python]¶
Return the signed local-axis twist angle from a wxyz quaternion.
- Return type:
float
Tensor Helpers¶
- kangengine.utils.tensor.resolve_device(device=None) device[python]¶
Return the public tensor device, defaulting to CPU.
- Return type:
device
- kangengine.utils.tensor.as_tensor(value, *, shape=None, device=None, dtype=torch.float32) Tensor[python]¶
Return a tensor on the requested public API device.
- Return type:
Tensor
- kangengine.utils.tensor.as_cpu_numpy(value, *, shape=None, dtype=<class 'numpy.float32'>) ndarray[python]¶
Copy or view a value as a CPU NumPy array for CPU-native bindings.
- Return type:
ndarray
- kangengine.utils.as_sim_buffer(
- value: ndarray | Tensor,
- *,
- shape: tuple[int, ...] | list[int] | Size | None = None,
- sim_device: str | device | None = None,
- dtype: dtype | dtype | Type[generic] | str = torch.float32,
Return a NumPy-backed CPU or Torch-backed CUDA simulation buffer.
- Return type:
SimBuffer
Environment Helpers¶
- kangengine.utils.env_utils.env_id_list(
- env_id: int | integer | Sequence[int] | ndarray | Tensor | None,
- num_envs: int,
Normalize scalar, sequence, tensor, or None env ids into a list.
- Return type:
list[int]
- kangengine.utils.env_utils.select_env_value(value, env_index: int, env_count: int, shape)[python]¶
Select one row from batched env values, or reshape a shared value.
- kangengine.utils.env_utils.select_optional_env_value(value, env_index: int, env_count: int, shape)[python]¶
Select an env value unless the optional value is absent.
Color And Debug Helpers¶
- kangengine.utils.color.preset_rgba(color_type, alpha: float = 1.0) list[float][python]¶
Return a ColorLibrary preset as an RGBA list with optional alpha.
- Return type:
list[float]
- kangengine.utils.debug_draw.log_debug_axes(
- app,
- path: str,
- origin,
- rotation,
- length: float = 0.18,
- width: float = 1.5,
- alpha: float = 0.95,
- hidden: bool = False,
Compatibility wrapper for
app.debug.overlay.axes(...).- Return type:
None