Ejemplo n.º 1
0
    def add_and_get_view(
            self, binary_data: gltf2_io_binary_data.BinaryData
    ) -> gltf2_io.BufferView:
        """Add binary data to the buffer. Return a glTF BufferView."""
        offset = len(self.__data)
        self.__data += binary_data.data

        # offsets should be a multiple of 4 --> therefore add padding if necessary
        padding = (4 - (binary_data.byte_length % 4)) % 4
        self.__data += b"\x00" * padding

        buffer_view = gltf2_io.BufferView(buffer=self.__buffer_index,
                                          byte_length=binary_data.byte_length,
                                          byte_offset=offset,
                                          byte_stride=None,
                                          extensions=None,
                                          extras=None,
                                          name=None,
                                          target=None)
        return buffer_view
Ejemplo n.º 2
0
    def __init__(self, export_settings):
        self.export_settings = export_settings
        self.__finalized = False

        copyright = export_settings[
            gltf2_blender_export_keys.COPYRIGHT] or None
        asset = gltf2_io.Asset(copyright=copyright,
                               extensions=None,
                               extras=None,
                               generator='Khronos glTF Blender I/O v' +
                               get_version_string(),
                               min_version=None,
                               version='2.0')

        self.component_nb_dict = {}
        self.component_nb_dict['SCALAR'] = 1
        self.component_nb_dict['VEC2'] = 2
        self.component_nb_dict['VEC3'] = 3
        self.component_nb_dict['VEC4'] = 4
        self.component_nb_dict['MAT2'] = 4
        self.component_nb_dict['MAT3'] = 9
        self.component_nb_dict['MAT4'] = 16

        self.__gltf = gltf2_io.Gltf(accessors=[],
                                    animations=[],
                                    asset=asset,
                                    buffers=[],
                                    buffer_views=[],
                                    cameras=[],
                                    extensions={},
                                    extensions_required=[],
                                    extensions_used=[],
                                    extras=None,
                                    images=[],
                                    materials=[],
                                    meshes=[],
                                    nodes=[],
                                    samplers=[],
                                    scene=-1,
                                    scenes=[],
                                    skins=[],
                                    textures=[])

        self.__buffer = gltf2_io_buffer.Buffer()
        self.__images = {}

        # mapping of all glTFChildOfRootProperty types to their corresponding root level arrays
        self.__childOfRootPropertyTypeLookup = {
            gltf2_io.Accessor: self.__gltf.accessors,
            gltf2_io.Animation: self.__gltf.animations,
            gltf2_io.Buffer: self.__gltf.buffers,
            gltf2_io.BufferView: self.__gltf.buffer_views,
            gltf2_io.Camera: self.__gltf.cameras,
            gltf2_io.Image: self.__gltf.images,
            gltf2_io.Material: self.__gltf.materials,
            gltf2_io.Mesh: self.__gltf.meshes,
            gltf2_io.Node: self.__gltf.nodes,
            gltf2_io.Sampler: self.__gltf.samplers,
            gltf2_io.Scene: self.__gltf.scenes,
            gltf2_io.Skin: self.__gltf.skins,
            gltf2_io.Texture: self.__gltf.textures
        }

        self.__propertyTypeLookup = [
            gltf2_io.AccessorSparseIndices, gltf2_io.AccessorSparse,
            gltf2_io.AccessorSparseValues, gltf2_io.AnimationChannel,
            gltf2_io.AnimationChannelTarget, gltf2_io.AnimationSampler,
            gltf2_io.Asset, gltf2_io.CameraOrthographic,
            gltf2_io.CameraPerspective, gltf2_io.MeshPrimitive,
            gltf2_io.TextureInfo, gltf2_io.MaterialPBRMetallicRoughness,
            gltf2_io.MaterialNormalTextureInfoClass,
            gltf2_io.MaterialOcclusionTextureInfoClass
        ]

        # These are the 8 predefined buffer views that asobo models use
        # No other buffer views should be created
        self.__asobo_buffer_views = {
            'bufferViewFloatMat4': None,
            'bufferViewAnimationFloatScalar': None,
            'bufferViewAnimationFloatVec3': None,
            'bufferViewAnimationFloatVec4': None,
            'BufferViewVertexND': None,
            'BufferViewIndex': None,
            'BufferViewVertex4Blend': None,
            'BufferViewVertex1Blend': None,
        }

        self.__asobo_buffer_views['bufferViewFloatMat4'] = gltf2_io.BufferView(
            buffer=gltf2_io_asobo_buffer.AsoboBuffer(),
            byte_length=0,
            byte_offset=None,
            byte_stride=None,
            extensions=None,
            extras=None,
            name='bufferViewFloatMat4',
            target=None)
        self.__gltf.buffer_views.append(
            self.__asobo_buffer_views['bufferViewFloatMat4'])

        self.__asobo_buffer_views[
            'bufferViewAnimationFloatScalar'] = gltf2_io.BufferView(
                buffer=gltf2_io_asobo_buffer.AsoboBuffer(),
                byte_length=0,
                byte_offset=0,
                byte_stride=None,
                extensions=None,
                extras=None,
                name='bufferViewAnimationFloatScalar',
                target=None)
        self.__gltf.buffer_views.append(
            self.__asobo_buffer_views['bufferViewAnimationFloatScalar'])

        self.__asobo_buffer_views[
            'bufferViewAnimationFloatVec3'] = gltf2_io.BufferView(
                buffer=gltf2_io_asobo_buffer.AsoboBuffer(),
                byte_length=0,
                byte_offset=0,
                byte_stride=None,
                extensions=None,
                extras=None,
                name='bufferViewAnimationFloatVec3',
                target=None)
        self.__gltf.buffer_views.append(
            self.__asobo_buffer_views['bufferViewAnimationFloatVec3'])

        self.__asobo_buffer_views[
            'bufferViewAnimationFloatVec4'] = gltf2_io.BufferView(
                buffer=gltf2_io_asobo_buffer.AsoboBuffer(),
                byte_length=0,
                byte_offset=0,
                byte_stride=None,
                extensions=None,
                extras=None,
                name='bufferViewAnimationFloatVec4',
                target=None)
        self.__gltf.buffer_views.append(
            self.__asobo_buffer_views['bufferViewAnimationFloatVec4'])

        self.__asobo_buffer_views['BufferViewVertexND'] = gltf2_io.BufferView(
            buffer=gltf2_io_asobo_buffer.AsoboBuffer(),
            byte_length=0,
            byte_offset=0,
            byte_stride=36,
            extensions=None,
            extras=None,
            name='BufferViewVertexND',
            target=34962)
        self.__gltf.buffer_views.append(
            self.__asobo_buffer_views['BufferViewVertexND'])

        self.__asobo_buffer_views['BufferViewIndex'] = gltf2_io.BufferView(
            buffer=gltf2_io_asobo_buffer.AsoboBuffer(),
            byte_length=0,
            byte_offset=0,
            byte_stride=None,
            extensions=None,
            extras=None,
            name='BufferViewIndex',
            target=34963)
        self.__gltf.buffer_views.append(
            self.__asobo_buffer_views['BufferViewIndex'])

        self.__asobo_buffer_views[
            'BufferViewVertex4Blend'] = gltf2_io.BufferView(
                buffer=gltf2_io_asobo_buffer.AsoboBuffer(),
                byte_length=0,
                byte_offset=0,
                byte_stride=48,
                extensions=None,
                extras=None,
                name='BufferViewVertex4Blend',
                target=34962)
        self.__gltf.buffer_views.append(
            self.__asobo_buffer_views['BufferViewVertex4Blend'])

        self.__asobo_buffer_views[
            'BufferViewVertex1Blend'] = gltf2_io.BufferView(
                buffer=gltf2_io_asobo_buffer.AsoboBuffer(),
                byte_length=0,
                byte_offset=0,
                byte_stride=44,
                extensions=None,
                extras=None,
                name='BufferViewVertex1Blend',
                target=34962)
        self.__gltf.buffer_views.append(
            self.__asobo_buffer_views['BufferViewVertex1Blend'])