fix saved glb length

Summary: Make GLB files report their own length correctly. They were off by 28.

Reviewed By: davidsonic

Differential Revision: D41838340

fbshipit-source-id: 9cd66e8337c142298d5ae1d7c27e51fd812d5c7b
This commit is contained in:
Jeremy Reizenstein 2022-12-13 05:34:45 -08:00 committed by Facebook GitHub Bot
parent de3a474d2b
commit b7a316232a
3 changed files with 18 additions and 4 deletions

View File

@ -740,7 +740,10 @@ class _GLTFWriter:
json_length = len(json_bytes)
# write header
header = struct.pack("<III", _GLTF_MAGIC, 2, json_length + byte_offset)
version = 2
total_header_length = 28 # (file header = 12) + 2 * (chunk header = 8)
file_length = json_length + byte_offset + total_header_length
header = struct.pack("<III", _GLTF_MAGIC, version, file_length)
self.buffer_stream.write(header)
# write json

View File

@ -155,6 +155,9 @@ class IO:
include_textures: If textures are present, whether to try to save
them.
"""
if not isinstance(data, Meshes):
raise ValueError("Meshes object expected.")
if len(data) != 1:
raise ValueError("Can only save a single mesh.")
@ -204,6 +207,9 @@ class IO:
path: file to write
binary: If there is a choice, whether to save in a binary format.
"""
if not isinstance(data, Pointclouds):
raise ValueError("Pointclouds object expected.")
if len(data) != 1:
raise ValueError("Can only save a single point cloud.")

View File

@ -4,6 +4,7 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import os.path
import unittest
from math import radians
@ -11,7 +12,7 @@ import numpy as np
import torch
from PIL import Image
from pytorch3d.io import IO
from pytorch3d.io.experimental_gltf_io import MeshGlbFormat
from pytorch3d.io.experimental_gltf_io import _read_header, MeshGlbFormat
from pytorch3d.renderer import (
AmbientLights,
BlendParams,
@ -46,10 +47,14 @@ def _load(path, **kwargs) -> Meshes:
return io.load_mesh(path, **kwargs)
def _write(mesh, path, **kwargs) -> bool:
def _write(mesh, path, **kwargs) -> None:
io = IO()
io.register_meshes_format(MeshGlbFormat())
return io.save_mesh(mesh, path, **kwargs)
io.save_mesh(mesh, path, **kwargs)
with open(path, "rb") as f:
_, stored_length = _read_header(f)
assert stored_length == os.path.getsize(path)
def _render(