[vtkusers] vtkMutableDirectedGraph and Python

Meehan, Bernard MEEHANBT at nv.doe.gov
Thu Nov 7 19:09:47 EST 2013


Thanks Jeff – that worked, I appreciate it.

If anyone was interested, this works with vtkpython (VTK 6.0):

import vtk

g = vtk.vtkMutableDirectedGraph()

verts = [g.AddVertex() for i in range(3)]

g.AddGraphEdge(verts[0], verts[1])
g.AddGraphEdge(verts[1], verts[2])
g.AddGraphEdge(verts[2], verts[0])

g_layout = vtk.vtkGraphLayout()
g_layout.SetInputData(g)
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.SetLayoutStrategyToPassThrough()

# 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.vtkGlyphSource2D()
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.GetRenderer().AddActor(actor)
g_layout_v.ResetCamera()
g_layout_v.Render()
g_layout_v.GetInteractor().Start()

From: Jeff Baumes <jeff.baumes at kitware.com<mailto:jeff.baumes at kitware.com>>
Date: Thursday, November 7, 2013 3:53 PM
To: Tim Meehan <meehanbt at nv.doe.gov<mailto:meehanbt at nv.doe.gov>>
Cc: "vtkusers at vtk.org<mailto:vtkusers at vtk.org>" <vtkusers at vtk.org<mailto:vtkusers at vtk.org>>
Subject: Re: [vtkusers] vtkMutableDirectedGraph and Python

In recent VTK, SetInput has been superceeded by SetInputData. Try using that method name and it should work for you.

Jeff

On Thursday, November 7, 2013, Meehan, Bernard wrote:
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()



_______________________________________________
Powered by www.kitware.com<http://www.kitware.com>

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20131108/5d546d60/attachment.htm>


More information about the vtkusers mailing list