Line data Source code
1 : import { t } from '@/i18n'
2 : import type { IContextMenuValue } from '@/lib/litegraph/src/types/contextMenu'
3 : import { useToast } from '@/components/ui/toast/toastStore'
4 : import type Load3d from '@comfyorg/viewport3d/load3d/Load3d'
5 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
6 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
7 : import { useLoad3dService } from '@/extensions/core/load3d/services/load3dService'
8 :
9 24 : const EXPORT_FORMATS = [
10 : { label: 'GLB', value: 'glb' },
11 : { label: 'OBJ', value: 'obj' },
12 : { label: 'STL', value: 'stl' },
13 : { label: 'FBX', value: 'fbx' }
14 : ] as const
15 :
16 : /**
17 : * Creates export menu items for a 3D node using the new extension API.
18 : * Returns an array of context menu items including a separator and export submenu.
19 : */
20 0 : export function createExportMenuItems(
21 : load3d: Load3d
22 : ): (IContextMenuValue | null)[] {
23 0 : return [
24 : null, // Separator
25 : {
26 : content: 'Save',
27 : has_submenu: true,
28 : callback: (_value, _options, event, prev_menu) => {
29 0 : const submenuOptions: IContextMenuValue[] = EXPORT_FORMATS.map(
30 : (format) => ({
31 : content: format.label,
32 : callback: () => {
33 0 : void (async () => {
34 0 : try {
35 0 : await load3d.exportModel(format.value)
36 0 : useToast().success(
37 : t('toastMessages.exportSuccess', {
38 : format: format.label
39 : })
40 : )
41 : } catch (error) {
42 0 : console.error('Export failed:', error)
43 0 : useToast().warning(
44 : t('toastMessages.failedToExportModel', {
45 : format: format.label
46 : })
47 : )
48 : }
49 : })()
50 : }
51 : })
52 : )
53 :
54 0 : new LiteGraph.ContextMenu(submenuOptions, {
55 : event,
56 : parentMenu: prev_menu
57 : })
58 : }
59 : }
60 : ]
61 : }
62 :
63 : /**
64 : * Export menu items for the node's 3D viewer.
65 : * Returns none when the node has no viewer or shows a splat model.
66 : */
67 12 : export function createNodeExportMenuItems(
68 : node: LGraphNode
69 : ): (IContextMenuValue | null)[] {
70 12 : const load3d = useLoad3dService().getLoad3d(node)
71 12 : if (!load3d || load3d.isSplatModel()) return []
72 :
73 0 : return createExportMenuItems(load3d)
74 : }
|