我正在嘗試編寫一個程序,該程序在按下箭頭鍵時顯示和循環(huán)顯示不同文件格式的圖像。我發(fā)現(xiàn)設置我的 reader->mapper->actor 管道然后啟動我的 RenderWindowInteractor 允許渲染 actor,但是將此代碼移動到 RenderWindowInteractor 的 KeyPressEvent 的回調(diào)不允許渲染 actor。一旦事件循環(huán)開始,是否需要額外的步驟來設置新的actor,或者我犯了其他錯誤?import vtk#Create the renderer that will display our actorsren = vtk.vtkRenderer()actor = None#Maps the data from the Nifti file 'filename' onto a vtkImageSlice actordef LoadNifti(): # load head MRI in NIFTI format reader = vtk.vtkNIFTIImageReader() reader.SetFileName("head.nii") reader.Update() # Add an ImageSliceMapper, which maps data to an ImageSlice Actor mapper = vtk.vtkImageSliceMapper() #Set the slice position to the camera focal point mapper.SliceAtFocalPointOn() mapper.SetInputConnection(reader.GetOutputPort()) # Add an ImageSlice actor, which represents a slice as an image global actor actor = vtk.vtkImageSlice() actor.SetMapper(mapper)#Placeholder for when I have DICOM workingdef ShowDICOM(filename): pass#Create a RenderWindow to display images from the RendererrenWin = vtk.vtkRenderWindow()renWin.AddRenderer(ren)#Wrap the RenderWindow in a RenderWindowInteractor to catch key and mouse eventsiren = vtk.vtkRenderWindowInteractor()iren.SetRenderWindow(renWin)def foo(): LoadNifti() ren.AddActor(actor)##Extend the default functionality of the interactor and its style with custom listenersdef KeyPressEvent(obj, ev): foo() print("This verifies that the callback is triggered")iren.AddObserver("KeyPressEvent", KeyPressEvent) #Start the program############################################################################ WHEN foo() IS PRESENT BELOW, THE ACTOR WILL RENDER IMMEDIATELY.# WHEN foo() IS ABSENT BELOW, CALLING foo() BY TRIGGERING THE KeyPressEvent# IS NOT SUFFICIENT TO HAVE THE ACTOR RENDER.###########################################################################foo()#According to the docs, the Start method will initialize iren and render renWin automaticallyiren.Start()
1 回答

子衿沉夜
TA貢獻1828條經(jīng)驗 獲得超3個贊
好吧,您絕對可以在回調(diào)中添加或刪除角色。要立即再次渲染場景,只需調(diào)用iren.Render()。
當您在沒有任何可見演員和明確定義的相機的情況下啟動渲染器時,ResetCamera通常需要。AutomaticResetCamera在初始化期間只調(diào)用一次,這就是為什么調(diào)用foo()beforeiren.Start()使對象可見的原因。
def foo():
LoadNifti()
ren.AddActor(actor)
ren.ResetCamera() # should that be necessary
iren.Render()
- 1 回答
- 0 關(guān)注
- 431 瀏覽
添加回答
舉報
0/150
提交
取消