|
const app = window.require('photoshop').app |
|
|
|
const batchPlay = require('photoshop').action.batchPlay |
|
const psapi = require('./psapi') |
|
|
|
async function moveLayersToGroup(group_id) { |
|
const activeLayers = await app.activeDocument.activeLayers |
|
const layerIDs = activeLayers.map((layer) => layer.id) |
|
const { executeAsModal } = require('photoshop').core |
|
await executeAsModal(async () => { |
|
await psapi.moveToGroupCommand(group_id, layerIDs) |
|
}) |
|
} |
|
|
|
async function createSnapshot() { |
|
const { executeAsModal } = require('photoshop').core |
|
|
|
|
|
|
|
|
|
let snapshotLayer, snapshotGroup |
|
try { |
|
const selectionInfo = await psapi.getSelectionInfoExe() |
|
await psapi.unSelectMarqueeExe() |
|
|
|
|
|
const allLayers = await app.activeDocument.layers |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let duplicatedLayers = [] |
|
|
|
|
|
const groupLayer = await psapi.createEmptyGroup() |
|
|
|
console.log('createSnapshot(), group_id:', groupLayer.id) |
|
|
|
let indexOffset = 0 |
|
|
|
const result1 = await executeAsModal(async () => { |
|
for (layer of allLayers) { |
|
if (layer.id == 1) { |
|
|
|
|
|
indexOffset = 1 |
|
continue |
|
} |
|
if (layer.visible) { |
|
const copyLayer = await layer.duplicate() |
|
duplicatedLayers.push(copyLayer) |
|
} |
|
} |
|
|
|
const layerIDs = duplicatedLayers.map((layer) => layer.id) |
|
console.log('createSnapshot, layerIDs:', layerIDs) |
|
|
|
|
|
psapi.selectLayers(duplicatedLayers) |
|
let group_index = await psapi.getLayerIndex(groupLayer.id) |
|
|
|
await psapi.moveToGroupCommand(group_index - indexOffset, layerIDs) |
|
|
|
await psapi.collapseGroup(duplicatedLayers[0]) |
|
snapshotLayer = app.activeDocument.activeLayers[0] |
|
await psapi.createSolidLayer(255, 255, 255) |
|
const whiteSolidLayer = app.activeDocument.activeLayers[0] |
|
await snapshotLayer.moveAbove(whiteSolidLayer) |
|
snapshotGroup = await psapi.createEmptyGroup() |
|
let snapshot_group_index = await psapi.getLayerIndex( |
|
snapshotGroup.id |
|
) |
|
|
|
await psapi.selectLayers([snapshotLayer, whiteSolidLayer]) |
|
await psapi.moveToGroupCommand( |
|
snapshot_group_index - indexOffset, |
|
[] |
|
) |
|
await psapi.selectLayers([snapshotGroup]) |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
await psapi.createMaskExe() |
|
|
|
|
|
}) |
|
|
|
return [snapshotLayer, snapshotGroup] |
|
} catch (e) { |
|
console.warn('createSnapshot Error:', e) |
|
} |
|
} |
|
|
|
function executeCommand(batchPlayCommandFunc) { |
|
const { executeAsModal } = require('photoshop').core |
|
try { |
|
executeAsModal(async () => { |
|
await batchPlayCommandFunc() |
|
}) |
|
} catch (e) { |
|
console.warn('executeCommand error:', e) |
|
} |
|
} |
|
|
|
async function snapAndFillExe(session_id) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
let snapAndFillLayers = [] |
|
await executeAsModal(async (context) => { |
|
const history_id = await context.hostControl.suspendHistory({ |
|
documentID: app.activeDocument.id, |
|
name: 'Img2Img layers', |
|
}) |
|
const selectionInfo = await psapi.getSelectionInfoExe() |
|
|
|
|
|
|
|
|
|
await psapi.snapshot_layerExe() |
|
const snapshotLayer = await app.activeDocument.activeLayers[0] |
|
const snapshotGroup = await psapi.createEmptyGroup() |
|
snapshotLayer.name = 'Init Image Snapshot -- temporary' |
|
snapshotGroup.name = 'Init Image Group -- temporary' |
|
|
|
|
|
await psapi.createSolidLayer(255, 255, 255) |
|
const whiteSolidLayer = await app.activeDocument.activeLayers[0] |
|
whiteSolidLayer.name = 'Background Color -- temporary' |
|
snapshotLayer.moveAbove(whiteSolidLayer) |
|
console.log('[snapshotLayer,snapshotGroup]:', [ |
|
snapshotLayer, |
|
snapshotGroup, |
|
]) |
|
|
|
|
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
|
|
await psapi.selectLayers([snapshotGroup]) |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
await psapi.createClippingMaskExe() |
|
|
|
await psapi.selectLayers([snapshotGroup]) |
|
|
|
snapAndFillLayers = [snapshotLayer, snapshotGroup, whiteSolidLayer] |
|
|
|
|
|
|
|
|
|
|
|
const image_info = await psapi.silentSetInitImage( |
|
snapshotGroup, |
|
session_id |
|
) |
|
const image_name = image_info['name'] |
|
const path = `./server/python_server/init_images/${image_name}` |
|
|
|
g_viewer_manager.initializeInitImage( |
|
snapshotGroup, |
|
snapshotLayer, |
|
whiteSolidLayer, |
|
path |
|
) |
|
|
|
for (layer of snapAndFillLayers) { |
|
layer.visible = false |
|
} |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
const layer_util = require('./utility/layer') |
|
await layer_util.collapseFolderExe([snapshotGroup], false) |
|
await context.hostControl.resumeHistory(history_id) |
|
}) |
|
console.log('snapAndFillLayers: ', snapAndFillLayers) |
|
return snapAndFillLayers |
|
} catch (e) { |
|
console.error(`snapAndFill error: ${e}`) |
|
} |
|
return [] |
|
} |
|
|
|
async function addClippingMaskToLayer(layer, selectionInfo) { |
|
await psapi.selectLayers([layer]) |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
await psapi.createClippingMaskExe() |
|
await psapi.selectLayers([layer]) |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
async function outpaintExe(session_id) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
let outpaintLayers = [] |
|
await executeAsModal(async (context) => { |
|
const history_id = await context.hostControl.suspendHistory({ |
|
documentID: app.activeDocument.id, |
|
name: 'Outpaint Mask Related layers', |
|
}) |
|
const selectionInfo = await psapi.getSelectionInfoExe() |
|
|
|
|
|
|
|
|
|
await psapi.snapshot_layerExe() |
|
const snapshotLayer = await app.activeDocument.activeLayers[0] |
|
snapshotLayer.name = 'Init Image Snapshot -- temporary' |
|
const snapshotGroup = await psapi.createEmptyGroup() |
|
|
|
snapshotGroup.name = 'Init Image Group -- temporary' |
|
await psapi.createSolidLayer(255, 255, 255) |
|
const whiteSolidLayer = await app.activeDocument.activeLayers[0] |
|
whiteSolidLayer.name = 'Background Color -- temporary' |
|
snapshotLayer.moveAbove(whiteSolidLayer) |
|
console.log('[snapshotLayer,snapshotGroup]:', [ |
|
snapshotLayer, |
|
snapshotGroup, |
|
]) |
|
|
|
|
|
await psapi.selectLayers([snapshotLayer]) |
|
await psapi.selectLayerChannelCommand() |
|
const snapshotMaskGroup = await psapi.createEmptyGroup() |
|
|
|
await psapi.createSolidLayer(0, 0, 0) |
|
let solid_black_layer = app.activeDocument.activeLayers[0] |
|
|
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
await psapi.snapshot_layerExe() |
|
const snapshotMaskLayer = await app.activeDocument.activeLayers[0] |
|
snapshotMaskLayer.name = 'Mask -- Paint White to Mask -- temporary' |
|
|
|
|
|
|
|
snapshotMaskGroup.name = 'Mask Group -- temporary' |
|
snapshotMaskLayer.moveBelow(solid_black_layer) |
|
await snapshotMaskGroup.moveAbove(snapshotGroup) |
|
await solid_black_layer.delete() |
|
|
|
await addClippingMaskToLayer(snapshotGroup, selectionInfo) |
|
|
|
const mask_info = await psapi.silentSetInitImageMask( |
|
snapshotMaskGroup, |
|
session_id |
|
) |
|
snapshotMaskGroup.visible = false |
|
|
|
const image_info = await psapi.silentSetInitImage( |
|
snapshotGroup, |
|
session_id |
|
) |
|
snapshotGroup.visible = false |
|
|
|
const init_image_name = image_info['name'] |
|
const init_path = `./server/python_server/init_images/${init_image_name}` |
|
|
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
await addClippingMaskToLayer(snapshotMaskGroup, selectionInfo) |
|
|
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
|
|
|
|
const mask_name = mask_info['name'] |
|
const mask_path = `./server/python_server/init_images/${mask_name}` |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
|
|
outpaintLayers = [ |
|
snapshotMaskGroup, |
|
snapshotMaskLayer, |
|
snapshotLayer, |
|
snapshotGroup, |
|
whiteSolidLayer, |
|
] |
|
|
|
|
|
|
|
g_viewer_manager.initializeMask( |
|
snapshotMaskGroup, |
|
snapshotMaskLayer, |
|
null, |
|
mask_path, |
|
mask_info['base64'] |
|
) |
|
|
|
|
|
|
|
g_viewer_manager.initializeInitImage( |
|
snapshotGroup, |
|
snapshotLayer, |
|
whiteSolidLayer, |
|
init_path |
|
) |
|
|
|
for (layer of outpaintLayers) { |
|
layer.visible = false |
|
} |
|
|
|
|
|
const layer_util = require('./utility/layer') |
|
await layer_util.collapseFolderExe( |
|
[snapshotGroup, snapshotMaskGroup], |
|
false |
|
) |
|
await context.hostControl.resumeHistory(history_id) |
|
}) |
|
console.log('outpaintLayers 2: ', outpaintLayers) |
|
return outpaintLayers |
|
} catch (e) { |
|
console.error(`outpaintExe error: ${e}`) |
|
} |
|
return [] |
|
} |
|
|
|
async function inpaintFasterExe(session_id) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
let inpaintLayers = [] |
|
await executeAsModal(async (context) => { |
|
const history_id = await context.hostControl.suspendHistory({ |
|
documentID: app.activeDocument.id, |
|
name: 'Inpaint Mask Related layers', |
|
}) |
|
const original_white_mark_layer = await app.activeDocument |
|
.activeLayers[0] |
|
original_white_mark_layer.visible = false |
|
|
|
const selectionInfo = await psapi.getSelectionInfoExe() |
|
|
|
|
|
const white_mark_layer = |
|
await app.activeDocument.activeLayers[0].duplicate() |
|
white_mark_layer.visible = true |
|
const mask_layer_opacity = await white_mark_layer.opacity |
|
white_mark_layer.opacity = 100 |
|
await psapi.selectLayers([white_mark_layer]) |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
await psapi.createClippingMaskExe() |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
white_mark_layer.visible = false |
|
white_mark_layer.name = 'Mask -- Paint White to Mask -- temporary' |
|
|
|
|
|
|
|
|
|
|
|
|
|
await psapi.unselectActiveLayersExe() |
|
|
|
await psapi.snapshot_layerExe() |
|
|
|
const snapshotLayer = await app.activeDocument.activeLayers[0] |
|
snapshotLayer.name = 'Init Image Snapshot -- temporary' |
|
const snapshotGroup = await psapi.createEmptyGroup() |
|
snapshotGroup.name = 'Init Image Group -- temporary' |
|
await psapi.createSolidLayer(255, 255, 255) |
|
const whiteSolidLayer = await app.activeDocument.activeLayers[0] |
|
whiteSolidLayer.name = 'Background Color -- temporary' |
|
await snapshotLayer.moveAbove(whiteSolidLayer) |
|
|
|
await psapi.selectLayers([snapshotGroup]) |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
await psapi.createClippingMaskExe() |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
const maskGroup = await psapi.createEmptyGroup() |
|
|
|
|
|
maskGroup.name = 'Mask Group -- temporary' |
|
|
|
await psapi.createSolidLayer(0, 0, 0) |
|
const blackSolidLayer = await app.activeDocument.activeLayers[0] |
|
blackSolidLayer.name = "Don't Edit -- temporary" |
|
|
|
white_mark_layer.moveAbove(blackSolidLayer) |
|
white_mark_layer.visible = true |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
console.log('[snapshotLayer,maskGroup]:', [ |
|
snapshotLayer, |
|
maskGroup, |
|
]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await psapi.selectLayers([maskGroup]) |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
await psapi.createClippingMaskExe() |
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
|
|
|
|
await psapi.selectLayers([maskGroup]) |
|
|
|
const mask_info = await psapi.silentSetInitImageMask( |
|
maskGroup, |
|
session_id |
|
) |
|
maskGroup.visible = false |
|
|
|
const mask_name = mask_info['name'] |
|
const mask_path = `./server/python_server/init_images/${mask_name}` |
|
|
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
await psapi.selectLayers([snapshotGroup]) |
|
|
|
const image_info = await psapi.silentSetInitImage( |
|
snapshotGroup, |
|
session_id |
|
) |
|
const image_name = image_info['name'] |
|
const path = `./server/python_server/init_images/${image_name}` |
|
|
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
|
|
|
|
|
|
|
|
|
|
await psapi.selectLayers([maskGroup]) |
|
inpaintLayers = [ |
|
maskGroup, |
|
white_mark_layer, |
|
blackSolidLayer, |
|
snapshotGroup, |
|
snapshotLayer, |
|
whiteSolidLayer, |
|
] |
|
|
|
|
|
|
|
g_viewer_manager.initializeMask( |
|
maskGroup, |
|
white_mark_layer, |
|
blackSolidLayer, |
|
mask_path, |
|
mask_info['bases64'] |
|
) |
|
|
|
|
|
|
|
g_viewer_manager.initializeInitImage( |
|
snapshotGroup, |
|
snapshotLayer, |
|
whiteSolidLayer, |
|
path |
|
) |
|
for (layer of inpaintLayers) { |
|
layer.visible = false |
|
} |
|
const layer_util = require('./utility/layer') |
|
|
|
await layer_util.collapseFolderExe( |
|
[snapshotGroup, maskGroup], |
|
false |
|
) |
|
white_mark_layer.opacity = mask_layer_opacity |
|
|
|
await context.hostControl.resumeHistory(history_id) |
|
}) |
|
return inpaintLayers |
|
} catch (e) { |
|
console.warn('inpaintFasterExe error:', e) |
|
} |
|
return [] |
|
} |
|
module.exports = { |
|
createSnapshot, |
|
|
|
moveLayersToGroup, |
|
executeCommand, |
|
outpaintExe, |
|
|
|
inpaintFasterExe, |
|
snapAndFillExe, |
|
addClippingMaskToLayer, |
|
} |
|
|