9

I'm using Blender 2.8+ and I found a web page containing a nice python script to generate a labyrinth. I copied the script in Blenders text editor and an error occurred at line 88:

bpy.context.scene.objects.link (object)

AttributeError: 'bpy_prop_collection' object has no attribute 'link' Error: Python script failed, check the message in the system console

Unfortunately, I have no idea what a 'bpy_prop_collection' is and I don't know where to start fixing this issue. What I need to do in order to make the script work for Blender 2.8x?

brockmann
  • 12,613
  • 4
  • 50
  • 93
kaloprat
  • 331
  • 1
  • 2
  • 7
  • 2
    Need waaay more information. "The error" is not a basis for debugging. At least paste error output and tell us what version you're using. – Frederik Steinmetz Dec 30 '19 at 13:26
  • Related https://blender.stackexchange.com/questions/95408/how-do-i-create-a-new-object-using-python-in-blender-2-80 Found via googling ""site:blender.stackexchange.com bpy_prop_collection' object has no attribute 'link'" For scripts like above, recommend adding a comment to the blog re the error, or now by adding a link to upgraded to 2.8x code supplied by @brockmann – batFINGER Jan 01 '20 at 03:01

1 Answers1

17

If you are interested: As of Blender 2.8x SceneObjects.link(object) is gone due to the new collection system. You can use CollectionObjects.link(my_object) instead so you just need to replace a single line to "update the script" for Blender 2.8x:

- bpy.context.scene.objects.link(object)
+ bpy.context.collection.objects.link(object)

Blender 2.80: Python API Changes


blender labyrinth generator 2.8x:

from random import shuffle 
from array import * 
import bpy

N=1 S=2 E=4 W=8

odir = {} odir[N] = S odir[S] = N odir[W] = E odir[E] = W

WIDTH=40 HEIGHT=40

coords = [] faces = []

d = [[0 for col in range(WIDTH)] for row in range(HEIGHT)]

for i in range(0,WIDTH): for j in range(0, HEIGHT): d[i][j] = 0

def carve( x, y ): dirs = [N,S,W,E] shuffle(dirs)

for i in dirs:
    print(i)
    nx = x
    if i == E:
        nx =  nx + 1
    if i == W:
        nx = nx - 1
    ny = y
    if i == S:
        ny = ny +1
    if i == N:
        ny = ny - 1
    if nx >= 0 and nx < WIDTH and ny >=0 and ny < HEIGHT and d[nx][ny] == 0:
            d[x][y] = d[x][y] | i
            d[nx][ny] = d[nx][ny]  | odir[i]
            carve(nx, ny)

carve(0,0)

idx=0 step = 0.1

for x in range(0,WIDTH): for y in range( 0, HEIGHT ): if ( d[x][y] & N ) == 0: coords.append((x * step , y * step, 0 )) coords.append(((x+1) * step, y * step, 0 )) coords.append(((x+1) * step, y * step, step )) coords.append((x * step, y * step, step )) faces.append(( idx, idx+1, idx+2, idx+3)) idx+=4 if ( d[x][y] & S ) == 0: coords.append((x * step , (y+1) * step, 0 )) coords.append(((x+1) * step, (y+1) * step, 0 )) coords.append(((x+1) * step, (y+1) * step, step )) coords.append((x * step, (y+1) * step, step )) faces.append(( idx, idx+1, idx+2, idx+3)) idx+=4 if ( d[x][y] & E ) == 0: coords.append(((x+1) * step, y * step, 0 )) coords.append(((x+1) * step, (y+1) * step, 0 )) coords.append(((x+1) * step, (y+1) * step, step )) coords.append(((x+1) * step, y * step, step )) faces.append(( idx, idx+1, idx+2, idx+3)) idx+=4 if ( d[x][y] & W ) == 0: coords.append((x * step, y * step, 0 )) coords.append((x * step, (y+1) * step, 0 )) coords.append((x * step, (y+1) * step, step )) coords.append((x * step, y * step, step )) faces.append(( idx, idx+1, idx+2, idx+3)) idx+=4

mesh = bpy.data.meshes.new(name="MyMesh")

object = bpy.data.objects.new( 'MESH', mesh ) bpy.context.scene.collection.objects.link( object )

mesh.from_pydata( coords, [], faces ) mesh.update( calc_edges=True )

Gorgious
  • 30,723
  • 2
  • 44
  • 101
brockmann
  • 12,613
  • 4
  • 50
  • 93