Skip to content
Snippets Groups Projects
Commit 278b2f61 authored by Jean-Christophe Fillion-Robin's avatar Jean-Christophe Fillion-Robin
Browse files

STYLE: src/Python: Improve readability using explicit import

It improves the readability of the examples and help the reader
understand where objects come from.

See also See https://stackoverflow.com/a/2386740/1539918


Otherwise, from "The Zen of Python":

  Explicit is better than implicit.

See https://www.python.org/dev/peps/pep-0020/


Former-commit-id: 7c14bcd8
parent a520be53
No related branches found
No related tags found
No related merge requests found
[flake8]
max-line-length = 150
ignore =
# F403 'from numpy import *' used; unable to detect undefined names
F403,
# F403 'from vtk import *' used; unable to detect undefined names
F403,
# F405 'XXXX' may be undefined, or defined from star imports
F405,
from __future__ import print_function
from vtk import *
import vtk
# ============ create source points ==============
print("Creating source points...")
......
from vtk import*
import vtk
# Setup the coordinates of eight points
......@@ -14,7 +14,7 @@ P7 = [0.0, 1.0, 1.0]
# Create the points
points = vtkPoints()
points = vtk.vtkPoints()
points.InsertNextPoint(P0)
points.InsertNextPoint(P1)
points.InsertNextPoint(P2)
......@@ -25,7 +25,7 @@ points.InsertNextPoint(P6)
points.InsertNextPoint(P7)
# Create a hexahedron from the points
hexa = vtkHexahedron()
hexa = vtk.vtkHexahedron()
hexa.GetPointIds().SetId(0, 0)
hexa.GetPointIds().SetId(1, 1)
hexa.GetPointIds().SetId(2, 2)
......@@ -36,22 +36,22 @@ hexa.GetPointIds().SetId(6, 6)
hexa.GetPointIds().SetId(7, 7)
# Add the hexahedron to a cell array
hexs = vtkCellArray()
hexs = vtk.vtkCellArray()
hexs.InsertNextCell(hexa)
# Add the points and hexahedron to an unstructured grid
uGrid = vtkUnstructuredGrid()
uGrid = vtk.vtkUnstructuredGrid()
uGrid.SetPoints(points)
uGrid.InsertNextCell(hexa.GetCellType(), hexa.GetPointIds())
surface = vtkDataSetSurfaceFilter()
surface = vtk.vtkDataSetSurfaceFilter()
surface.SetInputData(uGrid)
surface.Update()
aBeamMapper = vtkDataSetMapper()
aBeamMapper = vtk.vtkDataSetMapper()
aBeamMapper.SetInputConnection(surface.GetOutputPort())
aBeamActor = vtkActor()
aBeamActor = vtk.vtkActor()
aBeamActor.SetMapper(aBeamMapper)
aBeamActor.AddPosition(0, 0, 0)
aBeamActor.GetProperty().SetColor(1, 1, 0)
......@@ -61,32 +61,32 @@ aBeamActor.GetProperty().SetEdgeColor(1, 1, 1)
aBeamActor.GetProperty().SetLineWidth(1.5)
# create a plane to cut,here it cuts in the XZ direction (xz normal=(1,0,0);XY =(0,0,1),YZ =(0,1,0)
plane = vtkPlane()
plane = vtk.vtkPlane()
plane.SetOrigin(0.5, 0, 0)
plane.SetNormal(1, 0, 0)
# create cutter
cutter = vtkCutter()
cutter = vtk.vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInputData(aBeamActor.GetMapper().GetInput())
cutter.Update()
cutterMapper = vtkDataSetMapper()
cutterMapper = vtk.vtkDataSetMapper()
cutterMapper.SetInputConnection(cutter.GetOutputPort())
# create plane actor
planeActor = vtkActor()
planeActor = vtk.vtkActor()
planeActor.GetProperty().SetColor(1, 0.5, 0.5)
planeActor.GetProperty().SetLineWidth(2)
planeActor.SetMapper(cutterMapper)
# Setup a renderer, render window, and interactor
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
# renderWindow.SetWindowName("Test")
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Add the actor to the scene
......
......@@ -3,39 +3,39 @@
#
# by Panos Mavrogiorgos, email: pmav99 <> gmail
from vtk import *
import vtk.vtk
# The source file
file_name = "uGridEx.vtk"
# Read the source file.
reader = vtkUnstructuredGridReader()
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(file_name)
reader.Update() # Needed because of GetScalarRange
output = reader.GetOutput()
scalar_range = output.GetScalarRange()
# Create the mapper that corresponds the objects of the vtk file
# Create the mapper that corresponds the objects of the vtk.vtk file
# into graphics elements
mapper = vtkDataSetMapper()
mapper = vtk.vtkDataSetMapper()
mapper.SetInputData(output)
mapper.SetScalarRange(scalar_range)
# Create the Actor
actor = vtkActor()
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# Create the Renderer
renderer = vtkRenderer()
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1) # Set background to white
# Create the RendererWindow
renderer_window = vtkRenderWindow()
renderer_window = vtk.vtkRenderWindow()
renderer_window.AddRenderer(renderer)
# Create the RendererWindowInteractor and display the vtk_file
interactor = vtkRenderWindowInteractor()
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderer_window)
interactor.Initialize()
interactor.Start()
from vtk import *
import vtk
# setup points and vertices
Points = vtk.vtkPoints()
......
from vtk import *
import vtk
# setup points and vertices
Points = vtk.vtkPoints()
......
# An example from scipy cookbook demonstrating the use of numpy arrys in vtk
import vtk
from numpy import *
import numpy as np
# We begin by creating the data we want to render.
# For this tutorial, we create a 3D-image containing three overlaping cubes.
# This data can of course easily be replaced by data from a medical CT-scan or anything else three dimensional.
# The only limit is that the data must be reduced to unsigned 8 bit or 16 bit integers.
data_matrix = zeros([75, 75, 75], dtype=uint8)
data_matrix = np.zeros([75, 75, 75], dtype=np.uint8)
data_matrix[0:35, 0:35, 0:35] = 50
data_matrix[25:55, 25:55, 25:55] = 100
data_matrix[45:74, 45:74, 45:74] = 150
......
from vtk import*
import vtk
# Create a sphere
sphereSource = vtkSphereSource()
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0.0, 0.0, 0.0)
sphereSource.SetRadius(10)
sphereSource.Update()
# Create a mapper and actor
mapper = vtkPolyDataMapper()
mapper = vtk.vtk.vtkPolyDataMapper()
mapper.SetInputConnection(sphereSource.GetOutputPort())
actor = vtkActor()
actor = vtk.vtkActor()
actor.SetMapper(mapper)
camera = vtkCamera()
camera = vtk.vtkCamera()
camera.SetPosition(0, 0, 100)
camera.SetFocalPoint(0, 0, 0)
# Create a renderer, render window, and interactor
renderer = vtkRenderer()
renderer = vtk.vtkRenderer()
renderer.SetActiveCamera(camera)
renderWindow = vtkRenderWindow()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Add the actor to the scene
......
#!/usr/bin/python
from vtk import *
import vtk
def Sphere():
# create the quadric function definition
quadric = vtkQuadric()
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(1, 1, 1, 0, 0, 0, 0, 0, 0, 0)
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
......@@ -16,7 +16,7 @@ def Sphere():
def EllipticParaboloid():
# create the quadric function definition
quadric = vtkQuadric()
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(1, 1, 0, 0, 0, 0, 0, 0, -1, 0)
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
......@@ -27,7 +27,7 @@ def EllipticParaboloid():
def HyperbolicParaboloid():
# create the quadric function definition
quadric = vtkQuadric()
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(1, -1, 0, 0, 0, 0, 0, 0, 0, 0)
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
......@@ -38,7 +38,7 @@ def HyperbolicParaboloid():
def Cylinder():
# create the quadric function definition
quadric = vtkQuadric()
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(1, 1, 0, 0, 0, 0, 0, 0, 0, 0)
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
......@@ -49,7 +49,7 @@ def Cylinder():
def HyperboloidOneSheet():
# create the quadric function definition
quadric = vtkQuadric()
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(1, 1, -1, 0, 0, 0, 0, 0, 0, 0)
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
......@@ -60,7 +60,7 @@ def HyperboloidOneSheet():
def HyperboloidTwoSheets():
# create the quadric function definition
quadric = vtkQuadric()
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(1, 1, -1, 0, 0, 0, 0, 0, 0, 0)
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
......@@ -71,7 +71,7 @@ def HyperboloidTwoSheets():
def Ellipsoid():
# create the quadric function definition
quadric = vtkQuadric()
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(1, 1, 2, 0, 0, 0, 0, 0, 0, 0)
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
......@@ -82,7 +82,7 @@ def Ellipsoid():
def Cone():
# create the quadric function definition
quadric = vtkQuadric()
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(1, 1, -1, 0, 0, 0, 0, 0, 0, 0)
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
......@@ -92,7 +92,7 @@ def Cone():
def Other():
# create the quadric function definition
quadric = vtkQuadric()
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(.5, 1, .2, 0, .1, 0, 0, .2, 0, 0)
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
......@@ -102,7 +102,7 @@ def Other():
def PlotFunction(quadric, value):
# sample the quadric function
sample = vtkSampleFunction()
sample = vtk.vtkSampleFunction()
sample.SetSampleDimensions(50, 50, 50)
sample.SetImplicitFunction(quadric)
# double xmin = 0, xmax=1, ymin=0, ymax=1, zmin=0, zmax=1
......@@ -115,39 +115,39 @@ def PlotFunction(quadric, value):
sample.SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax)
# create the 0 isosurface
contours = vtkContourFilter()
contours = vtk.vtkContourFilter()
contours.SetInputConnection(sample.GetOutputPort())
contours.GenerateValues(1, value, value)
# map the contours to graphical primitives
contourMapper = vtkPolyDataMapper()
contourMapper = vtk.vtkPolyDataMapper()
contourMapper.SetInputConnection(contours.GetOutputPort())
contourMapper.SetScalarRange(0.0, 1.2)
# create an actor for the contours
contourActor = vtkActor()
contourActor = vtk.vtkActor()
contourActor.SetMapper(contourMapper)
# -- create a box around the function to indicate the sampling volume --
# create outline
outline = vtkOutlineFilter()
outline = vtk.vtkOutlineFilter()
outline.SetInputConnection(sample.GetOutputPort())
# map it to graphics primitives
outlineMapper = vtkPolyDataMapper()
outlineMapper = vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
# create an actor for it
outlineActor = vtkActor()
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)
outlineActor.GetProperty().SetColor(0, 0, 0)
# setup the window
ren1 = vtkRenderer()
renWin = vtkRenderWindow()
ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtkRenderWindowInteractor()
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# add the actors to the scene
......
from vtk import*
import vtk
# Create a sphere
textSource = vtkTextSource()
textSource = vtk.vtkTextSource()
textSource.SetText("Hello")
textSource.SetForegroundColor(1.0, 0.0, 0.0)
textSource.BackingOn()
textSource.Update()
# Create a mapper and actor
mapper = vtkPolyDataMapper()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(textSource.GetOutputPort())
actor = vtkActor()
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# Create a renderer, render window, and interactor
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Add the actor to the scene
......
#!/usr/bin/env python
from __future__ import print_function
from vtk import *
import vtk
tse = vtkTimeSourceExample()
tse = vtk.vtkTimeSourceExample()
ex = tse.GetExecutive()
tse.UpdateInformation()
......@@ -24,7 +24,7 @@ print(tse.GetOutput().GetBounds())
grid = tse.GetOutput()
print(grid)
tri = vtkDataSetTriangleFilter()
tri = vtk.vtkDataSetTriangleFilter()
tri.SetInputData(grid)
tri.SetTetrahedraOnly(1)
tri.Update()
......@@ -37,57 +37,57 @@ assert(iss > -1)
drange = [0, 1]
# Create transfer mapping scalar value to opacity.
opacityFunction = vtkPiecewiseFunction()
opacityFunction = vtk.vtkPiecewiseFunction()
opacityFunction.AddPoint(drange[0], 0.0)
opacityFunction.AddPoint(drange[1], 1.0)
# Create transfer mapping scalar value to color.
colorFunction = vtkColorTransferFunction()
colorFunction = vtk.vtkColorTransferFunction()
colorFunction.SetColorSpaceToHSV()
colorFunction.HSVWrapOff()
colorFunction.AddRGBPoint(drange[0], 0.0, 0.0, 1.0)
colorFunction.AddRGBPoint(drange[1], 1.0, 0.0, 0.0)
volumeProperty = vtkVolumeProperty()
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetScalarOpacity(opacityFunction)
volumeProperty.SetColor(colorFunction)
volumeProperty.ShadeOff()
volumeProperty.SetInterpolationTypeToLinear()
# volumeProperty.SetScalarOpacityUnitDistance(options.unit)
volumeMapper = vtkUnstructuredGridVolumeRayCastMapper()
# volumeMapper = vtkUnstructuredGridVolumeZSweepMapper()
# volumeMapper = vtkProjectedTetrahedraMapper()
volumeMapper = vtk.vtkUnstructuredGridVolumeRayCastMapper()
# volumeMapper = vtk.vtkUnstructuredGridVolumeZSweepMapper()
# volumeMapper = vtk.vtkProjectedTetrahedraMapper()
# volumeMapper.SetBlendModeToMaximumIntensity()
volumeMapper.SetInputData(output)
volume = vtkVolume()
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
# create a rendering window and renderer
renderer = vtkRenderer()
renderer = vtk.vtkRenderer()
renderer.SetBackground(0, 0, 0)
window = vtkRenderWindow()
window = vtk.vtkRenderWindow()
window.SetSize(512, 512)
window.AddRenderer(renderer)
interactor = vtkRenderWindowInteractor()
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(window)
style = vtkInteractorStyleTrackballCamera()
style = vtk.vtkInteractorStyleTrackballCamera()
interactor.SetInteractorStyle(style)
renderer.AddVolume(volume)
scalarBar = vtkScalarBarActor()
scalarBar = vtk.vtkScalarBarActor()
scalarBar.SetLookupTable(colorFunction)
scalarBar.SetOrientationToVertical()
scalarBar.SetPosition(0.85, 0.7)
scalarBar.SetPosition2(0.1, 0.3)
propT = vtkTextProperty()
propL = vtkTextProperty()
propT = vtk.vtkTextProperty()
propL = vtk.vtkTextProperty()
propT.SetFontFamilyToArial()
propT.ItalicOff()
propT.BoldOn()
......@@ -98,7 +98,7 @@ scalarBar.SetLabelFormat("%5.2f")
renderer.AddActor(scalarBar)
# setup the text and add it to the window
textActor = vtkTextActor()
textActor = vtk.vtkTextActor()
textActor.GetTextProperty().SetFontSize(12)
textActor.SetPosition2(10, 40)
renderer.AddActor2D(textActor)
......@@ -119,7 +119,7 @@ while time <= 1:
# TODO FIXME if this block is not here than the volume renders wrongly
# renderer.RemoveVolume(volume)
# del volume
volume = vtkVolume()
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
renderer.AddVolume(volume)
......
from vtk import*
import vtk
# Create a sphere
textSource = vtkVectorText()
textSource = vtk.vtkVectorText()
textSource.SetText("Hello")
textSource.Update()
# Create a mapper and actor
mapper = vtkPolyDataMapper()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(textSource.GetOutputPort())
actor = vtkActor()
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(1.0, 0.0, 0.0)
# Create a renderer, render window, and interactor
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Add the actor to the scene
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment