初步了解VTK装配体

VTK还不太了解,根据资料,

vtk.vtkAssembly 是 VTK库中的一个重要类,允许通过将多个vtkActor对象组合在一起来创建复杂的3D模型。

import vtk
import math
from vtk.util.colors import *
 
filenames = ["cylinder.stl","sphere.stl","torus.stl"]
dt = 1.0
renWin = vtk.vtkRenderWindow()
actor  = list()
joint1 = vtk.vtkAssembly()  
joint2 = vtk.vtkAssembly()
joint3 = vtk.vtkAssembly()

class MyInteractor(vtk.vtkInteractorStyleTrackballCamera):
    def __init__(self,parent=None):
        self.AddObserver("CharEvent",self.OnCharEvent)
        self.AddObserver("KeyPressEvent",self.OnKeyPressEvent)

    def OnCharEvent(self,obj,event):
        pass
    
    def OnKeyPressEvent(self,obj,event):
        global angle
        key = self.GetInteractor().GetKeySym()
        if(key == "Left"):
            joint1.RotateY(-dt)      
        if(key == "Right"):
            joint1.RotateY(dt)
            
        if(key == "Up"):
            joint2.RotateY(-dt)           
        if(key == "Down"):
            joint2.RotateY(dt)
            
        if(key == "a"):
            joint3.RotateY(-dt)           
        if(key == "d"):
            joint3.RotateY(dt)

        renWin.Render()
        return
    

def LoadSTL(filename):
    reader = vtk.vtkSTLReader()
    reader.SetFileName(filename)
    mapper = vtk.vtkPolyDataMapper() 
    mapper.SetInputConnection(reader.GetOutputPort())
    actor = vtk.vtkLODActor() 
    actor.SetMapper(mapper)
    return actor  

        
def CreateScene():
    ren = vtk.vtkRenderer()
    renWin.AddRenderer(ren)
     
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)
    style = MyInteractor()
    style.SetDefaultRenderer(ren)
    iren.SetInteractorStyle(style)
    
    for id, file in enumerate(filenames):
            actor.append(LoadSTL(file))
            r = vtk.vtkMath.Random(.4, 1.0)
            g = vtk.vtkMath.Random(.4, 1.0)
            b = vtk.vtkMath.Random(.4, 1.0)
            actor[id].GetProperty().SetDiffuseColor(r, g, b)
            actor[id].GetProperty().SetDiffuse(.8)
            actor[id].GetProperty().SetSpecular(.5)
            actor[id].GetProperty().SetSpecularColor(1.0,1.0,1.0)
            actor[id].GetProperty().SetSpecularPower(30.0)

    joint1.AddPart(actor[0])
    joint1.AddPart(joint2)
    joint2.AddPart(actor[1])
    joint2.AddPart(joint3)
    joint3.AddPart(actor[2])

    joint2.SetOrigin(100, 0, 0)  # initial elbow joint position
    joint3.SetOrigin(200, 0, 0)  # initial wrist joint position

    ren.AddActor(joint1)
    ren.GradientBackgroundOn()
    ren.SetBackground(.1, .1, .1)
    ren.SetBackground2(0.8,0.8,0.8)

    renWin.SetSize(600, 600)

    iren.Initialize()
    iren.Start()
        
if __name__ == "__main__":
    CreateScene()

这是网上的代码,它可以对多个STL文件进行装配;我随便找的stl文件,先看一下初步效果;如果有正确的多个文件,就能装配自己的复杂模型,和模型动画;

开始它定义了3个变量,

joint1 = vtk.vtkAssembly()  
joint2 = vtk.vtkAssembly()
joint3 = vtk.vtkAssembly()

joint是关节的意思,定义了3个关节;

一开始出来是这样的,

打左右键三个会一起动,打上下键其中一个不动,另2个动,就变成如下;

 打a键其中一个动,另两个不动,就变为如下;

 

先了解一下装配体的概念; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值