Module: OpenUSD::Format::Usdz::Writer

Defined in:
lib/openusd/format/usdz/writer.rb

Overview

Writes uncompressed, 64-byte-aligned USDZ packages.

Constant Summary collapse

LOCAL_SIGNATURE =

ZIP local-file-header signature.

0x04034B50
CENTRAL_SIGNATURE =

ZIP central-directory-entry signature.

0x02014B50
END_SIGNATURE =

ZIP end-of-central-directory signature.

0x06054B50
ALIGNMENT_EXTRA_ID =

OpenUSD alignment extra-field identifier.

0x1986
UINT32_MAX =

Largest value representable by non-ZIP64 records.

(2**32) - 1

Class Method Summary collapse

Class Method Details

.pack(path, root:, assets: []) ⇒ String

Package a root USD file and additional assets.

Returns:

  • (String)

    destination path



34
35
36
37
38
39
40
41
42
43
# File 'lib/openusd/format/usdz/writer.rb', line 34

def pack(path, root:, assets: [])
  root_path = File.expand_path(root)
  validate_root!(root_path)
  entries = [[File.basename(root_path), File.binread(root_path)]]
  entries.concat(Array(assets).map { |asset| asset_entry(asset) })
  write_entries(path, entries)
  path
rescue Errno::ENOENT => e
  raise PackageError, "package input not found: #{e.message}"
end

.write(layer, path) ⇒ String

Package one in-memory layer as a USDZ file.

Returns:

  • (String)

    destination path



26
27
28
29
30
# File 'lib/openusd/format/usdz/writer.rb', line 26

def write(layer, path)
  root_name = "#{File.basename(path, File.extname(path))}.usda"
  write_entries(path, [[root_name, layer.to_usda]])
  path
end

.write_entries(path, entries) ⇒ Integer

Write preloaded [name, bytes] entries.

Returns:

  • (Integer)

    bytes written



47
48
49
50
51
52
53
54
55
56
# File 'lib/openusd/format/usdz/writer.rb', line 47

def write_entries(path, entries)
  validate_entries!(entries)
  archive = String.new(encoding: Encoding::BINARY)
  central_records = entries.map { |name, data| write_local_entry(archive, name, data.b) }
  central_offset = archive.bytesize
  central_records.each { |record| archive << central_header(record) }
  central_size = archive.bytesize - central_offset
  archive << end_record(entries.length, central_size, central_offset)
  File.binwrite(path, archive)
end