How do you set (load) a jpg as an image texture?

Excuse me if I’m missing the obvious - I’m just learning the ropes:

I’ve searched many posts, but can’t find how to set jpg images (1.jpg, 2.jpg, …) to objects (cube1, cube2, …).

I thought it would be like setting colours with mat.rgbCol = [ …]

Any help appreciated.

Hi pa – cute nick : )

You set images to object (mesh) faces. Check the reference doc, if you haven’t already, class NMFace in module NMesh.

The doc: http://www.blender.org/modules/documentation/228PythonDoc/index.html

BTW: There are a few additions to Blender 2.28c not documented in this version of the doc (checking the release notes should cover all of them except NMesh.Modes dictionary and nmesh.set/getMode() methods). Criticism always welcome, the docs were not polished yet.

Thanks for the response. IanWill :smiley: Can you tell me if this is getting close?


import Blender
from Blender import NMesh

me = NMesh.GetRaw("Plane.001")   # This has image1.jpg as UV texture
f = me.faces[0]

f.image.setName("image2.jpg")       # Trying to set new image file
print "image filename is: ", f.image.getFilename()   # Still image1.jpg

NMesh.PutRaw(me,"Plane.001")
me.update()
Blender.Redraw()


I struggle with Python, but feel there should be a ‘setFilename’ method, in addition to the ‘name’, ‘filename’, ‘setName’, ‘getName’ methods listed by doing a “print dir(f.image)” , but there wasn’t.

Can you possibly give another pointer?

(PS: Are PutRaw, update() and Redraw all really necessary?? )

Hi,

Getting closer. But setname is not enough, you must actually load the image, with the Image module – Image.Load(). Then you use that image object with each face. That’s why a setFilename isn’t necessary (and isn’t desirable, actually). If the images are already loaded, then grab them with Image.Get().

About your second question, if you grab a mesh with GetRaw, make some changes to it and want to update it, simply use update, no need for PutRaw. If you create a new mesh, you can either use PutRaw or (improved in 2.28c) do this:
obj = Object.New(“Mesh”)
me = NMesh.New(“meshname”)

fill your new mesh here

in the end:

obj.link(me)

then link obj with some scene

About Redraw, it’s necessary to tell Blender that something changed and you want to see the change. If you don’t use redraw, then the change will only be visible when you put mouse focus on the related window. Blender itself uses lots of redraws, of course.

Thanks for the clarifications. :smiley: I’ll try the more general approach you show.

In case another newbie tries the test code above, the following should now work.

(You need eg a Plane with ‘Tex’ button in Paint Buttons already enabled – F, Paint Buttons, Tex). I don’t know how to do the equivalent in python yet - something with f.uv :-? ?)


import Blender 
from Blender import NMesh, Image

me = NMesh.GetRaw("meshname")    
f = me.faces[0] 
img = Image.Load("c:\image1.jpg")           # if from disk
        #  else use    img = Image.Get("myimage.jpg")  if previously loaded
f.image = img   

me.update()