Skip to content

Commit dc48d48

Browse files
authored
Merge pull request #2 from AbdullahAlfaraj/master
stable diffusion
2 parents cc159ab + e3841f8 commit dc48d48

16 files changed

+2164
-259
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ With Auto-Photoshop-StableDiffusion-Plugin, you can directly use the capabilitie
2626
- [prompt shortcut:](#prompt-shortcut)
2727
- [Rule of thumbs:](#rule-of-thumbs)
2828
- [FAQ and Known Issues](#faq-and-known-issues)
29+
- [What Photoshop version do I need to run the plugin?](#what-photoshop-version-do-i-need-to-run-the-plugin)
2930
- [Plugin Load Failed](#plugin-load-failed)
3031
- [No application are connected to the service](#no-application-are-connected-to-the-service)
3132
- [Load command failed in App with ID PS and Version X.X.X](#load-command-failed-in-app-with-id-ps-and-version-xxx)
@@ -162,6 +163,8 @@ as long as you have defined the relationsip in the prompt library tab
162163
* Don't open multiple photoshop documents. For now, this will break the plugin. Only work on one project at a time.
163164

164165
# FAQ and Known Issues
166+
## What Photoshop version do I need to run the plugin?
167+
The minimum Photoshop version that the plugin supports is Photoshop v24
165168

166169
## Plugin Load Failed
167170
There are a few issues that can result in this error, please follow the instructions for the corresponding error message in the UDT logs
@@ -170,7 +173,7 @@ There are a few issues that can result in this error, please follow the instruct
170173
This error occurs when Photoshop is not started before the plugin is attempted to be loaded. Simply start photoshop then restart UXP and load the plugin
171174

172175
### Load command failed in App with ID PS and Version X.X.X
173-
This error occurs when the version of Photoshop is mistmatched with the expected version of the plugin. This may be solved by editing the [manifest.json](manifest.json) file on line 8 to match your version of Photoshop.
176+
This error occurs when the version of Photoshop is mistmatched with the expected version of the plugin. This may be solved by editing the [manifest.json](manifest.json) file on line 8 to match your version of Photoshop.
174177
```
175178
"host": [
176179
{
@@ -179,6 +182,7 @@ This error occurs when the version of Photoshop is mistmatched with the expected
179182
}
180183
],
181184
```
185+
However, if you later upgraded your Photoshop to v24 or higher. you may need to change minVersion back to ```"minVersion": "24.0.0"``` for the plugin to function correctly.
182186

183187
## Exception in ASGI application / Expecting value: line 1 column 1
184188
This error occurs due to mismatched expectations between the plugin and the Automatic1111 backend.

export_png.js

+183-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const { executeAsModal } = require('photoshop').core
2+
const storage = require('uxp').storage
3+
const fs = storage.localFileSystem
4+
const formats = require("uxp").storage.formats;
15
async function exportPngCommand () {
26
const batchPlay = require('photoshop').action.batchPlay
37
// const result = await batchPlay { _obj: “exportSelectionAsFileTypePressed”}
@@ -33,12 +37,189 @@ async function exportPng () {
3337
try {
3438
await executeAsModal(exportPngCommand)
3539
} catch (e) {
36-
console.log('exportPng error:', e)
40+
console.warn('exportPng error:', e)
3741
}
3842
}
3943

4044

45+
const readPng = async (image_name) => {
46+
// image_name = 'test.png'
47+
try {
48+
await executeAsModal(
49+
async (control) => {
50+
// const tempFolder = await fs.getTemporaryFolder() ;
51+
const pluginFolder = await fs.getPluginFolder()
52+
53+
let init_images_dir = await pluginFolder.getEntry("./server/python_server/init_images")
54+
// let init_images_dir = await pluginFolder.getEntry(
55+
// './server/python_server/init_images'
56+
// )
57+
const file = await init_images_dir.createFile(image_name, {overwrite: true}) ;
58+
59+
const currentDocument = app.activeDocument ;
60+
await currentDocument.saveAs.png(
61+
file,
62+
{
63+
compression: 6,
64+
},
65+
true
66+
) ;
67+
68+
// const arrayBuffer = await file.read({format: formats.binary}) ;
69+
// console.log(arrayBuffer, 'arrayBuffer') ;
70+
},
71+
72+
{'commandName': 'readPng'}
73+
) ;
74+
} catch(e) {
75+
console.warn(e) ;
76+
}
77+
} ;
78+
79+
const psapi = require('./psapi')
80+
async function newExportPng (layer,image_name) {
81+
//store layers we want to export in variables
82+
// let layerToExports =
83+
// create new document
84+
// duplicate the layers to the new documnet
85+
//select the layer channel selectLayerChannelCommand
86+
//document.crop
87+
//export using readPng()
88+
89+
try {
90+
//get the active layers
91+
// const layersToExport = app.activeDocument.activeLayers
92+
93+
//create new document
94+
let exportDoc = await executeAsModal(async ()=>{
95+
96+
return await app.documents.add
97+
})
98+
99+
// for (layer of layersToExport) {
100+
await executeAsModal(async () => {
101+
console.log(layer.id)
102+
const dupLayer = await layer.duplicate(exportDoc)
103+
await psapi.selectLayers([dupLayer])
104+
await psapi.selectLayerChannelCommand()
105+
const selection_info = await psapi.getSelectionInfoExe()
106+
await exportDoc.crop(selection_info)
107+
// export_image_name = `${layer.name}.png`
108+
await readPng(image_name)
109+
// await exportDoc.closeWithoutSaving()
110+
})
111+
// }
112+
} catch (e) {
113+
console.warn(e)
114+
}
115+
}
116+
117+
118+
119+
120+
121+
122+
///////////////////////Start method 3///////////////////////
123+
//My saveFolder is assigned as a global var earlier in the plugin
124+
// var saveFolder = await require("uxp").storage.localFileSystem.getFolder();
125+
// var saveFolder = await require("uxp").storage.localFileSystem.getPluginFolder();
126+
127+
// // Again, my variable here is global and assigned earlier. The value is changed during a batch loop for each file.
128+
// var saveFile = await saveFolder.createFile("fileName.png");
129+
130+
// You need to assign a token before saving.
131+
// const saveFileToken = await require("uxp").storage.localFileSystem.createSessionToken(saveFile);
132+
133+
//To save the file
134+
// await savePNG(saveFileToken);
135+
136+
/*
137+
The save function is batchPlay. This is the only way I found to control the compression type. I could get the DOM to save as PNG. However, I could never get it to control the compressions type for PNG. That may be possible with the DOM but not documented... not sure.
138+
139+
For the save function, if running UXP AP1 version 1 then use "wait" for modalBehavior. This was a workaround for a PS bug that was causing issues with "fail".
140+
141+
If running on UXP API version 2 then use "execute" for modalBahavior, or remove the options and use {} so the options go to default.
142+
*/
143+
async function savePNG(saveDataTemp){
144+
145+
const batchPlay = require("photoshop").action.batchPlay;
146+
147+
148+
async function savePNGCommand(){
149+
150+
151+
var saveFolder = await require("uxp").storage.localFileSystem.getPluginFolder();
152+
153+
// Again, my variable here is global and assigned earlier. The value is changed during a batch loop for each file.
154+
var saveFile = await saveFolder.createFile("fileName.png");
155+
156+
const saveData = await require("uxp").storage.localFileSystem.createSessionToken(saveFile);
157+
158+
const result = await batchPlay(
159+
[
160+
{
161+
"_obj": "save",
162+
"as": {
163+
"_obj": "PNGFormat",
164+
"method": {
165+
"_enum": "PNGMethod",
166+
"_value": "quick"
167+
},
168+
"PNGInterlaceType": {
169+
"_enum": "PNGInterlaceType",
170+
"_value": "PNGInterlaceNone"
171+
},
172+
"PNGFilter": {
173+
"_enum": "PNGFilter",
174+
"_value": "PNGFilterAdaptive"
175+
},
176+
"compression": 6
177+
},
178+
"in": {
179+
"_path": saveData,
180+
"_kind": "local"
181+
},
182+
"saveStage": {
183+
"_enum": "saveStageType",
184+
"_value": "saveBegin"
185+
},
186+
"_isCommand": false,
187+
"_options": {
188+
"dialogOptions": "dontDisplay"
189+
}
190+
}
191+
],{
192+
"synchronousExecution": true,
193+
"modalBehavior": "execute"
194+
});
195+
196+
}
197+
await executeAsModal(async ()=>{
198+
savePNGCommand()
199+
});
200+
}
201+
202+
203+
204+
205+
206+
207+
208+
209+
210+
211+
////////////////////End method 3////////////////////////////
212+
213+
214+
215+
216+
217+
218+
41219

42220
module.exports = {
43-
exportPng
221+
exportPng,
222+
readPng,
223+
savePNG,
224+
newExportPng
44225
};

helper.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async function getSelectionInfo () {
7070
}
7171
],
7272
{
73-
synchronousExecution: false,
73+
synchronousExecution: true,
7474
modalBehavior: 'execute'
7575
}
7676
)
@@ -92,7 +92,7 @@ async function getSelectionInfo () {
9292
// console.dir({selection_info})
9393
return selection_info
9494
} catch (e) {
95-
console.log('selection info error', e)
95+
console.warn('selection info error', e)
9696
}
9797
}
9898

@@ -135,7 +135,7 @@ async function reselectBatchPlay(selectionInfo){
135135
}
136136
],
137137
{
138-
synchronousExecution: false,
138+
synchronousExecution: true,
139139
modalBehavior: 'execute'
140140
}
141141
)
@@ -144,7 +144,7 @@ async function reselectBatchPlay(selectionInfo){
144144
async function reselect(selectionInfo){
145145
await executeAsModal(async () => {
146146
reselectBatchPlay(selectionInfo)
147-
})
147+
}, {'commandName': 'reselect'})
148148

149149
}
150150

@@ -173,7 +173,7 @@ async function unSelect () {
173173
}
174174
],
175175
{
176-
synchronousExecution: false,
176+
synchronousExecution: true,
177177
modalBehavior: 'execute'
178178
}
179179
)
@@ -236,7 +236,7 @@ async function layerToSelection () {
236236

237237
console.log('unSelect')
238238

239-
await executeAsModal(unSelect)
239+
await executeAsModal(unSelect, {'commandName': 'unSelect'})
240240

241241
//scale layer
242242
async function scaleLayer (executionContext) {
@@ -248,7 +248,7 @@ async function layerToSelection () {
248248
console.log('scale_x_y_ratio:', scale_x_ratio, scale_y_ratio)
249249
activeLayer.scale(scale_x_ratio, scale_y_ratio)
250250
}
251-
await executeAsModal(scaleLayer)
251+
await executeAsModal(scaleLayer, {'commandName': 'scaleLayer'})
252252

253253

254254
async function moveLayerExe (layerToMove, selection_info) {
@@ -261,11 +261,11 @@ async function layerToSelection () {
261261
const activeLayer = await getActiveLayer()
262262
await executeAsModal(async () => {
263263
await moveLayerExe(activeLayer, selection_info)
264-
})
264+
}, {'commandName': 'moveLayerExe'})
265265

266266
reselect(selection_info)
267267
} catch (e) {
268-
console.log(e)
268+
console.warn(e)
269269
}
270270

271271
// await executeAsModal(layerToSelectionHelper)

0 commit comments

Comments
 (0)