How can I use SaveData to write an ASCII file in ParaView 3.98.1?

Hello,

I am writing an automation script for an old project and I need some help with pvpython from Paraview 3.98.1. The function SaveData() in this version does not exist. I found its implementation here and moved it to my code. How can I save a file as ASCII? Calling it like SaveData(filename, proxy=px, FileType='Ascii') saves my files as binaries (awkward behavior).

Kind Regards,
Anna

3.98.1 is very very old.

Any reason not to update ?

I need this version because some of my codes in the scripting pipeline handle very specific vtk files. Using the SaveData() function of the latest versions ended up creating different metadata in my final files, and when I process them it ends up destroying my results. It is easier at the moment to use an older version of Paraview than to modify all my codes.

Then just create a writer manually of the right format you want.

That is exactly what I do not want.

I would like to understand how SaveData() from version 3.98.1 works. When I save a vtk through Paraview’s graphical interface the file ends up being exactly the way I need it. I tried to use the trace of this version: it shows everything I do, except the part of saving the final file!

Because SaveData is not implemented in Python in ParaView 3.98.1

I found its implementation here and moved it to my code. The website is not working now, but it was yesterday. Maybe it is an internal problem? Anyway, the code is attached at the end of this reply.

It works and saves my files, but not as ASCII.

# -----------------------------------------------------------------------------

def SetProperties(proxy=None, **params):
	"""Sets one or more properties of the given pipeline object. If an argument
	is not provided, the active source is used. Pass a list of property_name=value
	pairs to this function to set property values. For example:
		SetProperties(Center=[1, 2, 3], Radius=3.5)
	"""
	if not proxy:
		proxy = active_objects.source
	properties = proxy.ListProperties()
	for param in params.keys():
		pyproxy = servermanager._getPyProxy(proxy)
		pyproxy.__setattr__(param, params[param])

# -----------------------------------------------------------------------------

def CreateWriter(filename, proxy=None, **extraArgs):
	"""Creates a writer that can write the data produced by the source proxy in
		the given file format (identified by the extension). If no source is
		provided, then the active source is used. This doesn't actually write the
		data, it simply creates the writer and returns it."""
	if not filename:
		raise RuntimeError ("filename must be specified")
	session = servermanager.ActiveConnection.Session
	writer_factory = servermanager.vtkSMProxyManager.GetProxyManager().GetWriterFactory()
	if writer_factory.GetNumberOfRegisteredPrototypes() == 0:
		writer_factory.UpdateAvailableWriters()
	if not proxy:
		proxy = GetActiveSource()
	if not proxy:
		raise RuntimeError ("Could not locate source to write")
	writer_proxy = writer_factory.CreateWriter(filename, proxy.SMProxy, proxy.Port)
	writer_proxy.UnRegister(None)
	pyproxy = servermanager._getPyProxy(writer_proxy)
	if pyproxy and extraArgs:
		SetProperties(pyproxy, **extraArgs)
	return pyproxy

# -----------------------------------------------------------------------------

def SaveData(filename, proxy=None, **extraArgs):
	"""Save data produced by 'proxy' in a file. If no proxy is specified the
	active source is used. Properties to configure the writer can be passed in
	as keyword arguments. Example usage:
		SaveData("sample.pvtp", source0)
		SaveData("sample.csv", FieldAssociation="Points")
	"""
	writer = CreateWriter(filename, proxy, **extraArgs)
	if not writer:
		raise RuntimeError ("Could not create writer for specified file or data type")
	writer.UpdateVTKObjects()
	writer.UpdatePipeline()
	del writer
	
# -----------------------------------------------------------------------------

Does this work ?

SaveData(filename, proxy=px, DataMode='Ascii')

The generated files are still binary. :frowning:

You’ll have to dig into the C++ code and run in debug mode to understand what is going on. Not a trivial task though.

1 Like

I found out I can also do the following to save my file as ASCII in this version:

write = servermanager.writers.DataSetWriter()
write.Input = vtk_file
write.FileName = filename
write.FileType = 'Ascii'
write.UpdatePipeline()

I do not know, however, how to link a proxy (see SaveData's call) using this approach since the class does not have a Proxy attribute.

Right, DataMode is for XML writers.

The link in question should be automatic (thanks to the SetProperties call) and your initial code seems correct.

1 Like

I edited the SaveData() implementation post because I forgot to show the main function.

Well, I used SaveData() to save a binary file with the proxy I need and then used DataSetWriter() to change my FileType to ASCII. It is not a beautiful solution since SaveData() is supposed to do that, but it does the job for now.