1 # SPDX-FileCopyrightText: 2018-2021 The glTF-Blender-IO authors
3 # SPDX-License-Identifier: Apache-2.0
7 from ...io
.com
import gltf2_io
8 from ...io
.exp
import gltf2_io_binary_data
12 """Class representing binary data for use in a glTF file as 'buffer' property."""
14 def __init__(self
, buffer_index
=0, initial_data
=None):
15 self
.__data
= bytearray(b
"")
16 if initial_data
is not None:
17 self
.__data
= bytearray(initial_data
.tobytes())
18 self
.__buffer
_index
= buffer_index
20 def add_and_get_view(self
, binary_data
: gltf2_io_binary_data
.BinaryData
) -> gltf2_io
.BufferView
:
21 """Add binary data to the buffer. Return a glTF BufferView."""
22 offset
= len(self
.__data
)
23 self
.__data
.extend(binary_data
.data
)
25 length
= binary_data
.byte_length
27 # offsets should be a multiple of 4 --> therefore add padding if necessary
28 padding
= (4 - (length
% 4)) % 4
29 self
.__data
.extend(b
"\x00" * padding
)
31 buffer_view
= gltf2_io
.BufferView(
32 buffer=self
.__buffer
_index
,
39 target
=binary_data
.bufferViewTarget
44 def byte_length(self
):
45 return len(self
.__data
)
53 def to_embed_string(self
):
54 return 'data:application/octet-stream;base64,' + base64
.b64encode(self
.__data
).decode('ascii')