# http://blender.stackexchange.com/questions/34930/how-do-place-an-image-to-the-side-of-a-cube-using-the-python-api
import bpy
def apply_image_to_face(template, fname):
imgs = [ img for img in bpy.data.images if img.type=='IMAGE' and img.filepath==fname]
if len(imgs)<1:
img = bpy.data.images.load(fname)
print(img.name)
else:
img = imgs[0]
tex = bpy.data.textures.new(fname, 'IMAGE')
tex.image = img
obj = bpy.data.objects.new("capybara", template.data)
mat = template.material_slots[1].material.copy()
tslot = mat.texture_slots[0]
if tslot is None:
tslot = mat.texture_slots.add()
tslot.texture = tex
tslot.texture_coords = 'UV'
obj.material_slots[1].link = 'OBJECT'
print(obj.material_slots[1].link)
obj.material_slots[1].material = mat
print("argh "+obj.material_slots[1].link)
return obj
# this needs to be the template object.
# It needs to already have a UV map.
# The first texture slot is for most of the cube.
# The second texture slot must exist and we will fill it with a newly-created material for the image
# one of the faces must already be assigned this second material.
template = bpy.data.objects["template"]
# template = bpy.context.active_object
obj = apply_image_to_face(template, "/home/thoth/art/drawing-practice/totoro.png")
obj.location= (0,1,0)
bpy.context.scene.objects.link(obj)
|
Blender python API quick-start
Syntax highlighting by Pygments.