mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.88k stars 571 forks source link

Convert trimesh graph to network graph #1891

Open atale480 opened 1 year ago

atale480 commented 1 year ago

How can I convert I trimesh graph to network graph?

mikedh commented 1 year ago

Hey, if you mean a scene graph you can use to_networkx:

In [1]: import trimesh
m = trim
In [2]: m = trimesh.load('models/cycloidal.3DXML')

In [3]: m
Out[3]: <trimesh.Scene(len(geometry)=13)>

In [4]: m.graph.to_networkx()
Out[4]: <networkx.classes.digraph.DiGraph at 0x7fc4915d9590>
atale480 commented 1 year ago

How is this method working in for you? It is giving me error.

In [1]: import trimesh
In [2]: m = trimesh.load('../Notebooks/data/MeshData/raw/1.off')

In [3]: m
Out[3]: <trimesh.Trimesh(vertices.shape=(4706, 3), faces.shape=(9408, 3))>

In [4]: m.graph.to_networkx()
Out[4]: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[4], line 1
----> 1 m.graph.to_networkx()

AttributeError: 'Trimesh' object has no attribute 'graph'
mikedh commented 1 year ago

Oh, you have a mesh not a scene. What do you mean by "network graph"? Like the edges? You could look at mesh.edges_unique

In [5]: m = trimesh.creation.box()

In [6]: m
Out[6]: <trimesh.Trimesh(vertices.shape=(8, 3), faces.shape=(12, 3))>

In [7]: m.edges_unique
Out[7]: 
array([[0, 1],
       [0, 2],
       [0, 3],
       [1, 3],
       [2, 3],
       [0, 4],
       [1, 4],
       [2, 4],
       [1, 5],
       [4, 5],
       [2, 6],
       [4, 6],
       [5, 6],
       [1, 7],
       [2, 7],
       [3, 7],
       [5, 7],
       [6, 7]])

In [8]: import networkx as nx

In [9]: nx.from_edgelist(m.edges_unique)
Out[9]: <networkx.classes.graph.Graph at 0x7fc491aee810>

In [10]: