writeJson after updating it result in loose data. #392
-
Update GLTF json make and merge it loose data. To Reproduce for (const modelPath of ['/app/3dmodels/adamHead/adamHead.gltf']) {
const modelDir = path.dirname(modelPath);
fs.ensureDirSync(modelDir+'/assets');
// Read in json the model
const jsonGltf = io.writeJSON(io.read(modelPath));
for (const index in jsonGltf.json.images || []) {
// copy the the image uri to the new directory
if (jsonGltf.json.images) {
const newAssetFile = 'assets/'+path.basename(String(jsonGltf.json.images[index].uri));
await fs.copy(modelDir+'/'+jsonGltf.json.images[index].uri, newDir + '/' + newAssetFile);
jsonGltf.json.images[index].uri = newAssetFile;
console.log(jsonGltf.json.images[index], newAssetFile);
}
}
// Merge into the new document
document.merge(io.readJSON(jsonGltf));
console.log('merged '+modelPath);
}
console.log('write');
io.write(newDir+'/model.gltf', document);
console.log('writed') The image URI should took the new value, instead is just get removed. The new JSON looks like:
instead of:
The file is still copied, so nothing wrong in my code. When i |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Working with the raw JSON data is unforgiving, I wouldn't recommend that for anything more than cosmetic changes, or to load something in memory that the NodeIO and WebIO classes wouldn't otherwise be able to access. But if you do go that route, note that when changing a URI, you must also change the corresponding key in the The preferred solution here would be to work with a Document instead of the raw JSON: const document = io.read('input.gltf');
fs.ensureDirSync('path/to/assets');
for (const texture of document.getRoot().listTextures()) {
texture.setURI('path/to/assets/' + path.basename(texture.getURI()));
}
io.write('output.gltf', document); |
Beta Was this translation helpful? Give feedback.
Working with the raw JSON data is unforgiving, I wouldn't recommend that for anything more than cosmetic changes, or to load something in memory that the NodeIO and WebIO classes wouldn't otherwise be able to access. But if you do go that route, note that when changing a URI, you must also change the corresponding key in the
jsonDocument.resources
object. Those resources are held in memory, soio.readJSON(...)
does not read anything from disk.The preferred solution here would be to work with a Document instead of the raw JSON: