Submitted by Anonymous on Tue, 2011-03-15 13:01
Posted in
Hi everyone,
This page has been created to introduce basic concepts of python scripting in Tulip (available since 3.5.0).
You'll find here various python snippets, the goal is to cover various part of Tulip libraries (tulip, OGL) and to provide users with simple examples to start with.
I - DFS, BFS:
This first snippet introduces two common algorithms for graph manipulation: DFS and BFS. Source come uncommented since they are pretty straightforward. Feel free to post comments or question on this tutorial if you need extra help:
# To cancel the modifications performed by the script # on the current graph, click on the undo button. from tulip import * from math import * from random import * from collections import deque # the updateVisualization() function can be called # during script execution to update the opened views # the main(graph) function must be defined # to run the script on the current graph def dfs(n, graph): marker = graph.getIntegerProperty("see"); marker.setNodeValue(n, marker.getNodeValue(n) + 1) if (marker.getNodeValue(n) > 1): return color = graph.getColorProperty("viewColor"); color.setNodeValue(n, tlp.Color(0, 255, 0 , 255)); updateVisualization(False) for ni in graph.getInOutNodes(n): dfs(ni, graph) color.setNodeValue(n, tlp.Color(0, 128, 128 , 255)); #==================================== def bfs(n, graph): marker = graph.getIntegerProperty("see"); color = graph.getColorProperty("viewColor"); fifo = deque([]) fifo.append(n) while(len(fifo) > 0): n = fifo.popleft() color.setNodeValue(n, tlp.Color(0, 128, 128 , 255)); for ni in graph.getInOutNodes(n): marker.setNodeValue(ni, marker.getNodeValue(ni) + 1) if (marker.getNodeValue(ni) < 2): color.setNodeValue(ni, tlp.Color(0, 255, 0 , 255)); fifo.append(ni); updateVisualization() #======================================== def main(graph) : # insert your script code here color = graph.getColorProperty("viewColor"); marker = graph.getIntegerProperty("see"); marker.setAllNodeValue(0) color.setAllNodeValue(tlp.Color(255,0,0,125)); updateVisualization(False) bfs(graph.getOneNode(), graph);
II- Regular polygons:
This snippet generate regular polygons on a graph's nodes:
# Powered by Python 2.6 # To cancel the modifications performed by the script # on the current graph, click on the undo button. from tulip import * import math # the updateVisualization() function can be called # during script execution to update the opened views # the main(graph) function must be defined # to run the script on the current graph def genRegularPolygonGraph(graph, viewLayout, numberOfSides, startAngle, sizeFactor): graph.clear() delta = (2.0* math.pi) / numberOfSides firstNode = None lastNode = None for i in range(numberOfSides): deltaX = math.cos(i * delta + startAngle) * sizeFactor deltaY = math.sin(i * delta + startAngle) * sizeFactor n = graph.addNode() viewLayout.setNodeValue(n, tlp.Coord(deltaX,deltaY,0)) if i == 0: firstNode = n else: graph.addEdge(lastNode, n) lastNode = n graph.addEdge(lastNode, firstNode) def main(graph) : viewBorderColor = graph.getColorProperty("viewBorderColor") viewBorderWidth = graph.getDoubleProperty("viewBorderWidth") viewColor = graph.getColorProperty("viewColor") viewFontSize = graph.getIntegerProperty("viewFontSize") viewLabelColor = graph.getColorProperty("viewLabelColor") viewLabelPosition = graph.getIntegerProperty("viewLabelPosition") viewLabel = graph.getStringProperty("viewLabel") viewLayout = graph.getLayoutProperty("viewLayout") viewMetaGraph = graph.getGraphProperty("viewMetaGraph") viewRotation = graph.getDoubleProperty("viewRotation") viewSelection = graph.getBooleanProperty("viewSelection") viewShape = graph.getIntegerProperty("viewShape") viewSize = graph.getSizeProperty("viewSize") viewSrcAnchorShape = graph.getIntegerProperty("viewSrcAnchorShape") viewSrcAnchorSize = graph.getSizeProperty("viewSrcAnchorSize") viewTgtAnchorShape = graph.getIntegerProperty("viewTgtAnchorShape") viewTgtAnchorSize = graph.getSizeProperty("viewTgtAnchorSize") for i in range(3,100): genRegularPolygonGraph(graph, viewLayout, i, 0, 20) viewLabel.setAllNodeValue(str(i)) updateVisualization()
from tulip import * # the updateVisualization(centerViews = True) function can be called # during script execution to update the opened views # the main(graph) function must be defined # to run the script on the current graph def main(graph) : viewBorderColor = graph.getColorProperty("viewBorderColor") viewBorderWidth = graph.getDoubleProperty("viewBorderWidth") viewColor = graph.getColorProperty("viewColor") viewFont = graph.getStringProperty("viewFont") viewFontSize = graph.getIntegerProperty("viewFontSize") viewLabel = graph.getStringProperty("viewLabel") viewLabelColor = graph.getColorProperty("viewLabelColor") viewLabelPosition = graph.getIntegerProperty("viewLabelPosition") viewLayout = graph.getLayoutProperty("viewLayout") viewMetaGraph = graph.getGraphProperty("viewMetaGraph") viewRotation = graph.getDoubleProperty("viewRotation") viewSelection = graph.getBooleanProperty("viewSelection") viewShape = graph.getIntegerProperty("viewShape") viewSize = graph.getSizeProperty("viewSize") viewSrcAnchorShape = graph.getIntegerProperty("viewSrcAnchorShape") viewSrcAnchorSize = graph.getSizeProperty("viewSrcAnchorSize") viewTexture = graph.getStringProperty("viewTexture") viewTgtAnchorShape = graph.getIntegerProperty("viewTgtAnchorShape") viewTgtAnchorSize = graph.getSizeProperty("viewTgtAnchorSize") layoutResult = graph.getLayoutProperty("layoutResult") treatedNodes = [] for n in graph.getNodes(): nodesEdges = {} layoutResult[n] = viewLayout[n] if(not n in treatedNodes): treatedNodes.append(n) for e in graph.getInOutEdges(n): opposite = graph.opposite(e, n) treatedNodes.append(opposite) if(not opposite in nodesEdges): nodesEdges[opposite] = [] nodesEdges[opposite].append(e) for n2 in nodesEdges: sourcePos = viewLayout[n] targetPos = viewLayout[n2] vector = sourcePos - targetPos vector /= vector.norm() normal = tlp.Coord(-vector.getY(), vector.getX(), vector.getZ()) xPlus = 0.5 xMinus = -0.5 for e in nodesEdges[n2]: isInEdge = graph.target(e) == n2 if isInEdge: x = xPlus else: x = xMinus quarterControlPoint = tlp.Coord(sourcePos + (targetPos - sourcePos)/4 + normal * x) threeQuartersControlPoint = tlp.Coord(sourcePos + (targetPos - sourcePos)*3/4 + normal * x) if(isInEdge): xPlus += 0.5; else: xMinus -= 0.5; newEdge = [] if (n == graph.source(e)): newEdge.append(quarterControlPoint) newEdge.append(threeQuartersControlPoint) else: newEdge.append(threeQuartersControlPoint) newEdge.append(quarterControlPoint) viewLayout[e] = newEdge print n