Ungroup Layers
photopea_ungroup_layersUngroup a layer group by moving all its child layers to the document root, removing the group folder while preserving contents.
Instructions
Dissolve a layer group, moving all child layers to the document root. The group folder is removed but its contents are preserved. Use get_layers to find group names.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| target | Yes | Name of the layer group to ungroup (use get_layers to find group names) |
Implementation Reference
- src/tools/layer.ts:189-202 (handler)The async handler function for photopea_ungroup_layers. It escapes the target layer group name, builds a JavaScript script to move all child layers out of the group to the document root, removes the now-empty group, sends an activity log, executes the script, and returns success/failure.
}, async (params) => { const safe = escapeString(params.target); const script = [ `var _d = app.activeDocument;`, `var _g = _d.layerSets.getByName('${safe}');`, `while (_g.layers.length > 0) { _g.layers[0].move(_d.layers[_d.layers.length - 1], ElementPlacement.PLACEBEFORE); }`, `_g.remove();`, `app.echoToOE('ok');`, ].join("\n"); bridge.sendActivity({ type: "activity", id: "", tool: "ungroup_layers", summary: `Ungroup: ${params.target}` }); const result = await bridge.executeScript(script); if (!result.success) return { isError: true, content: [{ type: "text" as const, text: result.error || "Failed to ungroup layers" }] }; return { content: [{ type: "text" as const, text: `Group ungrouped: ${params.target}` }] }; }); - src/tools/layer.ts:185-187 (schema)Input schema for photopea_ungroup_layers: requires a single string parameter 'target' (name of the layer group to ungroup).
inputSchema: { target: z.string().describe("Name of the layer group to ungroup (use get_layers to find group names)"), }, - src/tools/layer.ts:181-202 (registration)Registration of the tool 'photopea_ungroup_layers' using server.registerTool with title, description, inputSchema, annotations, and the handler function.
// photopea_ungroup_layers server.registerTool("photopea_ungroup_layers", { title: "Ungroup Layers", description: "Dissolve a layer group, moving all child layers to the document root. The group folder is removed but its contents are preserved. Use get_layers to find group names.", inputSchema: { target: z.string().describe("Name of the layer group to ungroup (use get_layers to find group names)"), }, annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false }, }, async (params) => { const safe = escapeString(params.target); const script = [ `var _d = app.activeDocument;`, `var _g = _d.layerSets.getByName('${safe}');`, `while (_g.layers.length > 0) { _g.layers[0].move(_d.layers[_d.layers.length - 1], ElementPlacement.PLACEBEFORE); }`, `_g.remove();`, `app.echoToOE('ok');`, ].join("\n"); bridge.sendActivity({ type: "activity", id: "", tool: "ungroup_layers", summary: `Ungroup: ${params.target}` }); const result = await bridge.executeScript(script); if (!result.success) return { isError: true, content: [{ type: "text" as const, text: result.error || "Failed to ungroup layers" }] }; return { content: [{ type: "text" as const, text: `Group ungrouped: ${params.target}` }] }; }); - src/bridge/script-builder.ts:44-46 (helper)The escapeString helper used by the handler to safely escape the target layer group name before embedding it in the JavaScript script.
export function escapeString(str: string): string { return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r"); }