Author Topic: Displaying/saving movies via the Python scripting interface  (Read 1906 times)

alexcr

  • Newbie
  • *
  • Posts: 11
Hi,

I'm trying to produce (show / save) animated movies via the Python scripting interface but it's not working. Does anyone happen to have a minimal working example accomplishing this?

Thanks in advance,
Alex

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 638
Re: Displaying/saving movies via the Python scripting interface
« Reply #1 on: June 02, 2017, 09:14:03 PM »
Hi Alex,
here is a simple example:

Code: [Select]
import ovito
from ovito.io import *
from ovito.vis import *

node = import_file("animation.dump.gz", multiple_frames = True)
node.add_to_scene()

vp = ovito.dataset.viewports.active_vp

settings = RenderSettings(
    filename = "movie.avi",
    size = (640, 480),
    range = RenderSettings.Range.ANIMATION       
)
vp.render(settings)

alexcr

  • Newbie
  • *
  • Posts: 11
Re: Displaying/saving movies via the Python scripting interface
« Reply #2 on: June 02, 2017, 09:40:57 PM »
Hi,

Thanks for the quick response, much appreciated. However it doesn't quite work. I do realize I had forgotten to provide a couple of details about the files I'm using. Those are .xyz individual files, i.e. there is one file per frame.

When I run the example you provided with the graphical interface, Ovito hangs.

I had previously tried this script :

Code: [Select]
file_sequence = sys.argv[1] + "_*_M.xyz"
node = import_file(file_sequence, columns = ["Particle Type", "Position.X", "Position.Y", "Position.Z"]) 

node.add_to_scene()

vp = ovito.dataset.viewports.active_vp
rs = RenderSettings(filename="anim.mp4", range = RenderSettings.Range.ANIMATION, size = (320,240), renderer = TachyonRenderer())   
anim = vp.render(rs)
anim.save("anim.mp4")

This seems to load the files correctly into Ovito's GUI, however it produces an empty (zero size) mp4 file.

Alex
« Last Edit: June 02, 2017, 09:50:00 PM by alexcr »

alexcr

  • Newbie
  • *
  • Posts: 11
Re: Displaying/saving movies via the Python scripting interface
« Reply #3 on: June 02, 2017, 09:51:32 PM »
By removing the last line of the script I posted, it seems to work. However this doesn't allow me to write additional text on the frames, but I can't figure out the correct syntax with the "save" method.

Additionally, I'd like to concatenate three datasets into a single animation. Is that possible with Ovito's scripting interface?

Thanks again,
Alex

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 638
Re: Displaying/saving movies via the Python scripting interface
« Reply #4 on: June 03, 2017, 05:32:13 PM »
The Viewport.render() method return an image object, which may be manipulated before saving it to a file with the save() method. But this shortcut is not meant for movies, only single frames.

For movies, you have to add a Python viewport overlay to the viewport being rendered. This is basically a custom Python function that gets called by OVITO every time a movie frame from the sequence is rendered. It allows you to draw on top of the rendered images before they are appended to the encoded movie file.

As you have already figured out yourself, you can use '*' in the filename passed to import_file() to load a sequence of data files. However, concatenating multiple sequences (with different base names or in different directories) is currently not supported. You will have to rename the files and put them all into a single directory to produce a single movie from them. Alternatively, you could render three sequences of image files with OVITO and then encode them all to a single video file using some external tool.

alexcr

  • Newbie
  • *
  • Posts: 11
Re: Displaying/saving movies via the Python scripting interface
« Reply #5 on: June 03, 2017, 05:57:28 PM »
Excellent. I'll try to add a viewport overlay then, and rename the files accordingly.

Thanks again for your answer. I appreciate it.

Have a nice weekend,
Alex