Line data Source code
1 : import { nextTick } from 'vue'
2 :
3 : import { useLoad3d } from '@/extensions/core/load3d/composables/useLoad3d'
4 : import { createNodeExportMenuItems } from '@/extensions/core/load3d/exportMenuHelper'
5 : import Load3DConfiguration from '@/extensions/core/load3d/Load3DConfiguration'
6 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
7 : import type { IContextMenuValue } from '@/lib/litegraph/src/types/contextMenu'
8 : import type {
9 : NodeExecutionOutput,
10 : NodeOutputWith,
11 : ResultItem
12 : } from '@comfyorg/api-client/executionTypes'
13 : import type { ComfyExtension } from '@/types/comfy'
14 :
15 : type SaveMeshOutput = NodeOutputWith<{
16 : '3d'?: ResultItem[]
17 : }>
18 : import { app } from '@/scripts/app'
19 : import type { NodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
20 : import { getNodeByLocatorId } from '@/utils/graphTraversalUtil'
21 :
22 0 : function applySaveGLBOutput(node: LGraphNode, fileInfo: ResultItem): void {
23 0 : const filePath = (fileInfo.subfolder ?? '') + '/' + (fileInfo.filename ?? '')
24 0 : const loadFolder = fileInfo.type as 'input' | 'output'
25 :
26 0 : const modelWidget = node.widgets?.find((w) => w.name === 'image')
27 0 : if (!modelWidget) return
28 :
29 0 : if (
30 : modelWidget.value === filePath &&
31 : node.properties['Last Time Model File'] === filePath &&
32 : node.properties['Last Time Model Folder'] === loadFolder
33 : ) {
34 0 : return
35 : }
36 :
37 0 : modelWidget.value = filePath
38 0 : node.properties['Last Time Model File'] = filePath
39 0 : node.properties['Last Time Model Folder'] = loadFolder
40 :
41 0 : useLoad3d(node).waitForLoad3d((load3d) => {
42 : const config = new Load3DConfiguration(load3d, node.properties)
43 0 : config.configureForSaveMesh(loadFolder, filePath, {
44 : silentOnNotFound: true
45 : })
46 : })
47 : }
48 :
49 24 : export const saveMeshExtension: ComfyExtension = {
50 : name: 'Comfy.SaveGLB',
51 :
52 : onNodeOutputsUpdated(
53 : nodeOutputs: Record<NodeLocatorId, NodeExecutionOutput>
54 : ) {
55 0 : for (const [locatorId, output] of Object.entries(nodeOutputs)) {
56 0 : const fileInfo = (output as SaveMeshOutput)['3d']?.[0]
57 0 : if (!fileInfo) continue
58 :
59 0 : const node = getNodeByLocatorId(app.rootGraph, locatorId)
60 0 : if (!node || node.constructor.comfyClass !== 'SaveGLB') continue
61 :
62 0 : applySaveGLBOutput(node, fileInfo)
63 : }
64 : },
65 :
66 : getNodeMenuItems(node: LGraphNode): (IContextMenuValue | null)[] {
67 12 : if (node.constructor.comfyClass !== 'SaveGLB') return []
68 :
69 0 : return createNodeExportMenuItems(node)
70 : },
71 :
72 : async nodeCreated(node: LGraphNode) {
73 26 : if (node.constructor.comfyClass !== 'SaveGLB') return
74 :
75 0 : const [oldWidth, oldHeight] = node.size
76 :
77 0 : node.setSize([Math.max(oldWidth, 400), Math.max(oldHeight, 550)])
78 :
79 0 : await nextTick()
80 :
81 0 : useLoad3d(node).onLoad3dReady((load3d) => {
82 0 : const modelWidget = node.widgets?.find((w) => w.name === 'image')
83 0 : if (!modelWidget) return
84 :
85 0 : const lastTimeModelFile = node.properties['Last Time Model File'] as
86 : | string
87 : | undefined
88 0 : const lastTimeModelFolder =
89 : (node.properties['Last Time Model Folder'] as
90 : | 'input'
91 : | 'output'
92 : | undefined) ?? 'output'
93 :
94 0 : if (!lastTimeModelFile) return
95 :
96 0 : modelWidget.value = lastTimeModelFile
97 :
98 : const config = new Load3DConfiguration(load3d, node.properties)
99 0 : config.configureForSaveMesh(lastTimeModelFolder, lastTimeModelFile, {
100 : silentOnNotFound: true
101 : })
102 : })
103 :
104 0 : const onExecuted = node.onExecuted
105 :
106 0 : node.onExecuted = function (output: SaveMeshOutput) {
107 0 : onExecuted?.call(this, output)
108 :
109 0 : const fileInfo = output['3d']?.[0]
110 :
111 0 : if (!fileInfo) return
112 :
113 0 : useLoad3d(node).waitForLoad3d((load3d) => {
114 0 : const modelWidget = node.widgets?.find((w) => w.name === 'image')
115 :
116 0 : if (modelWidget) {
117 0 : const filePath =
118 : (fileInfo.subfolder ?? '') + '/' + (fileInfo.filename ?? '')
119 :
120 0 : modelWidget.value = filePath
121 :
122 0 : const config = new Load3DConfiguration(load3d, node.properties)
123 :
124 0 : const loadFolder = fileInfo.type as 'input' | 'output'
125 :
126 0 : node.properties['Last Time Model File'] = filePath
127 0 : node.properties['Last Time Model Folder'] = loadFolder
128 :
129 0 : config.configureForSaveMesh(loadFolder, filePath, {
130 : silentOnNotFound: true
131 : })
132 : }
133 : })
134 : }
135 : }
136 : }
|