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.

fun isGroupable(blocks: List<DesignBlock>): Boolean

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

  • blocks: a non-empty array of block ids.

  • Returns whether the blocks can be grouped together.

fun group(blocks: List<DesignBlock>): DesignBlock

Group blocks together.

  • blocks: a non-empty array of block ids.

  • Returns the block id of the created group.

fun ungroup(block: DesignBlock)

Ungroups a group.

  • block: the group id from a previous call to group.
fun enterGroup(block: DesignBlock)

Changes selection from selected group to a block within that group.

Nothing happens if block is not a group.

Required scope: “editor/select”

  • block: the group id from a previous call to group.
fun exitGroup(block: DesignBlock)

Changes selection from a group’s selected block to that group.

Nothing happens if block is not a group.

Required scope: “editor/select”

  • block: a block id.

Full Code

Here’s the full code:

// Create blocks and append to scene
val member1 = engine.block.create(DesignBlockType.Graphic)
val member2 = engine.block.create(DesignBlockType.Graphic)
engine.block.appendChild(scene, child = member1)
engine.block.appendChild(scene, child = member2)
// Check whether the blocks may be grouped
if (engine.block.isGroupable(listOf(member1, member2))) {
val group = engine.block.group(listOf(member1, member2))
engine.block.setSelected(group, selected = true)
engine.block.enterGroup(group)
engine.block.setSelected(member1, selected = true)
engine.block.exitGroup(member1)
engine.block.ungroup(group)
}