Python wrapper for running instances of trisurf-ng
Samo Penic
2017-01-14 d2f7b0c053e93b4d409d9661dbaff2eedcf391c1
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:
5f0a35 83     def __init__(self,args,host,run, timestep=-1):
8ab985 84         self.host=host
SP 85         self.args=args
f49271 86         self.run=run
5f0a35 87         self.timestep=timestep
8ab985 88         self.renderer = vtkRenderer()
SP 89         self.actor=self.lastActor()
90         self.textactor=self.textActor()
91         self.renderer.AddActor(self.actor)
92         self.renderer.AddActor(self.textactor)
93         self.renderer.SetBackground(0, 0, 0) # Set background to white
94         # Create the RendererWindow
95         self.renderer_window = vtkRenderWindow()
96         self.renderer_window.AddRenderer(self.renderer)
97         self.renderer_window.SetSize(1200,600)
98     
f49271 99 #        self.renderer.SetViewport(0.0,0.0,0.5,1.0)
SP 100 #        rend=vtk.vtkRenderer()
101 #        rend.AddActor(self.actor)
102 #        rend.SetViewport(0.5,0.0,1.0,1.0)
103 #        self.renderer_window.AddRenderer(rend)    
8ab985 104 # Set up a check for aborting rendering.
SP 105         # Create the RendererWindowInteractor and display the vtk_file
106         interactor = vtkRenderWindowInteractor()
107         interactor.SetRenderWindow(self.renderer_window)
108         interactor.Initialize()
427578 109         interactor.AddObserver("TimerEvent", self.RenderUpdate)
f49271 110         timerIDR = interactor.CreateRepeatingTimer(10000)
8ab985 111         interactor.Start()
SP 112         return
113
114     def lastVTU(self):
f49271 115         #Dir=trisurf.Directory(maindir=self.host['runs'][self.run].maindir,simdir=self.host['runs'][self.run].subdir)
SP 116         Dir=self.run.Dir
117         #print(self.run.getLastVTU())
118         filename=os.path.join("./",Dir.fullpath(),self.run.getLastVTU())
119         #filename=os.path.join("./",Dir.fullpath(),self.host['runs'][self.run].getLastVTU())
8ab985 120         return filename
SP 121
122     def textActor(self):
123         textactor=vtkTextActor()
124         textactor.SetInput(self.filename)
125         tp=textactor.GetTextProperty()
126         tp.SetColor(1,1,1)
127         tp.SetFontSize(11)
128         textactor.SetDisplayPosition(20,30)
129         return textactor
130
131     def lastActor(self):
5f0a35 132         if(self.timestep<0):
SP 133             self.filename=self.lastVTU()
134         else:
135             self.filename=os.path.join("./",self.run.Dir.fullpath(),'timestep_{:06d}.vtu'.format(self.timestep))
8ab985 136         reader=vtkXMLUnstructuredGridReader()
SP 137         reader.SetFileName(self.filename)
138         reader.Update() # Needed because of GetScalarRange
139         output = reader.GetOutput()
140         scalar_range = output.GetScalarRange()
141         mapper = vtkDataSetMapper()
8b7022 142         mapper.SetInputData(output)
8ab985 143         mapper.SetScalarRange(scalar_range)
SP 144
145         # Create the Actor
146         actor = vtkActor()
147         actor.SetMapper(mapper)
148         return actor
149
150
151     def RenderUpdate(self, obj, event):
152         if(self.lastVTU()!=self.filename):
153             #print("updejt")
154             self.renderer.RemoveActor(self.actor)
155             self.renderer.RemoveActor(self.textactor)
156             self.actor=self.lastActor()
157             self.textactor=self.textActor()
158             self.renderer.AddActor(self.actor)
159             self.renderer.AddActor(self.textactor)
160             self.renderer_window.Render()
161         #self.render.RemoveActor(self.actor)
162         
163         return