Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 178

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 183

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 184

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 220

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 223

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 235

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 250

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 252

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 257

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 268

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 270

Deprecated: Function create_function() is deprecated in /homepages/8/d18699663/htdocs/clickandbuilds/SMF/ovito_forum/Sources/Load.php on line 273
Manipulating viewport camera to pan around still image in ovito/python script

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

pagordon

  • 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 ....
node.add_to_scene()
node.compute()
...
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
   vp.render(rs)

----------------------------------------------------------------------------------------------

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.

pagordon

  • 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).

Best,

Peter

--------------------------------------------------------------------------------------------------------
.... setup your scene ...

node.add_to_scene()
node.compute()

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)
   vp.render(rs)

--------------------------------------------------------------------------------------------------------