marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
1.98k stars 257 forks source link

Can't set up the camera to render Mesh #987

Closed vitalii-p closed 7 months ago

vitalii-p commented 7 months ago

I tried to set camera parameters for triangular mesh rendering, but seems like those parameters don't affect the output image. Here is my code for setting up the camera

   def setup_plotter(mesh: Mesh, camera_params: CameraParams):

    plotter = Plotter(offscreen=True, size=(camera_params.size, camera_params.size),bg='white')`

    plotter.add(mesh)

    camera = plotter.camera
    camera.SetPosition(*camera_params.position)  # Set the position of the camera
    camera.SetFocalPoint(*camera_params.look_at)  # Set the focal point of the camera
    camera.SetViewUp(*camera_params.up)  # Set the up vector of the camera
    camera.SetViewAngle(camera_params.fov())

    return plotter

Then I call

plotter.show().screenshot(out_filename)

Despite of changing SetPosition or SetFocalPoint - output image is still the same.

Currently I use vedo==2023.4.6, tried with 2023.5.0 - issue with the camera remains, but in addition I got wrong transformations when applied the same matrices via mesh.apply_transform.

Could you help me with camera setup? Thanks in advance!

marcomusy commented 7 months ago

Hi, you can setup the camera more simply by passing a dictionary:

###################################################
## Template python code to position this camera: ##
cam = dict(
    position=(44.1916, -32.0029, 19.2009),
    focal_point=(-1.21754, 9.83238e-4, -2.27213e-4),
    viewup=(0, 0, 1.00000),
    roll=-77.0346,
    distance=58.7786,
    clipping_range=(21.9889, 105.231),
)
show(mymeshes, camera=cam)

You get this template code by pressing Shift-C in the rendering window.

Also - if you change some settings you should call plotter.render() to update the scene.

About the second problem - i cannot help without a specific reproducible example. In vedo 5.0 there is a better management of transformations so I hope you should now get the correct behavior.

vitalii-p commented 7 months ago

Thanks a lot, @marcomusy ! You saved my day, your approach with dict definitely works!