Line data Source code
1 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
2 : import type { ComfyNodeDef } from '@comfyorg/object-info-parser'
3 : import { reportError } from '@comfyorg/telemetry/reportError'
4 : import { app } from '@/scripts/app'
5 : import { useExtensionService } from '@/platform/extensions/services/extensionService'
6 : import { useExtensionStore } from '@/platform/extensions/stores/extensionStore'
7 :
8 : import type { ComfyExtension } from '@/types/comfy'
9 :
10 : import { isThreeJsNode } from './nodeTypes'
11 : import { load3dSettings } from './load3dSettings'
12 : import { load3dWidgets } from './load3dWidgets'
13 :
14 : interface GraphNodeTypes {
15 : nodes?: readonly { type?: unknown }[]
16 : definitions?: { subgraphs?: readonly GraphNodeTypes[] }
17 : }
18 :
19 2355 : let load3dExtensionsLoaded = false
20 2355 : let load3dExtensionsLoading: Promise<ComfyExtension[] | undefined> | null = null
21 :
22 50 : function loadLoad3dExtensions(): Promise<ComfyExtension[] | undefined> {
23 50 : if (load3dExtensionsLoaded) return Promise.resolve([])
24 24 : load3dExtensionsLoading ??= importLoad3dExtensions()
25 24 : return load3dExtensionsLoading
26 : }
27 :
28 24 : async function importLoad3dExtensions(): Promise<ComfyExtension[] | undefined> {
29 24 : const disposers: (() => void)[] = []
30 24 : let extensions: ComfyExtension[]
31 24 : try {
32 24 : const [
33 : load3d,
34 : load3dAdvanced,
35 : load3dPreview,
36 : saveMesh,
37 : cameraInfo,
38 : cameraAngle,
39 : lightInfo
40 : ] = await Promise.all([
41 : import('./load3d'),
42 : import('./load3dAdvanced'),
43 : import('./load3dPreviewExtensions'),
44 : import('./saveMesh'),
45 : import('./cameraInfo'),
46 : import('./cameraAngle'),
47 : import('./lightInfo')
48 : ])
49 24 : extensions = [
50 : load3d.load3dExtension,
51 : load3d.preview3DExtension,
52 : load3d.preview3DAdvancedExtension,
53 : load3d.save3DAdvancedExtension,
54 : load3dAdvanced.load3dAdvancedExtension,
55 : load3dPreview.previewGaussianSplatExtension,
56 : load3dPreview.previewPointCloudExtension,
57 : load3dPreview.saveGaussianSplatExtension,
58 : load3dPreview.savePointCloudExtension,
59 : saveMesh.saveMeshExtension,
60 : cameraInfo.cameraInfoExtension,
61 : cameraAngle.cameraAngleExtension,
62 : lightInfo.lightInfoExtension
63 : ]
64 24 : useExtensionStore().markCoreExtensions(extensions)
65 24 : const { registerExtension } = useExtensionService()
66 24 : for (const extension of extensions)
67 312 : disposers.push(registerExtension(extension))
68 : } catch (cause) {
69 0 : disposers.forEach((dispose) => dispose())
70 0 : load3dExtensionsLoading = null
71 0 : reportError(cause, {
72 : errorType: 'error_loading_3d_extensions',
73 : surface: 'graph'
74 : })
75 0 : return undefined
76 : }
77 24 : load3dExtensionsLoaded = true
78 24 : const { isExtensionEnabled } = useExtensionStore()
79 24 : return extensions.filter((extension) => isExtensionEnabled(extension.name))
80 : }
81 :
82 4732 : function enableMeshUpload(nodeData: ComfyNodeDef) {
83 4732 : const modelFile = nodeData.input?.required?.model_file
84 4732 : if (!modelFile?.[1]) return
85 4732 : modelFile[1].mesh_upload = true
86 4732 : modelFile[1].upload_subfolder = '3d'
87 : }
88 :
89 4732 : function addPreview3dInput(nodeData: ComfyNodeDef) {
90 4732 : const input = (nodeData.input ??= {})
91 4732 : ;(input.required ??= {}).image = ['PREVIEW_3D']
92 : }
93 :
94 2366 : function addLoad3dAdvancedViewport(nodeData: ComfyNodeDef) {
95 2366 : enableMeshUpload(nodeData)
96 2366 : const required = nodeData.input?.required
97 2366 : if (required) required.viewport_state = ['LOAD_3D_ADVANCED', {}]
98 : }
99 :
100 2355 : const nodeDefPatches: Record<string, (nodeData: ComfyNodeDef) => void> = {
101 : Load3D: enableMeshUpload,
102 : Load3DAdvanced: addLoad3dAdvancedViewport,
103 : Preview3D: addPreview3dInput,
104 : SaveGLB: addPreview3dInput
105 : }
106 :
107 4243 : function containsThreeJsNode(graph: GraphNodeTypes): boolean {
108 4243 : return (
109 : (graph.nodes ?? []).some(
110 : ({ type }) => typeof type === 'string' && isThreeJsNode(type)
111 : ) || (graph.definitions?.subgraphs ?? []).some(containsThreeJsNode)
112 : )
113 : }
114 :
115 0 : async function replayNodeCreated(extension: ComfyExtension, node: LGraphNode) {
116 0 : try {
117 0 : await extension.nodeCreated?.(node, app)
118 : } catch (cause) {
119 0 : reportError(cause, {
120 : errorType: 'error_replaying_3d_node_created',
121 : surface: 'graph',
122 : context: { extension: extension.name }
123 : })
124 : }
125 : }
126 :
127 2355 : export const load3dLazyExtension: ComfyExtension = {
128 : name: 'Comfy.Load3DLazy',
129 : settings: load3dSettings,
130 :
131 : getCustomWidgets: () => load3dWidgets,
132 :
133 : beforeRegisterNodeDef(_nodeType: typeof LGraphNode, nodeData: ComfyNodeDef) {
134 2439372 : nodeDefPatches[nodeData.name]?.(nodeData)
135 : },
136 :
137 : async beforeConfigureGraph(graphData) {
138 3887 : if (containsThreeJsNode(graphData)) await loadLoad3dExtensions()
139 : },
140 :
141 : async nodeCreated(node: LGraphNode) {
142 23115 : const { comfyClass } = node.constructor
143 23115 : if (!comfyClass || !isThreeJsNode(comfyClass)) return
144 26 : const newExtensions = await loadLoad3dExtensions()
145 23115 : await Promise.all(
146 : (newExtensions ?? []).map((extension) =>
147 : replayNodeCreated(extension, node)
148 : )
149 : )
150 : }
151 : }
|