Reputation: 3337
I am using Blender 3.0 on Ubuntu 21,10 Why is this code not working? I am trying to randomly generate the x, y and z values of the six vertices of a cube and create it.
This code was inspired by Chris Holt's here https://youtu.be/mljWBuj0Gho
import bpy import random name="New Object" verts=[] edges=[] faces=[]
bed = 300 #in mm print("bed = " + str(bed))
xmin = -bed/2 ymin = -bed/2 zmin = -bed/2 xmax = bed/2 ymax = bed/2 zmax = bed/2
print("xmin" + str(xmin)) print("xmax" + str(xmax)) print("ymin" + str(ymin)) print("ymax" + str(ymax)) print("zmin" + str(zmin)) print("zmax" + str(zmax))
a1= random.randint(xmin,0) a2 = random.randint(ymin,0) a3 = random.randint(0,zmax)
b1 = random.randint(xmin,0) b2 = random.randint(0,ymax) b3 = random.randint(0,zmax)
c1 = random.randint(0,xmax) c2 = random.randint(0,ymax) c3 = random.randint(0,zmax)
d1 = random.randint(0,xmax) d2 = random.randint(ymin,0) d3 = random.randint(0,zmax)
e1 = random.randint(xmin,0) e2 = random.randint(ymin,0) e3 = random.randint(zmin,0)
f1 = random.randint(xmin,0) f2 = random.randint(0,ymax) f3 = random.randint(zmin,0)
g1 = random.randint(0,xmax) g2 = random.randint(0,ymax) g3 = random.randint(zmin,0) xmax
h1 = random.randint(0,xmax) h2 = random.randint(ymin,0) h3 = random.randint(zmin,0)
print("\n") print("\n") print(" a1 =" + str(a1)) print("a2 = " + str(a2)) print("a3 = " + str(a3))
print("\n") print("b1 = " + str(b1)) print("b2 = " + str(b2)) print("b3 = " + str(b3))
print("\n") print("c1 = " + str(c1)) print("c2 = " + str(c2)) print("c3 = " + str(c3))
print("\n") print("d1 = " + str(d1)) print("d2 = " + str(d2)) print("d3 = " + str(d3))
print("\n") print("e1 = " + str(a1)) print("e2 = " + str(e2)) print("e3 = " + str(e3))
print("\n") print("f 1= " + str(f1)) print("f 2 = " + str(f2)) print("f3 = " + str(f3))
print("\n") print("g1 = " + str(g1)) print("g2 = " + str(g2)) print("g3 = " + str(g3))
print("\n") print("h1 = " + str(h1)) print("h2 = " + str(h2)) print("h3 = " + str(h3)) print("end")
#8 vertices verts.append([# index 0 a1, #x a2, #y a3 #z ]) verts.append([# index 1 b1, #x b2, #y b3 #z ]) verts.append([# index 2 c1, #x c2, #y c3 #z ]) verts.append([# index 3 d1, # d2, #y d3 #z ]) verts.append([# index 4 e1, #x e2, #y e3 #z ]) verts.append([# index 5 f1, #x f2, #y f3 #z ]) verts.append([# index 6 g1, #x g2, #y g3 #z ]) verts.append([# index 7 h1, #x h2, #y h3 #z ])
#12 edges #top edges.append([0,1]) edges.append([1,2]) edges.append([2,3]) edges.append([3,0]) #bottom edges.append([4,5]) edges.append([5,6]) edges.append([6,7]) edges.append([7,4]) #connections edges.append([0,4]) edges.append([1,5]) edges.append([2,6]) edges.append([3,7])
print(verts[7]) print(edges[0])
#6 facess #top faces.append([0,1,2,3]) #bottom faces.append([4,5,6,7]) #side
faces.append([2,6,10,11])
faces.append([0,1,4,5])
faces.append([1,2,5,6])
faces.append([0,3,4,7])
print(faces[1])
mesh=bpy.data.meshes.new(name) print("mesh = " + str(mesh)) obj=bpy.data.objects.new(name,mesh) print("obj = " + str(obj)) col=bpy.data.collections.get("Collection") print("col = " + str(col)) col.objects.link(obj) bpy.context.view_layer.objects.active=obj #mesh.from_pydata(verts,edges,faces)mod_skin = obj.modifiers.new('Skin', 'SKIN')
The error message I am getting is
Traceback (most recent call last): File "/dem.py", line 188, in AttributeError: 'NoneType' object has no attribute 'objects' Error: Python script failed, check the message in the system console
Upvotes: 0
Views: 972
Reputation: 533
The bpy.data.collections
returns a bpy.prop_collection
in which you can search by key, using the get()
method. Here you seem to be searching for a key that doesn't exist. Which is returning None
and hence your error.
On iterating through the bpy.data.collections
object using a for loop
for collection in bpy.data.collections:
print(collection)
you can see that the only member is
<bpy_struct, Collection("Collection") at 0x7f20e4359848>
This is just the default collection that blender has when you open a new project. If you create a new collection named "Collection 2" or something the for
loop will give you another collection object.
so perhaps what you meant is
bpy.data.collections.get("Collection")
which will give you the default collection?
Upvotes: 1