Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add specular lighting
  • Loading branch information
adam-urbanczyk committed Dec 6, 2024
commit ef73e0bb049cb59c1ed9776c0886ae3db2c7dff2
36 changes: 25 additions & 11 deletions cadquery/vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
vtkMapper,
vtkRenderWindowInteractor,
vtkActor,
vtkProp,
vtkPolyDataMapper,
vtkAssembly,
)
Expand All @@ -27,9 +28,13 @@
DEFAULT_PT_SIZE = 7.5
DEFAULT_PT_COLOR = "darkviolet"

SPECULAR = 0.3
SPECULAR_POWER = 100
SPECULAR_COLOR = vtkNamedColors().GetColor3d("White")

ShapeLike = Union[Shape, Workplane, Assembly, Sketch, TopoDS_Shape]
Showable = Union[
ShapeLike, List[ShapeLike], Vector, List[Vector], vtkActor, List[vtkActor]
ShapeLike, List[ShapeLike], Vector, List[Vector], vtkProp, List[vtkProp]
]


Expand All @@ -54,15 +59,15 @@

def _split_showables(
objs,
) -> Tuple[List[ShapeLike], List[Vector], List[Location], List[vtkActor]]:
) -> Tuple[List[ShapeLike], List[Vector], List[Location], List[vtkProp]]:
"""
Split into showables and others.
"""

rv_s: List[ShapeLike] = []
rv_v: List[Vector] = []
rv_l: List[Location] = []
rv_a: List[vtkActor] = []
rv_a: List[vtkProp] = []

for el in objs:
if instance_of(el, ShapeLike):
Expand All @@ -71,8 +76,8 @@
rv_v.append(el)
elif isinstance(el, Location):
rv_l.append(el)
elif isinstance(el, vtkActor):
elif isinstance(el, vtkProp):
rv_a.append(el)

Check warning on line 80 in cadquery/vis.py

View check run for this annotation

Codecov / codecov/patch

cadquery/vis.py#L80

Added line #L80 was not covered by tests
elif isinstance(el, list):
tmp1, tmp2, tmp3, tmp4 = _split_showables(el) # split recursively

Expand Down Expand Up @@ -143,14 +148,15 @@
alpha: float = 1,
tolerance: float = 1e-3,
edges: bool = False,
specular: bool = True,
**kwrags: Any,
):
"""
Show CQ objects using VTK.
"""

# split objects
shapes, vecs, locs, acts = _split_showables(objs)
shapes, vecs, locs, props = _split_showables(objs)

# construct the assy
assy = _to_assy(*shapes, alpha=alpha)
Expand All @@ -165,10 +171,18 @@
win.SetWindowName("CQ viewer")

# get renderer and actor
if edges:
ren = win.GetRenderers().GetFirstRenderer()
for act in ren.GetActors():
act.GetProperty().EdgeVisibilityOn()
ren = win.GetRenderers().GetFirstRenderer()
for act in ren.GetActors():

propt = act.GetProperty()

if edges:
propt.EdgeVisibilityOn()

if specular:
propt.SetSpecular(SPECULAR)
propt.SetSpecularPower(SPECULAR_POWER)
propt.SetSpecularColor(SPECULAR_COLOR)

# rendering related settings
win.SetMultiSamples(16)
Expand Down Expand Up @@ -218,8 +232,8 @@
renderer.AddActor(axs)

# add other vtk actors
for a in acts:
renderer.AddActor(a)
for p in props:
renderer.AddActor(p)

Check warning on line 236 in cadquery/vis.py

View check run for this annotation

Codecov / codecov/patch

cadquery/vis.py#L236

Added line #L236 was not covered by tests

# initialize and set size
inter.Initialize()
Expand Down