Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get object names in a 3D scene correctly using trimesh #2321

Open
nikhil-convrseai opened this issue Nov 7, 2024 · 2 comments
Open

Comments

@nikhil-convrseai
Copy link

I'm working on a project in which i want to import a 3d scene file and perform some preprocessing on it and then use the preprocessed information in threejs. ThreeJs and Blender use same naming convention of objects i.e. there is a 3d object and then there is a mesh which is the data of that object. But when I'm loading the file with trimesh, I'm not able to get the same names in geometry or graph attributes. How can I get the same names using trimesh?

I'm using glb file but have to import fbx and obj as well.

@mikedh
Copy link
Owner

mikedh commented Nov 8, 2024

Hey, yeah it might take some tinkering to get it to line up exactly right. I think the core challenge is that GLTF "meshes" include multiple "primitives," a data structure trimesh just considers another mesh. Hence the suffixes being appended to GLTF mesh names by trimesh.util.unique_name. I think trimesh.load(..., merge_primitives=True) gets you pretty close:

In [1]: import trimesh

In [2]: a = trimesh.load('models/CesiumMilkTruck.glb')

In [3]: a.geometry
Out[3]: 
OrderedDict([('Cesium_Milk_Truck',
              <trimesh.Trimesh(vertices.shape=(1856, 3), faces.shape=(1744, 3))>),
             ('Cesium_Milk_Truck_1',
              <trimesh.Trimesh(vertices.shape=(72, 3), faces.shape=(56, 3))>),
             ('Cesium_Milk_Truck_2',
              <trimesh.Trimesh(vertices.shape=(464, 3), faces.shape=(288, 3))>),
             ('Wheels',
              <trimesh.Trimesh(vertices.shape=(586, 3), faces.shape=(768, 3))>)])

In [4]: a.graph.nodes_geometry
Out[4]: ['0_4f9167', '0_5e6e5e', '0_b87655', '2', '4']

In [5]: b = trimesh.load('models/CesiumMilkTruck.glb', merge_primitives=True)

In [6]: b.geometry
Out[6]: 
OrderedDict([('Cesium_Milk_Truck',
              <trimesh.Trimesh(vertices.shape=(2392, 3), faces.shape=(2088, 3))>),
             ('Wheels',
              <trimesh.Trimesh(vertices.shape=(586, 3), faces.shape=(768, 3))>)])

In [7]: b.graph.nodes_geometry
Out[7]: ['0', '2', '4']

@nikhil-convrseai
Copy link
Author

Hey, I tried this and it did achieve somewhat but still some object names are coming which are not in the scene, how can I achieve it completely as that is important part for the processing that i'm doing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants