Skip to content

Scripting.en

Claude Code edited this page Nov 20, 2026 · 2 revisions

Scripting

中文 · English

KubeJS is for expressing "when to play, how much influence, where the target is" — it never reimplements skinning. Java advances animation, IK, physics, skinning and rendering once per tick; script callbacks run once per character tick before pose evaluation and never inside a bone, vertex, physics substep or render loop.

Two entry points

Entry API / execution Shared automatically
kubejs/client_scripts/*.js KubeJS global RigWorks.register(id, callback), ticked for the local character no — the global script folder is not uploaded
kubejs/rigworks/<avatar>/ manifest.json + mappings.json + controller.js in a restricted, isolated Rhino environment per player yes, subject to sharing switches and the server protocol

The two environments do not share an API. Global frame.model() examples cannot be copied into a dedicated package; distributable packages use the validated api.command(...) form.

Minimal vanilla baseline

RigWorks.register('pack:player', function (frame) {
  frame.defaultAnimation()
})

Put it in kubejs/client_scripts/ and run /kubejs reload client_scripts. Select Scripted in the character settings or run /assimploader character mode scripted, and enable a calibrated character. Registering selects the controller but does not silently enable player model replacement.

defaultAnimation() keeps the full vanilla pose including native item actions; defaultMotion() (alias locomotion()) keeps locomotion and head aiming and leaves upper-body item actions to the script. They are not synonyms — do not call both in one tick. CLIPS mode does not run this global controller.

Global controller API

RigWorks provides register(id, callback), unregister(id), clear(), controllers(), use(id), useDefault(), active(), failure(), enable(boolean), profile(resource), reload() and status(). mode() / mode('vanilla'|'scripted'|'clips') and physicsEnabled() / physicsEnabled(boolean) expose the same independent options as the GUI and commands.

Registration and selection, plus profile and enable settings, are allowed during top-level client-script loading on Forge's mod-loading thread. Character handles and frame controls require the client thread.

frame

Call Meaning
frame.input() this tick's player state snapshot (read-only Map)
frame.state() script state for the current character/model session
frame.bone('right_hand') the model's real node name from the humanoid mapping
frame.clip('walk') the state or clip name mapped to this intent in the profile
frame.model() playback, blending and processor controls for the current animation instance
frame.profile(), frame.mappings profile and mapping data

frame is valid only for this callback; do not retain it. state() belongs to the current binding and is the place for initialisation flags, action IDs, last input and phase — you do not need a fresh state machine every tick. It is cleared on controller replacement, model reload, or world/player replacement.

Input fields

Common fields: onGround, speed, sprinting, crouching, swimming, elytra, usingItem, swingActive, swingGeneration, swingProgress, mainItem, offItem, primaryArm, climbDirection, airPhase, frozen, riptide. The full set is defined by the snapshot implementation in the repository's CharacterActionFrame.

Read Java Maps with .get('field'), coerce numbers with Number(...) and compare strings with String(...). Do not rely on Java Map property syntax or boxed truthiness.

Poses, clips and solvers

Control Prerequisites and limits
frame.ikChains(), ik(name, enabled), ikTarget(name,x,y,z), ikStatus(name) the character solver JSON must define the named chain first; targets are model-space
model.processorDefinition(...) + processorTarget(...) your own CCD chain; create it once for the real skeleton
frame.contacts(), contact(id, mainHand), contactWeight(w), noContact() the equipment definition has grip rules and the contact policy is enabled — see Hand Contacts
frame.morphs(), morph(name,weight) the named morph already exists in the solver definition
frame.motions(), motion(name,loop), seekMotion(seconds), pauseMotion(bool) an installed solver motion definition — see Animation
frame.physics(), physicsBodies(), body(id), impulse(...), torqueImpulse(...) a physics definition and a running visual physics service
frame.ragdoll(bool), automaticRagdoll() temporary visual ragdoll override and restoring the automatic policy

model.pose(id, options) accepts bones (node to {position, rotation, scale, weight}), space (local or model), stage (pre_ik or post_physics), additive and weight. Rotations are bind/rest-relative Euler XYZ degrees. space:'model' converts anatomical model axes through the skin's reference pose, including imported bone roll. Translation uses the imported node's local units — do not assume every bone uses metres.

At most 64 script layer/pose IDs and 256 bones per pose. Missing clips, bones or solver definitions cannot be invented by a script.

ikTarget and processorTarget do not accept screen coordinates. Sockets, character root transform, imported units and model facing all affect the world→model conversion. For weapons, calibrate the equipment socket/grip first and use contact IK for the support hand, so the two systems do not fight over the same arm.

Dedicated avatar packages

Author folder:

kubejs/rigworks/<avatar>/
  manifest.json
  mappings.json
  controller.js
{
  "version": 1,
  "id": "example:avatar",
  "profile": "example:models/avatar/profile.json",
  "controller": "example:avatar",
  "scripts": ["controller.js"],
  "mappings": "mappings.json"
}

Packages contain JSON/JS sources only — no FBX, textures, sounds or native libraries; those resources must already exist on every client. AvatarPackageRepository.forGame(gameDirectory) scans the folder, refresh() scans all author packages, and reload(id) replaces only that avatar. Invalid reloads report an error and keep the last published bundle. Downloaded cache folders are excluded from author discovery.

Every local/remote owner has its own interpreter, globals, callbacks and state; resetting one session does not affect others. Commands are data, validated in Java before the engine mutates, and arbitrary method calls are not forwarded.

Sandbox budget

The isolated environment uses Rhino's interpreter mode and disables Java reflection, Java object wrapping, eval, the global Function constructor, regular expressions and class loaders, and seals native constructors and prototypes.

Per-callback limits: 32 source files, 64 KiB per source, a 512 KiB source bundle, 64 KiB frame input, 128 commands, 32 KiB combined output, 16 KiB JSON state, 100,000 interpreter instructions and a 50 ms owner-thread CPU watchdog (plus a 5-second wall-clock watchdog). Platforms with allocation counters also enforce a 32 MiB allocation allowance.

This is a restricted in-process interpreter, not an OS process sandbox or a hard JVM memory quota. A single native operation is checked after it returns, and the deadline is not a hard real-time GPU/frame budget. Missing Rhino yields a native fallback session instead of eagerly loading the optional dependency class.

Reloading

Edit global JS with /kubejs reload client_scripts. The pinned development version wraps the standard CLIENT reload path in a transaction through Mixins: registration, selection, unregister and clear write to an empty staging registry on the loading thread; success publishes it atomically and advances the revision once, while failure preserves the published callbacks, active selection and session state.

The mixin plugin activates only for the inspected KubeJS version and contains no KubeJS class references, so packs without KubeJS never resolve the optional targets. Other KubeJS versions need their entry point and log method re-verified.

Edit FBX/materials with F3+T and wait for asynchronous re-import; old handles stop working. Edit processor JSON by explicitly calling reloadProcessor after a resource reload, or rebuild the character binding.

Demos

Runnable examples live in the repository's examples/kubejs/ and examples/rigworks/avatar_behavior/; they are not installed into existing packs automatically. Install one primary controller at a time so "last registered wins" does not override your intent.

Clone this wiki locally