pymaid
pymaid copied to clipboard
Exporting skeletons as object files
I know that there's a feature in the GUI to export skeleton selections as .obj files, but can one do the same using PyMaid?
Yes, in theory! In practice it depends a bit on what exactly it is you are after.
The obj format is (to my knowledge) most commonly used for meshes, i.e. an object with faces and vertices. It's pretty straight forward to convert a skeleton into a tubular mesh. OBJ appears to also support curve/line objects though. Not sure which of the two option CATMAID is exporting?
Thanks for the response! I'm looking for a mesh file that I can pop into other rendering software. But would like to do it programmatically. Do you have any recommendations for accomplishing that from python using the coordinate and radius data?
That functionality fortunately already exists. Here's a minimal example:
>>> import pymaid
>>> import navis
>>> # Grab your CATMAID skeleton
>>> s = pymaid.get_neuron()
>>> type(s)
pymaid.core.CatmaidNeuron
>>> # Turn the skeleton into a tubular mesh
>>> # You can tune the resolution using `tube_points` parameter
>>> m = navis.conversion.tree2meshneuron(s)
>>> type(m)
navis.core.mesh.MeshNeuron
>>> # Save as OBJ
>>> _ = m.trimesh.export('skeleton_mesh.obj')
Original skeleton in yellow; mesh generated from skeleton in blue.
A couple notes:
- These meshes can get fairly large. You could consider e.g. downsampling the neuron before or after turning it into a mesh.
- The mesh will look funny if any of the node radii are missing or <=0 and the function will give a warning if that's the case. You can clip radii at a minimum radius like so:
>>> s.nodes.loc[s.nodes.radius.fillna(0) <= 0), 'radius'] = 100