Skip to main content
The Code API allows you to seamlessly integrate interactive 3D experiences into your projects, offering greater control and customization. It enables you to trigger actions, modify object properties, and create dynamic, responsive 3D environments directly from your existing code and user interface.
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.
Then, to access the API, create a SplineController object and pass it to SplineView initializer for the view you want to control.
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).
The function returns 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 a task() 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).
Or you can query the spline object first, and then trigger the event:
You can find a list of all of the Spline Events you can pass to theĀ emitEvent(:_) function in the Spline EventsĀ section.

Handling multiple views

If you have several SplineView’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:
  1. On Xcode, go to File > Open and select the unzipped folder with your project;
  2. Select iPhone as the build device – you can use your physical device or a simulator;
  3. Trigger the preview on Xcode or press ā–¶ļøĀ to build your project.
Before Building
  1. You might need to go into Signing & Capabilities, and under Signing, add your team manually.
  2. Optionally, make sure to change the Bundle Identifier to a unique one.
3D Scene:

API

Spline Controller Methods

You can call all these different methods on the SplineController instance.

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.

Spline Events

These are all the Spline event types that you can pass to the addEventListener(:),Ā emitEvent(:) andĀ emitEventReverse(_:) functions.