Skip to content

feat: Enhance vis.py show routine #1855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions cadquery/vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,11 @@ def show(
zoom: float = 1.0,
roll: float = -35,
elevation: float = -45,
azimuth: float = 0,
position: Optional[Tuple[float, float, float]] = None,
focus: Optional[Tuple[float, float, float]] = None,
viewup: Optional[Tuple[float, float, float]] = None,
clipping_range: Optional[Tuple[float, float]] = None,
width: Union[int, float] = 0.5,
height: Union[int, float] = 0.5,
trihedron: bool = True,
Expand Down Expand Up @@ -491,17 +494,27 @@ def show(

# set camera
camera = renderer.GetActiveCamera()

# Reset orientation to known state
renderer.ResetCamera()

# Update camera position with user provided absolute positions
if viewup:
camera.SetViewUp(*viewup)
if position:
camera.SetPosition(*position)
if focus:
camera.SetFocalPoint(*focus)

# Update camera position with user defined relative positions
camera.Roll(roll)
camera.Elevation(elevation)
camera.Zoom(zoom)
camera.Azimuth(azimuth)

if position or focus:
if position:
camera.SetPosition(*position)
if focus:
camera.SetFocalPoint(*focus)
else:
renderer.ResetCamera()
# Update camera view frustum
camera.Zoom(zoom)
if clipping_range:
camera.SetClippingRange(*clipping_range)

# initialize and set size
inter.Initialize()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,14 @@ def test_camera_position(wp, patch_vtk):
show(wp, position=(0, 0, 1), focus=(0, 0.1, 0))
show(wp, focus=(0, 0.1, 0))
show(wp, position=(0, 0, 1))

# Specify Z up
show(wp, viewup=(0, 0, 1), position=(0, -1, 0), focus=(0, 0.1, 0))
show(wp, focus=(0, 0.1, 0))
show(wp, position=(0, -1, 0))
show(wp, viewup=(0, 0, 1))


def test_frustrum_clipping_range(wp, patch_vtk):

show(wp, zoom=2.0, clipping_range=(1, 100))