In this example, we will show you how to initialize the CreativeEditor SDK from scratch and add a star shape.
We create an empty scene via try engine.scene.create()
which sets up the default scene block with a camera attached.
Afterwards, the scene can be populated by creating additional blocks and appending them to the scene.
See Modifying Scenes for more details.
let scene = try engine.scene.create()
We first add a page with func create(_ type: DesignBlockType) throws -> DesignBlockID
specifying a .page
and set a parent-child relationship between the scene and this page.
let page = try engine.block.create(.page)try engine.block.appendChild(to: scene, child: page)
To this page, we add a graphic design block, again with func create(_ type: DesignBlockType) throws -> DesignBlockID
.
To make it more interesting, we set a star shape and a color fill to this block to give it a visual representation.
Like for the page, we set the parent-child relationship between the page and the newly added block.
From then on, modifications to this block are relative to the page.
let block = try engine.block.create(.graphic)try engine.block.setShape(block, shape: engine.block.createShape(.star))try engine.block.setFill(block, fill: engine.block.createFill(.color))try engine.block.appendChild(to: page, child: block)
This example first appends a page child to the scene as would typically be done but it is not strictly necessary and any child block can be appended directly to a scene.
To later save your scene, see Saving Scenes.
Full Code
Here’s the full code:
import Foundationimport IMGLYEngine
@MainActorfunc createSceneFromScratch(engine: Engine) async throws { let scene = try engine.scene.create()
let page = try engine.block.create(.page) try engine.block.appendChild(to: scene, child: page)
let block = try engine.block.create(.graphic) try engine.block.setShape(block, shape: engine.block.createShape(.star)) try engine.block.setFill(block, fill: engine.block.createFill(.color)) try engine.block.appendChild(to: page, child: block)}