1 Star 0 Fork 0

王云朋/Auto-Photoshop-StableDiffusion-Plugin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
psapi.js 39.89 KB
一键复制 编辑 原始数据 按行查看 历史
Abdullah Alfaraj 提交于 2023-08-17 13:14 . refactoring the main tab
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
const app = window.require('photoshop').app
const batchPlay = require('photoshop').action.batchPlay
const { executeAsModal } = require('photoshop').core
const constants = require('photoshop').constants
// console.log(constants.ResampleMethod.BILINEAR)
// const export_png = require('./export_png')
// const { layerToSelection } = require('./helper')
const storage = require('uxp').storage
const fs = storage.localFileSystem
async function createSolidLayer(r, g, b) {
console.warn('this function is deprecated')
await executeAsModal(async () => {
const result = await batchPlay(
[
{
_obj: 'make',
_target: [
{
_ref: 'contentLayer',
},
],
using: {
_obj: 'contentLayer',
type: {
_obj: 'solidColorLayer',
color: {
_obj: 'RGBColor',
red: r,
grain: g,
blue: b,
},
},
},
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
})
}
async function makeGroupCommand() {
const result = await batchPlay(
[
{
_obj: 'make',
_target: [
{
_ref: 'layerSection',
},
],
layerSectionStart: 405,
layerSectionEnd: 406,
name: 'Group 16',
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
console.log('makeGroupCommand: ', result)
return result
}
async function createEmptyGroup(name = 'New Group') {
let groupLayer
await executeAsModal(async () => {
await makeGroupCommand()
groupLayer = app.activeDocument.activeLayers[0]
groupLayer.name = name
})
console.log('groupLayer:', groupLayer)
return groupLayer
}
async function moveToGroupCommand(to_index, layerIDs) {
const batchPlay = require('photoshop').action.batchPlay
console.log('to_index:', to_index)
console.log('layerIDs:', layerIDs)
const result = await batchPlay(
[
{
_obj: 'move',
_target: [
{
_ref: 'layer',
_enum: 'ordinal',
_value: 'targetEnum',
},
],
to: {
_ref: 'layer',
_index: to_index,
},
adjustment: false,
version: 5,
layerID: layerIDs,
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
}
function MoveToGroupExe(toIndex, layerIDs) {
try {
executeAsModal(async () => {
await moveToGroupCommand(toIndex, layerIDs)
})
} catch (e) {
console.warn('executeCommand error:', e)
}
}
async function getIndexCommand(layer_id) {
const idx = batchPlay(
[
{
_obj: 'get',
_target: [
{
_property: 'itemIndex',
},
{
_ref: 'layer',
// _name: 'myGroup'
_id: layer_id,
},
],
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{ synchronousExecution: true }
)[0]['itemIndex']
console.log('index:', idx)
return idx
}
async function getLayerIndex(layer_id) {
const { executeAsModal } = require('photoshop').core
try {
let index
await executeAsModal(async () => {
index = await getIndexCommand(layer_id)
console.log('getIndex: ', index)
})
return index
} catch (e) {
console.warn('getIndex error:', e)
}
}
async function unselectActiveLayers() {
const layers = await app.activeDocument.activeLayers
for (layer of layers) {
layer.selected = false
}
}
async function unselectActiveLayersExe() {
await executeAsModal(async () => {
await unselectActiveLayers()
})
}
async function selectLayers(layers) {
await unselectActiveLayers()
for (layer of layers) {
try {
if (layer) {
const is_visible = layer.visible // don't change the visibility when selecting the layer
layer.selected = true
layer.visible = is_visible
}
} catch (e) {
console.warn(e)
}
}
}
async function setVisibleExe(layer, is_visible) {
try {
await executeAsModal(async () => {
layer.visible = is_visible
})
} catch (e) {
console.warn(e)
}
}
async function selectLayersExe(layers) {
await executeAsModal(async () => {
await selectLayers(layers)
})
}
async function selectGroup(layer) {
await unselectActiveLayers()
layer.parent.selected = true
}
async function collapseGroup(layer) {
await selectGroup(layer)
await app.activeDocument.activeLayers[0].merge()
}
async function createMaskCommand() {
const batchPlay = require('photoshop').action.batchPlay
const result = await batchPlay(
[
{
_obj: 'make',
new: {
_class: 'channel',
},
at: {
_ref: 'channel',
_enum: 'channel',
_value: 'mask',
},
using: {
_enum: 'userMaskEnabled',
_value: 'revealSelection',
},
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
}
async function createMaskExe() {
const { executeAsModal } = require('photoshop').core
await executeAsModal(createMaskCommand)
}
//unselect the rectangular marquee selection area
async function unSelectMarqueeCommand() {
const batchPlay = require('photoshop').action.batchPlay
const result = await batchPlay(
[
{
_obj: 'set',
_target: [
{
_ref: 'channel',
_property: 'selection',
},
],
to: {
_enum: 'ordinal',
_value: 'none',
},
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
return result
}
async function unSelectMarqueeExe() {
try {
await executeAsModal(unSelectMarqueeCommand)
} catch (e) {
console.warn(e)
}
}
//REFACTOR: move to selection.js
async function selectMarqueeRectangularToolExe() {
async function selectMarqueeRectangularToolCommand() {
const result = await batchPlay(
[
{
_obj: 'select',
_target: [
{
_ref: 'marqueeRectTool',
},
],
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
return result
}
await executeAsModal(async () => {
await selectMarqueeRectangularToolCommand()
})
}
async function promptForMarqueeTool() {
console.warn('promptForMarqueeTool is deprecated use Notification class')
;(async () => {
const r1 = await dialog_box.prompt(
'Please Select a Rectangular Area',
'You Forgot to select a Rectangular Area',
['Cancel', 'Rectangular Marquee']
)
if ((r1 || 'Rectangular Marquee') !== 'Rectangular Marquee') {
/* cancelled or No */
console.log('cancel')
} else {
/* Yes */
console.log('Rectangular Marquee')
selectMarqueeRectangularToolExe()
}
})()
}
async function selectLayerChannelCommand() {
// const result = await batchPlay(
// [
// {
// _obj: 'set',
// _target: [
// {
// _ref: 'channel',
// _property: 'selection'
// }
// ],
// to: {
// _ref: [
// {
// _ref: 'channel',
// _enum: 'channel',
// _value: 'transparencyEnum'
// },
// {
// _ref: 'layer',
// _name: 'Group 5'
// }
// ]
// },
// _options: {
// dialogOptions: 'dontDisplay'
// }
// }
// ],
// {
// synchronousExecution: false,
// modalBehavior: 'execute'
// }
// )
const batchPlay = require('photoshop').action.batchPlay
const result = await batchPlay(
[
{
_obj: 'set',
_target: [
{
_ref: 'channel',
_property: 'selection',
},
],
to: {
_ref: 'channel',
_enum: 'channel',
_value: 'transparencyEnum',
},
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
}
async function getSelectionInfoCommand() {
// console.warn('getSelectionInfoCommand is deprecated use SelectionInfoDesc')
const result = await batchPlay(
[
{
_obj: 'get',
_target: [
{
_property: 'selection',
},
{
_ref: 'document',
_id: app.activeDocument._id,
},
],
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
return result
}
function isSelectionValid(selection) {
// console.warn(
// 'isSelectionValid is deprecated use selection.isSelectionValid instead'
// )
if (
selection && // check if the selection is defined
selection.hasOwnProperty('left') &&
selection.hasOwnProperty('right') &&
selection.hasOwnProperty('top') &&
selection.hasOwnProperty('bottom')
) {
return true
}
return false
}
async function getSelectionInfoExe() {
// console.log('getSelectionInfo was called')
// console.warn(
// 'getSelectionInfoExe is deprecated use selection.getSelectionInfoExe instead'
// )
try {
const selection = (await executeAsModal(getSelectionInfoCommand))[0]
.selection
if (isSelectionValid(selection)) {
let selection_info = {
left: selection.left._value,
right: selection.right._value,
bottom: selection.bottom._value,
top: selection.top._value,
height: selection.bottom._value - selection.top._value,
width: selection.right._value - selection.left._value,
}
// console.dir({selection_info})
return selection_info
}
} catch (e) {
console.warn('selection info error', e)
}
}
async function reSelectMarqueeCommand(selectionInfo) {
const result = await batchPlay(
[
{
_obj: 'set',
_target: [
{
_ref: 'channel',
_property: 'selection',
},
],
to: {
_obj: 'rectangle',
top: {
_unit: 'pixelsUnit',
_value: selectionInfo.top,
},
left: {
_unit: 'pixelsUnit',
_value: selectionInfo.left,
},
bottom: {
_unit: 'pixelsUnit',
_value: selectionInfo.bottom,
},
right: {
_unit: 'pixelsUnit',
_value: selectionInfo.right,
},
},
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
}
async function reSelectMarqueeExe(selectionInfo) {
try {
if (isSelectionValid(selectionInfo)) {
//only try to reactivate the selection area if it is valid
await executeAsModal(async () => {
await reSelectMarqueeCommand(selectionInfo)
})
}
} catch (e) {
console.warn(e)
}
}
async function snapshot_layer() {
let psAction = require('photoshop').action
// const ids = (await app.activeDocument.activeLayers).map(layer => layer.id)
const ids = await app.activeDocument.layers.map((layer) => layer.id)
let command = [
// Select All Layers current layer
{
_obj: 'selectAllLayers',
_target: [
{ _enum: 'ordinal', _ref: 'layer', _value: 'targetEnum' },
],
},
// Duplicate current layer
// {"ID":[459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513],"_obj":"duplicate","_target":[{"_enum":"ordinal","_ref":"layer","_value":"targetEnum"}],"version":5},
{
ID: ids,
_obj: 'duplicate',
_target: [
{ _enum: 'ordinal', _ref: 'layer', _value: 'targetEnum' },
],
// version: 5
},
// Merge Layers
{ _obj: 'mergeLayersNew' },
// Make
{
_obj: 'make',
at: { _enum: 'channel', _ref: 'channel', _value: 'mask' },
new: { _class: 'channel' },
using: { _enum: 'userMaskEnabled', _value: 'revealSelection' },
},
// Set Selection
{
_obj: 'set',
_target: [{ _property: 'selection', _ref: 'channel' }],
to: { _enum: 'ordinal', _ref: 'channel', _value: 'targetEnum' },
},
]
const result = await psAction.batchPlay(command, {
synchronousExecution: true,
modalBehavior: 'execute',
})
console.log('snapshot_layer: result: ', result)
return result
}
async function snapshot_layer_new() {
//similar to snapshot_layer() but fixes the problem with smart effects
let psAction = require('photoshop').action
const ids = await app.activeDocument.layers.map((layer) => layer.id)
const selection_info = await getSelectionInfoExe()
let create_snapshot_layer_command = [
// Select All Layers current layer
{
_obj: 'selectAllLayers',
_target: [
{ _enum: 'ordinal', _ref: 'layer', _value: 'targetEnum' },
],
},
// Duplicate current layer
{
ID: ids,
_obj: 'duplicate',
_target: [
{ _enum: 'ordinal', _ref: 'layer', _value: 'targetEnum' },
],
// version: 5
},
// Merge Layers
{ _obj: 'mergeLayersNew' },
]
const result = await psAction.batchPlay(create_snapshot_layer_command, {
synchronousExecution: true,
modalBehavior: 'execute',
})
await reSelectMarqueeExe(selection_info) //reselect the selection area for the mask
//make a mask of the selection area
const make_mask_command = [
// Make
{
_obj: 'make',
at: { _enum: 'channel', _ref: 'channel', _value: 'mask' },
new: { _class: 'channel' },
using: { _enum: 'userMaskEnabled', _value: 'revealSelection' },
},
// // Set Selection
// {
// _obj: 'set',
// _target: [{ _property: 'selection', _ref: 'channel' }],
// to: { _enum: 'ordinal', _ref: 'channel', _value: 'targetEnum' },
// },
]
const result_2 = await psAction.batchPlay(make_mask_command, {
synchronousExecution: true,
modalBehavior: 'execute',
})
await reSelectMarqueeExe(selection_info) //reselect the selection area again so we don't break other functionality
console.log('snapshot_layer: result: ', result)
return result
}
async function snapshot_layerExe() {
try {
await executeAsModal(
async () => {
await snapshot_layer_new()
},
{
commandName: `Canvas Snapshot...`,
}
)
} catch (e) {
console.error(e)
}
}
async function snapshot_layer_no_slide() {
let psAction = require('photoshop').action
// const ids = (await app.activeDocument.activeLayers).map(layer => layer.id)
const ids = await app.activeDocument.layers.map((layer) => layer.id)
let command = [
// Select All Layers current layer
{
_obj: 'selectAllLayers',
_target: [
{ _enum: 'ordinal', _ref: 'layer', _value: 'targetEnum' },
],
},
// Duplicate current layer
// {"ID":[459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513],"_obj":"duplicate","_target":[{"_enum":"ordinal","_ref":"layer","_value":"targetEnum"}],"version":5},
{
ID: ids,
_obj: 'duplicate',
_target: [
{ _enum: 'ordinal', _ref: 'layer', _value: 'targetEnum' },
],
// version: 5
},
// // Merge Layers
// { _obj: 'mergeLayersNew' },
// // Make
// {
// _obj: 'make',
// at: { _enum: 'channel', _ref: 'channel', _value: 'mask' },
// new: { _class: 'channel' },
// using: { _enum: 'userMaskEnabled', _value: 'revealSelection' },
// },
// // Set Selection
// {
// _obj: 'set',
// _target: [{ _property: 'selection', _ref: 'channel' }],
// to: { _enum: 'ordinal', _ref: 'channel', _value: 'targetEnum' },
// },
]
const result = await psAction.batchPlay(command, {
synchronousExecution: true,
modalBehavior: 'execute',
})
console.log('snapshot_layer: result: ', result)
return result
}
async function snapshot_layer_no_slide_Exe() {
try {
await executeAsModal(
async () => {
await snapshot_layer_no_slide()
},
{
commandName: 'Action Commands',
}
)
} catch (e) {
console.error(e)
}
}
// await runModalFunction();
async function fillAndGroup() {
let result
let psAction = require('photoshop').action
// let newCommand =[
// // snapshotLayer
// // makeGroupCommand
// // Make fill layer
// {"_obj":"make","_target":[{"_ref":"contentLayer"}],"using":{"_obj":"contentLayer","type":{"_obj":"solidColorLayer","color":{"_obj":"RGBColor","blue":255.0,"grain":255.0,"red":255.0}}}},
// ]
let command = [
// Make fill layer
{
_obj: 'make',
_target: [{ _ref: 'contentLayer' }],
using: {
_obj: 'contentLayer',
type: {
_obj: 'solidColorLayer',
color: {
_obj: 'RGBColor',
blue: 255.0,
grain: 255.0,
red: 255.0,
},
},
},
},
// Move current layer
// {"_obj":"move","_target":[{"_enum":"ordinal","_ref":"layer","_value":"targetEnum"}],"adjustment":false,"layerID":[17],"to":{"_index":7,"_ref":"layer"},"version":5},
// Select layer “Layer 4 copy”
// {"_obj":"select","_target":[{"_name":"Layer 4 copy","_ref":"layer"}],"layerID":[17,15],"makeVisible":false,"selectionModifier":{"_enum":"selectionModifierType","_value":"addToSelectionContinuous"}},
// Make Group
// {"_obj":"make","_target":[{"_ref":"layerSection"}],"from":{"_enum":"ordinal","_ref":"layer","_value":"targetEnum"},"layerSectionEnd":19,"layerSectionStart":18,"name":"Group 1"}
]
const snapshotLayer = await app.activeDocument.activeLayers[0]
await makeGroupCommand()
const groupLayer = app.activeDocument.activeLayers[0]
result = await psAction.batchPlay(command, {})
const fillLayer = app.activeDocument.activeLayers[0]
snapshotLayer.moveAbove(fillLayer)
// await app.activeDocument.activeLayers[0].moveAbove()
// const layerIDs = []
// const to_index = await getIndexCommand(groupLayer.id)
// await moveToGroupCommand(to_index, layerIDs)
}
async function fillAndGroupExe() {
await require('photoshop').core.executeAsModal(fillAndGroup, {
commandName: 'Action Commands',
})
}
async function fastSnapshot() {
await snapshot_layerExe()
await fillAndGroupExe()
}
function layerToFileName(layer, session_id) {
file_name = `${layer.name}_${layer.id}_${session_id}`
return file_name
}
function layerNameToFileName(layer_name, layer_id, session_id) {
file_name = `${layer_name}_${layer_id}_${session_id}`
return file_name
}
async function cleanLayers(layers) {
// g_init_image_related_layers = {}
// g_mask_related_layers = {}
console.log('cleanLayers() -> layers:', layers)
for (layer of layers) {
try {
if (layer_util.Layer.doesLayerExist(layer)) {
await executeAsModal(async () => {
await layer.delete()
})
}
} catch (e) {
console.warn(
'warning attempting to a delete layer,layer.name: ',
layer.name,
layer,
e
)
continue
}
}
return []
}
// async function cleanLayersOutpaint(layers){
// //delete group mask layer
// //delete mask layer
// //delete group init image layer
// //delete init image layer (snapshot layer)
// //delete fill layer
// for (layer of layers){
// try {
// await executeAsModal(async ()=>{await layer.delete()})}
// catch(e){
// console.warn("warning attempting to a delete layer: ",e)
// }
// }
// return []
// }
// async function cleanLayersInpaint(layers){
// //delete group mask layer
// //delete white mask layer
// //delete the black fill layer
// //delete init image layer (snapshot layer)
// for (layer of layers){
// await executeAsModal(async ()=>{await layer.delete()})
// }
// return []
// }
async function createClippingMaskExe() {
const batchPlay = require('photoshop').action.batchPlay
async function createClippingMaskCommand() {
const result = await batchPlay(
[
{
_obj: 'make',
new: {
_class: 'channel',
},
at: {
_ref: 'channel',
_enum: 'channel',
_value: 'mask',
},
using: {
_enum: 'userMaskEnabled',
_value: 'revealSelection',
},
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
}
await executeAsModal(async () => {
await createClippingMaskCommand()
})
}
//REFACTOR: delete and replace with getSelectionInfoExe()
async function checkIfSelectionAreaIsActive() {
try {
let isSelectionAreaValid = await getSelectionInfoExe()
return isSelectionAreaValid
} catch (e) {
console.warn(e)
}
}
async function saveUniqueDocumentIdExe(new_id) {
const batchPlay = require('photoshop').action.batchPlay
async function saveUniqueDocumentIdCommand() {
const batchPlay = require('photoshop').action.batchPlay
const result = await batchPlay(
[
{
_obj: 'set',
_target: [
{
_ref: 'property',
_property: 'fileInfo',
},
{
_ref: 'document',
_enum: 'ordinal',
_value: 'targetEnum',
},
],
to: {
_obj: 'fileInfo',
caption: new_id,
keywords: [new_id],
},
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
}
await executeAsModal(async () => {
await saveUniqueDocumentIdCommand()
})
}
async function readUniqueDocumentIdExe() {
const batchPlay = require('photoshop').action.batchPlay
async function readUniqueDocumentIdCommand() {
const batchPlay = require('photoshop').action.batchPlay
const result = await batchPlay(
[
{
_obj: 'get',
_target: [
{
_ref: 'property',
_property: 'fileInfo',
},
{
_ref: 'document',
_enum: 'ordinal',
_value: 'targetEnum',
},
],
// to: {
// _obj: 'fileInfo',
// caption: new_id,
// keywords: [new_id]
// },
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
console.log('readUniqueDocumentIdCommand: result ', result)
return result
}
let uniqueDocumentId = ''
try {
await executeAsModal(async () => {
uniqueDocumentId = (await readUniqueDocumentIdCommand())[0].fileInfo
.caption
if (typeof uniqueDocumentId === 'string') {
uniqueDocumentId = uniqueDocumentId.trim()
}
})
} catch (e) {
console.warn('readUniqueDocumentIdExe: ', e)
uniqueDocumentId = ''
}
return uniqueDocumentId
}
const readPng = async (image_name) => {
//it will save the document then read it. store it in memory
// image_name = 'test.png'
try {
let img_buffer
await executeAsModal(
async (control) => {
const folder = await fs.getTemporaryFolder()
// const pluginFolder = await fs.getPluginFolder()
// let init_images_dir = await pluginFolder.getEntry(
// './server/python_server/init_images'
// )
const file = await folder.createFile(image_name, {
overwrite: true,
})
const currentDocument = app.activeDocument
await currentDocument.saveAs.png(
file,
// {
// compression: 6,
// },
null,
true
)
const arrayBuffer = await file.read({ format: formats.binary })
// console.log(arrayBuffer, 'arrayBuffer') ;
img_buffer = arrayBuffer
},
{ commandName: 'readPng' }
)
return img_buffer
} catch (e) {
console.warn(e)
}
}
async function selectCanvasCommand() {
// const result = await batchPlay(
// [
// {
// _obj: "historyStateChanged",
// documentID: 1044,
// ID: 1058,
// name: "Select Canvas",
// hasEnglish: true,
// itemIndex: 7,
// commandID: 1017,
// _options: {
// dialogOptions: "dontDisplay"
// }
// }
// ],{
// synchronousExecution: true,
// modalBehavior: "execute"
// });
const result = await batchPlay(
[
{
_obj: 'set',
_target: [
{
_ref: 'channel',
_property: 'selection',
},
],
to: {
_enum: 'ordinal',
_value: 'allEnum',
},
_options: {
dialogOptions: 'dontDisplay',
},
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
return result
}
async function selectCanvasExe() {
await executeAsModal(
async () => {
await selectCanvasCommand()
},
{ commandName: 'selectCanvasExe' }
)
}
async function newExportPng(layer, image_name, width, height) {
//store layers we want to export in variables
// let layerToExports =
// create new document
// duplicate the layers to the new documnet
//select the layer channel selectLayerChannelCommand
//document.crop
//export using readPng()
try {
//get the active layers
// const layersToExport = app.activeDocument.activeLayers
//create new document
// let exportDoc = await executeAsModal(app.documents.add)
let exportDoc
const makeDoc = async () => {
let exportDoc = await app.documents.add({
width: width,
height: height,
resolution: await app.activeDocument.resolution,
mode: 'RGBColorMode',
fill: 'transparent',
})
}
let image_buffer
await executeAsModal(
async () => {
// await app.documents.add()
await makeDoc()
exportDoc = app.activeDocument
console.log('exportDoc.id: ', exportDoc.id)
// for (layer of layersToExport) {
console.log(layer.id)
const dupLayer = await layer.duplicate(exportDoc)
await selectLayers([dupLayer])
// await selectLayerChannelCommand()
await selectCanvasExe()
const canvas_selection_info = await getSelectionInfoExe()
await layerToSelection(canvas_selection_info)
// const selection_info = await getSelectionInfoExe()
// await exportDoc.crop(selection_info)
// export_image_name = `${layer.name}.png`
image_buffer = await readPng(image_name)
await exportDoc.closeWithoutSaving()
},
{ commandName: 'NewExportPng' }
)
return image_buffer
// }
} catch (e) {
console.error(`newExportPng error: ,${e}`)
}
}
async function tempExportPng(layer, image_name, width, height) {
//store layers we want to export in variables
// let layerToExports =
// create new document
// duplicate the layers to the new documnet
//select the layer channel selectLayerChannelCommand
//document.crop
//export using readPng()
try {
//get the active layers
// const layersToExport = app.activeDocument.activeLayers
//create new document
// let exportDoc = await executeAsModal(app.documents.add)
let exportDoc
const makeDoc = async () => {
let exportDoc = await app.documents.add({
width: width,
height: height,
resolution: await app.activeDocument.resolution,
mode: 'RGBColorMode',
fill: 'transparent',
})
}
await executeAsModal(
async () => {
// await app.documents.add()
await makeDoc()
exportDoc = app.activeDocument
console.log('exportDoc.id: ', exportDoc.id)
// for (layer of layersToExport) {
console.log(layer.id)
const dupLayer = await layer.duplicate(exportDoc)
await selectLayers([dupLayer])
// await selectLayerChannelCommand()
await selectCanvasExe()
const canvas_selection_info = await getSelectionInfoExe()
await layerToSelection(canvas_selection_info)
// const selection_info = await getSelectionInfoExe()
// await exportDoc.crop(selection_info)
// export_image_name = `${layer.name}.png`
await readPng(image_name)
await exportDoc.closeWithoutSaving()
},
{ commandName: 'tempExportPng' }
)
// }
} catch (e) {
console.error(`newExportPng error: ,${e}`)
}
}
async function mergeVisibleCommand() {
const result = await batchPlay(
[
{
_obj: 'mergeVisible',
duplicate: true,
_isCommand: true,
// "_options": {
// // "dialogOptions": "dontDisplay"
// }
},
],
{
synchronousExecution: true,
modalBehavior: 'execute',
}
)
return result
}
async function mergeVisibleExe() {
await executeAsModal(async () => {
await mergeVisibleCommand()
})
}
async function layerToSelection(selection_info) {
//store active layer for later
try {
//Store selection info
//unSelect
//move layer
//scale layer
//Select from selection info
// let selection_info = await getSelectionInfo()
console.log('selection_info:', selection_info)
console.log('unSelect')
function getLayerSize(layer) {
console.log('layer.bounds:')
console.dir(layer.bounds)
const bounds = layer.bounds
const height = bounds.bottom - bounds.top
const width = bounds.right - bounds.left
return {
height: height,
width: width,
left: bounds.left,
right: bounds.right,
top: bounds.top,
bottom: bounds.bottom,
}
}
//scale layer
async function scaleLayer(layer, selection_info) {
console.log('scaleLayer got called')
// const activeLayer = getActiveLayer()
// const activeLayer = await app.activeDocument.activeLayers[0]
let layer_info = getLayerSize(layer)
scale_x_ratio = (selection_info.width / layer_info.width) * 100
scale_y_ratio = (selection_info.height / layer_info.height) * 100
console.log('scale_x_y_ratio:', scale_x_ratio, scale_y_ratio)
// await layer.scale(
// scale_x_ratio,
// scale_y_ratio,
// constants.ResampleMethod.BILINEAR
// )
await layer_util.Layer.resizeImageExe(scale_x_ratio, scale_y_ratio)
}
async function moveLayerExe(layerToMove, selection_info) {
let layer_info = getLayerSize(layerToMove)
top_dist = layer_info.top - selection_info.top
left_dist = layer_info.left - selection_info.left
await layerToMove.translate(-left_dist, -top_dist)
}
// const activeLayer = await getActiveLayer()
//store all active layers
const activeLayers = await app.activeDocument.activeLayers
await unSelectMarqueeExe()
// await executeAsModal(unSelect, {'commandName': 'unSelect'})
// await executeAsModal(scaleLayer, {'commandName': 'scaleLayer'})
await executeAsModal(
async () => {
for (let layer of activeLayers) {
await selectLayers([layer]) // make sure only one layer is selected
await scaleLayer(layer, selection_info) //scale to selection size
await moveLayerExe(layer, selection_info) //move to selection
}
},
{ commandName: 'moveLayerExe' }
)
// await reselect(selection_info)
} catch (e) {
console.warn(e)
}
}
function executeCommandExe(commandFunc) {
try {
;(async () => {
await executeAsModal(async () => {
await commandFunc()
})
})()
} catch (e) {
console.warn(e)
}
}
async function executeDescExe(Desc) {
try {
await executeAsModal(async () => {
const result = await batchPlay([Desc], {
synchronousExecution: true,
modalBehavior: 'execute',
})
console.log(result)
})
} catch (e) {
console.warn(e)
}
}
module.exports = {
createSolidLayer,
createEmptyGroup,
getLayerIndex,
collapseGroup,
moveToGroupCommand,
MoveToGroupExe,
selectLayers,
selectLayersExe,
unselectActiveLayers,
unselectActiveLayersExe,
createMaskExe,
getSelectionInfoExe,
unSelectMarqueeCommand,
unSelectMarqueeExe,
reSelectMarqueeExe,
selectLayerChannelCommand,
snapshot_layer,
snapshot_layerExe,
fillAndGroupExe,
fastSnapshot,
layerToFileName,
layerNameToFileName,
cleanLayers,
createClippingMaskExe,
checkIfSelectionAreaIsActive,
selectMarqueeRectangularToolExe,
promptForMarqueeTool,
saveUniqueDocumentIdExe,
readUniqueDocumentIdExe,
newExportPng,
mergeVisibleExe,
selectCanvasExe,
layerToSelection,
isSelectionValid,
snapshot_layer_no_slide_Exe,
setVisibleExe,
executeCommandExe,
executeDescExe,
getSelectionInfoCommand,
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wentijieda/Auto-Photoshop-StableDiffusion-Plugin.git
[email protected]:wentijieda/Auto-Photoshop-StableDiffusion-Plugin.git
wentijieda
Auto-Photoshop-StableDiffusion-Plugin
Auto-Photoshop-StableDiffusion-Plugin
master

搜索帮助