Skip to content

Commit 9ac6c76

Browse files
BUG: Fix a few issues with the view function.
1. Improve handling Mayavi objects to be far more robust. 2. Fix an issue with using lists as default arguments. This would cause problems when view is called multiple times resulting in the last call rendering all the previous actors as well.
1 parent bf4a10c commit 9ac6c76

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

itkwidgets/widget_viewer.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,12 @@
3333

3434
have_mayavi = False
3535
try:
36-
import mayavi.modules # noqa: F401
36+
from mayavi.core.pipeline_base import PipelineBase # noqa: F401
3737
have_mayavi = True
3838
except ImportError:
3939
pass
4040

4141

42-
from IPython.core.debugger import set_trace
43-
44-
4542
def get_ioloop():
4643
import IPython
4744
import zmq
@@ -774,6 +771,25 @@ def roi_slice(self):
774771
return tuple(slices)
775772

776773

774+
def _process_mayavi_actors(objects):
775+
from tvtk.api import tvtk
776+
from mayavi.core.scene import Scene
777+
from tvtk.pyface.tvtk_scene import TVTKScene
778+
actors = []
779+
for a in objects:
780+
if isinstance(a, Scene) and a.scene is not None:
781+
actors.extend([tvtk.to_vtk(x) for x in a.scene.renderer.actors])
782+
elif isinstance(a, TVTKScene):
783+
actors.extend([tvtk.to_vtk(x) for x in a.renderer.actors])
784+
elif isinstance(a, PipelineBase):
785+
actors.extend([tvtk.to_vtk(x) for x in a.actors])
786+
if hasattr(a, 'actor'):
787+
actors.extend([tvtk.to_vtk(x) for x in a.actor.actors])
788+
else:
789+
actors.append(a)
790+
return actors
791+
792+
777793
def view(image=None, # noqa: C901
778794
label_image=None, # noqa: C901
779795
label_image_names=None, # noqa: C901
@@ -785,11 +801,11 @@ def view(image=None, # noqa: C901
785801
interpolation=True,
786802
gradient_opacity=0.22, opacity_gaussians=None, channels=None,
787803
slicing_planes=False, shadow=True, blend_mode='composite',
788-
point_sets=[],
789-
point_set_colors=[], point_set_opacities=[],
790-
point_set_representations=[], point_set_sizes=[],
791-
geometries=[],
792-
geometry_colors=[], geometry_opacities=[],
804+
point_sets=None,
805+
point_set_colors=None, point_set_opacities=None,
806+
point_set_representations=None, point_set_sizes=None,
807+
geometries=None,
808+
geometry_colors=None, geometry_opacities=None,
793809
ui_collapsed=False, rotate=False, annotations=True, axes=False, mode='v',
794810
**kwargs):
795811
"""View the image and/or point sets and/or geometries.
@@ -991,6 +1007,23 @@ def view(image=None, # noqa: C901
9911007
widget.
9921008
"""
9931009

1010+
if point_sets is None:
1011+
point_sets = []
1012+
if point_set_colors is None:
1013+
point_set_colors = []
1014+
if point_set_opacities is None:
1015+
point_set_opacities = []
1016+
if point_set_representations is None:
1017+
point_set_representations = []
1018+
if point_set_sizes is None:
1019+
point_set_sizes = []
1020+
if geometries is None:
1021+
geometries = []
1022+
if geometry_colors is None:
1023+
geometry_colors = []
1024+
if geometry_opacities is None:
1025+
geometry_opacities = []
1026+
9941027
# this block allows the user to pass already formed vtkActor vtkVolume
9951028
# objects
9961029
actors = kwargs.pop("actors", None)
@@ -1000,17 +1033,10 @@ def view(image=None, # noqa: C901
10001033
actors = [actors]
10011034

10021035
images = []
1036+
if have_mayavi:
1037+
actors = _process_mayavi_actors(actors)
10031038

10041039
for a in actors:
1005-
if have_mayavi:
1006-
from mayavi.modules import surface
1007-
from mayavi.modules import iso_surface
1008-
from tvtk.api import tvtk
1009-
if isinstance(a, surface.Surface):
1010-
a = tvtk.to_vtk(a.actor.actor)
1011-
elif isinstance(a, iso_surface.IsoSurface):
1012-
a = tvtk.to_vtk(a.actor.actor)
1013-
10141040
if isinstance(a, vtk.vtkAssembly): # unpack assemblies
10151041
cl = vtk.vtkPropCollection()
10161042
a.GetActors(cl)
@@ -1045,7 +1071,8 @@ def view(image=None, # noqa: C901
10451071
tp.Update()
10461072
poly = tp.GetOutput()
10471073
prop = a.GetProperty()
1048-
if poly.GetNumberOfPolys() or poly.GetNumberOfStrips() or poly.GetNumberOfLines():
1074+
if (poly.GetNumberOfPolys() or poly.GetNumberOfStrips()
1075+
or poly.GetNumberOfLines()):
10491076
geometries.insert(0, poly)
10501077
geometry_colors.insert(0, prop.GetColor())
10511078
geometry_opacities.insert(0, prop.GetOpacity())
@@ -1078,7 +1105,8 @@ def view(image=None, # noqa: C901
10781105
point_set_opacities=point_set_opacities,
10791106
point_set_representations=point_set_representations,
10801107
point_set_sizes=point_set_sizes,
1081-
geometries=geometries, geometry_colors=geometry_colors, geometry_opacities=geometry_opacities,
1108+
geometries=geometries, geometry_colors=geometry_colors,
1109+
geometry_opacities=geometry_opacities,
10821110
rotate=rotate, ui_collapsed=ui_collapsed,
10831111
annotations=annotations, axes=axes, mode=mode,
10841112
**kwargs)

0 commit comments

Comments
 (0)