Exemple #1
0
def parseB3DM(data, indent=0):
    s_indent = '\t' * indent
    b3dm_decoder = b3dm.B3DM()
    b3dm_decoder.readBinary(data)

    print("%sB3DM File:" % (s_indent))
    printFeatureBatch(b3dm_decoder, s_indent)
Exemple #2
0
def main():
    """ Convert GLTF to GLB, with optional additional I3DM or B3DM encoding"""

    # Parse options and get results
    parser = argparse.ArgumentParser(description='Converts GLTF to GLB')
    parser.add_argument("-i", "--i3dm", type=str, \
                        help="Export i3dm, with optional path to JSON instance table data")
    parser.add_argument("-b", "--b3dm", type=str, \
                        help="Export b3dm, with optional path to JSON batch table data")
    parser.add_argument(
        "-o",
        "--output",
        required=False,
        default=None,
        help="Optional output path (defaults to the path of the input file")
    parser.add_argument("filename")
    args = parser.parse_args()

    # Make sure the input file is *.glb
    if not args.filename.endswith('.glb'):
        print("Failed to create packed binary GLB file: input is not *.glb")
        sys.exit(-1)

    with open(args.filename, 'r') as f:
        glb = f.read()

    if args.b3dm != None:
        ext = 'b3dm'
    elif args.i3dm != None:
        ext = 'i3dm'
    else:
        ext = 'glb'

    fname_out = os.path.splitext(os.path.basename(
        args.filename))[0] + '.' + ext
    if None != args.output:
        if "" == os.path.basename(args.output):
            fname_out = os.path.join(fname_out, fname_out)
        else:
            fname_out = args.output
    else:
        fname_out = os.path.join(os.path.dirname(args.filename), fname_out)

    if args.b3dm != None:
        b3dm_encoder = b3dm.B3DM()
        if len(args.b3dm):
            with open(args.b3dm, 'r') as f:
                b3dm_json = json.loads(f.read())
                print b3dm_json
                b3dm_encoder.loadJSONBatch(b3dm_json, False)

        with open(fname_out, 'w') as f:
            f.write(b3dm_encoder.writeBinary(glb))
    elif args.i3dm != None:
        raise NotImplementedError
    else:
        # This is kinda pointless
        with open(fname_out, 'w') as f:
            f.write(glb)
Exemple #3
0
def main():
    """ Convert GLTF to GLB"""

    # Parse options and get results
    parser = argparse.ArgumentParser(description='Converts GLTF to GLB')
    parser.add_argument("-e", "--embed", action="store_true", \
         help="Embed textures or shares into binary GLTF file")
    parser.add_argument("-c", "--cesium", action="store_true", \
         help="sets the old body buffer name for compatibility with Cesium [UNNECESSARY - DEPRECATED]")
    parser.add_argument("-i", "--i3dm", type=str, \
         help="Export i3dm, with optional path to JSON instance table data")
    parser.add_argument("-b", "--b3dm", type=str, \
         help="Export b3dm, with optional path to JSON batch table data")
    parser.add_argument(
        "-o",
        "--output",
        required=False,
        default=None,
        help="Optional output path (defaults to the path of the input file")
    parser.add_argument("filename")
    args = parser.parse_args()

    embed = {}
    if args.embed:
        for t in EMBED_ARR:
            embed[t] = True

    # Make sure the input file is *.gltf
    if not args.filename.endswith('.gltf'):
        print("Failed to create binary GLTF file: input is not *.gltf")
        sys.exit(-1)

    with open(args.filename, 'rb') as f:
        gltf = f.read()
    gltf = gltf.decode('utf-8')
    scene = json.loads(gltf)

    # Set up body_encoder
    body_encoder = BodyEncoder(containing_dir=os.path.dirname(args.filename))

    # Iterate the buffers in the scene:
    for buf_id, buf in enumerate(scene["buffers"]):
        buf_type = buf.get("type", None)
        if buf_type and buf_type != 'arraybuffer':
            raise TypeError("Buffer type %s not supported: %s" %
                            (buf_type, buf_id))

        try:
            length = buf["byteLength"]
        except:
            length = None

        offset, length = body_encoder.addToBody(buf["uri"], length)

        try:
            buf["extras"]
        except KeyError:
            buf["extras"] = {}
        buf["extras"]["byteOffset"] = offset

    # Iterate over the bufferViews to
    # move buffers into the single GLB buffer body
    for bufview_id, bufview in enumerate(scene["bufferViews"]):
        buf_id = bufview["buffer"]
        try:
            referenced_buf = scene["buffers"][buf_id]
        except KeyError:
            raise KeyError("Buffer ID reference not found: %s" % (buf_id))

        scene["bufferViews"][bufview_id]["buffer"] = 0
        try:
            scene["bufferViews"][bufview_id]["byteOffset"] += referenced_buf[
                "extras"]["byteOffset"]
        except KeyError:
            scene["bufferViews"][bufview_id]["byteOffset"] = referenced_buf[
                "extras"]["byteOffset"]

    # Iterate over the shaders
    if 'shaders' in embed and 'shaders' in scene:
        for shader_id, shader in scene["shaders"].iteritems():
            uri = shader["uri"]
            del scene["shaders"][shader_id]["uri"]

            offset, length = body_encoder.addToBody(uri, None)

            scene["bufferViews"].append({
                'buffer': 0,
                'byteLength': length,
                'byteOffset': offset
            })
            scene["shaders"][shader_id]["bufferView"] = len(
                scene["bufferViews"]) - 1

    # Iterate over images
    if 'textures' in embed and 'images' in scene:
        for image_id, image in enumerate(scene["images"]):
            uri = image["uri"]
            del scene["images"][image_id]["uri"]

            offset, length = body_encoder.addToBody(uri, None)

            scene["bufferViews"].append({
                'buffer': 0,
                'byteLength': length,
                'byteOffset': offset
            })
            scene["images"][image_id]["bufferView"] = len(
                scene["bufferViews"]) - 1
            scene["images"][image_id]["mimeType"] = "image/png"

    scene["buffers"] = [{
        'byteLength': body_encoder.body_length
    }]

    new_scene_str = bytearray(
        json.dumps(scene, separators=(',', ':'), sort_keys=True))
    encoder = GLBEncoder(new_scene_str, body_encoder)
    if args.b3dm != None:
        ext = 'b3dm'
    elif args.i3dm != None:
        ext = 'i3dm'
    else:
        ext = 'glb'
    #print("Exporting %s" % (ext))

    fname_out = os.path.splitext(os.path.basename(
        args.filename))[0] + '.' + ext
    if None != args.output:
        if "" == os.path.basename(args.output):
            fname_out = os.path.join(fname_out, fname_out)
        else:
            fname_out = args.output
    else:
        fname_out = os.path.join(os.path.dirname(args.filename), fname_out)

    if args.b3dm != None:
        glb = encoder.exportString()
        b3dm_encoder = b3dm.B3DM()
        if len(args.b3dm):
            with open(args.b3dm, 'rb') as f:
                b3dm_json = json.loads(f.read())
                print b3dm_json
                b3dm_encoder.loadJSONBatch(b3dm_json)

        with open(fname_out, 'wb') as f:
            f.write(b3dm_encoder.writeBinary(glb))
    elif args.i3dm != None:
        raise NotImplementedError
    else:
        encoder.export(fname_out)
Exemple #4
0
def main():
    """ Convert GLTF to GLB, with optional additional I3DM or B3DM encoding"""

    # Parse options and get results
    parser = argparse.ArgumentParser(description='Converts GLTF to GLB')
    parser.add_argument("-i", "--i3dm", type=str, \
                        help="Export i3dm, with required path to input JSON instance table data. Supports only embedded GLTFs")
    parser.add_argument("-b", "--b3dm", type=str, \
                        help="Export b3dm, with optional path to input JSON batch table data")
    parser.add_argument("-o", "--output", required=False, default=None, \
                        help="Optional output path (defaults to the path of the input file")
    parser.add_argument("-u", "--unpack", action='store_true', \
                        help="Unpack rather than create b3dm file")
    parser.add_argument("filename")
    args = parser.parse_args()

    if args.b3dm and args.unpack and args.filename:
        b3dm_decoder = b3dm.B3DM()
        with open(args.filename, 'rb') as f:
            data = f.read()
            b3dm_decoder.readBinary(data)
        with open(args.filename + '.glb', 'wb') as f:
            output_data = b3dm_decoder.getGLTFBin()
            f.write(output_data)
        sys.exit(0)

    # Make sure the input file is *.glb
    if not args.filename.endswith('.glb'):
        print("Failed to create packed binary GLB file: input is not *.glb")
        sys.exit(-1)

    with open(args.filename, 'r') as f:
        glb = f.read()

    if args.b3dm != None:
        ext = 'b3dm'
    elif args.i3dm != None:
        ext = 'i3dm'
    else:
        ext = 'glb'

    fname_out = os.path.splitext(os.path.basename(
        args.filename))[0] + '.' + ext
    if None != args.output:
        if "" == os.path.basename(args.output):
            fname_out = os.path.join(fname_out, fname_out)
        else:
            fname_out = args.output
    else:
        fname_out = os.path.join(os.path.dirname(args.filename), fname_out)

    if args.b3dm != None:
        b3dm_encoder = b3dm.B3DM()
        if len(args.b3dm):
            with open(args.b3dm, 'r') as f:
                b3dm_json = json.loads(f.read())
                #print b3dm_json
                b3dm_encoder.loadJSONBatch(b3dm_json, False)

        with open(fname_out, 'w') as f:
            f.write(b3dm_encoder.writeBinary(glb))

    elif args.i3dm != None:
        i3dm_encoder = i3dm.I3DM()
        if not (len(args.i3dm)):
            raise ValueError("-i/--i3dm requires a JSON instance table")
        else:
            with open(args.i3dm, 'r') as f:
                i3dm_json = json.loads(f.read())
            i3dm_encoder.loadJSONInstances(i3dm_json, False)

        with open(fname_out, 'w') as f:
            f.write(i3dm_encoder.writeBinary(glb,
                                             True))  # Second arg: embed gltf

    else:
        # This is kinda pointless
        with open(fname_out, 'w') as f:
            f.write(glb)
Exemple #5
0
def main():
    """ Convert GLTF to GLB, with optional additional I3DM or B3DM encoding"""

    # Parse options and get results
    parser = argparse.ArgumentParser(description='Converts GLTF to GLB')
    parser.add_argument("-e", "--embed", action="store_true", \
         help="Embed textures or shares into binary GLTF file")
    parser.add_argument("-c", "--cesium", action="store_true", \
         help="sets the old body buffer name for compatibility with Cesium [UNNECESSARY - DEPRECATED]")
    parser.add_argument("-i", "--i3dm", type=str, \
                        help="Export i3dm, with required path to input JSON instance table data. Supports only embedded GLTFs")
    parser.add_argument("-b", "--b3dm", type=str, \
                        help="Export b3dm, with optional path to input JSON batch table data")
    parser.add_argument(
        "-o",
        "--output",
        required=False,
        default=None,
        help="Optional output path (defaults to the path of the input file")
    parser.add_argument("filename")
    args = parser.parse_args()

    embed = {}
    if args.embed:
        for t in EMBED_ARR:
            embed[t] = True

    # Make sure the input file is *.gltf
    if not args.filename.endswith('.gltf'):
        print("Failed to create binary GLTF file: input is not *.gltf")
        sys.exit(-1)

    with open(args.filename, 'r') as f:
        gltf = f.read()
    gltf = gltf.decode('utf-8')
    scene = json.loads(gltf)

    # Set up body_encoder
    body_encoder = BodyEncoder(containing_dir=os.path.dirname(args.filename))

    # Let GLTF parser know that it is using the Binary GLTF extension
    try:
        scene["extensionsUsed"].append(BINARY_EXTENSION)
    except (KeyError, TypeError):
        scene["extensionsUsed"] = [BINARY_EXTENSION]

    # Iterate the buffers in the scene:
    for buf_id, buf in scene["buffers"].iteritems():
        buf_type = buf["type"]
        if buf_type and buf_type != 'arraybuffer':
            raise TypeError("Buffer type %s not supported: %s" %
                            (buf_type, buf_id))

        try:
            length = buf["byteLength"]
        except:
            length = None

        offset, length = body_encoder.addToBody(buf["uri"], length)
        try:
            buf["extras"]
        except KeyError:
            buf["extras"] = {}
        buf["extras"]["byteOffset"] = offset

    # Iterate over the bufferViews to
    # move buffers into the single GLB buffer body
    for bufview_id, bufview in scene["bufferViews"].iteritems():
        buf_id = bufview["buffer"]
        try:
            referenced_buf = scene["buffers"][buf_id]
        except KeyError:
            raise KeyError("Buffer ID reference not found: %s" % (buf_id))

        scene["bufferViews"][bufview_id]["buffer"] = BINARY_BUFFER
        try:
            scene["bufferViews"][bufview_id]["byteOffset"] += referenced_buf[
                "extras"]["byteOffset"]
        except KeyError:
            scene["bufferViews"][bufview_id]["byteOffset"] = referenced_buf[
                "extras"]["byteOffset"]

    # Iterate over the shaders
    if 'shaders' in embed and 'shaders' in scene:
        for shader_id, shader in scene["shaders"].iteritems():
            uri = shader["uri"]
            del scene["shaders"][shader_id]["uri"]

            offset, length = body_encoder.addToBody(uri, None)
            bufview_id = BINARY_BUFFER + '_shader_' + str(shader_id)
            scene["shaders"][shader_id]["extensions"] = \
             {BINARY_EXTENSION: {'bufferView': bufview_id}}

            scene["bufferViews"][bufview_id] = \
             {'buffer': BINARY_BUFFER, 'byteLength': length, 'byteOffset': offset}

    # Iterate over images
    if 'textures' in embed and 'images' in scene:
        for image_id, image in scene["images"].iteritems():
            uri = image["uri"]
            offset, length = body_encoder.addToBody(uri, None)

            bufview_id = BINARY_BUFFER + '_images_' + str(image_id)
            # TODO: Add extension properties
            scene["images"][image_id]["extensions"] = \
             {BINARY_EXTENSION: {\
              'bufferView': bufview_id,\
              'mimeType': 'image/i-dont-know',\
              'height': 9999,\
              'width': 9999\
             }}
            del scene["images"][image_id]["uri"]

            scene["bufferViews"][bufview_id] = \
             {'buffer': BINARY_BUFFER, 'byteLength': length, 'byteOffset': offset}

    scene["buffers"] = {
        BINARY_BUFFER: {
            'byteLength': body_encoder.body_length,
            'uri': ''
        }
    }

    new_scene_str = bytearray(
        json.dumps(scene, separators=(',', ':'), sort_keys=True))
    encoder = GLBEncoder(new_scene_str, body_encoder)
    if args.b3dm != None:
        ext = 'b3dm'
    elif args.i3dm != None:
        ext = 'i3dm'
    else:
        ext = 'glb'
    #print("Exporting %s" % (ext))

    fname_out = os.path.splitext(os.path.basename(
        args.filename))[0] + '.' + ext
    if None != args.output:
        if "" == os.path.basename(args.output):
            fname_out = os.path.join(fname_out, fname_out)
        else:
            fname_out = args.output
    else:
        fname_out = os.path.join(os.path.dirname(args.filename), fname_out)

    if args.b3dm != None:
        glb = encoder.exportString()
        b3dm_encoder = b3dm.B3DM()
        if len(args.b3dm):
            with open(args.b3dm, 'r') as f:
                b3dm_json = json.loads(f.read())
            b3dm_encoder.loadJSONBatch(b3dm_json, False)

        with open(fname_out, 'w') as f:
            f.write(b3dm_encoder.writeBinary(glb))

    elif args.i3dm != None:
        glb = encoder.exportString()
        i3dm_encoder = i3dm.I3DM()
        if not (len(args.i3dm)):
            raise ValueError("-i/--i3dm requires a JSON instance table")
        else:
            with open(args.i3dm, 'r') as f:
                i3dm_json = json.loads(f.read())
            i3dm_encoder.loadJSONInstances(i3dm_json)

        with open(fname_out, 'w') as f:
            f.write(i3dm_encoder.writeBinary(glb,
                                             True))  # Second arg: embed gltf

    else:
        encoder.export(fname_out)