Materials/Textures – mapping¶
Objectives¶
Mapping is a common method to put pictures on objects. With simple objects it is possible to use a script. This is demonstrated in this station. Move the final cube to see all pictures applied. How to animate the cube is shown in an other station. |
Instructions¶
Tasks: |
---|
- Start the functions and have a look to the resulting output.
- Replace the images with your own.
- Alter the code, let one face empty.
Sample images¶
Download (pictures_uv_mapping.zip)
Putting images (2D) on a body (3D) is named UV-Mapping. The letters U and V are used, because X, Y and Z always used for positioning an object in a 3D-World. Like materials textures are managed independent form any object. Textures are also never directly connected to an object, instead a material is used. So wie have a chain: texture » material » body.
In our first example a picture is placed on one site of a cube:
The second example is showing different images on all faces of a cube:
Have I heard the term »Mindcraft« (hava a look at exercise tree):
One image on all faces¶
#!bpy
"""
Name: 'mat1.py'
Blender: 2.69
Group: 'Sample'
Tooltip: 'Put one image on each site of a cube'
"""
import bpy
import os
def uvMapperCube(obj):
""" Put image to an object"""
# used names
matname = "matUVMapping"
texname = "texUVMapping"
# new material
if not matname in bpy.data.materials:
material = bpy.data.materials.new(matname)
material.diffuse_color = (0, .5, .4)
obj.data.materials.append(material)
# new texture
texUV = bpy.data.textures.new(texname, type="IMAGE")
image_path = os.path.expanduser("//wuerfelbilder/blume.jpg")
image = bpy.data.images.load(image_path)
texUV.image = image
# connect textur with material
bpy.data.materials[matname].texture_slots.add()
bpy.data.materials[matname].active_texture = texUV
bpy.data.materials[matname].texture_slots[0].texture_coords = "GLOBAL"
bpy.data.materials[matname].texture_slots[0].mapping = "CUBE"
def delete_old_stuff():
# escape edit mode
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
# delete all mesh objects
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()
# delete all materials
for i in bpy.data.materials.values():
bpy.data.materials.remove(i)
# delete all textures
for i in bpy.data.textures.values():
bpy.data.textures.remove(i)
# delete all images
for i in bpy.data.images.values():
# delete image path, this is only possible without a user
i.user_clear()
# delete all, except »Render Result«
if i.name != "Render Result":
bpy.data.images.remove(i)
if __name__ == "__main__":
delete_old_stuff()
bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
obj = bpy.context.scene.objects.active
obj.name = 'image-as-uvmapping'
obj = bpy.context.scene.objects['image-as-uvmapping']
uvMapperCube(obj)
One image on each Face¶
#!bpy
"""
Name: 'mat2.py'
Blender: 2.69
Group: 'Sample'
Tooltip: 'Put six different images to cube'
"""
import bpy
import os
def uvMapperCube(obj):
""" Put images to an object"""
# used names
namen = {'blume.jpg': ('mat01', 'tex01'),
'boot.jpg': ('mat02', ' tex02'),
'jueterbog.jpg': ('mat03', 'tex03'),
'kopf.jpg': ('mat04', 'tex04'),
'moster.jpg': ('mat05', 'tex05'),
'telefon.jpg': ('mat06', 'tex06'),
#'warnung.jpg': ('mat07', 'tex07')
}
for i in namen.keys():
# new material
matname = namen[i][0]
if not matname in bpy.data.materials:
material = bpy.data.materials.new(matname)
material.diffuse_color = (0, .5, .4)
obj.data.materials.append(material)
# new texture
texUV = bpy.data.textures.new(namen[i][1], type="IMAGE")
image_path = os.path.expanduser("//wuerfelbilder/{}".format(i))
image = bpy.data.images.load(image_path)
texUV.image = image
# connect texture with material
bpy.data.materials[matname].texture_slots.add()
bpy.data.materials[matname].active_texture = texUV
bpy.data.materials[matname].texture_slots[0].texture_coords = "ORCO"
bpy.data.materials[matname].texture_slots[0].mapping = "CUBE"
# apply one material to one face
for face in obj.data.polygons:
face.material_index = face.index
def delete_old_stuff():
# escape edit mode
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
# delete all mesh objects
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()
# delete all materials
for i in bpy.data.materials.values():
bpy.data.materials.remove(i)
# delete all textures
for i in bpy.data.textures.values():
bpy.data.textures.remove(i)
# delete all images
for i in bpy.data.images.values():
# delete image path, this is only possible without a user
i.user_clear()
# delete all, except »Render Result«
if i.name != "Render Result":
bpy.data.images.remove(i)
if __name__ == "__main__":
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
delete_old_stuff()
bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
obj = bpy.context.scene.objects.active
obj.name = 'image-as-uvmapping'
obj = bpy.context.scene.objects['image-as-uvmapping']
uvMapperCube(obj)