English
Appearance
English
Appearance
Related references:
In the advanced Web API, most capabilities are centered around an "object handle".
The most common entry point is to fetch by name:
const obj = await api.getObject('rabbit');If you already know the object name in the scene, this is the most direct way.
You can also retrieve all objects at once:
const objects = await api.getAllObjects();Naming Recommendation
If you plan to manipulate objects frequently from the host page, give them clear names in the scene editor first.
getObject() does not return an Object3D you can manipulate directly. It returns a handle that communicates across the iframe boundary.
So code like this does not work:
const obj = await api.getObject('rabbit');
obj.position.x = 1; // wrongThe correct approach is:
const obj = await api.getObject('rabbit');
await api.setPosition(obj, 1, 0, 0);const name = await api.getObjectName(obj);
await api.setObjectName(obj, 'rabbit-main');const visible = await api.getObjectVisible(obj);
await api.setObjectVisible(obj, !visible);// Read the current render order
const renderOrder = await api.getRenderOrder(obj);
// Set the current render order
await api.setRenderOrder(obj, 10);This is useful when you need one object to stay more reliably in front of others.
// Check whether clicking is disabled
const isDisableClick = await api.getDisableClick(obj);
// Disable clicking
await api.setDisableClick(obj, true);This prevents the object from being clicked.
const [x, y, z] = await api.getPosition(obj);
await api.setPosition(obj, x, y + 0.2, z);const [x, y, z, order] = await api.getRotation(obj);
// Note: the values are radians, not degrees
await api.setRotation(obj, 0, Math.PI / 2, 0, 'YXZ');const [x, y, z, w] = await api.getQuaternion(obj);
await api.setQuaternion(obj, 0, 0, 0, 1);const [x, y, z] = await api.getScale(obj);
await api.setScale(obj, 1.2, 1.2, 1.2);await api.lookAt(obj, 0, 0, 0);const [width, height, depth] = await api.getSize(obj);The size comes from a runtime bounding-box calculation, which makes it useful for layout decisions, hit-area estimation, and dynamic scaling.
const children = await api.getChildren(obj);
const mesh = await api.getChildByProperty(obj, 'name', 'rabbit-head');This set of APIs is especially useful for glTF models, because their materials, events, and other behavior often need to be applied to internal child nodes.
You can listen for object-level events. Current support includes:
click - fires when the object is clicked. Event bubbling is supported. Because of asynchronous behavior, stopping propagation is not currently supported.Supported object types: all 3D object types.
const obj = await api.getObject('rabbit');
function onClick(event) {
const { type, name, object, target } = event;
// type - click
// name - the name of the clicked child 3D object, or the object itself
// object - the handle of the clicked child 3D object, or the object itself
// target - the object currently listening for the click event
// The click event bubbles upward through parents until the root scene.
}
await api.on('click', onClick, obj);play - fires when audio, video, animation, or animated images start playingSupported object types: video, model, animated image, and audio.
const obj = await api.getObject('rabbit');
function onPlay(event) {
// Different object types and different media play types expose different info fields.
const { type, target, ...info } = event;
}
await api.on('play', onPlay, obj);pause - fires when audio, video, animation, or animated images pauseSupported object types: video, model, animated image, and audio.
const obj = await api.getObject('rabbit');
function onPause(event) {
const { type, target, ...info } = event;
}
await api.on('pause', onPause, obj);ended - fires when audio, video, animation, or animated images finish naturallySupported object types: video, model, animated image, and audio.
const obj = await api.getObject('rabbit');
function onEnded(event) {
const { type, target, ...info } = event;
}
await api.on('ended', onEnded, obj);replay - fires when video, animation, or animated images replaySupported object types: video, model, animated image, and audio.
const obj = await api.getObject('rabbit');
function onReplay(event) {
const { type, target, ...info } = event;
}
await api.on('replay', onReplay, obj);In real projects, a reliable approach is:
loadSceneEnd or sceneStartgetObject() or getAllObjects()