├── .gitignore ├── LICENSE ├── README.md ├── b3dm.py ├── batchtable.py ├── featuretable.py ├── i3dm.py ├── instancetable.py ├── packcmpt.py ├── packglb.py ├── pnts.py └── tile3dinfo.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Geopipe, Inc. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gltf2glb 2 | Tools to manipulate GLTF/GLB-containing files such as .b3dm, .i3dm, .pnts, and .cmpt. 3 | 4 | Created by Christopher Mitchell, Ph.D. et alia of Geopipe, Inc. 5 | 6 | This project was initially intended to provide a GLTF-to-GLB packer, inspired by 7 | the desire to use https://github.com/Qantas94Heavy/binary-gltf-util without running Javascript 8 | as a server-side language. It has evolved to remove its first component (gltf2glb.py), instead 9 | providing the tools shown below to pack and unpack GLTF/GLB files into and out of other containers. 10 | 11 | Usage 12 | ----- 13 | 14 | ### packcmpt ### 15 | ``` 16 | $ ./packcmpt.py -h 17 | usage: packcmpt.py [-h] -o OUTPUT input_files [input_files ...] 18 | 19 | Packs one or more i3dm and/or b3dm files into a cmpt 20 | 21 | positional arguments: 22 | input_files 23 | 24 | optional arguments: 25 | -h, --help show this help message and exit 26 | -o OUTPUT, --output OUTPUT Output cmpt file 27 | -u Unpack the output CMPT file instead of creating it (incomplete) 28 | ``` 29 | ### i3dm ### 30 | ``` 31 | $ ./i3dm.py -h 32 | usage: usage: i3dm.py [-h] -i I3DM -g GLB -o OUTPUT 33 | 34 | Generate an i3dm file from JSON describing instances, and a GLB to instance 35 | 36 | required arguments: 37 | -i, --i3dm JSON for instance semantics 38 | -g, --glb Path to GLB file to instance 39 | -o, --output Path to i3dm file to output 40 | 41 | optional arguments: 42 | -h, --help show this help message and exit 43 | 44 | License 45 | ------- 46 | (c) 2016-2021 Geopipe, Inc. and licensed under the BSD 3-Clause license. See LICENSE. 47 | -------------------------------------------------------------------------------- /b3dm.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | #-------------------------------------------- 4 | # b3dm.py: Component of GLTF to GLB converter 5 | # (c) 2016 Geopipe, Inc. 6 | # All rights reserved. See LICENSE. 7 | #-------------------------------------------- 8 | 9 | import struct 10 | from batchtable import BatchTable 11 | from featuretable import FeatureTable 12 | 13 | B3DM_MAGIC = 'b3dm' 14 | B3DM_VERSION = 1 15 | B3DM_HEADER_LEN = 28 16 | 17 | class B3DM: 18 | def __init__(self): 19 | self.batch_table = BatchTable() 20 | self.feature_table = FeatureTable() 21 | self.gltf_bin = bytearray() 22 | 23 | def loadJSONBatch(self, data_in, object_wise = True): 24 | self.batch_table.loadJSONBatch(data_in, object_wise) 25 | 26 | def loadJSONFeature(self, data_in, object_wise = True): 27 | self.feature_table.loadJSONBatch(data_in, object_wise) 28 | 29 | def writeBinary(self, gltf_bin, num_batch_features = 0, num_feature_features = 0): 30 | 31 | # Add the required field BATCH_LENGTH to the feature table, 32 | # as well as any other required globals 33 | num_batch_features = max(num_batch_features, self.batch_table.getNumFeatures()) 34 | self.feature_table.addGlobal('BATCH_LENGTH', num_batch_features) 35 | num_feature_features = max(num_feature_features, self.feature_table.getNumFeatures()) 36 | 37 | self.batch_table.finalize() 38 | self.feature_table.finalize() 39 | 40 | # Generate the header 41 | output = self.writeHeader(gltf_bin, num_batch_features, num_feature_features) 42 | 43 | # Add the feature table JSON to the output 44 | feature_json = self.feature_table.getFeatureJSON() 45 | output.extend(feature_json) 46 | 47 | # Add the feature table binary to the output 48 | feature_bin = self.feature_table.getFeatureBin() 49 | output.extend(feature_bin) 50 | 51 | # Add the batch table JSON to the output 52 | batch_json = self.batch_table.getBatchJSON() 53 | output.extend(batch_json) 54 | 55 | # Add the batch table binary to the output 56 | batch_bin = self.batch_table.getBatchBin() 57 | output.extend(batch_bin) 58 | 59 | # Add the GLTF model body to the output 60 | output.extend(gltf_bin) 61 | 62 | return output 63 | 64 | def writeHeader(self, gltf_bin, num_feature_features, num_batch_features): 65 | len_feature_json = len(self.feature_table.getFeatureJSON()) 66 | len_feature_bin = len(self.feature_table.getFeatureBin()) 67 | len_batch_json = len(self.batch_table.getBatchJSON()) 68 | len_batch_bin = len(self.batch_table.getBatchBin()) 69 | 70 | length = B3DM_HEADER_LEN + \ 71 | len_feature_json + len_feature_bin + \ 72 | len_batch_json + len_batch_bin + \ 73 | len(gltf_bin) 74 | 75 | output = bytearray() 76 | output.extend(B3DM_MAGIC.encode('utf-8')) 77 | output.extend(struct.pack(' B3DM_VERSION: 107 | raise IOError("Unrecognized magic %s or bad version %d" % (self.magic, self.version)) 108 | 109 | self.length = self.unpack(' I3DM_VERSION: 143 | raise IOError("Unrecognized magic %s or bad version %d" % (self.magic, self.version)) 144 | 145 | self.length = self.unpack(' CMPT_VERSION: 92 | raise IOError("Unrecognized magic string %s or bad version %d" % (magic, version)) 93 | 94 | self.length = self.unpack(' PNTS_VERSION: 115 | raise IOError("Unrecognized magic %s or bad version %d" % (self.magic, self.version)) 116 | 117 | self.length = self.unpack('