Material – colored labyrinth

Objectives

a So far, our objects are unique grey. Now lets make the new world a little bit colorful. Therefor we are using materials and/or textures.

Instructions

Tasks:
  1. Construct your own new level as shown in the last station.
  2. Create new types of objects for the character $ and @ (ie. cone, cylinder).
  3. Add different colors for every type of object.

Das Labyrinth

Creating the labyrinth is the same procedure as before but in addition we add a material.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#! bpy
"""
Name: 'colored_labyrinth.py'
Blender: 269
Group: 'Materials'
Tooltip: 'Generates colorful objects by materials'
"""
import bpy

level_00 = ["###################",
            "#.                #",
            "#        $        #",
            "#                 #",
            "#                 #",
            "#        @        #",
            "#                 #",
            "#                 #",
            "#                 #",
            "#                 #",
            "###################"]


level_01 = ["    #####           ",
            "    #   #           ",
            "    #$  #           ",
            "  ###  $##          ",
            "  #  $ $ #          ",
            "### # ## #   ###### ",
            "#   # ## #####  ..# ",
            "# $  $          ..# ",
            "##### ### #@##  ..# ",
            "    #     ######### ",
            "    #######         "]

MATERIAL_RED = bpy.data.materials.new('Red Material')


def sokobanLevel(level):
    """Generating a labyrinth with colored cubes"""

    cols = len(level[0])
    rows = len(level)

    for row in range(rows):
        for i in range(cols):
            if level[row][i] == '#':
                bpy.ops.mesh.primitive_cube_add(location=(row*2, i*2, 0))
                obj = bpy.context.object
                setColor(obj, MATERIAL_RED, (1, 0, 0))


def setColor(obj, material, color):
    material.diffuse_color = color
    material.specular_hardness = 200
    obj.data.materials.append(material)


if __name__ == '__main__':
    sokobanLevel(level_00)

Create a new material

Materials in Blender are distinguished by name and exists independent from objects. Therefor it is possible to put one material to different objects.

MATERIAL_RED = bpy.data.materials.new('Red Material')

Save object

The new object is allocated to a variable named »obj«.

    for row in range(rows):
        for i in range(cols):
            if level[row][i] == '#':
                bpy.ops.mesh.primitive_cube_add(location=(row*2, i*2, 0))
                obj = bpy.context.object
                setColor(obj, MATERIAL_RED, (1, 0, 0))

Function: setColor

The function setColor needs tree parameters: object, material and color.

                setColor(obj, MATERIAL_RED, (1, 0, 0))

Allocate material

Within setColor the parameters are used to change the given object.

def setColor(obj, material, color):
    material.diffuse_color = color
    material.specular_hardness = 200
    obj.data.materials.append(material)

Result

../../../_images/colored_labyrinth.png