Hi all
I'm trying to colour and scale spherical glyphs using two different scalar arrays in the Polydata.Unfortunately, what I seem to be able to do is to colour *and* scale according to the one scalar array, but not be able to scale by one scalar and colour by the other. The code attached:
Can anyone help what wrong with my code?
This is data file:
1,6,3
1,6,2
2,6,4
2,3,5
6,3,2
6,3,2
from vtk import *
from vtk.tk.vtkTkRenderWindowInteractor import *
from Tkinter import *
def RawDataExample():
""" Creates 2D data inside a vtkTkRenderWindowInteractor. """
def SetScalar1():
#color grid by first set of scalars
myGrid.GetPointData().SetScalars(myFirstScalar)
win.Render()
return
def SetScalar2():
##color grid by second set of scalars
myGrid.GetPointData().SetScalars(mySecondScalar)
win.Render()
return
def SetScalar3():
myGrid.GetPointData().SetScalars(myThirdScalar)
win.Render()
return
# create root window
root = Tk()
root.title("Raw Data 1")
f=Frame(root)
f.pack()
heading=Label(f,text="Raw Data")
heading.pack()
quit=Button(f,text="Quit",command=root.destroy)
quit.pack()
#set up a structured points and two arrays to hold sets of data
myGrid=vtkPolyData()
myPoint=vtkPoints()
myFirstScalar = vtkIntArray()
myFirstScalar.SetName("myfirst")
mySecondScalar = vtkIntArray()
mySecondScalar.SetName("mysecond")
myThirdScalar=vtkIntArray()
myThirdScalar.SetName("mythird")
myGlyph=vtk.vtkGlyph3D()
#set up buttons to swap from 1 scalar array to the other
Scalar1btn= Button(f,text= "scalar1", command=SetScalar1)
Scalar2btn= Button(f,text= "scalar2", command=SetScalar2)
Scalar3btn= Button(f,text= "scalar3", command=SetScalar3)
Scalar1btn.pack()
Scalar2btn.pack()
Scalar3btn.pack()
win = vtkTkRenderWindowInteractor(root, width=300, height=300)
win.Initialize()
win.GlobalWarningDisplayOff()
def quit(obj=root):
obj.destroy()
ren = vtk.vtkRenderer()
win.GetRenderWindow().AddRenderer(ren)
## #set grid with 6 points
## myGrid.SetDimensions(3,2,1)
## myGrid.SetSpacing(1,1,0)
## myGrid.SetOrigin(0.,0.,0.)
# read some data for 6 points
f=open('textdata.txt', 'r')
for line in f:
#read in a line and split up into a list delimited by ,
linelist = line.split(',')
print linelist
#put two numbers into two integer arrays
myPoint.InsertNextPoint((float)(linelist[0]),(float)(linelist[2]),0)
myFirstScalar.InsertNextValue((int)(linelist[0]))
mySecondScalar.InsertNextValue((int)(linelist[1]))
myThirdScalar.InsertNextValue((int)(linelist[2]))
data = vtk.vtkFloatArray()
data.SetNumberOfComponents(2)
data.SetNumberOfTuples(3)
data.CopyComponent(0, myFirstScalar, 0)
data.CopyComponent(1, myFouthScalar, 0)
data.SetName("data")
myGrid.SetPoints(myPoint)
myGrid.GetPointData().AddArray(data)
myGrid.GetPointData().SetActiveScalars("data")
cone=vtk.vtkSphereSource()
myGlyph.SetInput(myGrid)
myGlyph.SetSource(cone.GetOutput())
myGlyph.ClampingOff()
myGlyph.SetScaleModeToScaleByScalar()
myGlyph.SetScaleFactor(.1)
myGlyph.SetColorModeToColorByScalar()
myMapper = vtk.vtkPolyDataMapper()
myMapper.SetInput(myGlyph.GetOutput())
myMapper.ScalarVisibilityOn()
myMapper.ColorByArrayComponent("data", 1)
myMapper.SetScalarRange(0,6)
myActor = vtk.vtkActor()
myActor.SetMapper(myMapper)
ren.AddActor(myActor)
# pack the win into the tk root
win.pack(fill='both', expand=1)
win.Start()
# start the tk mainloop
root.mainloop()
RawDataExample()