vtkOBJReader 将 OBJ 作为单独的 vtkPolyData 对象导入 [英] vtkOBJReader import OBJ as individual vtkPolyData objects

查看:130
本文介绍了vtkOBJReader 将 OBJ 作为单独的 vtkPolyData 对象导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是让 vtkPolyData(通过 vtk OBJ 导入获得理想的颜色/纹理)传递给 k3d 查看器使用;

My intention is to get the vtkPolyData(ideally colored/textured via the vtk OBJ import) to pass to k3d viewer using;

k3d.vtk_poly_data(<vtkPolyData>)

vtkGLTFReader 提供了一个 vtkMultiBlock,它可以很容易地迭代以访问多个 vtkPolyData 对象,如图所示 这里?

vtkGLTFReader gives a vtkMultiBlock that which can be easily iterated over to get access to multiple vtkPolyData objects as shown here?

另一方面,vtkOBJReader 返回一个 vtkPolyData 对象,该对象包含一个 obj 中的所有对象.所以我失去了对单个对象及其 guids/ids 的引用;

vtkOBJReader on the other hand returns a vtkPolyData object that encompasses all the objects in one obj. So I have lost my reference to individual objects and their guids/ids;

reader = vtk.vtkOBJReader() 
reader.SetFileName('Sample.obj')
reader.Update() 
vtkPolyData = reader.GetOutput() #Problem! all .OBJ elements in one vtkPolyData instance

  1. 是否可以访问单个 vtkPolyData 对象来自 .OBJ 而不将 .OBJ 拆分为多个文件?
  2. 是一个 vtkMultiBlock 版本的可从 vtk python 访问的数据模块?或者这只能通过 js 版本的 vtk 实现,如 这里

由于 vtkImporter 类取代了 vtkReader 类并且已经正确显示了带有材质 (.mtl) 的对象,我怀疑有一种方法可以通过 Render_Window 对象访问几何体,但我无法管理.

Since the vtkImporter class supersedes vtkReader class and already correctly displays objects with material (.mtl) I suspect there is a way to get access the geometry through the Render_Window object, but I could not manage this.

解决方案;

importer = vtk.vtkOBJImporter()
importer.SetFileName('Sample.obj')
importer.SetFileNameMTL('Sample.mtl')

#preview with material using the importer
importer.Read()
importer.InitializeObjectBase()
importer.GetRenderer()
vtkRenderWindowInteractor = vtk.vtkRenderWindowInteractor()
Render_Window = importer.GetRenderWindow()
vtkRenderWindowInteractor.SetRenderWindow(Render_Window)

actors = Renderer.GetActors()
actors.InitTraversal() #Unpacks OBJ !

#Iterate over actors to get PolyData!
pds = []
for a in range(actors.GetNumberOfItems()):
    actor = actors.GetNextActor()

    # OBJImporter turns texture interpolation off
    if actor.GetTexture():
        actor.GetTexture().InterpolateOn()

    pd = actor.GetMapper().GetInput()
    
    #mapper = vtk.vtkPolyDataMapper(actor.GetMapper()) #Throws TypeError: Method requires a string argument!
    mapper = actor.GetMapper() #use this in python
    mapper.SetInputData(pd)
    pds.append(pd)

vtkRenderWindowInteractor.Start() 

推荐答案

您正在寻找导入器和渲染器的正确方向.渲染器对象包含角色,每个角色都有一个映射器,其中包含一个多数据作为输入.

You are looking in the right direction wiht the importer and the renderer. The renderer object contains actors, each one has a mapper that contains a polydata as input.

参见这个 cxx 示例(python API 是相同的,除了您可以直接通过 renderer.GetActors() 进行迭代):https://lorensen.github.io/VTKExamples/site/Cxx/IO/OBJImporter/

See this cxx example (python API is the same, except you can directly iterate through renderer.GetActors()): https://lorensen.github.io/VTKExamples/site/Cxx/IO/OBJImporter/

这篇关于vtkOBJReader 将 OBJ 作为单独的 vtkPolyData 对象导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆