diff --git a/outpaint.js b/outpaint.js index 5556a29..c9d59e8 100644 --- a/outpaint.js +++ b/outpaint.js @@ -204,8 +204,8 @@ async function snapAndFillExe(session_id) { layer.visible = false } await psapi.reSelectMarqueeExe(selectionInfo) - const util_layer = require('./utility/layer') - await util_layer.collapseFolderExe([snapshotGroup], false) + const layer_util = require('./utility/layer') + await layer_util.collapseFolderExe([snapshotGroup], false) }) console.log('snapAndFillLayers: ', snapAndFillLayers) return snapAndFillLayers @@ -321,8 +321,8 @@ async function outpaintFasterExe(session_id) { } //collapse the folders - const util_layer = require('./utility/layer') - await util_layer.collapseFolderExe( + const layer_util = require('./utility/layer') + await layer_util.collapseFolderExe( [snapshotGroup, snapshotMaskGroup], false ) @@ -456,8 +456,8 @@ async function outpaintExe(session_id) { } //collapse the folders - const util_layer = require('./utility/layer') - await util_layer.collapseFolderExe( + const layer_util = require('./utility/layer') + await layer_util.collapseFolderExe( [snapshotGroup, snapshotMaskGroup], false ) @@ -614,9 +614,9 @@ async function inpaintFasterExe(session_id) { for (layer of inpaintLayers) { layer.visible = false } - const util_layer = require('./utility/layer') + const layer_util = require('./utility/layer') - await util_layer.collapseFolderExe( + await layer_util.collapseFolderExe( [snapshotGroup, maskGroup], false ) diff --git a/utility/io.js b/utility/io.js index 313e442..80cbdba 100644 --- a/utility/io.js +++ b/utility/io.js @@ -1,5 +1,6 @@ const batchPlay = require('photoshop').action.batchPlay const psapi = require('../psapi') +const layer_util = require('../utility/layer') async function snapShotLayer() { //snapshot layer with no mask let command = [ @@ -110,32 +111,44 @@ async function snapShotLayerExe() { } class IO { - constructor() { - this.io_helper = new IOHelper() + // constructor() {} + static async exportWebp(layer) { + await executeAsModal(async () => { + //we assume we have a valid layer rectangular image/layer, no transparency + const doc_entry = await getCurrentDocFolder() //get the main document folder before we switch doc + const layer_info = await layer_util.Layer.getLayerInfo(layer) + //*) create a new document + const new_doc = await IOHelper.createDocumentExe( + layer_info.width, + layer_info.height + ) + const new_layer = await layer_util.Layer.duplicateToDoc( + layer, + new_doc + ) + //*) resize the layer to the same dimension as the document + + await layer_util.Layer.moveTo(new_layer, 0, 0) //move to the top left corner + // + await IOHelper.saveAsWebpExe(doc_entry) //save current document as .webp file, save it into doc_entry folder + }) } - async exportWebp() { - //*) snapshot the current visible layers of the document - //*) - //create a new document - // - await this.io_helper.saveAsWebpExe() //save current document as .webp file - } - async exportPng() {} - async exportDoc() {} - async exportLayer() {} + static async exportPng() {} + static async exportDoc() {} + static async exportLayer() {} } class IOHelper { - constructor() {} - - async saveAsWebp() { + static async saveAsWebp(doc_entry) { + //doc_entry must be in dataFolder or tempFolder + //save document as webp const document_id = app.activeDocument.id - doc_entery = await getCurrentDocFolder() - file_entery = await doc_entery.createFile('temp.webp', { + // doc_entry = await getCurrentDocFolder() + const file_entry = await doc_entry.createFile('temp.webp', { overwrite: true, }) - const token = await fs.createSessionToken(file_entery) + const token = await fs.createSessionToken(file_entry) const result = await batchPlay( [ { @@ -175,11 +188,28 @@ class IOHelper { return result } - async saveAsWebpExe() { + static async saveAsWebpExe(doc_entry) { await executeAsModal(async () => { - await saveAsWebp() + await this.saveAsWebp(doc_entry) }) } + static async createDocumentExe(width, height) { + let new_doc + try { + await executeAsModal(async () => { + new_doc = await app.documents.add({ + width: width, + height: height, + resolution: await app.activeDocument.resolution, + mode: 'RGBColorMode', + fill: 'transparent', + }) + }) + } catch (e) { + console.warn(e) + } + return new_doc + } } module.exports = { diff --git a/utility/layer.js b/utility/layer.js index e26d384..385eed2 100644 --- a/utility/layer.js +++ b/utility/layer.js @@ -139,6 +139,7 @@ class Layer { const left_dist = layer_info.left - to_x console.log('-left_dist, -top_dist', -left_dist, -top_dist) await layer.translate(-left_dist, -top_dist) + // await reSelectMarqueeExe(selection_info) } catch (e) { console.warn(e) @@ -150,6 +151,11 @@ class Layer { } static resizeTo() {} static fitSelection() {} + static async duplicateToDoc(layer, to_doc) { + const dupLayer = await layer.duplicate(to_doc) + // await selectLayers([dupLayer]) + return dupLayer + } static {} } module.exports = { diff --git a/utility/session.js b/utility/session.js index efc0b93..6fa1c6a 100644 --- a/utility/session.js +++ b/utility/session.js @@ -95,7 +95,7 @@ class GenerationSession { this.isFirstGeneration = true // only before the first generation is requested should this be true // const is_visible = await this.outputGroup.visible - await util_layer.collapseFolderExe([this.outputGroup], false) // close the folder group + await layer_util.collapseFolderExe([this.outputGroup], false) // close the folder group // this.outputGroup.visible = is_visible if ( @@ -105,7 +105,7 @@ class GenerationSession { //create "Mask -- Paint White to Mask -- temporary" layer if current session was inpiant and the selected session is inpaint // the current inpaint session ended on inpaint g_b_mask_layer_exist = false - await util_layer.deleteLayers([g_inpaint_mask_layer]) + await layer_util.deleteLayers([g_inpaint_mask_layer]) await createTempInpaintMaskLayer() } } catch (e) { @@ -118,7 +118,7 @@ class GenerationSession { if (this.prevOutputGroup) { // const is_visible = await this.prevOutputGroup.visible - await util_layer.collapseFolderExe( + await layer_util.collapseFolderExe( [this.prevOutputGroup], false ) // close the folder group