Hi guys,
I currently have a python vtk function that loads a obj file with texture information. Ideally I would prefer to use paraview simple for all my pipelines. I can load the mesh in paraview simple but can not find a way to load the texture as defined in the mtl file
This is the vtk function that works. Is there an equivalent in simple?
def load_vtk_mesh(self, renderer, job_path: str, source: str = "polycam", file_extension=".obj"):
"""
Load an OBJ mesh with optional MTL/texture into the vtk_context renderer.
"""
obj_file = next((f for f in os.listdir(job_path) if f.lower().endswith(file_extension)), None)
if not obj_file:
raise FileNotFoundError(f"No {file_extension} file found in {job_path}")
obj_path = os.path.join(job_path, obj_file)
# optional MTL and texture
mtl_file = next((os.path.join(job_path, f) for f in os.listdir(job_path) if f.lower().endswith(".mtl")), None)
texture_folder = os.path.join(job_path, "textures")
texture_file = next((os.path.join(texture_folder, f) for f in os.listdir(texture_folder) if f.lower().endswith(".jpg")), None) \
if os.path.isdir(texture_folder) else None
# read OBJ
reader = vtkOBJReader()
reader.SetFileName(obj_path)
reader.Update()
polydata = reader.GetOutput()
self.vtk_current_point_cloud = polydata
# Apply source-specific transform
polydata = self.apply_source_transform(polydata, source)
self.vtk_current_point_cloud = polydata
# mapper & actor
mapper = vtkPolyDataMapper()
mapper.SetInputData(polydata)
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetRepresentationToSurface()
# apply texture if available
tcoords = polydata.GetPointData().GetTCoords()
if texture_file and tcoords:
texture_reader = vtkJPEGReader()
texture_reader.SetFileName(texture_file)
texture_reader.Update()
texture = vtkTexture()
texture.SetInputConnection(texture_reader.GetOutputPort())
texture.InterpolateOn()
actor.SetTexture(texture)
self.current_texture = texture
# <<< store the actor for later clipping
self.current_actor = actor
# add actor to renderer
#renderer = self.vtk_context.renderer3D
renderer.RemoveAllViewProps()
renderer.SetBackground(0, 0, 0)
renderer.AddActor(actor)
# add grid
self.grid_actor = None # force re-create
self.add_3d_grid(renderer)
# setup camera
bounds = self.current_actor.GetBounds() # exact mesh bounds
center = [(bounds[0]+bounds[1])/2, (bounds[2]+bounds[3])/2, (bounds[4]+bounds[5])/2]
diag = max(bounds[1]-bounds[0], bounds[3]-bounds[2], bounds[5]-bounds[4])
camera = renderer.GetActiveCamera()
camera.SetFocalPoint(*center)
camera.SetPosition(center[0], center[1], center[2] + diag*2)
camera.SetViewUp(0, 1, 0)
renderer.ResetCameraClippingRange()
renderer.Render()