This guide outlines the steps to integrate Spatial Editor projects with Spatial SDK apps. Once integrated, your Spatial Editor project will be located in the scenes directory of your Spatial SDK app:
How Spatial Editor and code work together
Spatial Editor and your Kotlin code serve complementary roles:
Spatial Editor is a visual tool for designing 3D scene layouts. You use it to position objects, set up panels, configure lighting, and arrange your scene without writing code. Spatial Editor saves your work as a composition (a .metaspatial project file) and exports it as a .glxf file.
Your Kotlin code loads the exported .glxf file at runtime, and adds behavior and interactivity. For example, Spatial Editor positions a button panel in 3D space, while your code defines what happens when the button is pressed.
You can also create entities and position objects entirely in code without using Spatial Editor. Spatial Editor is optional but recommended for complex scene layouts because it provides a visual preview and makes spatial arrangement easier to iterate on.
Set up the Spatial SDK Gradle plugin
The Spatial SDK Gradle plugin is used to process your Spatial Editor project and export it so it can be used by your app. Meta collects telemetry data from the Spatial SDK Gradle Plugin to help improve MPT Products. You can read the Supplemental Meta Platforms Technologies Privacy Policy to learn more.
To get started, ensure you have the following:
Android Studio installed.
A new Android Studio Spatial SDK project created, ideally using a starter sample as a template.
Spatial Editor installed, which includes the Spatial Editor CLI for export tasks.
Next, with your project open in Android Studio:
Open the project’s libs.versions.toml file.
In the [versions] block, add a variable for the Spatial SDK version.
spatialsdk = "0.15.0"
All Spatial SDK dependencies reference this variable, so you update every version by changing this single line.
In the [plugins] block, add the Meta Spatial Plugin.
meta-spatial-plugin = { id = "com.meta.spatial.plugin", version.ref = "spatialsdk" }
In app/build.gradle.kts, add the following to the plugins block:
Find the tasks available with the plugin in the Gradle sidebar under the spatial directory. Open the Gradle sidebar by clicking on the Gradle icon in the right sidebar of Android Studio. Android Studio may not configure all Gradle tasks by default. To configure all Gradle tasks so you can see and run them, enable Configure all Gradle tasks during Gradle Sync in File > Settings > Experimental under the Gradle header. All Gradle tasks can be found under Tasks/spatial.
To execute any of the tasks, either double-click them in the Gradle sidebar or run ./gradlew _task name_.
Export Spatial Editor projects
The export task uses the Spatial Editor CLI to export your Spatial Editor project to glXF format compositions that your app inflates at runtime. You can export the entire project, a single composition, or a single object. To prepare your Spatial Editor project for use in your Spatial SDK project, modify the app-level build.gradle.kts file as follows.
Set up exports
The configuration below demonstrates three export scenarios:
Full project export: Exports your entire Spatial Editor project, including all compositions and objects, to a single destination folder.
Composition-specific export: Exports one composition and its contents. Use this to modularize your assets, or to include only certain parts of your project.
Individual object export: Exports a single object. Use this for reusable assets that you place in specific locations within your app’s asset structure.
Each export item defines a projectPath (your main .metaspatial file) and an outputPath (where the exported assets go in your app). The optional compositionPath and spatialEditorObjectPath parameters target specific parts of your project for export.
You can also set a custom path to the Spatial Editor CLI.
val projectDir = layout.projectDirectory
val sceneDirectory = projectDir.dir("scenes")
spatial {
allowUsageDataCollection = true
scenes {
// Set cliPath only when Meta Spatial Editor is outside its default install location.
// The paths below show the expected shape:
// Mac:
// cliPath.set("/Applications/Meta Spatial Editor.app/Contents/MacOS/CLI")
// Windows (replace V16 with your installed version folder):
// cliPath.set("C:/Program Files/Meta Spatial Editor/V16/Resources/CLI.exe")
exportItems {
// Export your entire project, including all compositions and objects
item {
projectPath.set(sceneDirectory.file("Main.metaspatial"))
outputPath.set(projectDir.dir("src/main/assets/scenes"))
}
// Export only a specific composition and its contents
item {
projectPath.set(sceneDirectory.file("Main.metaspatial"))
outputPath.set(projectDir.dir("src/main/assets/composition/comp"))
compositionPath.set(sceneDirectory.file("Comp1/Main.metaspatialcomp"))
}
// Export only a specific object
item {
projectPath.set(sceneDirectory.file("Main.metaspatial"))
outputPath.set(projectDir.dir("src/main/assets/obj1"))
spatialEditorObjectPath.set(sceneDirectory.file("duck/Main.metaspatialobj"))
}
}
}
}
cliPath.set(...): the location of the CLI executable. If you don’t set it, the plugin searches predefined locations, and a build that defines export items fails when the CLI is not found.
exportItems: Defines a list of projects and asset directories to be exported.
Ensure you replace the example CLI path with the correct path to the Spatial Editor executable on your system.
The export also runs during an app build, because the plugin registers its collected output as a generated assets directory for each build variant.
Components in exported scenes
The export task converts more than 3D models and materials. It also preserves the component data that defines object behavior and properties, so the components you attach to an object in Spatial Editor are applied to the entity when your app inflates the scene. Components are what connect what you design visually in Spatial Editor with what you control programmatically in your Spatial SDK code.
Make custom components available in Spatial Editor
Spatial SDK projects use components for data storage. There are two ways to make custom components available in the Spatial Editor UI.
Define components in XML
Add a component XML file in app/src/main/components. The Spatial Editor will read the XML defined in the folder and display your custom components in UI. If you prefer to use another directory, refer to Specify the custom components folder.
Define components in Kotlin (removed)
In SDK versions prior to v0.10.1, a generateComponents Gradle task was available for Kotlin-defined components to synchronize the component definition over to Spatial Editor (so Spatial Editor could recognize these components). This task has been removed, and component generation from XML is now automatic at build time. Define your components using XML in app/src/main/components as described in this section.
Load and change between compositions
The following sample demonstrates how to change between two compositions (Composition1 and Composition2) when a UI button is pressed (button0):
If your app uses hot reload, the default reload mode recreates the entities in a reloaded composition. See Hot reload for the reload modes and for how to assign entity references so they stay valid after a reload.
Populate a panel using Spatial SDK
When populating panels using Spatial SDK, you can differentiate them by setting a unique ID in the Properties panel of Spatial Editor. The ID should reference an ID resource (R.id.panel_name) instead of a layout resource, as the layout is specified separately in the registration.
Here are several methods to layout and define panel content:
Use LayoutXMLPanelRegistration to define panel UI using layout XML.
Use ActivityPanelRegistration to define panel UI using an Activity.
Use IntentPanelRegistration to define panel UI using an Intent (allowing you to pass information along to your inflated Activity).
Use ViewPanelRegistration to define panel UI using a View at runtime.
For example, this script populates a small panel using Jetpack Compose:
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
@Composable
fun MyPanel() {
Column(modifier = Modifier.padding(16.dp)) {
Text(text = "Panel Title", style = MaterialTheme.typography.headlineSmall)
Spacer(modifier = Modifier.height(8.dp))
Text(text = "This is a panel with some content")
}
}