[vtkusers] Rendering Problem
Shari Rolnick
shari at loralee.net
Sun Apr 23 21:00:44 EDT 2006
Hi,
I'm trying to create a visualization and save it to a file. The code
creates the visualization I want, but not until after I click in the
rendering window. That means that I don't get anything in the saved
image but a blank screen. I've tried moving around the statements, but
nothing seems to work. Any ideas what I'm doing wrong?
Thanks!
Shari
Here is my code:
#!/usr/bin/env python
import os
import re
import vtk
import PropertySet
# Set axes
propSet = PropertySet.PropertySet('viz.properties')
xAxis = propSet.xAxisVariable
yAxis = propSet.yAxisVariable
zAxis = propSet.zAxisVariable
scalar = propSet.scalarVariable
# Set color stuff
minHue = propSet.minHue
maxHue = propSet.maxHue
minSaturation = propSet.minSaturation
maxSaturation = propSet.maxSaturation
minValue = propSet.minValue
maxValue = propSet.maxValue
# Set scale factor
scaleFactor = propSet.scaleFactor
scaleFactor = float(scaleFactor)
# extract data from field as polydata (just points), then extract
scalars
dataObjectReader = vtk.vtkDataObjectReader()
dataObjectReader.SetFileName("schoolData.vtk")
do2ds = vtk.vtkDataObjectToDataSetFilter()
do2ds.SetInput(dataObjectReader.GetOutput())
do2ds.SetPointComponent(0, xAxis, 0)
do2ds.SetPointComponent(1, yAxis, 0)
do2ds.SetPointComponent(2, zAxis, 0)
do2ds.Update()
dataSet = vtk.vtkPolyData()
# Get number of points
dataSet = do2ds.GetOutput()
numPoints = dataSet.GetNumberOfPoints()
dataSet.Update()
fd2adf = vtk.vtkFieldDataToAttributeDataFilter()
fd2adf.SetInput(do2ds.GetOutput())
fd2adf.SetInputFieldToDataObjectField()
fd2adf.SetOutputAttributeDataToPointData()
fd2adf.SetScalarComponent(0, scalar, 0)
fd2adf.Update()
scalarRange = fd2adf.GetOutput().GetScalarRange()
# create glyph
cone = vtk.vtkConeSource()
cone.SetResolution(6)
# create lookup table
lut = vtk.vtkLookupTable()
lut.SetHueRange(float(minHue), float(maxHue))
lut.SetSaturationRange(float(minSaturation), float(maxSaturation))
lut.SetValueRange(float(minValue), float(maxValue))
lut.Build()
glyph = vtk.vtkGlyph3D()
glyph.SetInput(fd2adf.GetOutput())
glyph.SetSource(cone.GetOutput())
glyph.SetVectorModeToUseVector()
glyph.SetScaleModeToScaleByScalar()
glyph.SetScaleFactor(scaleFactor)
glyph.SetColorModeToColorByScalar()
glyph.SetIndexModeToScalar()
glyphMapper = vtk.vtkPolyDataMapper()
glyphMapper.SetLookupTable(lut)
glyphMapper.SetScalarRange(scalarRange)
glyphMapper.SetColorModeToMapScalars()
glyphMapper.SetInput(glyph.GetOutput())
glyphActor = vtk.vtkActor()
glyphActor.SetMapper(glyphMapper)
# Find maximum value and use it to scale the axes
bounds = dataSet.GetBounds()
print("bounds:"), bounds
listBounds = list(bounds)
listBounds.sort()
sortedBounds = tuple(listBounds)
# create axes
axes = vtk.vtkAxes()
axes.SetScaleFactor(sortedBounds[5])
maxValString = str(sortedBounds[5])
decimalIndex = maxValString.find('.')
maxValString = maxValString[0:decimalIndex]
# We will use this number to decide how many 0's to put in our division
-
# used to find the radius of the axes
orderOfMagnitude = len(maxValString) - 1
radiusDivisorStr = '1'
count = 0
while count < orderOfMagnitude:
radiusDivisorStr += '0'
count += 1
radiusDivisorInt = int(radiusDivisorStr)
axesTubes = vtk.vtkTubeFilter()
axesTubes.SetInput(axes.GetOutput())
axesTubes.SetRadius(axes.GetScaleFactor() / radiusDivisorInt)
axesTubes.SetNumberOfSides(12)
axesMapper = vtk.vtkPolyDataMapper()
axesMapper.SetInput(axesTubes.GetOutput())
axesActor = vtk.vtkActor()
axesActor.SetMapper(axesMapper)
# label the axes
XText = vtk.vtkVectorText()
XText.SetText(xAxis)
XTextMapper = vtk.vtkPolyDataMapper()
XTextMapper.SetInput(XText.GetOutput())
XActor = vtk.vtkFollower()
XActor.SetMapper(XTextMapper)
XActor.SetScale(10.0, 10.0, 10.0)
XActor.SetPosition(100, -10, -10)
XActor.GetProperty().SetColor(1.0, 1.0, 1.0)
YText = vtk.vtkVectorText()
YText.SetText(yAxis)
YTextMapper = vtk.vtkPolyDataMapper()
YTextMapper.SetInput(YText.GetOutput())
YActor = vtk.vtkFollower()
YActor.SetMapper(YTextMapper)
YActor.SetScale(10.0, 10.0, 10.0)
YActor.SetPosition(-10, 100, -10)
YActor.GetProperty().SetColor(1.0, 1.0, 1.0)
ZText = vtk.vtkVectorText()
ZText.SetText(zAxis)
ZTextMapper = vtk.vtkPolyDataMapper()
ZTextMapper.SetInput(ZText.GetOutput())
ZActor = vtk.vtkFollower()
ZActor.SetMapper(ZTextMapper)
ZActor.SetScale(10.0, 10.0, 10.0)
ZActor.SetPosition(-10, -10, 100)
ZActor.GetProperty().SetColor(1.0, 1.0, 1.0)
# Add picking
# Create a cell picker.
picker = vtk.vtkCellPicker()
# Create a text mapper and actor to display the results of picking.
textMapper = vtk.vtkTextMapper()
tprop = textMapper.GetTextProperty()
tprop.SetFontFamilyToArial()
tprop.SetFontSize(14)
tprop.BoldOn()
tprop.ShadowOn()
tprop.SetColor(1, 0, 0)
textActor = vtk.vtkActor2D()
textActor.VisibilityOff()
textActor.SetMapper(textMapper)
# Create a Python function to create the text for the text mapper used
# to display the results of picking.
def annotatePick(object, event):
global picker, textActor, textMapper
if picker.GetCellId() < 0:
textActor.VisibilityOff()
else:
selPt = picker.GetSelectionPoint()
pickPos = picker.GetPickPosition()
pickActor = picker.GetActor()
pickMapper = picker.GetMapper()
textMapper.SetInput("(%.6f, %.6f, %.6f)"%pickPos)
textActor.SetPosition(selPt[:2])
textActor.VisibilityOn()
# Now at the end of the pick event call the above function.
picker.AddObserver("EndPickEvent", annotatePick)
# Use bounds of dataset to determine focal point
bounds = dataSet.GetBounds()
xFocus = (bounds[0] + bounds[1]) / 2
yFocus = (bounds[2] + bounds[3]) / 2
zFocus = (bounds[4] + bounds[5]) / 2
center = dataSet.GetCenter()
camera = vtk.vtkCamera()
camera.SetFocalPoint(center[0], center[1], center[2])
camera.SetPosition(1000, 1000, 1000)
camera.SetViewUp(1, 0, 0)
# render the image
#Graphics Stuff
ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
renWin.SetWindowName("vtk - Field Data")
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Add the actors to the renderer, set the background and size
ren1.AddActor(axesActor)
ren1.AddActor(XActor)
ren1.AddActor(YActor)
ren1.AddActor(ZActor)
ren1.AddActor(glyphActor)
ren1.AddActor2D(textActor)
ren1.SetActiveCamera(camera)
renWin.SetSize(800, 800)
renWin.Render()
print("About to write image file")
windowToImage = vtk.vtkWindowToImageFilter()
windowToImage.SetInput(renWin)
bitmapWriter = vtk.vtkBMPWriter()
bitmapWriter.SetInput(windowToImage.GetOutput())
bitmapWriter.SetFileName("TestFileName.bmp")
bitmapWriter.Write()
print("Image file written")
iren.SetPicker(picker)
iren.Initialize()
iren.Start()
--
Shari Rolnick <shari at loralee.net>
More information about the vtkusers
mailing list