0
$\begingroup$

The following error keeps showing up in my script:

AttributeError: 'bpy_prop_collection' object has no attribute 'new' 

How do I fix this?

import bpy

# Set the size of the brick
brick_width = 0.5
brick_height = 0.25
brick_depth = 0.2

# Create a new mesh and object
mesh = bpy.data.meshes.new("Brick")
obj = bpy.data.objects.new("Brick", mesh)

# Set the location and dimensions of the object
obj.location = (0, 0, 0)
obj.dimensions = (brick_width, brick_height, brick_depth)

# Add vertices to the mesh
v1 = mesh.vertices.new((0, 0, 0))
v2 = mesh.vertices.new((brick_width, 0, 0))
v3 = mesh.vertices.new((brick_width, brick_depth, 0))
v4 = mesh.vertices.new((0, brick_depth, 0))
v5 = mesh.vertices.new((0, 0, brick_height))
v6 = mesh.vertices.new((brick_width, 0, brick_height))
v7 = mesh.vertices.new((brick_width, brick_depth, brick_height))
v8 = mesh.vertices.new((0, brick_depth, brick_height))

# Create edges and faces for the brick
mesh.edges.new((v1, v2))
mesh.edges.new((v2, v3))
mesh.edges.new((v3, v4))
mesh.edges.new((v4, v1))
mesh.edges.new((v1, v5))
mesh.edges.new((v2, v6))
mesh.edges.new((v3, v7))
mesh.edges.new((v4, v8))
mesh.edges.new((v5, v6))
mesh.edges.new((v6, v7))
mesh.edges.new((v7, v8))
mesh.edges.new((v8, v5))
mesh.faces.new((v1, v2, v3, v4))
mesh.faces.new((v5, v6, v7, v8))
mesh.faces.new((v1, v4, v8, v5))
mesh.faces.new((v2, v6, v7, v3))
mesh.faces.new((v4, v3, v7, v8))
mesh.faces.new((v1, v5, v6, v2))

# Create a new material
mat = bpy.data.materials.new("BrickMaterial")
mat.diffuse_color = (0.8, 0.4, 0.2, 1.0)

# Assign the material to the object
obj.data.materials.append(mat)

# Add the object to the scene
scene = bpy.context.scene
scene.collection.objects.link(obj)
```
$\endgroup$
1
  • $\begingroup$ Hello ! If you don't mind me asking, was this generated by an AI assisted tool ? If so, would you mind sharing the prompt you used to generate this ? $\endgroup$
    – Gorgious
    Dec 23, 2022 at 7:37

1 Answer 1

1
$\begingroup$

As you have noticed, when you run your script, it errors out with AttributeError: 'bpy_prop_collection' object has no attribute 'new' and positions the cursor at the end of line v1 = mesh.vertices.new((0, 0, 0)).

enter image description here

So apparently there is no method or property with the name new on mesh.vertices. You can rewrite your script like this but take note the generated brick is very small so you will either need to increase the brick_width etc dimensions or zoom in with Numpad . (dot) after you have generated your object.

import bpy

# Set the size of the brick
brick_width = 0.5
brick_height = 0.25
brick_depth = 0.2

# Create a new mesh and object
mesh = bpy.data.meshes.new("Brick")
obj = bpy.data.objects.new("Brick", mesh)

# Set the location and dimensions of the object
obj.location = (0, 0, 0)
obj.dimensions = (brick_width, brick_height, brick_depth)

# Add vertices to the mesh
v1 = (0, 0, 0)
v2 = (brick_width, 0, 0)
v3 = (brick_width, brick_depth, 0)
v4 = (0, brick_depth, 0)
v5 = (0, 0, brick_height)
v6 = (brick_width, 0, brick_height)
v7 = (brick_width, brick_depth, brick_height)
v8 = (0, brick_depth, brick_height)

verts = (v1, v2, v3, v4, v5, v6, v7, v8)

faces = ((1, 5, 4, 0),
         (6, 5, 1, 2),
         (0, 3, 2, 1),
         (6, 2, 3, 7),
         (4, 7, 3, 0),
         (5, 6, 7, 4))

mesh.from_pydata(verts, [], faces)

# Create a new material
mat = bpy.data.materials.new("BrickMaterial")
mat.diffuse_color = (0.8, 0.4, 0.2, 1.0)

# Assign the material to the object
obj.data.materials.append(mat)

# Add the object to the scene
scene = bpy.context.scene
scene.collection.objects.link(obj)
$\endgroup$

You must log in to answer this question.

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