Search code examples
paraviewpvpython

Paraview python 'Zoom to Box'


I am trying to make a script which does basically what tool 'Zoom to Box' does. Track option is out of question as it doesn't track camera movement.

I've found this online, fixed it to work, but keep getting '3d cam position is yet TODO' This is very old and maybe there are new options to do this? Thanks for the tips...

I could also try doing it by using classic camera commands like:

source=GetActiveSource()
#view = GetRenderView()
#view.CameraFocalPoint = [1, 0, 0]
#view.CameraViewAngle = 90
#view.CameraViewUp = [0, 0, 0]
#view.CameraPosition = [0, 0, 0]
#view.ViewSize = [1528, 542]
#view.ResetCamera()

But I'm not sure there is a way to zoom?

Fixed script from the link above:

source=GetActiveSource()
rep = Show(source)

# run the pipeline here to get the bounds
Render()

bounds = source.GetDataInformation().GetBounds()
bounds_dx = bounds[1] - bounds[0]
bounds_dy = bounds[3] - bounds[2]
bounds_dz = bounds[5] - bounds[4]
bounds_cx = (bounds[0] + bounds[1])/2.0
bounds_cy = (bounds[2] + bounds[3])/2.0
bounds_cz = (bounds[4] + bounds[5])/2.0

if bounds_dx == 0:
# yz
    dimMode = 2
    aspect = bounds_dz/bounds_dy

elif bounds_dy == 0:
# xz
    dimMode = 1
    aspect = bounds_dz/bounds_dx

elif bounds_dz == 0:
# xy
    dimMode = 0
    aspect = bounds_dy/bounds_dx

else:
# 3d
    dimMode = 3
    aspect = 1.0  # TODO

lastObj = source

view = GetRenderView()
# view.ViewTime = steps[step] # unwanted
# view.UseOffscreenRenderingForScreenshots = 0 # obsolete

rep = Show(lastObj)
# rep.Representation = 'Outline' # unwanted
Render()

# position the camera
# far  = config.camFac
far = 1
near = 0

if dimMode == 0:
   # xy
    pos = max(bounds_dx, bounds_dy)
    camUp = [0.0, 1.0, 0.0]
    camPos = [bounds_cx, bounds_cy,  pos*far]
    camFoc = [bounds_cx, bounds_cy, -pos*near]

elif dimMode == 1:
   # xz
    pos = max(bounds_dx, bounds_dz)
    camUp = [0.0, 0.0, 1.0]
    camPos = [bounds_cx, -pos*far,  bounds_cz]
    camFoc = [bounds_cx,  pos*near, bounds_cz]

elif dimMode == 2:
   # yz
    pos = max(bounds_dy, bounds_dz)
    camUp = [0.0, 0.0, 1.0]
    camPos = [ pos*far, bounds_cy, bounds_cz]
    camFoc = [-pos*near, bounds_cy, bounds_cz]

else:
   # 3d
    print('3d cam position is yet TODO')
    camUp=[0,0,0]
    camPos=[1,0,0]
    camFoc=[0,0,0]

view = GetRenderView()
view.CameraViewUp = camUp
view.CameraPosition = camPos
view.CameraFocalPoint = camFoc
#view.UseOffscreenRenderingForScreenshots = 0 # obsolete
view.CenterAxesVisibility = 0

ren = Render()

#width = int(config.outputWidth)
#height = int(config.outputWidth*aspect)

Solution

  • So I just went ahead and did it this way:

    import sys
    from paraview.simple import *
    
    # Camera Position
    # positional coordinates of the camera
    # zoom is achieved by adjusting x, y, z values (moving camera closer/further away) depending on focal point
    # e. g. [10,0,4] - camera will be looking at object from this coordinate
    CamPos = sys.argv[1]
    
    # Camera Focal Point
    # point of interest for the camera
    # the point will be in the center of the screen and camera will rotate towards him in its position
    # e. g. [0,0,0] - camera will focus its center on this coordinate
    CamFocPoint = sys.argv[2]
    
    # Camera View Up
    # defining which way is up in the view
    # uses values <-1;1> for each vector component
    # to achieve angled view, use same value '1' for two vector components
    # e. g. [0,0,1] - achieves having highest z component at the top
    CamViewUp = sys.argv[3]
    
    # getting active view
    camera = GetActiveCamera()
    
    # setting based on user definition
    camera.SetPosition(CamPos[0], CamPos[1], CamPos[2])
    camera.SetFocalPoint(CamFocPoint[0], CamFocPoint[1], CamFocPoint[2])
    camera.SetViewUp(CamViewUp[0], CamViewUp[1], CamViewUp[2])
    
    # making sure angle is right
    camera.SetViewAngle(30)
    
    # rendering view
    Render()