Module I

Objectives

a Large projects can no longer manage them in a file. The source code will quickly become confusing. Therefore, there is, as in other languages also, for Python a solution that is shown here.

Instructions

Tasks:
  1. Move parts of the source code in a second file (levels.py) from.
  2. Import the outsourced parts.
  3. Check whether the program behaves, as previously working.
  4. Add a third level in the file levels.py.

One module for levels

To separate level definitions from the code that creates the maze we create two files. The first one is named levels.py. From now on, a game designer could create new levels, while a programmer cares about the game logic.

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

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


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

The main module

Reusing code from other files (other authors/programmers) is achieved with the import statement.

 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
#! bpy
"""
Name: 'sokoban.py'
Blender: 269
Group: 'Modules'
Tooltip: 'Creating a game called Sokoban and using modules'
"""
import bpy
from levels import level_00
from levels import level_01


def sokobanLevel(level):
    """Create a maze with 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))

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

Errors

Again and again, errors occur and it is important to find the cause. For example a small typo occurred. The errors are always shown as a traceback in the console.

Traceback (most recent call last):
File "/Users/.../sokoban.py", line 10, in <module>
ImportError: cannot import name Level_01
Error: Python script fail, look in the console for now...

The file path in this Traceback is shorten. Important is the line number, often indicating the main cause of the error. Also important is the type of error, in this case the ImportError, because Python is case-sensitive. So the Error will disappear, if you replace L with l.

Connecting Python scripts with a Blender file

The classical way to find modules in Python is limited in Blender. The easiest workaround is to include the Python scripts into the Blender file.

Step by step

  1. Create a new blend file.
  2. Load every Python script.
  3. For each file call the menu: Text | Make internal.
../../_images/make-internal.png

Save the Blender file and all modules are available.

External Editors

The internal Editor is not so bad, but if you like to use your prefered Editor, for example emacs, vi on Linux or PyCharm on Windows, you can edit the external Python scripts. A small icon is indicating, that you have altered the content of the file and you are forced to reload the script, starting with a right click on this icon.

../../_images/out-of-sync.png