Line data Source code
1 : import { z } from 'zod'
2 :
3 : import type {
4 : CameraState,
5 : LoadFolder,
6 : Model3DInfo
7 : } from '@comfyorg/viewport3d/load3d/interfaces'
8 :
9 : export interface Model3DOutput {
10 : filePath: string
11 : folder?: LoadFolder
12 : cameraState?: CameraState
13 : modelTransform?: Model3DInfo[number]
14 : }
15 :
16 24 : const zVector3 = z.object({ x: z.number(), y: z.number(), z: z.number() })
17 :
18 24 : const zQuaternion = zVector3.extend({ w: z.number() })
19 :
20 24 : const zCameraShape = z.object({
21 : position: zVector3,
22 : target: zVector3,
23 : zoom: z.number(),
24 : cameraType: z.enum(['perspective', 'orthographic']),
25 : quaternion: zQuaternion.optional(),
26 : useCustomUp: z.boolean().optional(),
27 : customUp: zVector3.optional(),
28 : fov: z.number().optional(),
29 : aspect: z.number().optional(),
30 : near: z.number().optional(),
31 : far: z.number().optional(),
32 : frustum: z
33 : .object({
34 : left: z.number(),
35 : right: z.number(),
36 : top: z.number(),
37 : bottom: z.number()
38 : })
39 : .optional()
40 : })
41 :
42 24 : const zCameraState = z.custom<CameraState>(
43 : (value) => zCameraShape.safeParse(value).success
44 : )
45 :
46 24 : const zModelTransform = z.object({
47 : position: zVector3,
48 : quaternion: zQuaternion,
49 : scale: zVector3
50 : })
51 :
52 24 : const zLoadFolder = z.enum(['output', 'temp'])
53 :
54 24 : const zModel3DItem = z.object({
55 : filename: z.string().min(1),
56 : subfolder: z.string().optional().catch(undefined),
57 : type: zLoadFolder.optional().catch(undefined)
58 : })
59 :
60 24 : const zOptionalList = z.array(z.unknown()).optional().catch(undefined)
61 :
62 24 : const zModel3DNodeOutput = z.object({
63 : '3d': zOptionalList,
64 : camera_info: zOptionalList,
65 : model_3d_info: zOptionalList,
66 : result: zOptionalList
67 : })
68 :
69 0 : function parseOptional<T>(schema: z.ZodType<T>, value: unknown): T | undefined {
70 0 : const parsed = schema.safeParse(value)
71 0 : return parsed.success ? parsed.data : undefined
72 : }
73 :
74 0 : export function readModel3DOutput(output: unknown): Model3DOutput | null {
75 0 : const parsed = zModel3DNodeOutput.safeParse(output)
76 0 : if (!parsed.success) return null
77 0 : const { '3d': items, camera_info, model_3d_info, result } = parsed.data
78 :
79 0 : const parsedItem = zModel3DItem.safeParse(items?.[0])
80 0 : if (parsedItem.success) {
81 0 : const item = parsedItem.data
82 0 : return {
83 : filePath: item.subfolder
84 : ? `${item.subfolder}/${item.filename}`
85 : : item.filename,
86 : folder: item.type,
87 : cameraState: parseOptional(zCameraState, camera_info?.[0]),
88 : modelTransform: parseOptional(zModelTransform, model_3d_info?.[0])
89 : }
90 : }
91 :
92 0 : const [filePath, cameraInfo, modelInfo] = result ?? []
93 0 : if (typeof filePath !== 'string' || !filePath) return null
94 0 : return {
95 : filePath,
96 : cameraState: parseOptional(zCameraState, cameraInfo),
97 : modelTransform: parseOptional(
98 : zModelTransform,
99 : Array.isArray(modelInfo) ? modelInfo[0] : undefined
100 : )
101 : }
102 : }
|