Beispiel #1
0
def fix_uris(fn: str, gltf: GLTF):
    dn = os.path.dirname(fn)
    resource: GLTFResource
    uri2new = {}
    for i, resource in list(enumerate(gltf.resources)):
        # logger.info(resource=resource.uri)
        if resource.data is None:
            f = os.path.join(dn, resource.uri)
            resource.data = read_bytes_from_file(f)
        md5 = get_md5(resource.data)
        o = os.path.basename(resource.uri)
        newuri = f"{md5}-{o}"
        resource2 = GLBResource(resource.data)
        resource2._uri = newuri
        gltf.resources.append(resource2)
        resource3 = gltf.convert_to_file_resource(resource2, filename=newuri)
        gltf.resources.append(resource3)

        gltf.resources.remove(resource)

        uri2new[resource.uri] = newuri

    if gltf.model:
        image: Image
        if gltf.model.images:
            for image in gltf.model.images:
                if image.uri in uri2new:
                    image.uri = uri2new[image.uri]
        buffer: Buffer
        if gltf.model.buffers:
            for buffer in gltf.model.buffers:
                if buffer.uri in uri2new:
                    buffer.uri = uri2new[buffer.uri]
Beispiel #2
0
def pack_values(gltf: GLTF, uri: str, x: List[Tuple[float, ...]], btype) -> PV:
    md52PV = gltf.md52PV
    vertex_bytearray = bytearray()
    for vertex in x:
        for value in vertex:
            vertex_bytearray.extend(struct.pack("f", value))
    width = len(x[0])
    nelements = len(x)
    bytelen = len(vertex_bytearray)
    mins = [
        min([operator.itemgetter(i)(vertex) for vertex in x])
        for i in range(width)
    ]
    maxs = [
        max([operator.itemgetter(i)(vertex) for vertex in x])
        for i in range(width)
    ]

    md5 = get_md5(bytes(vertex_bytearray))
    if md5 not in md52PV:
        # noinspection PyTypeChecker

        fr = FileResource(uri, data=vertex_bytearray)
        gltf.resources.append(fr)
        buffer = Buffer(byteLength=bytelen, uri=uri)

        bi = add_buffer(gltf, buffer)
        bv = BufferView(buffer=bi,
                        byteOffset=0,
                        byteLength=bytelen,
                        target=BufferTarget.ARRAY_BUFFER.value)
        buffer_view_index = add_buffer_view(gltf, bv)
        # noinspection PyTypeChecker
        accessor = Accessor(
            bufferView=buffer_view_index,
            byteOffset=0,
            componentType=ComponentType.FLOAT.value,
            count=nelements,
            type=btype,
            min=mins,
            max=maxs,
        )

        accessor_index = add_accessor(gltf, accessor)
        md52PV[md5] = PV(
            buffer_index=bi,
            accessor_index=accessor_index,
            buffer_view_index=buffer_view_index,
            bytelen=bytelen,
        )
    return md52PV[md5]
Beispiel #3
0
def make_texture(gltf: GLTF, fn: str) -> int:
    data = read_bytes_from_file(fn)
    md5 = get_md5(data)
    bn = os.path.basename(fn)
    noext, ext = os.path.splitext(bn)
    if fn not in gltf.fn2texture_index:
        uri = f"textures/{noext}-{md5}{ext}"
        rf = FileResource(uri, data=data)
        gltf.resources.append(rf)
        image = Image(uri=uri)
        image_index = add_image(gltf, image)
        texture = Texture(sampler=0, source=image_index)
        texture_index = add_texture(gltf, texture)
        gltf.fn2texture_index[fn] = texture_index

    return gltf.fn2texture_index[fn]