[vtkusers] vtkMutableDirectedGraph and Python
Meehan, Bernard
MEEHANBT at nv.doe.gov
Thu Nov 7 17:28:42 EST 2013
I was trying to convert some of the C++ examples into python, mostly so
that I could learn about how all of the classes work together, and can't
seem to get a vtkMutableDirectedGraph hooked up with a vtkGraphLayout in a
pipeline Š the original code was here
(http://www.vtk.org/Wiki/VTK/Examples/Cxx/Graphs/VisualizeDirectedGraph).
Here is what I had done in Python:
import vtk
g = vtk.vtkMutableDirectedGraph()
verts = [g.AddVertex() for i in range(4)]
g.AddGraphEdge(verts[0], verts[1])
g.AddGraphEdge(verts[1], verts[2])
g.AddGraphEdge(verts[2], verts[0])
g_layout = vtk.vtkGraphLayout()
# If I uncomment this line and run, I get the following error:
# TypeError: SetInputConnection argument 1: method requires a
vtkAlgorithmOutput, a vtkMutableDirectedGraph was provided.
# g_layout.SetInputConnection(g)
# If I uncomment this line and run, I get the following error:
# AttributeError: SetInput
# g_layout.SetInput(g)
# ?? any ideas on how to hook these up ??
g_layout.SetLayoutStrategy(vtk.vtkSimple2DLayoutStrategy())
# Do layout manually before handing graph to the view. This allows us to
know
# the positions of the edge arrows.
g_layout_v = vtk.vtkGraphLayoutView()
# Tell the view to use the vertex layout we provide.
g_layout_v.SetStrategyToPassThrough()
# The arrows will be positioned on a straight line between the two
vertices so
# tell the view not to draw arcs for the parallel edges.
g_layout_v.SetEdgeLayoutStrategyToPassThrough()
# Add the graph to the view. This will render vertices and edges, but not
the
# edge arrows.
g_layout_v.AddRepresentationFromInputConnection(g_layout.GetOutputPort())
# Manually create an actor containing the glyphed arrows.
g2pd = vtk.vtkGraphToPolyData()
g2pd.SetInputConnection(g_layout.GetOutputPort())
g2pd.EdgeGlyphOutputOn()
# Set the position (0: edge start, 1: edge end) where the edge arrows
should
# go.
g2pd.SetEdgeGlyphPosition(0.98)
# Make a simple edge arrow for glyphing.
arrow = vtk.vtkArrowSource()
arrow.SetGlyphTypeToEdgeArrow()
arrow.SetScale(0.1)
arrow.Update() # necessary?
# Use a vtkGlyph3D to repeat the glyph on all edges.
glyph = vtk.vtkGlyph3D()
glyph.SetInputConnection(0, g2pd.GetOutputPort(1)) # what?
glyph.SetInputConnection(1, arrow.GetOutputPort())
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(glyph.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
g_layout_v.ResetCamera()
g_layout_v.Render()
g_layout_v.GetInteractor().Start()
More information about the vtkusers
mailing list