Skip to content

Group and Ungroup Objects

In this example, we will show you how to use the CreativeEditor SDK’s CreativeEngine to group blocks through the block API. Groups form a cohesive unit.

Grouping

Multiple blocks can be grouped together to form a cohesive unit. A group being a block, it can itself be part of a group.

public func isGroupable(_ ids: [DesignBlockID]) throws -> Bool

Confirms that a given set of blocks can be grouped together.

  • ids:: A non-empty array of block ids.
  • Returns: Whether the blocks can be grouped together.
public func group(_ ids: [DesignBlockID]) throws -> DesignBlockID

Group blocks together.

  • ids:: A non-empty array of block ids.
  • Returns: The block id of the created group.
public func ungroup(_ id: DesignBlockID) throws

Ungroups a group.

  • id:: The group id from a previous call to group.
public func enterGroup(_ id: DesignBlockID) throws

Changes selection from selected group to a block within that group. Nothing happens if id is not a group. Required scope: “editor/select”

  • id:: The group id from a previous call to group.
public func exitGroup(_ id: DesignBlockID) throws

Changes selection from a group’s selected block to that group. Nothing happens if the id is not part of a group. Required scope: “editor/select”

  • id:: A block id.

Full Code

Here’s the full code:

// Create blocks and append to scene
let member1 = try engine.block.create(.graphic)
let member2 = try engine.block.create(.graphic)
try engine.block.appendChild(to: scene, child: member1)
try engine.block.appendChild(to: scene, child: member2)
// Check whether the blocks may be grouped
if try engine.block.isGroupable([member1, member2]) {
let group = try engine.block.group([member1, member2])
try engine.block.setSelected(group, selected: true)
try engine.block.enterGroup(group)
try engine.block.setSelected(member1, selected: true)
try engine.block.exitGroup(member1)
try engine.block.ungroup(group)
}