Important: The Code API allows your native interface to control properties in the Spline scene, and vice versa. However, itās important to note that this is different from Real-time data APIs feature. To learn more about real-time data APIs, go here Real-time API).
Key capabilities
- Event Listeners: React to real-time events within your 3D scenes.
- Adjust Properties: Dynamically update position, scale, rotation, and more.
- Trigger Transitions: Create transitions and animations based on events.
- Adjust variables: Retrieve and update variables in a Spline scene.
Usage
Getting started
First, make sure youāre familiar with Native 3D Embeds for iOS and you can already embed 3D content in your SwiftUI projects.Info: A controller object is only able to control a single view. Do NOT use the same controller for more views. See Handling multiple views section.
Read and modify Spline objects
You can query any Spline object in the scene viaĀfindObject(name:)Ā orĀ findObject(id:) functions. Then, you can read or write any of its properties.
(You can get the ID of the object from the right-click context panel on the object).
nil if an object with the specified name was not found.
Note that SplineView loads its content asynchronously, and so these and all other functions may return nil or may do nothing when the scene file still hasnāt finished loading. This is especially important when you want to access the API right at the start. For this, see the next example.
Listen to events
You can listen to any Spline Event you set in the Events panel of the editor by attaching a listener to the controller. You can find a list of all of the Spline Event listeners in the API section. To register event listeners when the view loads, make sure to use atask() or onAppear() modifier on the view that holds loaded content (SplineContent) and not the SplineView itself.
Trigger Spline events from outside
You can trigger any animation Event you set in the Events panel in the Spline Editor. You can use theĀemitEvent(_:)Ā function, passing the event type and the name or ID of your object.
(You can get the ID of the object from the right-click context panel on the object).
emitEvent(:_) function in the Spline EventsĀ section.
Handling multiple views
If you have severalSplineViewās in a SwiftUI view hierarchy, and multiple of them are using the Code API, each view must have its own SplineController object.
Updating scene variables
If you set up variables in your Spline scene from the editor, you can change them from code after the scene is loaded.Note: If in the Spline Editor you have multiple variables with the same name, only the first one will be updated, so make sure to give unique names to the variables you want to update. Also note that the values you pass to your variables will be cast into their original type (number, boolean, or string).
Download Xcode Demo
See how it works by downloading an Xcode demo project using Splineās cloud iOS embeds and SwiftUI Code API. File Trying out the demo:- On Xcode, go to File > Open and select the unzipped folder with your project;
- Select iPhone as the build device ā you can use your physical device or a simulator;
- Trigger the preview on Xcode or press ā¶ļøĀ to build your project.
- You might need to go into
Signing & Capabilities, and underSigning, add your team manually. - Optionally, make sure to change the
Bundle Identifierto a unique one.
API
Spline Controller Methods
You can call all these different methods on theSplineController instance.
| Name | Description |
|---|---|
| addEventListener(_:callback:) | Add an event listener for Spline events |
| emitEvent(_:nameOrUUID:) | Triggers a Spline event associated to an object with the provided name or UUID. Starts from the first state to the last state. |
| emitEventReverse(_:nameOrUUID:) | Triggers a Spline event associated to an object with the provided name or UUID in reverse order. Starts from the last state to the first state. |
| findObject(id:) | Searches through the sceneās children and returns the object with that UUID. |
| findObject(name:) | Searches through the sceneās children and returns the first object with that name. |
| setZoom(_:) | Sets the camera zoom, expects a number value > 0 where 1 is base zoom. |
| setNumberVariable(name:value:) | Updates value for passed variable by name. |
| setBoolVariable(name:value:) | Updates value for passed variable by name. |
| setStringVariable(name:value:) | Updates value for passed variable by name. |
| getNumberVariable(name:) | Get current value for a specific variable from its name |
| getBoolVariable(name:) | Get current value for a specific variable from its name |
| getStringVariable(name:) | Get current value for a specific variable from its name |
| stop() | Stop/Pause all rendering controls and events. |
| play() | Play/Resume rendering, controls and events. |
| setBackgroundColor(_:) | Manually sets the scene/canvas background color with a SIMD color value. |
Spline Objects Methods and Properties
After retrieving a Spline Object withĀcontroller.findObject(id:) orĀ controller.findObject(name:), there are a variety of properties and methods you can call on those.
| Name | Description |
|---|---|
| name | Gets / Sets object name. |
| uuid | Gets object uuid. |
| position | Gets / Sets object position. |
| rotation | Gets / Sets object rotation. |
| scale | Gets / Sets object scale. |
| visible | Gets / Sets object vibility. |
| intensity | Only for light objects. Used to change the light intensity. |
| emitEvent(_:) | Force trigger an event defined in Spline Editor. |
| emitEventReverse(_:) | Force trigger an event defined in Spline Editor. |
Spline Events
These are all the Spline event types that you can pass to theaddEventListener(:),Ā emitEvent(:) andĀ emitEventReverse(_:) functions.
| Name | Description |
|---|---|
| .mouseUp | Refers to the SplineĀ Mouse UpĀ event type |
| .mouseDown | Refers to the SplineĀ Mouse DownĀ event type |
| .mousePress | Refers to the SplineĀ Mouse PressĀ event type |
| .mouseHover | Refers to the SplineĀ Mouse HoverĀ event type |
| .keyUp | Refers to the SplineĀ Key UpĀ event type |
| .keyDown | Refers to the SplineĀ Key DownĀ event type |
| .keyPress | Refers to the SplineĀ Key PressĀ event type |
| .start | Refers to the SplineĀ StartĀ event type |
| .lookAt | Refers to the SplineĀ Look AtĀ event type |
| .follow | Refers to the SplineĀ FollowĀ event type |

