MultipleRenderWindows 创建多个渲染窗口

一:主要的知识点

1、说明

本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程------逐行解析官网所有Python示例-CSDN博客

2、知识点纪要

本段代码主要涉及的有①多渲染窗口的开启(共用一个camera)

二:代码及注释

python 复制代码
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkFiltersSources import vtkConeSource, vtkCubeSource, vtkCylinderSource, vtkSphereSource
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)

def get_sources():
    sources = list()


    sphere = vtkSphereSource()
    sphere.SetCenter(0.0, 0.0, 0.0)
    sphere.Update()
    sources.append(sphere)

    cone = vtkConeSource()
    cone.SetCenter(0.0, 0.0, 0.0)
    cone.SetDirection(0, 1, 0)
    cone.Update()
    sources.append(cone)

    cube = vtkCubeSource()
    cube.SetCenter(0.0, 0.0, 0.0)
    cube.Update()
    sources.append(cube)

    cylinder = vtkCylinderSource()
    cylinder.SetCenter(0.0, 0.0, 0.0)
    cylinder.Update()
    sources.append(cylinder)

    return sources

def main():
    simultaneous_update = True  # True表示四个窗口一起动

    colors = vtkNamedColors()
    ren_bkg = ['AliceBlue', 'GhostWhite', 'WhiteSmoke', 'Seashell']
    actor_color = ['Bisque', 'RosyBrown', 'Goldenrod', 'Chocolate']

    width = 300
    height = 300
    dx = 20
    dy = 40
    w = width + dx
    h = height + dy

    interactors = []
    running = [True, True, True, True]

    camera = None
    sources = get_sources()

    kpis = []

    for i in range(0, 4):
        ren_win = vtkRenderWindow()
        ren_win.SetSize(width, height)
        renderer = vtkRenderer()
        if i == 0:
            camera = renderer.GetActiveCamera()
            camera.Azimuth(30)
            camera.Elevation(30)
        else:
            renderer.SetActiveCamera(camera)

        ren_win.AddRenderer(renderer)
        iren = vtkRenderWindowInteractor()
        interactors.append(iren)
        iren.SetRenderWindow(ren_win)

        ren_win.Render()
        ren_win.SetWindowName('MultipleRenderWindows {:d}'.format(i))
        ren_win.SetPosition((i % 2) * w, h - (i // 2) * h)

        mapper = vtkPolyDataMapper()
        mapper.SetInputConnection(sources[i].GetOutputPort())

        actor = vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().SetColor(colors.GetColor3d(actor_color[i]))

        renderer.AddActor(actor)
        renderer.SetBackground(colors.GetColor3d(ren_bkg[i]))

        renderer.ResetCamera()

        kpis.append(KeyPressInteractorStyle(parent=iren))
        interactors[i].SetInteractorStyle(kpis[i])
        kpis[i].status = running[i]
    if simultaneous_update:
        interactors[0].Initialize()
        while all(x is True for x in running): # 死循环,一直执行
            for i in range(0, 4):
                running[i] = kpis[i].status
                if running[i]:
                    interactors[i].ProcessEvents()
                    interactors[i].Render()
                else:
                    interactors[i].TerminateApp()
                    print('Window', i, 'has stopped running.')
    else:
        interactors[0].Start()

class KeyPressInteractorStyle(vtkInteractorStyleTrackballCamera):

    def __init__(self, parent=None, status=True):
        self.parent = vtkRenderWindowInteractor()
        self.status = status
        if parent is not None:
            self.parent = parent

        self.AddObserver('KeyPressEvent', self.key_press_event)

    def key_press_event(self, obj, event):
        key = self.parent.GetKeySym().lower()
        if key == 'e' or key == 'q':
            self.status = False
        return

if __name__ == '__main__':
    main()
相关推荐
津津有味道3 小时前
Python写入URI网址到Ntag 424 DNA标签配置开启动态UID计数器镜像
linux·python·nfc·动态uid·424·cma加密数据
2401_841495643 小时前
【Python高级编程】Python 核心语法速查演示
python·字符串·集合·列表·元组·字典·运算符
2301_811958383 小时前
服务器自己账号下安装conda
linux·python·conda
百***78753 小时前
Mistral 3极速接入指南:3步上手+核心能力解析+避坑手册
人工智能·python·开源
LCG米4 小时前
嵌入式Python开发:MicroPython在物联网硬件上的实战应用案例
python·单片机·物联网
nvd114 小时前
SQLAlchemy 2.0 类型注解指南:`Mapped` 与 `mapped_column`
python
让学习成为一种生活方式4 小时前
AGAT v1.6.0 安装与使用--生信工具72
人工智能·python·机器学习
小陈phd4 小时前
Python MCP 工具开发入门:Server、Client 和 LLM 集成
开发语言·python·github
ShenLiang20254 小时前
识别SQL里的列名
大数据·人工智能·python