Author Topic: Python script slice modifier  (Read 112 times)

Haomin

  • Newbie
  • *
  • Posts: 11
Python script slice modifier
« on: January 30, 2019, 07:39:31 PM »
Dear all,

I read the documentation of SliceModifier. It doesn't take the 'Align view direction to plane normal' as an argument. I want to render plenty of image with aligning the view direction to plane normal. If anyone has any suggestion on how to implement with the python script?

Thanks,
Haomin

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 197
Re: Python script slice modifier
« Reply #1 on: January 31, 2019, 11:27:57 AM »
Dear Haomin,

just to confirm, your rendering images programmatically and you're asking about the Slice modifier in the Python API, right?
Note that the Slice modifier takes an argument "normal", which is the normal vector of the slicing plane and by default is set to (1,0,0). You can adapt that to your needs, e.g.

Code: [Select]
from ovito modifiers import SliceModifier
pipeline.modifiers.append( SliceModifier( normal = ( 1, 1, 2)) )
https://ovito.org/manual_testing/python/modules/ovito_modifiers.html#ovito.modifiers.SliceModifier

Edit:   You can then set up the viewport’s virtual camera and e.g. control the viewing direction vector like this:

Code: [Select]
from ovito.vis import Viewport
vp = Viewport(type = Viewport.Type.Ortho, camera_dir = (1, 1, 2))
vp.zoom_all()
https://ovito.org/manual_testing/python/modules/ovito_vis.html,


-Constanze
« Last Edit: February 06, 2019, 12:00:09 PM by Constanze Kalcher »

Haomin

  • Newbie
  • *
  • Posts: 11
Re: Python script slice modifier
« Reply #2 on: February 01, 2019, 09:59:22 PM »
Dear Constanze,

Thanks for your reply. It solves my question.

Haomin

Haomin

  • Newbie
  • *
  • Posts: 11
Render camera_dir problem
« Reply #3 on: February 05, 2019, 08:37:43 PM »
Dear all,

I was trying to adjust my camera position as well as direction for each loop of the rendering using the python API. However, I got this error:

AttributeError: 'ovito.plugins.PyScript.Viewport' object attribute 'camera_dir' is read-only

I am using Ovito 3.0.0. Is anyone know how to solve it?

Thanks,
Haomin
« Last Edit: February 05, 2019, 08:40:35 PM by Haomin »

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 197
Re: Render camera_dir problem
« Reply #4 on: February 05, 2019, 09:37:38 PM »
Dear Haomin,

please post the code that's causing you this error.

-Constanze

Haomin

  • Newbie
  • *
  • Posts: 11
Re: Python script slice modifier
« Reply #5 on: February 06, 2019, 10:28:03 PM »
Dear Constanze,

This is my code:

Code: [Select]
def render(num, position, pipeline):
    # save images
    end = len(position)
    start = 0
    selectAtoms = genRandomAtoms(start, end, num, position) # pick random point in the structure
    vectors = genVector(num) # generate random vector
    vp = Viewport(type = Viewport.Type.Ortho)
    # seeting appearance of particles
    appearance(pipeline)
    for i in range(num):
        v1 = selectAtoms[i]
        v2 = vectors[i]
        norm_v2 = math.sqrt(v2[0] ** 2 + v2[1] ** 2 + v2[2] ** 2)
        d = distance(v1, v2)
        viewDis = 100 # setting viewing distance
        x, y, z = v2[0]/norm_v2, v2[1]/norm_v2, v2[2]/norm_v2
        l, j, k = v1[0] + x * viewDis, v1[1] + y * viewDis, v1[2] + z * viewDis
        pipeline.modifiers[2].normal = (x, y, z) # Adjust slicing params
        pipeline.modifiers[2].distance = d
        vp.camera_dir = (x, y, z) # adjust camera direction
        vp.camera_pos = (l, j, k) # adjust camera position
        #vp.zoom_all()
        image = vp.render_image(size = (480, 480), \
                                background = (0,0,0), \
                                renderer = OpenGLRenderer())
        image.save('/Users/haominliu/Downloads/image%r.png'%i)

I just restarted the software and then it works. But I still have another question. I tried two different camera position (l, j, k) and 100*(l, j, k), but the images look the same. Am I doing wrong in controlling the scale? I want to have a zoom out image.

Thank you,
Haomin
« Last Edit: February 06, 2019, 10:30:41 PM by Haomin »

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 197
Re: Python script slice modifier
« Reply #6 on: February 07, 2019, 12:16:47 PM »
Dear Haomin,

from the code snippet you sent I cannot find any reason why you would get an attribute error
AttributeError: 'ovito.plugins.PyScript.Viewport' object attribute 'camera_dir' is read-only,
so I guess you already resolved that yourself. Let me know if that's not the case.

Concerning the camera position, you chose a parallel projection viewport of type "Ortho".
In this case, the camera position has no influence on the size of the object your viewing.
What you could do is either adapt the field of view (fov) parameter,
Code: [Select]
vp.fov or change to a different viewport of type "Perspective".

One more note, I think you don't need the last line image.save(), you can pass the filename
directly to the the render_image() function instead.

Code: [Select]

vp.render_image( size = (480, 480), \
              background = (0,0,0), \
              renderer = OpenGLRenderer(),\
              filename = "/Users/h ...image{}r.png".format(i) )

-Constanze

« Last Edit: February 07, 2019, 12:24:05 PM by Constanze Kalcher »

Haomin

  • Newbie
  • *
  • Posts: 11
Re: Python script slice modifier
« Reply #7 on: February 07, 2019, 04:13:56 PM »
Hello Constanze,

Yes, I just solved the error and vp.fov is exactly what I need. Thank you very much.

Haomin