I would like to apply an image to an object using python.
Anyone have a suggestion or maybe a link to some info reguarding this?
BaDbOyHeRe
I would like to apply an image to an object using python.
Anyone have a suggestion or maybe a link to some info reguarding this?
BaDbOyHeRe
Actually, I am trying to get python to load an image file and apply it to the surface of an object. To give you a better example, I am trying to have my webcam capture pictures to the hard drive, and have python grab them and apply to an object every Nth frame. I have been studying the VideoCapture script which uses PIL library. Furthermore, to have a script draw the image in a script window like the draw_pic example. I have found that it is possible to do this in the script window(working with PIL to convert the image format from jpeg to tga), but not sure that applying it to an object is possible (at least for me) at the moment. I am learning python each free moment I can find.
You can assign images as uv-textures, but it is a bit unreliable and might crash blender when you don’t do it exactly right.
The Blender.Image module is meant for this, the Load() function loads an image. You can then assign an image to a mesh face:
import Blender
ob = Blender.Object.get(objectname)
me = ob.getData()
img = Blender.Image.Load(image_filename)
if img:
if me.hasFaceUV():
for f in me.faces:
f.image = img
f.flag = Blender.NMesh.FaceModes.TEX
f.transp = Blender.NMesh.FaceTranspModes.SOLID
me.setMaterials([])
me.update()
Blender.Redraw()
else:
print "Mesh has no uv coords"
else:
print "Can't load image"
This doesn’t really work as it should, the texflag is not registered it seems. Also the reason the material is removed, is because blender seems to set the usercount to -1 or something like that (it doesn’t exist any longer?), which will make blender crash when rendering. The material name will turn red when this happens.
Also the mesh must have uv-coords, but of course you could set them in the code as well.
thank you for your reply. And sorry people for my double posting. thanks for your help
Ray