File size: 2,841 Bytes
6da4e64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Small ComfyUI output node for preserving generated-view camera provenance."""

from __future__ import annotations

import json
from datetime import datetime, timezone
from pathlib import Path

import folder_paths


class KineWorldSaveCameraBundle:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "camera_poses": ("TENSOR",),
                "camera_intrinsics": ("TENSOR",),
                "filename_prefix": (
                    "STRING",
                    {"default": "kineworld/camera-bundle", "multiline": False},
                ),
            }
        }

    RETURN_TYPES = ("STRING",)
    RETURN_NAMES = ("camera_bundle_path",)
    OUTPUT_NODE = True
    FUNCTION = "save"
    CATEGORY = "KineJing/Provenance"

    def save(self, camera_poses, camera_intrinsics, filename_prefix):
        poses = camera_poses.detach().cpu().float()
        intrinsics = camera_intrinsics.detach().cpu().float()
        if poses.ndim != 3 or tuple(poses.shape[-2:]) != (4, 4):
            raise ValueError(f"Expected camera poses [N,4,4], got {tuple(poses.shape)}")
        if intrinsics.ndim != 3 or tuple(intrinsics.shape[-2:]) != (3, 3):
            raise ValueError(f"Expected camera intrinsics [N,3,3], got {tuple(intrinsics.shape)}")
        if poses.shape[0] != intrinsics.shape[0]:
            raise ValueError("Pose and intrinsics counts differ")

        output_root = Path(folder_paths.get_output_directory()).resolve()
        relative = Path(str(filename_prefix).strip().replace("\\", "/") + ".json")
        if relative.is_absolute() or ".." in relative.parts:
            raise ValueError("filename_prefix must stay inside the ComfyUI output directory")
        output_path = (output_root / relative).resolve()
        if output_root not in output_path.parents:
            raise ValueError("Resolved camera bundle path escapes the output directory")
        output_path.parent.mkdir(parents=True, exist_ok=True)

        record = {
            "schema": "kineworld.camera-bundle.v1",
            "created_at": datetime.now(timezone.utc).isoformat(),
            "frame_count": int(poses.shape[0]),
            "pose_convention": "camera_to_world",
            "intrinsics_convention": "pixel-space 3x3 K matrix after output crop and resize",
            "provenance": ["observed"] + ["generated"] * max(0, int(poses.shape[0]) - 1),
            "camera_poses": poses.tolist(),
            "camera_intrinsics": intrinsics.tolist(),
        }
        output_path.write_text(json.dumps(record, indent=2), encoding="utf-8")
        return {"ui": {"text": [str(output_path)]}, "result": (str(output_path),)}


NODE_CLASS_MAPPINGS = {"KineWorldSaveCameraBundle": KineWorldSaveCameraBundle}
NODE_DISPLAY_NAME_MAPPINGS = {"KineWorldSaveCameraBundle": "KineJing Save Camera Bundle"}