LCOV - code coverage report
Current view: top level - src/extensions/core/load3d - load3dPreviewExtensions.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 10.8 % 83 9
Test Date: 2026-11-17 04:17:53

            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 { createNodeExportMenuItems } from '@/extensions/core/load3d/exportMenuHelper'
       8              : import type {
       9              :   CameraConfig,
      10              :   LoadFolder,
      11              :   Model3DInfo
      12              : } from '@comfyorg/viewport3d/load3d/interfaces'
      13              : import type Load3d from '@comfyorg/viewport3d/load3d/Load3d'
      14              : import Load3DConfiguration from '@/extensions/core/load3d/Load3DConfiguration'
      15              : import type { Model3DOutput } from '@/extensions/core/load3d/model3dOutput'
      16              : import { readModel3DOutput } from '@/extensions/core/load3d/model3dOutput'
      17              : import { t } from '@/i18n'
      18              : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
      19              : import type { IContextMenuValue } from '@/lib/litegraph/src/types/contextMenu'
      20              : import { useToast } from '@/components/ui/toast/toastStore'
      21              : import type { NodeExecutionOutput } from '@comfyorg/api-client/executionTypes'
      22              : import { app } from '@/scripts/app'
      23              : import type { ComfyExtension } from '@/types/comfy'
      24              : import type { NodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
      25              : import { getNodeByLocatorId } from '@/utils/graphTraversalUtil'
      26              : 
      27            0 : function applyResultToLoad3d(
      28              :   node: LGraphNode,
      29              :   load3d: Load3d,
      30              :   reported: Model3DOutput,
      31              :   loadFolder: LoadFolder
      32              : ): void {
      33            0 :   const { cameraState, modelTransform } = reported
      34            0 :   const normalizedPath = reported.filePath.replaceAll('\\', '/')
      35            0 :   const folder = reported.folder ?? loadFolder
      36            0 :   node.properties['Last Time Model File'] = normalizedPath
      37            0 :   node.properties['Last Time Model Folder'] = folder
      38            0 :   if (cameraState) {
      39            0 :     const existing = node.properties['Camera Config'] as
      40              :       | CameraConfig
      41              :       | undefined
      42            0 :     node.properties['Camera Config'] = {
      43              :       cameraType: load3d.getCurrentCameraType(),
      44              :       fov: 75,
      45              :       ...existing,
      46              :       state: cameraState
      47              :     }
      48              :   }
      49              : 
      50              :   const config = new Load3DConfiguration(load3d, node.properties)
      51            0 :   config.configureForSaveMesh(folder, normalizedPath, {
      52              :     silentOnNotFound: true
      53              :   })
      54              : 
      55            0 :   const targetGeneration = load3d.currentLoadGeneration
      56            0 :   void load3d.whenLoadIdle().then(() => {
      57            0 :     if (load3d.currentLoadGeneration !== targetGeneration) return
      58            0 :     if (cameraState) load3d.setCameraState(cameraState)
      59            0 :     if (modelTransform) load3d.applyModelTransform(modelTransform)
      60            0 :     load3d.forceRender()
      61              :   })
      62              : }
      63              : 
      64           96 : function createPreview3DExtension(
      65              :   comfyClass: string,
      66              :   extensionName: string,
      67              :   loadFolder: LoadFolder
      68              : ): ComfyExtension {
      69           96 :   const applyPreviewOutput = (
      70              :     node: LGraphNode,
      71              :     reported: Model3DOutput
      72              :   ): void => {
      73            0 :     useLoad3d(node).waitForLoad3d((load3d) => {
      74            0 :       applyResultToLoad3d(node, load3d, reported, loadFolder)
      75              :     })
      76              :   }
      77              : 
      78           96 :   return {
      79              :     name: extensionName,
      80              : 
      81              :     onNodeOutputsUpdated(
      82              :       nodeOutputs: Record<NodeLocatorId, NodeExecutionOutput>
      83              :     ) {
      84            0 :       for (const [locatorId, output] of Object.entries(nodeOutputs)) {
      85            0 :         const reported = readModel3DOutput(output)
      86            0 :         if (!reported) continue
      87              : 
      88            0 :         const node = getNodeByLocatorId(app.rootGraph, locatorId)
      89            0 :         if (!node || node.constructor.comfyClass !== comfyClass) continue
      90              : 
      91            0 :         applyPreviewOutput(node, reported)
      92              :       }
      93              :     },
      94              : 
      95              :     getNodeMenuItems(node: LGraphNode): (IContextMenuValue | null)[] {
      96           48 :       if (node.constructor.comfyClass !== comfyClass) return []
      97              : 
      98            0 :       return createNodeExportMenuItems(node)
      99              :     },
     100              : 
     101              :     async nodeCreated(node: LGraphNode) {
     102          104 :       if (node.constructor.comfyClass !== comfyClass) return
     103              : 
     104            0 :       const [oldWidth, oldHeight] = node.size
     105            0 :       node.setSize([Math.max(oldWidth, 400), Math.max(oldHeight, 550)])
     106              : 
     107            0 :       await nextTick()
     108              : 
     109            0 :       const onExecuted = node.onExecuted
     110            0 :       const { onLoad3dReady, waitForLoad3d } = useLoad3d(node)
     111              : 
     112            0 :       onLoad3dReady((load3d) => {
     113            0 :         const lastTimeModelFile = node.properties['Last Time Model File']
     114            0 :         if (!lastTimeModelFile) return
     115              : 
     116            0 :         const lastTimeModelFolder = node.properties['Last Time Model Folder']
     117            0 :         const folder =
     118              :           lastTimeModelFolder === 'temp' || lastTimeModelFolder === 'output'
     119              :             ? lastTimeModelFolder
     120              :             : loadFolder
     121              : 
     122              :         const config = new Load3DConfiguration(load3d, node.properties)
     123            0 :         config.configureForSaveMesh(folder, lastTimeModelFile as string, {
     124              :           silentOnNotFound: true
     125              :         })
     126              : 
     127              :         const cameraConfig = node.properties['Camera Config'] as
     128              :           | CameraConfig
     129              :           | undefined
     130            0 :         const cameraState = cameraConfig?.state
     131            0 :         const targetGeneration = load3d.currentLoadGeneration
     132            0 :         void load3d.whenLoadIdle().then(() => {
     133            0 :           if (load3d.currentLoadGeneration !== targetGeneration) return
     134            0 :           if (cameraState) load3d.setCameraState(cameraState)
     135            0 :           load3d.forceRender()
     136              :         })
     137              :       })
     138              : 
     139            0 :       waitForLoad3d((load3d) => {
     140            0 :         const resolveLoad3d = () => nodeToLoad3dMap.get(node) ?? load3d
     141              : 
     142            0 :         const sceneWidget = node.widgets?.find(
     143              :           (w) => w.name === 'viewport_state'
     144              :         )
     145            0 :         const widthWidget = node.widgets?.find((w) => w.name === 'width')
     146            0 :         const heightWidget = node.widgets?.find((w) => w.name === 'height')
     147              : 
     148            0 :         if (widthWidget && heightWidget) {
     149            0 :           load3d.setTargetSize(
     150              :             widthWidget.value as number,
     151              :             heightWidget.value as number
     152              :           )
     153            0 :           widthWidget.callback = (value: number) => {
     154            0 :             resolveLoad3d().setTargetSize(value, heightWidget.value as number)
     155              :           }
     156            0 :           heightWidget.callback = (value: number) => {
     157            0 :             resolveLoad3d().setTargetSize(widthWidget.value as number, value)
     158              :           }
     159              :         }
     160              : 
     161            0 :         if (sceneWidget) {
     162            0 :           sceneWidget.serializeValue = async () => {
     163            0 :             const currentLoad3d = nodeToLoad3dMap.get(node)
     164            0 :             if (!currentLoad3d) {
     165            0 :               console.error('No load3d instance found for node')
     166            0 :               return null
     167              :             }
     168              : 
     169            0 :             const cameraConfig: CameraConfig = (node.properties[
     170              :               'Camera Config'
     171              :             ] as CameraConfig | undefined) || {
     172              :               cameraType: currentLoad3d.getCurrentCameraType(),
     173              :               fov: currentLoad3d.cameraManager.perspectiveCamera.fov
     174              :             }
     175            0 :             const cameraState = currentLoad3d.getCameraState()
     176            0 :             cameraConfig.state = cameraState
     177            0 :             node.properties['Camera Config'] = cameraConfig
     178              : 
     179            0 :             const modelInfo = currentLoad3d.getModelInfo()
     180              :             const model_3d_info: Model3DInfo = modelInfo ? [modelInfo] : []
     181              : 
     182            0 :             return {
     183              :               image: '',
     184              :               mask: '',
     185              :               normal: '',
     186              :               camera_info: cameraState,
     187              :               recording: '',
     188              :               model_3d_info
     189              :             }
     190              :           }
     191              :         }
     192              : 
     193            0 :         node.onExecuted = function (output: NodeExecutionOutput) {
     194            0 :           onExecuted?.call(this, output)
     195              : 
     196            0 :           const reported = readModel3DOutput(output)
     197            0 :           if (!reported) {
     198            0 :             const msg = t('toastMessages.unableToGetModelFilePath')
     199            0 :             console.error(msg)
     200            0 :             useToast().warning(msg)
     201            0 :             return
     202              :           }
     203              : 
     204            0 :           applyResultToLoad3d(node, resolveLoad3d(), reported, loadFolder)
     205              :         }
     206              :       })
     207              :     }
     208              :   }
     209              : }
     210              : 
     211           24 : export const previewGaussianSplatExtension: ComfyExtension =
     212              :   createPreview3DExtension(
     213              :     'PreviewGaussianSplat',
     214              :     'Comfy.PreviewGaussianSplat',
     215              :     'temp'
     216              :   )
     217              : 
     218           24 : export const previewPointCloudExtension: ComfyExtension =
     219              :   createPreview3DExtension(
     220              :     'PreviewPointCloud',
     221              :     'Comfy.PreviewPointCloud',
     222              :     'temp'
     223              :   )
     224              : 
     225           24 : export const saveGaussianSplatExtension: ComfyExtension =
     226              :   createPreview3DExtension(
     227              :     'SaveGaussianSplat',
     228              :     'Comfy.SaveGaussianSplat',
     229              :     'output'
     230              :   )
     231              : 
     232           24 : export const savePointCloudExtension: ComfyExtension = createPreview3DExtension(
     233              :   'SavePointCloud',
     234              :   'Comfy.SavePointCloud',
     235              :   'output'
     236              : )
        

Generated by: LCOV version 2.0-1