From dfe59eb3157e9e0ab2df2ef828928a0216a215d0 Mon Sep 17 00:00:00 2001 From: Andrew Maclean <andrew.amaclean@gmail.com> Date: Tue, 5 Oct 2021 16:13:59 +1100 Subject: [PATCH] Utilities updated --- src/Python/Utilities/Animation.py | 28 +++++-- src/Python/Utilities/LUTUtilities.py | 81 +++++++++++--------- src/Python/Utilities/SaveSceneToFieldData.py | 46 +++++++---- src/Python/Utilities/SaveSceneToFile.py | 43 +++++++---- src/Python/Utilities/Screenshot.py | 36 ++++++--- src/Python/Utilities/ShareCamera.py | 46 ++++++++--- src/Python/Utilities/VTKVersion.py | 6 +- src/Python/Utilities/VTKWithNumpy.py | 37 ++++++--- 8 files changed, 215 insertions(+), 108 deletions(-) diff --git a/src/Python/Utilities/Animation.py b/src/Python/Utilities/Animation.py index 31dfecc0b30..ec287af2497 100755 --- a/src/Python/Utilities/Animation.py +++ b/src/Python/Utilities/Animation.py @@ -1,4 +1,16 @@ -import vtkmodules.all as vtk +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import vtkNamedColors +from vtkmodules.vtkFiltersSources import vtkSphereSource +from vtkmodules.vtkRenderingCore import ( + vtkActor, + vtkPolyDataMapper, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer +) class vtkTimerCallback(): @@ -23,19 +35,19 @@ class vtkTimerCallback(): def main(): - colors = vtk.vtkNamedColors() + colors = vtkNamedColors() # Create a sphere - sphereSource = vtk.vtkSphereSource() + sphereSource = vtkSphereSource() sphereSource.SetCenter(0.0, 0.0, 0.0) sphereSource.SetRadius(2) sphereSource.SetPhiResolution(30) sphereSource.SetThetaResolution(30) # Create a mapper and actor - mapper = vtk.vtkPolyDataMapper() + mapper = vtkPolyDataMapper() mapper.SetInputConnection(sphereSource.GetOutputPort()) - actor = vtk.vtkActor() + actor = vtkActor() actor.GetProperty().SetColor(colors.GetColor3d("Peacock")) actor.GetProperty().SetSpecular(0.6) actor.GetProperty().SetSpecularPower(30) @@ -43,13 +55,13 @@ def main(): # actor.SetPosition(-5, -5, 0) # Setup a renderer, render window, and interactor - renderer = vtk.vtkRenderer() + renderer = vtkRenderer() renderer.SetBackground(colors.GetColor3d("MistyRose")) - renderWindow = vtk.vtkRenderWindow() + renderWindow = vtkRenderWindow() renderWindow.SetWindowName("Animation") renderWindow.AddRenderer(renderer) - renderWindowInteractor = vtk.vtkRenderWindowInteractor() + renderWindowInteractor = vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) # Add the actor to the scene diff --git a/src/Python/Utilities/LUTUtilities.py b/src/Python/Utilities/LUTUtilities.py index e7dae1b3b6f..bd4cff2262c 100755 --- a/src/Python/Utilities/LUTUtilities.py +++ b/src/Python/Utilities/LUTUtilities.py @@ -1,27 +1,34 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from __future__ import print_function - import sys -import vtkmodules.all as vtk +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import vtkColorSeries +from vtkmodules.vtkCommonCore import ( + vtkLookupTable, + vtkVariant, + vtkVariantArray +) class LUTUtilities(object): - ''' + """ Utilities for displaying and comparing lookup tables. - ''' + """ def __init__(self): pass def DisplayLUTAsString(self, lut): - ''' + """ Display the contents of the lookup table. :param: lut - the lookup table. :return: a string containing the table data. - ''' + """ str = '' tv = lut.GetNumberOfTableValues() dR = lut.GetTableRange() @@ -58,13 +65,13 @@ class LUTUtilities(object): return str def AssembleRGBAString(self, rgba): - ''' + """ Display the contents of the rgba as a series of strings: decimal [r g b a], integer [r g b a] where r,g ,b a are in the range 0..255 and 0xrrggba . :param: The rgba string. :return: A string in the above format. - ''' + """ s = '[' + ', '.join(['{:0.6f}'.format(x) for x in rgba]) + ']' ucrgb = [int(x * 255) for x in rgba] t = '[' + ', '.join(['{:3d}'.format(x) for x in ucrgb]) + ']' @@ -74,12 +81,12 @@ class LUTUtilities(object): return res def CompareLUTs(self, lut1, lut2): - ''' + """ Compare two lookup tables. :param: lut1 - the lookup table. :param: lut2 - the lookup table. :return: True if the tables are the same. - ''' + """ res = [True, ''] if lut1.GetIndexedLookup() != lut2.GetIndexedLookup(): res[0] = False @@ -146,14 +153,14 @@ class LUTUtilities(object): return res def CompareRGBA(self, rgba1, rgba2): - ''' + """ Compare two rgba lists. rgba can be a hexadecimal string, or a list of rgb or rgba colors. :param: rgba1 - the color. :param: rgba2 - the color. :return: True if the colors are the same. - ''' + """ if len(rgba1) != len(rgba2): return False if isinstance(rgba1, str): @@ -167,12 +174,12 @@ class LUTUtilities(object): def GetAllColorSchemes(): - ''' + """ Get all the color scheme names. :return: a map of the names keyed on their index. - ''' + """ colorSchemes = dict() - colorSeries = vtk.vtkColorSeries() + colorSeries = vtkColorSeries() for i in range(colorSeries.GetNumberOfColorSchemes()): colorSeries.SetColorScheme(i) colorSchemes[i] = colorSeries.GetColorSchemeName() @@ -180,11 +187,11 @@ def GetAllColorSchemes(): def AvailableColorSchemes(colorSchemes): - ''' + """ The available color scheme indexes and names. :param: colorSchemes - a map of the names keyed on their index. :return: a string if the indexes and names. - ''' + """ str = '' for k, v in colorSchemes.items(): str += '{:3d}\t{:s}\n'.format(k, v) @@ -192,21 +199,21 @@ def AvailableColorSchemes(colorSchemes): def DisplayAvailableColorSchemes(): - ''' + """ Display the available color schemes. - ''' + """ line = "-----------------------------------------------------------------------------\n" colorSchemes = GetAllColorSchemes() print(line + AvailableColorSchemes(colorSchemes) + line) def DisplayResults(reason, lut1, lut2): - ''' + """ Display the lookup tables and reason for failure. :param: reason - the reason. :param: lut1 - the first lookup table. :param: lut2 - the second lookup table. - ''' + """ lutUtilities = LUTUtilities() line = "-----------------------------------------------------------------------------\n" print(line + reason + "\n") @@ -216,13 +223,13 @@ def DisplayResults(reason, lut1, lut2): def TestTables(lut1, lut2, expected=True): - ''' + """ Test pairs of lookup tables. :param: lut1 - the first lookup table. :param: lut2 - the second lookup table. :param: expected - if False a fail is expected. :return: True/False. - ''' + """ lutUtilities = LUTUtilities() comparison = lutUtilities.CompareLUTs(lut1, lut2) if comparison[0] != expected: @@ -233,14 +240,14 @@ def TestTables(lut1, lut2, expected=True): def TestLookupTables(lutMode): - ''' + """ Test various combinations of lookup tables. :param: lutMode - if True the tables are ordinal, categorical otherwise. :return: True if all tests passed. - ''' - lut1 = vtk.vtkLookupTable() - lut2 = vtk.vtkLookupTable() - colorSeries = vtk.vtkColorSeries() + """ + lut1 = vtkLookupTable() + lut2 = vtkLookupTable() + colorSeries = vtkColorSeries() colorSeriesEnum = colorSeries.SPECTRUM colorSeries.SetColorScheme(colorSeriesEnum) @@ -253,13 +260,13 @@ def TestLookupTables(lutMode): lut2.SetNanColor(1, 0, 0, 1) if not lutMode: # For the annotation just use a letter of the alphabet. - values1 = vtk.vtkVariantArray() - values2 = vtk.vtkVariantArray() + values1 = vtkVariantArray() + values2 = vtkVariantArray() str = "abcdefghijklmnopqrstuvwxyz" for i in range(lut1.GetNumberOfTableValues()): - values1.InsertNextValue(vtk.vtkVariant(str[i])) + values1.InsertNextValue(vtkVariant(str[i])) for i in range(lut2.GetNumberOfTableValues()): - values2.InsertNextValue(vtk.vtkVariant(str[i])) + values2.InsertNextValue(vtkVariant(str[i])) for i in range(values1.GetNumberOfTuples()): lut1.SetAnnotation(i, values1.GetValue(i).ToString()) for i in range(values2.GetNumberOfTuples()): @@ -285,7 +292,7 @@ def TestLookupTables(lutMode): # Different color colorSeriesEnum = colorSeries.COOL colorSeries.SetColorScheme(colorSeriesEnum) - lut3 = vtk.vtkLookupTable() + lut3 = vtkLookupTable() colorSeries.BuildLookupTable(lut3) lut3.IndexedLookupOff() res &= TestTables(lut1, lut3, False) @@ -298,12 +305,12 @@ def TestLookupTables(lutMode): # Different color colorSeriesEnum = colorSeries.COOL colorSeries.SetColorScheme(colorSeriesEnum) - lut3 = vtk.vtkLookupTable() + lut3 = vtkLookupTable() colorSeries.BuildLookupTable(lut3) - values = vtk.vtkVariantArray() + values = vtkVariantArray() str = "abcdefghijklmnopqrstuvwxyz" for i in range(lut3.GetNumberOfTableValues()): - values.InsertNextValue(vtk.vtkVariant(str[i])) + values.InsertNextValue(vtkVariant(str[i])) for i in range(values.GetNumberOfTuples()): lut3.SetAnnotation(i, values.GetValue(i).ToString()) colorSeries.BuildLookupTable(lut3) diff --git a/src/Python/Utilities/SaveSceneToFieldData.py b/src/Python/Utilities/SaveSceneToFieldData.py index 58bfec57a85..07df05d0cff 100755 --- a/src/Python/Utilities/SaveSceneToFieldData.py +++ b/src/Python/Utilities/SaveSceneToFieldData.py @@ -1,29 +1,47 @@ #!/usr/bin/env python -import vtkmodules.all as vtk +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import vtkNamedColors +from vtkmodules.vtkCommonCore import vtkStringArray +from vtkmodules.vtkIOGeometry import ( + vtkBYUReader, + vtkOBJReader, + vtkSTLReader +) +from vtkmodules.vtkIOPLY import vtkPLYReader +from vtkmodules.vtkRenderingCore import ( + vtkActor, + vtkPolyDataMapper, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer +) def main(): pd_fn = get_program_parameters() - colors = vtk.vtkNamedColors() + colors = vtkNamedColors() polyData = ReadPolyData(pd_fn) - mapper = vtk.vtkPolyDataMapper() + mapper = vtkPolyDataMapper() mapper.SetInputData(polyData) - actor = vtk.vtkActor() + actor = vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetDiffuseColor(colors.GetColor3d('Crimson')) actor.GetProperty().SetSpecular(.6) actor.GetProperty().SetSpecularPower(30) - renderer = vtk.vtkRenderer() - renderWindow = vtk.vtkRenderWindow() + renderer = vtkRenderer() + renderWindow = vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindow.SetWindowName('SaveSceneToFieldData') - renderWindowInteractor = vtk.vtkRenderWindowInteractor() + renderWindowInteractor = vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) renderer.AddActor(actor) @@ -61,32 +79,32 @@ def ReadPolyData(file_name): path, extension = os.path.splitext(file_name) extension = extension.lower() if extension == '.ply': - reader = vtk.vtkPLYReader() + reader = vtkPLYReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.vtp': - reader = vtk.vtkXMLpoly_dataReader() + reader = vtkXMLpoly_dataReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.obj': - reader = vtk.vtkOBJReader() + reader = vtkOBJReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.stl': - reader = vtk.vtkSTLReader() + reader = vtkSTLReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.vtk': - reader = vtk.vtkpoly_dataReader() + reader = vtkpoly_dataReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.g': - reader = vtk.vtkBYUReader() + reader = vtkBYUReader() reader.SetGeometryFileName(file_name) reader.Update() poly_data = reader.GetOutput() @@ -112,7 +130,7 @@ def SaveSceneToFieldData(data, actor, camera): buffer = '' for k, v in res.items(): buffer += k + ' ' + v + '\n' - cameraArray = vtk.vtkStringArray() + cameraArray = vtkStringArray() cameraArray.SetNumberOfValues(1) cameraArray.SetValue(0, buffer) cameraArray.SetName('Camera') diff --git a/src/Python/Utilities/SaveSceneToFile.py b/src/Python/Utilities/SaveSceneToFile.py index 2b7fa1c0363..6eb4d80e839 100755 --- a/src/Python/Utilities/SaveSceneToFile.py +++ b/src/Python/Utilities/SaveSceneToFile.py @@ -1,29 +1,46 @@ #!/usr/bin/env python -import vtkmodules.all as vtk +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import vtkNamedColors +from vtkmodules.vtkIOGeometry import ( + vtkBYUReader, + vtkOBJReader, + vtkSTLReader +) +from vtkmodules.vtkIOPLY import vtkPLYReader +from vtkmodules.vtkRenderingCore import ( + vtkActor, + vtkPolyDataMapper, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer +) def main(): pd_fn, scene_fn = get_program_parameters() - colors = vtk.vtkNamedColors() + colors = vtkNamedColors() polyData = ReadPolyData(pd_fn) - mapper = vtk.vtkPolyDataMapper() + mapper = vtkPolyDataMapper() mapper.SetInputData(polyData) - actor = vtk.vtkActor() + actor = vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetDiffuseColor(colors.GetColor3d('Crimson')) actor.GetProperty().SetSpecular(.6) actor.GetProperty().SetSpecularPower(30) - renderer = vtk.vtkRenderer() - renderWindow = vtk.vtkRenderWindow() + renderer = vtkRenderer() + renderWindow = vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindow.SetWindowName('SaveSceneToFile') - renderWindowInteractor = vtk.vtkRenderWindowInteractor() + renderWindowInteractor = vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) renderer.AddActor(actor) @@ -62,32 +79,32 @@ def ReadPolyData(file_name): path, extension = os.path.splitext(file_name) extension = extension.lower() if extension == '.ply': - reader = vtk.vtkPLYReader() + reader = vtkPLYReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.vtp': - reader = vtk.vtkXMLpoly_dataReader() + reader = vtkXMLpoly_dataReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.obj': - reader = vtk.vtkOBJReader() + reader = vtkOBJReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.stl': - reader = vtk.vtkSTLReader() + reader = vtkSTLReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.vtk': - reader = vtk.vtkpoly_dataReader() + reader = vtkpoly_dataReader() reader.SetFileName(file_name) reader.Update() poly_data = reader.GetOutput() elif extension == '.g': - reader = vtk.vtkBYUReader() + reader = vtkBYUReader() reader.SetGeometryFileName(file_name) reader.Update() poly_data = reader.GetOutput() diff --git a/src/Python/Utilities/Screenshot.py b/src/Python/Utilities/Screenshot.py index 83adc762bc1..8fd046effcd 100755 --- a/src/Python/Utilities/Screenshot.py +++ b/src/Python/Utilities/Screenshot.py @@ -1,32 +1,48 @@ -import vtkmodules.all as vtk +#!/usr/bin/env python + +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import vtkNamedColors +from vtkmodules.vtkFiltersSources import vtkSphereSource +from vtkmodules.vtkIOImage import vtkPNGWriter +from vtkmodules.vtkRenderingCore import ( + vtkActor, + vtkPolyDataMapper, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer, + vtkWindowToImageFilter +) def main(): - colors = vtk.vtkNamedColors() + colors = vtkNamedColors() # create a rendering window and renderer - ren = vtk.vtkRenderer() - renWin = vtk.vtkRenderWindow() + ren = vtkRenderer() + renWin = vtkRenderWindow() renWin.AddRenderer(ren) renWin.SetWindowName('Screenshot') # create a renderwindowinteractor - iren = vtk.vtkRenderWindowInteractor() + iren = vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # create source - source = vtk.vtkSphereSource() + source = vtkSphereSource() source.SetCenter(0, 0, 0) source.SetRadius(5.0) source.SetPhiResolution(30) source.SetThetaResolution(30) # mapper - mapper = vtk.vtkPolyDataMapper() + mapper = vtkPolyDataMapper() mapper.SetInputConnection(source.GetOutputPort()) # actor - actor = vtk.vtkActor() + actor = vtkActor() actor.GetProperty().SetColor(colors.GetColor3d('IndianRed')) actor.GetProperty().SetSpecular(0.6) actor.GetProperty().SetSpecularPower(30) @@ -39,13 +55,13 @@ def main(): renWin.Render() # screenshot code: - w2if = vtk.vtkWindowToImageFilter() + w2if = vtkWindowToImageFilter() w2if.SetInput(renWin) w2if.SetInputBufferTypeToRGB() w2if.ReadFrontBufferOff() w2if.Update() - writer = vtk.vtkPNGWriter() + writer = vtkPNGWriter() writer.SetFileName('TestScreenshot.png') writer.SetInputConnection(w2if.GetOutputPort()) writer.Write() diff --git a/src/Python/Utilities/ShareCamera.py b/src/Python/Utilities/ShareCamera.py index ce6e65de116..e3a8136f332 100755 --- a/src/Python/Utilities/ShareCamera.py +++ b/src/Python/Utilities/ShareCamera.py @@ -1,22 +1,44 @@ -import vtkmodules.all as vtk +#!/usr/bin/env python + +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import ( + vtkColorSeries, + vtkNamedColors +) +from vtkmodules.vtkFiltersSources import ( + vtkConeSource, + vtkCubeSource, + vtkCylinderSource, + vtkSphereSource +) +from vtkmodules.vtkRenderingCore import ( + vtkActor, + vtkPolyDataMapper, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer +) def main(): - colors = vtk.vtkNamedColors() + colors = vtkNamedColors() # We store background colors in a vector. Then we extract the red, green and # blue components later when coloring the reneder background. rendererColors = list() - colorSeries = vtk.vtkColorSeries() + colorSeries = vtkColorSeries() colorSeries.SetColorSchemeByName('Brewer Qualitative Pastel2') rendererColors.append(colorSeries.GetColor(0)) rendererColors.append(colorSeries.GetColor(1)) rendererColors.append(colorSeries.GetColor(2)) rendererColors.append(colorSeries.GetColor(3)) - renderWindow = vtk.vtkRenderWindow() + renderWindow = vtkRenderWindow() - renderWindowInteractor = vtk.vtkRenderWindowInteractor() + renderWindowInteractor = vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) @@ -32,37 +54,37 @@ def main(): for i in range(0, 4): if i == 0: # Create a sphere - sphereSource = vtk.vtkSphereSource() + sphereSource = vtkSphereSource() sphereSource.SetCenter(0.0, 0.0, 0.0) sphereSource.Update() sources.append(sphereSource) elif i == 1: # Create a cone - coneSource = vtk.vtkConeSource() + coneSource = vtkConeSource() coneSource.SetCenter(0.0, 0.0, 0.0) coneSource.Update() sources.append(coneSource) elif i == 2: # Create a cube - cubeSource = vtk.vtkCubeSource() + cubeSource = vtkCubeSource() cubeSource.SetCenter(0.0, 0.0, 0.0) cubeSource.Update() sources.append(cubeSource) else: # Create a cylinder - cylinderSource = vtk.vtkCylinderSource() + cylinderSource = vtkCylinderSource() cylinderSource.SetCenter(0.0, 0.0, 0.0) cylinderSource.Update() sources.append(cylinderSource) - mapper = vtk.vtkPolyDataMapper() + mapper = vtkPolyDataMapper() mapper.SetInputConnection(sources[i].GetOutputPort()) - actor = vtk.vtkActor() + actor = vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetColor(colors.GetColor3d('Tomato')) - renderer = vtk.vtkRenderer() + renderer = vtkRenderer() renderer.AddActor(actor) r = rendererColors[i].GetRed() / 255.0 g = rendererColors[i].GetGreen() / 255.0 diff --git a/src/Python/Utilities/VTKVersion.py b/src/Python/Utilities/VTKVersion.py index 4a7a737fc37..7a6de29b853 100755 --- a/src/Python/Utilities/VTKVersion.py +++ b/src/Python/Utilities/VTKVersion.py @@ -1,5 +1,5 @@ -from __future__ import print_function +#!/usr/bin/env python -import vtkmodules.all as vtk +from vtkmodules.vtkCommonCore import vtkVersion -print(vtk.vtkVersion.GetVTKSourceVersion()) +print(vtkVersion.GetVTKSourceVersion()) diff --git a/src/Python/Utilities/VTKWithNumpy.py b/src/Python/Utilities/VTKWithNumpy.py index a00a1a87ea9..5db95be4ea7 100755 --- a/src/Python/Utilities/VTKWithNumpy.py +++ b/src/Python/Utilities/VTKWithNumpy.py @@ -1,11 +1,26 @@ # An example from scipy cookbook demonstrating the use of numpy arrays in vtk import numpy as np -import vtkmodules.all as vtk +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +from vtkmodules.vtkCommonColor import vtkNamedColors +from vtkmodules.vtkCommonDataModel import vtkPiecewiseFunction +from vtkmodules.vtkIOImage import vtkImageImport +from vtkmodules.vtkRenderingCore import ( + vtkColorTransferFunction, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer, + vtkVolume, + vtkVolumeProperty +) +from vtkmodules.vtkRenderingVolume import vtkFixedPointVolumeRayCastMapper +# noinspection PyUnresolvedReferences +from vtkmodules.vtkRenderingVolumeOpenGL2 import vtkOpenGLRayCastImageDisplayHelper def main(): - colors = vtk.vtkNamedColors() + colors = vtkNamedColors() # We begin by creating the data we want to render. # For this tutorial, we create a 3D-image containing three overlaping cubes. @@ -19,7 +34,7 @@ def main(): # For VTK to be able to use the data, it must be stored as a VTK-image. # This can be done by the vtkImageImport-class which # imports raw data and stores it. - dataImporter = vtk.vtkImageImport() + dataImporter = vtkImageImport() # The previously created array is converted to a string of chars and imported. data_string = data_matrix.tobytes() dataImporter.CopyImportVoidPointer(data_string, len(data_string)) @@ -39,7 +54,7 @@ def main(): # The following class is used to store transparency-values for later retrival. # In our case, we want the value 0 to be # completely opaque whereas the three different cubes are given different transparency-values to show how it works. - alphaChannelFunc = vtk.vtkPiecewiseFunction() + alphaChannelFunc = vtkPiecewiseFunction() alphaChannelFunc.AddPoint(0, 0.0) alphaChannelFunc.AddPoint(50, 0.05) alphaChannelFunc.AddPoint(100, 0.1) @@ -47,7 +62,7 @@ def main(): # This class stores color data and can create color tables from a few color points. # For this demo, we want the three cubes to be of the colors red green and blue. - colorFunc = vtk.vtkColorTransferFunction() + colorFunc = vtkColorTransferFunction() colorFunc.AddRGBPoint(50, 1.0, 0.0, 0.0) colorFunc.AddRGBPoint(100, 0.0, 1.0, 0.0) colorFunc.AddRGBPoint(150, 0.0, 0.0, 1.0) @@ -55,25 +70,25 @@ def main(): # The previous two classes stored properties. # Because we want to apply these properties to the volume we want to render, # we have to store them in a class that stores volume properties. - volumeProperty = vtk.vtkVolumeProperty() + volumeProperty = vtkVolumeProperty() volumeProperty.SetColor(colorFunc) volumeProperty.SetScalarOpacity(alphaChannelFunc) - volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper() + volumeMapper = vtkFixedPointVolumeRayCastMapper() volumeMapper.SetInputConnection(dataImporter.GetOutputPort()) # The class vtkVolume is used to pair the previously declared volume as well as the properties # to be used when rendering that volume. - volume = vtk.vtkVolume() + volume = vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) # With almost everything else ready, its time to initialize the renderer and window, as well as # creating a method for exiting the application - renderer = vtk.vtkRenderer() - renderWin = vtk.vtkRenderWindow() + renderer = vtkRenderer() + renderWin = vtkRenderWindow() renderWin.AddRenderer(renderer) - renderInteractor = vtk.vtkRenderWindowInteractor() + renderInteractor = vtkRenderWindowInteractor() renderInteractor.SetRenderWindow(renderWin) # We add the volume to the renderer ... -- GitLab