Line data Source code
1 : import { nextTick } from 'vue'
2 :
3 : import {
4 : nodeToLoad3dMap,
5 : useLoad3d
6 : } from '@/extensions/core/load3d/composables/useLoad3d'
7 : import { createExportMenuItems } from '@/extensions/core/load3d/exportMenuHelper'
8 : import type { CameraConfig } from '@comfyorg/viewport3d/load3d/interfaces'
9 : import Load3DConfiguration from '@/extensions/core/load3d/Load3DConfiguration'
10 : import { findNumericWidget } from '@/extensions/core/load3d/findNumericWidget'
11 : import { snapshotLoad3dState } from '@/extensions/core/load3d/load3dSerialize'
12 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
13 : import type { IContextMenuValue } from '@/lib/litegraph/src/types/contextMenu'
14 : import { useLoad3dService } from '@/extensions/core/load3d/services/load3dService'
15 : import type { ComfyExtension } from '@/types/comfy'
16 :
17 24 : export const load3dAdvancedExtension: ComfyExtension = {
18 : name: 'Comfy.Load3DAdvanced',
19 :
20 : getNodeMenuItems(node: LGraphNode): (IContextMenuValue | null)[] {
21 12 : if (node.constructor.comfyClass !== 'Load3DAdvanced') return []
22 :
23 0 : const load3d = useLoad3dService().getLoad3d(node)
24 0 : if (!load3d) return []
25 :
26 0 : return createExportMenuItems(load3d)
27 : },
28 :
29 : async nodeCreated(node: LGraphNode) {
30 26 : if (node.constructor.comfyClass !== 'Load3DAdvanced') return
31 :
32 0 : const [oldWidth, oldHeight] = node.size
33 0 : node.setSize([Math.max(oldWidth, 300), Math.max(oldHeight, 600)])
34 :
35 0 : await nextTick()
36 :
37 0 : useLoad3d(node).onLoad3dReady((load3d) => {
38 0 : const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
39 0 : const width = findNumericWidget(node.widgets, 'width')
40 0 : const height = findNumericWidget(node.widgets, 'height')
41 0 : if (!modelWidget || !width || !height) return
42 :
43 : const cameraConfig = node.properties['Camera Config'] as
44 : | CameraConfig
45 : | undefined
46 0 : const cameraState = cameraConfig?.state
47 :
48 : const config = new Load3DConfiguration(load3d, node.properties)
49 0 : config.configure({
50 : loadFolder: 'input',
51 : modelWidget,
52 : cameraState,
53 : width,
54 : height
55 : })
56 : })
57 :
58 0 : useLoad3d(node).waitForLoad3d(() => {
59 0 : const sceneWidget = node.widgets?.find((w) => w.name === 'viewport_state')
60 0 : if (!sceneWidget) return
61 :
62 0 : sceneWidget.serializeValue = async () => {
63 0 : const currentLoad3d = nodeToLoad3dMap.get(node)
64 0 : if (!currentLoad3d) {
65 0 : console.error('No load3d instance found for node')
66 0 : return null
67 : }
68 0 : return snapshotLoad3dState(node, currentLoad3d)
69 : }
70 : })
71 : }
72 : }
|