Skip to content


# ConstructTree

VTKEx/Python/Graphs/ConstructTree

Other languages

See (Cxx)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

ConstructTree.py

    #!/usr/bin/env python

    import vtk

    def main():
        graph = vtk.vtkMutableDirectedGraph()

        v1 = graph.AddVertex()
        v2 = graph.AddChild(v1)
        graph.AddChild(v1)
        graph.AddChild(v2)

        #equivalent to:
        #V1 = g.AddVertex()
        #V2 = g.AddVertex()
        #V3 = g.AddVertex()
        #V4 = g.AddVertex()

        #g.AddEdge ( V1, V2 )
        #g.AddEdge ( V1, V3 )
        #g.AddEdge ( V2, V4 )

        tree = vtk.vtkTree()
        success = tree.CheckedShallowCopy(graph)
        print ('Success?', success)
        #std::cout << "Success? " << success << std::endl

        treeLayoutView = vtk.vtkGraphLayoutView()
        treeLayoutView.AddRepresentationFromInput(tree)
        treeLayoutView.SetLayoutStrategyToTree()
        treeLayoutView.ResetCamera()
        treeLayoutView.Render()
        treeLayoutView.GetInteractor().Start()

    if __name__ == '__main__':
        main()