template: operator_mesh_add.py did not work in my Blender of today, change for bmesh!

trying to execute it gives:

RuntimeError: Error: Traceback (most recent call last):
File “\operator_mesh_add.py”, line 89, in execute
AttributeError: ‘Mesh’ object has no attribute ‘faces’

Experimenting I got it to work using bmesh!


import bpy
import bmesh

def add_box(width, height, depth):
    """
    This function takes inputs and returns vertex and face arrays.
    no actual mesh data creation is done here.
    """

    vertices = [1.0, 1.0, -1.0,
                1.0, -1.0, -1.0,
                -1.0, -1.0, -1.0,
                -1.0, 1.0, -1.0,
                1.0, 1.0, 1.0,
                1.0, -1.0, 1.0,
                -1.0, -1.0, 1.0,
                -1.0, 1.0, 1.0,
                ]

    faces = [0, 1, 2, 3,
             4, 7, 6, 5,
             0, 4, 5, 1,
             1, 5, 6, 2,
             2, 6, 7, 3,
             4, 0, 3, 7,
            ]

    # apply size
    for i in range(0, len(vertices), 3):
        vertices[i] *= width
        vertices[i + 1] *= depth
        vertices[i + 2] *= height

    return vertices, faces


from bpy.props import FloatProperty, BoolProperty, FloatVectorProperty


class AddBox(bpy.types.Operator):
    '''Add a simple box mesh'''
    bl_idname = "mesh.primitive_box_add"
    bl_label = "Add Box"
    bl_options = {'REGISTER', 'UNDO'}

    width = FloatProperty(
            name="Width",
            description="Box Width",
            min=0.01, max=100.0,
            default=1.0,
            )
    height = FloatProperty(
            name="Height",
            description="Box Height",
            min=0.01, max=100.0,
            default=1.0,
            )
    depth = FloatProperty(
            name="Depth",
            description="Box Depth",
            min=0.01, max=100.0,
            default=1.0,
            )

    # generic transform props
    view_align = BoolProperty(
            name="Align to View",
            default=False,
            )
    location = FloatVectorProperty(
            name="Location",
            subtype='TRANSLATION',
            )
    rotation = FloatVectorProperty(
            name="Rotation",
            subtype='EULER',
            )

    def execute(self, context):

        verts_loc, faces = add_box(self.width,
                                     self.height,
                                     self.depth,
                                     )

        mesh = bpy.data.meshes.new("Box")

        bm = bmesh.new()
        nb_verts = len(verts_loc) // 3
        for el in range(nb_verts):
            startI = range(el*3,el*3+3)
            bm.verts.new([verts_loc[i] for i in startI])
        nb_faces = len(faces)// 4
        for el in range(nb_faces):
            startI = range(el*4,el*4 +4)
            tmp = [bm.verts[faces[i]] for i in startI]
#            print(tmp)
            bm.faces.new(tmp)
#        mesh.vertices.add(len(verts_loc) // 3)
#        mesh.faces.add(len(faces) // 4)

#        mesh.vertices.foreach_set("co", verts_loc)
#        mesh.faces.foreach_set("vertices_raw", faces)

        bm.to_mesh(mesh)
        print(len(mesh.vertices),len(mesh.polygons))
        mesh.update()

        # add the mesh as an object into the scene with this utility module
        
        from bpy_extras import object_utils
        object_utils.object_data_add(context, mesh, operator=self)
        
        return {'FINISHED'}


def menu_func(self, context):
    self.layout.operator(AddBox.bl_idname, icon='MESH_CUBE')


def register():
    bpy.utils.register_class(AddBox)
    bpy.types.INFO_MT_mesh_add.append(menu_func)


def unregister():
    bpy.utils.unregister_class(AddBox)
    bpy.types.INFO_MT_mesh_add.remove(menu_func)

if __name__ == "__main__":
    register()

    # test call
    bpy.ops.mesh.primitive_box_add()

Yeah, they changed faces to tessfaces and apparently missed updating this script, might want to report it as a bug.

committed fix r45279.
thanks

jippie, I was very close, it is even ‘easier’ now :wink: