# Question on using SlicerGT Texture mapping

**URL:** <https://discourse.slicer.org/t/question-on-using-slicergt-texture-mapping/16971>\
**Category:** Development\
**Created:** [April 7, 2021, 3:47am UTC](https://discourse.slicer.org/t/question-on-using-slicergt-texture-mapping/16971 "2021-04-07T03:47:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackxiong](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@jackxiong](https://discourse.slicer.org/u/jackxiong)\
**Post date:** [April 7, 2021, 3:47am UTC](https://discourse.slicer.org/t/question-on-using-slicergt-texture-mapping/16971/1 "2021-04-07T03:47:10Z")

</div>

Hi Andras, maybe this is a simple question, but I really need your help. I have the requirement of using gradient color jpg file make a texture mapping on 3D surface, I have draft code with pure vtk, and it got the expected result:

import vtk

# Create a render window

ren = vtk.vtkRenderer()  
renWin = vtk.vtkRenderWindow()  
renWin.AddRenderer(ren)  
renWin.SetSize(480,480)  
iren = vtk.vtkRenderWindowInteractor()  
iren.SetRenderWindow(renWin)

# Generate a sphere

sphere = vtk.vtkSphereSource()

# Read the image data from a file

reader = vtk.vtkJPEGReader()  
reader.SetFileName(“gradientColor.jpeg”)

# Create texture object

texture = vtk.vtkTexture()  
texture.SetInputConnection(reader.GetOutputPort())

#Map texture coordinates  
map\_to\_plane = vtk.vtkTextureMapToPlane()  
map\_to\_plane.SetInputConnection(sphere.GetOutputPort())

# Create mapper and set the mapped texture as input

mapper = vtk.vtkPolyDataMapper()  
mapper.SetInputConnection(map\_to\_plane.GetOutputPort())

# Create actor and set the mapper and the texture

actor = vtk.vtkActor()  
actor.SetMapper(mapper)  
actor.SetTexture(texture)

ren.AddActor(actor)

iren.Initialize()  
renWin.Render()  
iren.Start()

The result as below picture:

 ![图片](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/4/54b9984fb905675a46a38bfcd2ce21da6feb3500.jpeg)

I have two questions here:  
The first one:  
When I try to use SlicerIGT extension Texture Model, it has not got the expected result. The steps as below:

v = slicer.util.getNode(‘View1’)  
v.SetBoxVisible(False)

# Sphere

sphereSource = vtk.vtkSphereSource()  
sphereSource.SetCenter(0,0,0)  
sphereSource.SetRadius(60.0)  
sphereSource.Update()  
sphereSource = sphereSource.GetOutput() # vtkPolyData()

# Create a model with the sphere and add it to scene

modelNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLModelNode’)  
modelNode.SetAndObservePolyData(sphereSource)  
modelNode.CreateDefaultDisplayNodes()  
modelNode.SetName(‘My sphere’)

and then I come back to main UI in 3D Slicer and use the Texture model as below:

 ![图片](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/f/5f7cb3b7b80b5552a0ce1a97f34ec6aa7a08bbb7.jpeg)

But as you see, the result is not expected. What am I wrong here?

The second one is: How can I use python script to generate the texture mapping model? Should I use below code:

def showTextureOnModel(self, modelNode, textureImageNode):  
modelDisplayNode = modelNode.GetDisplayNode()  
modelDisplayNode.SetBackfaceCulling(0)  
textureImageFlipVert = vtk.vtkImageFlip()  
textureImageFlipVert.SetFilteredAxis(1)  
textureImageFlipVert.SetInputConnection(textureImageNode.GetImageDataConnection())  
modelDisplayNode.SetTextureImageDataConnection(textureImageFlipVert.GetOutputPort())

maybe the question is simple and stupid, but I really need your help and suggestion. And I cannot access the website: [onedrive.live.com](http://onedrive.live.com), is there another website for the SlicerIGT tutorial?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jackxiong](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@jackxiong](https://discourse.slicer.org/u/jackxiong)\
**Post date:** [April 8, 2021, 1:27am UTC](https://discourse.slicer.org/t/question-on-using-slicergt-texture-mapping/16971/2 "2021-04-08T01:27:12Z")

</div>

Hi Buddies and experts in the community, anybody knows the solution? I don’t know how to convert the python script to 3D Slicer console(as the script in 1st section, it do the sphere texture mapping in pure vtk library very well, but I cannot convert the python script in 3D Slicer) . I refer to the link [Documentation/Nightly/ScriptRepository - Slicer Wiki](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Access_VTK_actor_properties), but seems take no effect.

Your suggestions are appreciated  
Thanks again!

---

<div class="post-metadata">

**Author:** ![jackxiong](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@jackxiong](https://discourse.slicer.org/u/jackxiong)\
**Post date:** [April 8, 2021, 9:20am UTC](https://discourse.slicer.org/t/question-on-using-slicergt-texture-mapping/16971/3 "2021-04-08T09:20:05Z")

</div>

Never mind, I have already solved the problem, refer to below link [Documentation/Nightly/ScriptRepository - Slicer Wiki](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Accessing_views.2C_renderers.2C_and_cameras) convert python script & run in 3D Slicer python console correctly.
