I have built a python state saving script for paraview. The reason for the script was that while the current paraview lookmark feature allows the user to apply large pipelines onto different data sets, there is no easy way to switch between different custom views.
<br><br>By this I mean where the camera is, or what multiple objects are colored by, or a threshold values. With this script you can build a view you want, than store it. For example say you want to save a view that is looking from x+, coloring on z elevation. Than later you can load
<br>this view back, and that will cause all the objects to revert to the properties you had when you save the state, in this case your camera would return to x+ and the coloring would revert to z elevation.<br><br>If you have any questions please feel free to email me.
<br><br>#Source Code Follows<br><br>#!/usr/bin/env python<br>#Created by Robert Maynard, 2007<br>#MIRARCO - Mining Innovation<br>#use at your own risk<br><br>import paraview<br>import cPickle as pickle<br><br>class State:
<br> '''Allows the user to save states, which is all the display properties for each Pipeline object and camera. After saving<br> states you can than load them back up, reverting all the object and camera properties to what they where
<br> on saving. The set of views can also be saved to a file by using pickle so that you can build up multiple viewStates<br> and than display them at a latter time<br> <br> Currently Supports:<br> saving a state by calling
<br> addView(statename) statename can be numeric / string<br> <br> Loading a state:<br> loadView(statename) will load the respective state. If nothing changes<br> click on the render window of paraview, that should fix the render display.
<br> <br> Pickle Saving all States:<br> saveState(filepath+filename) will pickle all the states so that you can load them up at a different time<br> <br> loadState(filepath+filename) will unpickle all the states so that you can run previously generated states
<br> <br> Bug:<br> Currently items added after a view is stored are not turned off/on when loading view that do not effect <br> that item. This may or may not be helpfull to you<br> <br> <br> '''
<br> def __init__(self):<br> #storage for a list of view objects<br> self.view = list() <br> self.proxies = {1:'displays',2:'sources', 3:'view_modules'} <br> <br> def loadState(self,file):
<br> '''loads a pickle view file, the passed in value needs<br> to be a string that is the path to the pickled view'''<br> #loads a State file<br> try:<br> #open an ascii pickle file
<br> self.view = pickle.load(open(file))<br> except IOError:<br> print "file not found" <br> <br> def saveState(self,file):<br> '''save the current view collection as a ascii pickle'''
<br> pickle.dump(self.view,open(file,'w'))<br> <br> def deleteView(self,name):<br> '''deletes the first occurance of a name from the view list'''<br> for item in self.view:<br>
if (<a href="http://item.name">item.name</a> == name):<br> del item<br> return True<br> return False <br> <br> def renameView(self,oldName, newName):<br> '''rename a view with a new name'''
<br> for item in self.view:<br> if (<a href="http://item.name">item.name</a> == oldName):<br> <a href="http://item.name">item.name</a> = newName<br> return True<br> return False <br> <br> def containsView(self,name):
<br> '''checks to see if a view with the name already exists'''<br> return (name in self.view)<br> <br> def listViews(self):<br> '''lists all the views sorted by name'''
<br> viewNames = list()<br> for item in self.view:<br> viewNames.append(<a href="http://item.name">item.name</a>)<br> #collect all the views and than sort them<br> viewNames.sort()<br> #display all the names
<br> for name in viewNames:<br> print name<br> def loadView(self,name):<br> '''loads a select view that has already be saved.<br> ex: state.loadView(1)'''<br> for v in self.view
:<br> if (<a href="http://v.name">v.name</a> == name): <br> self.__load(v.proxy, v.items) <br> <br> def addView(self,name):<br> '''adds the current state of all items in paraview + cameras as a view.
<br> loading this view will restore all the saved items to the current state<br> ex: state.addView(1)'''<br> for key in self.proxies.keys(): <br> newView = view(name, key, self.__getProperties(key))
<br> self.view.append(newView)<br> <br> <br> def __load(self, proxyKey, items):<br> #load the current view object <br> proxy = paraview.pyProxyManager().GetProxiesInGroup(self.proxies[proxyKey])
<br> <br> #reconstruct all the properties. <br> if (self.proxies[proxyKey] == 'display'):<br> sources = self.__buildSourceNames(proxy)<br> for sourceName,disp in zip(sources,proxy):<br> for i in items:
<br> if (i[0] == sourceName):<br> self.__reconstructDisplay(proxy, disp, i[1]) <br> break<br> else: <br> for name in proxy:<br> for i in items:
<br> if (i[0] == name):<br> self.__reconstructDisplay(proxy, name, i[1])<br> if (self.proxies[proxyKey] == 'view_modules'):<br> proxy[name].StillRender() <br> break
<br> <br> def __reconstructDisplay(self,proxy, name, propertyList):<br> #sets all the properties back to their stored values, by<br> #using eval. Hopefully a safer way is found to do this<br> for items in propertyList:
<br> property = items[0] <br> value = items[1] <br> if (len(value) == 1): <br> eval('proxy[name].%s(value[0])' %('Set'+property))<br> elif (len(value) == 2):
<br> eval('proxy[name].%s(value[0],value[1])' %('Set'+property))<br> elif (len(value) == 3):<br> eval('proxy[name].%s(value[0],value[1],value[2])' %('Set'+property))
<br> proxy[name].UpdateVTKObjects() <br> <br> def __getProperties(self, proxyKey): <br> #get all the data needed in a view here<br> proxy = paraview.pyProxyManager().GetProxiesInGroup(self.proxies
[proxyKey]) <br> totalProperties = list()<br> if (self.proxies[proxyKey] == 'display'):<br> #build a list of source names that correspond to the display objects<br> #so that load order wont effect display assocation
<br> sourceNames = self.__buildSourceNames(proxy) <br> #get all the properties from each display object <br> for name,item in zip(sourceNames,proxy): <br> totalProperties.append( (name ,self.__getPropertyValues(proxy,item)) )
<br> else:<br> #using the stored name for everything else<br> for name in proxy:<br> totalProperties.append( (name ,self.__getPropertyValues(proxy,name)) ) <br> <br> return totalProperties
<br> <br> def __getPropertyValues(self,proxy, name): <br> propertyList = proxy[name].ListProperties()<br> properties = list() <br> for property in propertyList: <br> try:
<br> value = eval('proxy[name].%s()' %("Get"+property)) <br> float(value[0]) <br> properties.append( (property,value) )<br> #there is a lot of properties we cant save, stuff that is references to other paraview objects
<br> #and methods that need no paramaters, instead of making a list of those, instead we just ignore<br> #the errors and dont add them to the list of properties we will store<br> except IndexError:<br>
pass<br> except TypeError:<br> pass<br> except ValueError:<br> pass<br> except AttributeError:<br> pass<br> except SyntaxError:<br> pass<br> return properties
<br> <br> def __buildSourceNames(self, proxy):<br> #builds all the source names for each display, by using the <br> #dump tool procyLabel method <br> sourceNames = list()<br> for key in proxy.keys
():<br> sourceNames.append(self.__proxyLabel(proxy[key].GetInput()[0]))<br> return sourceNames<br> <br> def __proxyLabel( self, proxy ):<br> """Returns the given proxy's PV displayed name. Much thanks from the DumpTool for
<br> doing this great function""" <br> proxyVTK = proxy.SMProxy<br> pmanager = paraview.pyProxyManager()<br> proxies = paraview.pyProxyManager().GetProxiesOnConnection(paraview.ActiveConnection
)<br> for group in proxies:<br> idLabel = pmanager.IsProxyInGroup( proxyVTK, group )<br> if idLabel:<br> return idLabel<br> return None<br> <br>class view:<br> #merely a storage container of view properties
<br> def __init__(self, name, proxy, items):<br> self.proxy = proxy<br> <a href="http://self.name">self.name</a> = name<br> self.items = items<br> <br> def __getstate__(self): <br> #custom pickle data, so that all the items are stored on pickling
<br> pickleDict = dict()<br> pickleDict['proxy']=self.proxy<br> pickleDict['items']=self.items<br> pickleDict['name']=<a href="http://self.name">self.name</a><br> return pickleDict<br>
<br> def __setstate__(self,dict):<br> #restore the items and view name when unpickling<br> self.proxy = dict['proxy']<br> self.items = dict['items']<br> <a href="http://self.name">self.name</a>
= dict['name']<br> self.__dict__.update(dict) # update attributes <br>