Skip to content

Instantly share code, notes, and snippets.

@mhogg
Created December 6, 2021 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhogg/683bec1afa357f8f75b5d171a2008e6e to your computer and use it in GitHub Desktop.
Save mhogg/683bec1afa357f8f75b5d171a2008e6e to your computer and use it in GitHub Desktop.
# Ref: https://github.com/Beastmaster/itk-python-example/blob/master/vtkSlidebar.py
# Ref: https://kitware.github.io/vtk-examples/site/Cxx/Widgets/Slider/
import vtk
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0.0,0.0,0.0)
sphereSource.SetRadius(4.0)
sphereSource.Update()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(sphereSource.GetOutput())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetInterpolationToFlat()
renderer = vtk.vtkRenderer()
renderWin = vtk.vtkRenderWindow()
renderWin.AddRenderer(renderer)
renWinInteractor = vtk.vtkRenderWindowInteractor()
renWinInteractor.SetRenderWindow(renderWin)
renderer.AddActor(actor)
renderWin.Render()
#build a slide bar
slideBar = vtk.vtkSliderRepresentation2D()
slideBar.SetMinimumValue(3.0)
slideBar.SetMaximumValue(20.0)
slideBar.SetValue(sphereSource.GetPhiResolution())
slideBar.GetSliderProperty().SetColor([c/255. for c in (0,204,204)])
slideBar.GetSelectedProperty().SetColor([c/255. for c in (153,255,255)])
slideBar.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
slideBar.GetPoint1Coordinate().SetValue(0.1 ,0.07)
slideBar.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
slideBar.GetPoint2Coordinate().SetValue(0.9, 0.07)
slideBar.SetSliderLength(0.02)
slideBar.SetSliderWidth(0.04)
slideBar.SetEndCapWidth(0.04)
slideBar.SetEndCapLength(0.005)
slideBar.SetTubeWidth(0.005)
sliderWidget = vtk.vtkSliderWidget()
sliderWidget.SetInteractor(renWinInteractor)
sliderWidget.SetRepresentation(slideBar)
sliderWidget.EnabledOn()
class vtkSliderCallback:
def __init__(self, Sphere):
self.Sphere = Sphere
def execute(self, obj, event):
slider = obj
value = int(slider.GetRepresentation().GetValue())
self.Sphere.SetPhiResolution(value)
self.Sphere.SetThetaResolution(value)
self.Sphere.Update()
cb = vtkSliderCallback(sphereSource)
sliderWidget.AddObserver("InteractionEvent", cb.execute)
renderer.ResetCamera()
renWinInteractor.Initialize()
renderWin.Render()
renderWin.SetWindowName('VTK Viewer')
renWinInteractor.Start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment