The previous version of the state loader script I released previously was designed around the old python wrappings. With the new and improved servermanager wrappings released, I decided to update the script to handle the better way of doing things. The biggest improvement was the total removal of all eval commands from the script, which makes me allot happier.
<br><br>Even if you don't really need the script, it should provide a decent overview of the numerous new commands, and style of the servermanager wrappings. <br><br>#!/usr/bin/env python<br><br>from paraview.servermanager
import *<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> Extra Commands:<br> deleteView(name), will delete the view<br> deleteAllViews, will clear the view list<br> renameView(oldName, newName), renames the view currently called oldName, to newName
<br> containsView(name), Will tell you if the name is already being used by a view<br> listViews(), Will list all the names of the views currently made<br> debugView(name), Will dump the properties of the view, for testing
<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:'representations',2:'sources', 3:'views'}<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 deleteAllViews(self):<br> '''deletes all views from the list'''
<br> del self.view<br> self.view = list()<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 debugView(self, name):<br> '''debug print the view''' <br> for item in self.view:<br> print item.items<br> print " ------- "
<br> <br> def listViews(self):<br> '''lists all the views sorted by name'''<br> viewNames = list()<br> for item in self.view:<br> if ( (<a href="http://item.name">item.name</a> in viewNames)==False):
<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> <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> def __load(self, proxyKey, items):
<br> #load the current view object <br> proxy = self.__getProxiesInGroup(self.proxies[proxyKey]) <br> <br> #reconstruct all the properties. <br> if (proxyKey == 1):<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> 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 (proxyKey == 3):<br> proxy[name].StillRender()
<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> proxy[name].SetPropertyWithName(property, value) <br> proxy[name].UpdateVTKObjects() <br> <br> def __getProperties(self, proxyKey):
<br> #get all the data needed in a view here<br> proxy = self.__getProxiesInGroup(self.proxies[proxyKey]) <br> totalProperties = list()<br> if (proxyKey == 1):<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> return totalProperties<br> <br> def __getPropertyValues(self,proxy, name): <br> properties = list() <br> for property in proxy[name]: <br> name = property.Proxy.GetPropertyName
(property.SMProperty) <br> try: <br> value = property.GetData() <br> float(value) <br> except (IndexError,TypeError,ValueError,AttributeError,SyntaxError):
<br> #we can only pickle floats,ints, and strings. So we need a way<br> #to make sure we dont save any paraview reference. This would change<br> #if the servermanager became pickleable
<br> try:<br> float(value[0])<br> except (IndexError,TypeError,ValueError,AttributeError,SyntaxError):<br> pass<br> else:<br> properties.append( (name,value) ) <br> else:
<br> properties.append( (name,value) )<br> <br> return properties<br> <br> def __getProxiesInGroup(self,name):<br> pxm = servermanager.ProxyManager()<br> proxy = pxm.GetProxiesInGroup(name)
<br> return proxy <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> inputValue = proxy[key].Input <br> sourceNames.append(self.__proxyLabel(inputValue[0])) <br> return sourceNames<br> <br> def __proxyLabel( self, outputPort ):
<br> """Returns the given proxy's PV displayed name. Much thanks from the DumpTool for<br> doing this great function""" <br> pxm = servermanager.ProxyManager()<br> proxy =
outputPort.Proxy<br> proxyGroup = pxm.GetProxiesInGroup(self.proxies[2])<br> for name in proxyGroup:<br> if (proxyGroup[name] == proxy):<br> return name<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>