ha-material

Übersicht: Python & Blender


Objectives

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.

blender-basics/h-material/colored-labyrinth.py (Source)

#! bpy
"""
Name: 'colored_labyrinth.py'
Blender: 2.7x
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.

blender-basics/h-material/colored-labyrinth.py (Source)

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

Save object

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

blender-basics/h-material/colored-labyrinth.py (Source)

    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.

blender-basics/h-material/colored-labyrinth.py (Source)

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

Allocate material

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

blender-basics/h-material/colored-labyrinth.py (Source)

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

Result

/blender-basics/h-material/colored-labyrinth.png

Comments