1
$\begingroup$

I found a project on github at https://github.com/eFFeeMMe/bge-traffic. I can run this model on the blender game engine successfully. There are city.py,cityengine.py and makecity.py in the project.I guess the makecity.py is the main code to grnerate the city.blend.Here is the code: import bpy

def instances_from_ascii(txt):
    becomes = {' ': 'tile_Grass0',
               '-': 'tile_RoadH',
               '|': 'tile_RoadV',
               '+': 'tile_RoadX',
               '0': 'tile_Building0',
               '1': 'tile_Building1',
               '2': 'tile_Building2',
    }

    cell_w = 2.
    cell_h = 2.

    lines = txt.splitlines()
    h = len(lines)
    w = max(len(line) for line in lines)

    grid = [[None for y in range(h)] for x in range(w)]

    for y, line in enumerate(lines):
        for x, symbol in enumerate(line):
            obj_x = x * cell_w
            obj_y = - y * cell_h # So that when in default blender game projection, orientation is like text
            if symbol in becomes:
                group_name = becomes[symbol]
                bpy.ops.object.group_instance_add(group=group_name,
                                                  view_align=False,
                                                  location=(obj_x, obj_y, 0.))
                grid[x][y] = o = bpy.context.selected_objects[0]
                bpy.ops.object.game_property_new(type='STRING', name="adj_e")
                bpy.ops.object.game_property_new(type='STRING', name="adj_w")
                bpy.ops.object.game_property_new(type='STRING', name="adj_n")
                bpy.ops.object.game_property_new(type='STRING', name="adj_s")
                o.game.properties['adj_e'].value = ''
                o.game.properties['adj_w'].value = ''
                o.game.properties['adj_n'].value = ''
                o.game.properties['adj_s'].value = ''

    # Setting graph links. Sadly, grid[w-1][h-1] won't be linked.
    for x in range(w - 1):
        for y in range(h - 1):
            o = grid[x][y]
            if o:
                a = grid[x+1][y]
                b = grid[x][y+1]
                if a:
                    o.game.properties['adj_e'].value = a.name
                    a.game.properties['adj_w'].value = o.name
                if b:
                    o.game.properties['adj_s'].value = b.name
                    b.game.properties['adj_n'].value = o.name


instances_from_ascii("""
11201101201201201101210202101020121
0        |     |     |            0
2        |     |     |            1
1        |     |     +-----+      0
1   1    |     |     |     |      1
0 0   2  | 10  |  0  | 1   |      2
2    1   |  2  | 2   |  2  |      1
0      20|    1| 1   |   0 |      0
0 21 01  | 012 | 0 21|102  |0     2
1--------+-----+-----+-----+------1
0      22|     |     | 12 0       1
2        |     |     |0    21     1
1        |     |     |   1 0      1
1        |     +-----+------------1
1        |            1           0
""")

Now I change the text such as change '0' to ' ',but there had anything changed.How can I change the city?How did it generate the city.blend?

$\endgroup$

1 Answer 1

0
$\begingroup$

You will have to run that python code within blender's text editor.

Possible duplicate of https://stackoverflow.com/questions/11604548/running-python-script-in-blender

$\endgroup$
2
  • $\begingroup$ Thanks,I have run the script but unfortunately it didn't work when I click 3D View.Here is something in the terminal: $\endgroup$
    – zhouzhou
    Aug 10, 2017 at 7:45
  • $\begingroup$ It works when I delete the original model.Thank you very much! $\endgroup$
    – zhouzhou
    Aug 10, 2017 at 8:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .