vtk render unstructured grid and set active camera

hi all,

i’m trying to do a render of a unstructured grid by setting the camera before interactor. When i render just the unstructured grid alll works fine but when i try to set a custom camera at a given possition it gives an empty window. What i’m doing is:

actor = vtk.vtkActor()
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
camera = vtk.vtkCamera()

# set actor mapper which is a DataSetMapper

# set actor on renderer

# set renderer on window

# set window in interactor

# additional configurations
renderer.SetBackground(0, 0, 0)  # black
renderWindow.SetSize(600, 600)

# camera configuration
# using same coordinates that the default active camera gives for this actor.
camera.SetPosition(570320.4375, 7343062.5, 155036.19074423693)

# set camera on renderer
# if i dont set this, the render will work correctly

# render initialize and start

i could think that the object is too big to render correctly but using default camera works fine then when i set a “custom” camera with same coordinates it doesnt work, any idea on what’s happening?


Try simply getting the active camera and setting its attribute like the position. i.e.:

camera = renderer.GetActiveCamera()

Using a new camera object requires a bit more set up and nuance.

Also, if you’re working in Python: vtki is a package that provides a streamlined interface to VTK to make these kinds of task far simpler so that you do not need to worry about the nuances of VTK rendering.

For example, your code would translate to:

import vtki
from vtki import examples

# Use a black background by default
vtki.rcParams['background'] = 'black'

# Heres an example VTK PolyData object
mesh = examples.load_airplane()
# You could use your own VTK data object here instead.
# mesh = vtki.wrap(my_vtk_data_object)

p = vtki.Plotter()
p.add_mesh(mesh, color='orange')

location = (3140., 2919., 2375.) # Add your camera position
focus = mesh.center
viewup = (0,0,1)
p.camera_position = (location, focus, viewup)

p.show(window_size=(600, 600))

Does your cam look in wrong direction? Copy Focalpoint from your other camera. Check if black screen changes of you change backgroundcolor

I am faced with the same problem (Win 10, python 3.6). I used the above code, replaced ‘camera = vtk.vtkCamera()’ with ‘camera=render.GetActiveCamera()’, and did nothing to the camera. But it dose not work and still gives an empty window. It works all right when ‘camera=render.GetActiveCamera()’ is not used, just as stated above.