Module II

Objectives

a Now we connect tree Python script to build a complete szene

Instructions

Tasks:
  1. What are the differences to the modules, used before.

  2. Create your own scene and manage the objects in separate modules (files).

  3. Add a module for the ground. Let it look like a chessboard.
    See also learning station: materials

Module: checker.py

#! bpy
"""
Name: 'checker.py'
Blender: 269
Group: 'Modules'
Tooltip: 'Create a checker'
"""
import bpy


def checker(location=(0, 0, 0)):
    '''Checker build from a cone and a sphere'''

    # save the context
    scn = bpy.context.scene

    # list of names 
    parts = ['head', 'body', 'checker']

    # head
    bpy.ops.mesh.primitive_uv_sphere_add(location=(2, 2, .9))
    obj = bpy.context.object
    obj.scale[0] = .6
    obj.scale[1] = .6
    obj.scale[2] = .6
    obj.name = parts[0]

    scn.objects.active = scn.objects[parts[0]]

    # body
    bpy.ops.mesh.primitive_cone_add(location=(2, 2, 0))
    obj = bpy.context.object
    obj.scale[0] = .8
    obj.scale[1] = .8
    obj.name = parts[1]
    scn.objects.active = scn.objects[parts[1]]

    # connecting the parts
    scn.objects[parts[0]].select = True
    scn.objects[parts[1]].select = True
    bpy.ops.object.join()

    obj.name = parts[2]
    obj.location = location

if __name__ == '__main__':
    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete()
    checker(location=(2, 2, 2))

Module: levels.py

#! bpy
"""
Name: 'levels.py'
Blender: 269
Group: 'Modules'
Tooltip: 'Maze definitions'
"""

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


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

Main programm: sokoban.py

#! bpy
"""
Name: 'sokoban_m2.py'
Blender: 269
Group: 'Modules'
Tooltip: 'Create a maze'
"""
import bpy
import levels
from checker import checker


def sokobanLevel(level):
    """Create a level managed by modules"""

    cols = len(level[0])
    rows = len(level)
    
    for row in range(rows):
        for i in range(cols):
            coords = (row*2, i*2, 0)
            if level[row][i] == '#':
                bpy.ops.mesh.primitive_cube_add(location=coords)
            elif level[row][i] == '@':
                checker(location=coords)
            elif level[row][i] == '$':
                bpy.ops.mesh.primitive_cone_add(location=coords)

if __name__ == '__main__':
    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete()
    sokobanLevel(levels.level_00)

Result

../../_images/scene_complete.png