ussue with join primitives #1220
-
I am attempting to merge objects based on a provided array by using a JSON array to identify the objects that need to be merged. I assign each object the same material so that I can merge them using Below is my const { NodeIO, Material } = require('@gltf-transform/core');
const { joinPrimitives } = require('@gltf-transform/functions');
const { KHRMeshQuantization, KHRTextureTransform } = require('@gltf-transform/extensions');
const fs = require('fs');
const { transformJson } = require('./produce_json_array.js');
// Initialize GLTF IO
const io = new NodeIO().registerExtensions([KHRMeshQuantization, KHRTextureTransform])
// Your GLB filename
const inputGLB = './gltf_transform_join/UIManager.glb';
// Load the GLB file
io.read(inputGLB)
.then(document => {
const root = document.getRoot();
const prims = document.getRoot()
.listMeshes()
.flatMap((mesh) => mesh.listPrimitives());
const result = joinPrimitives([prims[0], prims[1]]);
const parsed_json = JSON.parse(fs.readFileSync("./gltf_transform_join/UIManager.json", 'utf8'));
const all_glb_objects = root.listNodes().map(node => node.getName());
const objectsArray = transformJson(parsed_json, all_glb_objects).filter(array => array.length > 1);
let material_to_use=null
// Process each array object
objectsArray.forEach((arrayObject, index) => {
try {
// Retrieve all nodes (or a subset based on the identifiers in arrayObject)
let nodes = [];
root.listNodes().forEach(node => {
if (arrayObject.includes(node.getName())) {
material_to_use = node.getMesh().listPrimitives()[0].getMaterial()
if (node.getMesh()) {
const primitives = node.getMesh().listPrimitives();
primitives.forEach(primitive => {
primitive.setMaterial(material_to_use)
})
nodes = [...nodes, ...primitives]
}
}
});
const result = joinPrimitives(nodes);
// console.log(`Processing ${nodes.length} nodes`)
// For each node, check and join compatible meshes
} catch (error) {
// Log and save error
console.error("An error occurred:", error);
}
});
// Save the modified document
io.write('output_final.glb', document);
})
.catch(error => console.error('Error loading GLB:', error)); Here is the JSON extraction script: function transformJson(inputJson, glb_objects = []) {
const groups = {};
inputJson.data.collection.forEach(item => {
if (!groups[item.name]) {
groups[item.name] = [];
}
if (glb_objects.includes(String(item.objectid))) {
groups[item.name].push(String(item.objectid));
}
});
return Object.values(groups);
}
exports.transformJson = transformJson; I am using the following JSON and GLB files: Drive Link. Your guidance on resolving this issue would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
When I run this script on the given files as-is, I get the following error:
That much is expected, since (for testing purposes) we're just merging two arbitrary primitives, and haven't checked that they're compatible. It sounds like you are getting a different error though, and that is concerning ... how are you executing the code? I'm using Node.js v18.80.0 and running something like: node ./join.js If you're using a bundler or a build tool before running the code, that might be important to the error as well. Also note that if you just want to merge whatever can be merged, this would be a simpler place to start: async function main() {
const document = await io.read(inputGLB);
await document.transform(flatten(), join());
} It might be worth testing that as well, as we try to figure out what's going on. |
Beta Was this translation helpful? Give feedback.
When I run this script on the given files as-is, I get the following error:
That much is expected, since (for testing purposes) we're just merging two arbitrary primitives, and haven't checked that they're compatible.
It sounds like you are getting a different error though, and that is concerning ... how are you executing the code? I'm using Node.js v18.80.0 and running something like:
If you're using a bundler or a build tool before running the code, that might be important to the error as well.
Also note that if you just want to merge whatever can be merged…