Python wrapper for running instances of trisurf-ng
Samo Penic
2017-01-14 f49271578b8fac02db14966e7aab121879f9b736
commit | author | age
8ab985 1 import os,sys
SP 2 from . import trisurf
8b7022 3 try:
8ab985 4     from vtk import *
8b7022 5 except:
SP 6     print("Vtk rendering works if you manually install vtk7 for python3")
7     exit(1)
8ab985 8
SP 9 class MultiRender:
10     def __init__(self,args,host):
11         target_runs=getRargetRunIdxList(args)
12         if target_runs==None:
13             target_runs=list(range(1,len(host['runs'])+1))
14         nruns=len(target_runs)
15         #prepare rendering window
16         self.renderer_window = vtkRenderWindow()
17         self.renderer_window.AddRenderer(self.renderer)
18         self.renderer_window.SetSize(1200,600)
19         interactor = vtkRenderWindowInteractor()
20         interactor.SetRenderWindow(self.renderer_window)
21         interactor.Initialize()
22         interactor.AddObserver("TimerEvent", self.RenderUpdate)
23         timerIDR = interactor.CreateRepeatingTimer(1000)
24         self.filename=[]
25         self.renderer=[]
26         i=0
27         for run in target_runs:    
28             #for each target run calculate renderer location
29             r.vtkRenderer()
30             r.SetBackground(0,0,0)
31             p=1.0/float(nruns)
32             x1=i*p
33             x2=(i+1)*p
34             r.SetViewport(x1,0.0,x2,1.0)
35             self.renderer.Append(r)
36             self.renderer_window.AddRenderer(r)
37             i=i+1
38         #call Renderer object with Run, renderer
39         #start endless loop of interactor
40         interactor.Start()
41
42     def lastVTU(self,run):
43         Dir=trisurf.Directory(maindir=run.maindir,simdir=run.subdir)
44         filename=os.path.join("./",Dir.fullpath(),run.getLastVTU())
45         return filename
46
47     def lastActorForRun(self,run):
48         filename=self.lastVTU(run)
49         reader=vtkXMLUnstructuredGridReader()
50         reader.SetFileName(self.filename)
51         reader.Update() # Needed because of GetScalarRange
52         output = reader.GetOutput()
53         scalar_range = output.GetScalarRange()
54         mapper = vtkDataSetMapper()
8b7022 55         mapper.SetInputData(output)
8ab985 56         mapper.SetScalarRange(scalar_range)
SP 57
58         # Create the Actor
59         actor = vtkActor()
60         actor.SetMapper(mapper)
61         return actor
62
63
64
65     def RenderUpdate(self, obj, event):
66         i=0
67         for run in runs:
68             if(self.lastVTU(run)!=self.filename[i]):
69                 #print("updejt")
70                 self.renderer.RemoveActor(self.actor)
71                 self.renderer.RemoveActor(self.textactor)
72                 self.actor=self.lastActor()
73                 self.textactor=self.textActor()
74                 self.renderer.AddActor(self.actor)
75                 self.renderer.AddActor(self.textactor)
76                 self.renderer_window.Render()
77             #self.render.RemoveActor(self.actor)
78             i=i+1    
79         return
80
81
82 class Renderer:
f49271 83     def __init__(self,args,host,run):
8ab985 84         self.host=host
SP 85         self.args=args
f49271 86         self.run=run
8ab985 87         self.renderer = vtkRenderer()
SP 88         self.actor=self.lastActor()
89         self.textactor=self.textActor()
90         self.renderer.AddActor(self.actor)
91         self.renderer.AddActor(self.textactor)
92         self.renderer.SetBackground(0, 0, 0) # Set background to white
93         # Create the RendererWindow
94         self.renderer_window = vtkRenderWindow()
95         self.renderer_window.AddRenderer(self.renderer)
96         self.renderer_window.SetSize(1200,600)
97     
f49271 98 #        self.renderer.SetViewport(0.0,0.0,0.5,1.0)
SP 99 #        rend=vtk.vtkRenderer()
100 #        rend.AddActor(self.actor)
101 #        rend.SetViewport(0.5,0.0,1.0,1.0)
102 #        self.renderer_window.AddRenderer(rend)    
8ab985 103 # Set up a check for aborting rendering.
SP 104         # Create the RendererWindowInteractor and display the vtk_file
105         interactor = vtkRenderWindowInteractor()
106         interactor.SetRenderWindow(self.renderer_window)
107         interactor.Initialize()
427578 108         interactor.AddObserver("TimerEvent", self.RenderUpdate)
f49271 109         timerIDR = interactor.CreateRepeatingTimer(10000)
8ab985 110         interactor.Start()
SP 111         return
112
113     def lastVTU(self):
f49271 114         #Dir=trisurf.Directory(maindir=self.host['runs'][self.run].maindir,simdir=self.host['runs'][self.run].subdir)
SP 115         Dir=self.run.Dir
116         #print(self.run.getLastVTU())
117         filename=os.path.join("./",Dir.fullpath(),self.run.getLastVTU())
118         #filename=os.path.join("./",Dir.fullpath(),self.host['runs'][self.run].getLastVTU())
8ab985 119         return filename
SP 120
121     def textActor(self):
122         textactor=vtkTextActor()
123         textactor.SetInput(self.filename)
124         tp=textactor.GetTextProperty()
125         tp.SetColor(1,1,1)
126         tp.SetFontSize(11)
127         textactor.SetDisplayPosition(20,30)
128         return textactor
129
130     def lastActor(self):
131         self.filename=self.lastVTU()
132         reader=vtkXMLUnstructuredGridReader()
133         reader.SetFileName(self.filename)
134         reader.Update() # Needed because of GetScalarRange
135         output = reader.GetOutput()
136         scalar_range = output.GetScalarRange()
137         mapper = vtkDataSetMapper()
8b7022 138         mapper.SetInputData(output)
8ab985 139         mapper.SetScalarRange(scalar_range)
SP 140
141         # Create the Actor
142         actor = vtkActor()
143         actor.SetMapper(mapper)
144         return actor
145
146
147     def RenderUpdate(self, obj, event):
148         if(self.lastVTU()!=self.filename):
149             #print("updejt")
150             self.renderer.RemoveActor(self.actor)
151             self.renderer.RemoveActor(self.textactor)
152             self.actor=self.lastActor()
153             self.textactor=self.textActor()
154             self.renderer.AddActor(self.actor)
155             self.renderer.AddActor(self.textactor)
156             self.renderer_window.Render()
157         #self.render.RemoveActor(self.actor)
158         
159         return