I am wondering how to export tubes to a .obj file. I am trying to visualize Networkx graphs with vtk and create a model, but the vtkOBJExporter() will not export the tubes. I have tried turning capping on but that did not yield any results. I have written my snippet of code that builds the tubes below. Thanks.
lines = vtk.vtkCellArray()
i=0
for e in G.edges:
u=e[0]
v=e[1]
lines.InsertNextCell(2)
for n in (u,v):
(x,y,z)=node_pos[n]
points.InsertPoint(i, x, y, z)
lines.InsertCellPoint(i)
i=i+1
edgeData.SetPoints(points)
edgeData.SetLines(lines)
Tubes = vtk.vtkTubeFilter()
Tubes.SetNumberOfSides(16)
Tubes.SetCapping(True)
Tubes.SetInputData(edgeData)
Tubes.SetRadius(0.01) # edge RADIUS
Tubes.CappingOn()
Is that your entire code? You know, vtkExport (and its subclasses) exports a scene. If you’re not creating vtkActors, etc. it indeed won’t export the tubes.
That is not my entire code my apologies. I created a vtkActor for the tubes however they are still not being exported. Thanks for the response. Below is my entire code.
# set node positions
np={}
for n in G.nodes():
np[n]=node_pos[n]
nodePoints = vtk.vtkPoints()
i=0
for (x,y,z) in np.values():
nodePoints.InsertPoint(i, x, y, z)
i=i+1
# Create a polydata to be glyphed.
inputData = vtk.vtkPolyData()
inputData.SetPoints(nodePoints)
# Use sphere as glyph source.
balls = vtk.vtkSphereSource()
balls.SetRadius(.1)
balls.SetPhiResolution(20)
balls.SetThetaResolution(20)
glyphPoints = vtk.vtkGlyph3D()
glyphPoints.SetInputData(inputData)
glyphPoints.SetSourceData(balls.GetOutput())
glyphMapper = vtk.vtkPolyDataMapper()
glyphMapper.SetInputData(glyphPoints.GetOutput())
glyph = vtk.vtkActor()
glyph.SetMapper(glyphMapper)
glyph.GetProperty().SetDiffuseColor(plum)
glyph.GetProperty().SetSpecular(.3)
glyph.GetProperty().SetSpecularPower(30)
# Generate the polyline for the spline.
points = vtk.vtkPoints()
edgeData = vtk.vtkPolyData()
# Edges
lines = vtk.vtkCellArray()
i=0
for e in G.edges:
u=e[0]
v=e[1]
lines.InsertNextCell(2)
for n in (u,v):
(x,y,z)=node_pos[n]
points.InsertPoint(i, x, y, z)
lines.InsertCellPoint(i)
i=i+1
edgeData.SetPoints(points)
edgeData.SetLines(lines)
Tubes = vtk.vtkTubeFilter()
Tubes.CappingOn()
Tubes.SetNumberOfSides(16)
Tubes.SetInputData(edgeData)
Tubes.SetRadius(0.06) # edge RADIUS
Tubes.Update()
profileMapper = vtk.vtkPolyDataMapper()
profileMapper.SetInputData(Tubes.GetOutput())
profile = vtk.vtkActor()
profile.SetMapper(profileMapper)
profile.GetProperty().SetDiffuseColor(banana)
profile.GetProperty().SetSpecular(.3)
profile.GetProperty().SetSpecularPower(30)
balls.Update()
glyphPoints.Update()
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren.AddActor(profile)
ren.AddActor(glyph)
renWin.SetSize(1000, 1000)
iren.Initialize()
renWin.Render()
iren.Start()
writer = vtk.vtkOBJExporter()
writer.SetFilePrefix('test3')
writer.SetInput(renWin)
writer.Write()
Try moving Tubes.Update() to where balls.Update() and glyphPoints.Update() are. Maybe the file writer requires that the algorithms be executed after their outputs are connected to their respective vtkActors.
Nothing changed after moving it unfortunately. Still renders it perfectly, however after exporting and opening the .obj file in Meshlab there are still no tubes. Thanks for the help by the way!
Well, maybe that is a bug. You can report it here: https://gitlab.kitware.com/vtk/vtk/-/issues . If it is not a bug you’ll get a feedback of what you might be doing wrong. Since your code is stand-alone, I think you have a good chance to have it solved. Don’t forget to include everything (e.g. imports, input data, etc.).