Author Topic: Manipulating viewport camera to pan around still image in ovito/python script  (Read 1886 times)


  • Newbie
  • *
  • Posts: 2
Hi -

I am attempting to write a script that will render a series of images where the camera moves in such a way as to rotate the scene. I am assuming the syntax (or pseudocode) would proceed along the lines of:

node =  import data ...
.... process data pipeline with whatever modifiers ....
vp = Viewport(
   type = Viewport.Type.PERSPECTIVE

rs = RenderSettings(
   ....rendering details

for i in range(0,numberOfFrames) :
   vp.camera_pos = ... some function of i
   vp.camera_dir = .... some function of i
   vp.fov = .... maybe some function of i


I am having a hard time deducing from the documentation the proper coordinate system of the camera position, direction vector, and field of view parameters.  In particular, where is the origin and what are the units of length associated with the viewport coordinate system?

I would generally like to be able to simply take an image and rotate it continuously around an axis (say, the z-axis), and if there is some simple script example that does this out there, I'd be very appreciative if you could share it!

Thanks in advance,

Peter Gordon

« Last Edit: May 09, 2017, 08:16:48 PM by pagordon »

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 638
Hi Peter,

Your pseudo-code looks good. That's the way it should be done.

There is no special "viewport coordinate system" in Ovito. A viewport's camera defines what can be seen through that viewport. Its coordinates and orientation (the direction vector) are specified in the global scene coordinate system. Units of length match the ones used by your simulation dataset (Ovito itself doesn't specify any particular units of length). You have to position the camera relative to your model (the simulation box). The origin of the scene coordinate system may be located in one of the corners of the simulation box or in its center. That depends on where the simulation data came from and how your simulation has been set up. Check the properties panel of the "Simulation Cell" object in Ovito to find out where the corner of the cell is positioned relative to the scene origin.

The "fov" parameter specifies an angle in radians (for a perspective projection, like in your case). I think Ovito uses 35 degrees, that is 35*math.pi/180 in radians, by default.

Let me know if you have further questions. I don't have an example script at hand. But if you get stuck again, I can write one.


  • Newbie
  • *
  • Posts: 2
Alex -

Thanks for the quick reply. Soon after the post I stumbled on the SimulationCell object and then things started to make sense to me.

In case its useful to readers, I post below a short snippet below which effectively rotates the view around the z-axis and captures a series of frames of the scene that can then be post-processed into an animation (with say, ImageMagick or your favorite movie-making tool).



.... setup your scene ...


vp = Viewport(
   type = Viewport.Type.PERSPECTIVE,
   fov = (1.0)
rs = RenderSettings(
   size = (640,640),
   background_color = (0.0,0.0,0.0)

# boxorigin is defined as center of simulation cell (in whatever coordinates of the input frame)
# use this as focus point around which camera viewport will pan
cell = node.source.cell
x0 = cell.matrix[:,3]
xlat = cell.matrix[:,0]
ylat = cell.matrix[:,1]
zlat = cell.matrix[:,2]
boxorigin = numpy.add(x0, 0.5*xlat)
boxorigin = numpy.add(boxorigin, 0.5*ylat)
boxorigin = numpy.add(boxorigin, 0.5*zlat)

# camera will circle an orbit a distance rorbit - defined in increments of 1/2 the box length
# (assuming a cubic cell domain here)
# otherwise better to take increment as the max length among cell dimensions.
nincr = 4
rorbit = nincr*numpy.linalg.norm(0.5*xlat)

# position camera over sequence to rotate around z-axis and render images
nframes = 120
for i in range(0,nframes) :
   rs.filename = 'image_' + str(i).zfill(5) + '.png'

   theta = float(i)*2.0*3.14159/float(nframes)
   px = rorbit*math.cos(theta) 
   py = rorbit*math.sin(theta) 
   pz = 0.0
   dx = -(px-boxorigin[0])
   dy = -(py-boxorigin[1])
   dz = -(pz-boxorigin[2])
   vp.camera_pos = (px,py,pz)
   vp.camera_dir = (dx,dy,dz)