Skip to main content
Removed some lines of code to keep it clear
Source Link
DGRL
  • 162
  • 9
    bl_info = {
    "name": "Test object",
    "author": "DGRL",
    "version": (1, 0),
    "blender": (2, 80, 1),
    "location": "View3D > Add > Mesh > DGRL",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "wiki_url": "",
    "category": "Add Mesh",
}

import bpy
import bmesh
import mathutils
from mathutils import Vector
from bpy.types import Operator
from bpy_extras.object_utils import AddObjectHelper, object_data_add



def add_object(self, context):
    
    
    verts = [(1, 1, 0), (-1, 1, 0), (-1, 0.9, 0), (-0.25, 0.9, 0),] 

    
    mesh = bpy.data.meshes.new("mesh") 
    obj = bpy.data.objects.new("MyObject", mesh)

    scene = bpy.context.scene
    bpy.context.collection.objects.link(obj) 
    bpy.context.view_layer.objects.active = obj 
    bpy.context.active_object.select_set(state=True)

    mesh = bpy.context.object.data
    bm = bmesh.new()
    
    for v in verts:
        bm.verts.new(v) 
    
    dgrl = bm.faces.new(bm.verts)
    bottom = dgrl
    top = bmesh.ops.extrude_face_region(bm, geom=[bottom])
    bmesh.ops.translate(bm, vec=Vector((0,0,1)), verts=[v for v in top["geom"] if isinstance(v,bmesh.types.BMVert)])   
    bm.to_mesh(mesh)  
    bm.free()
    

    **object_data_add(context, mesh, operator=self)**  *<-- THIS LINE MAKES A SECOND BMESH BUT WITHOUT THERE IS NO ADJUST LAST OPERATION MENU!*
    


class OBJECT_OT_add_object(Operator, AddObjectHelper):
    """Create a new Mesh Object"""
    bl_idname = "mesh.add_object"
    bl_label = "Add Mesh Object"
    bl_options = {'REGISTER', 'UNDO'}


    def execute(self, context):

        add_object(self, context)

        return {'FINISHED'}

def add_object_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_object.bl_idname,
        text="Add Object",
        icon='PLUGIN')

def add_object_manual_map():
    url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
    url_manual_mapping = (
        ("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
    )
    return url_manual_prefix, url_manual_mapping


def register():
    bpy.utils.register_class(OBJECT_OT_add_object)
    bpy.utils.register_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_object)
    bpy.utils.unregister_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)


if __name__ == "__main__":
    register()
    bl_info = {
    "name": "Test object",
    "author": "DGRL",
    "version": (1, 0),
    "blender": (2, 80, 1),
    "location": "View3D > Add > Mesh > DGRL",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "wiki_url": "",
    "category": "Add Mesh",
}

import bpy
import bmesh
import mathutils
from mathutils import Vector
from bpy.types import Operator
from bpy_extras.object_utils import AddObjectHelper, object_data_add



def add_object(self, context):
    
    
    verts = [(1, 1, 0), (-1, 1, 0), (-1, 0.9, 0), (-0.25, 0.9, 0),] 

    
    mesh = bpy.data.meshes.new("mesh") 
    obj = bpy.data.objects.new("MyObject", mesh)

    scene = bpy.context.scene
    bpy.context.collection.objects.link(obj) 
    bpy.context.view_layer.objects.active = obj 
    bpy.context.active_object.select_set(state=True)

    mesh = bpy.context.object.data
    bm = bmesh.new()
    
    for v in verts:
        bm.verts.new(v) 
    
    dgrl = bm.faces.new(bm.verts)
    bottom = dgrl
    top = bmesh.ops.extrude_face_region(bm, geom=[bottom])
    bmesh.ops.translate(bm, vec=Vector((0,0,1)), verts=[v for v in top["geom"] if isinstance(v,bmesh.types.BMVert)])   
    bm.to_mesh(mesh)  
    bm.free()
    

    **object_data_add(context, mesh, operator=self)**  *<-- THIS LINE MAKES A SECOND BMESH BUT WITHOUT THERE IS NO ADJUST LAST OPERATION MENU!*
    


class OBJECT_OT_add_object(Operator, AddObjectHelper):
    """Create a new Mesh Object"""
    bl_idname = "mesh.add_object"
    bl_label = "Add Mesh Object"
    bl_options = {'REGISTER', 'UNDO'}


    def execute(self, context):

        add_object(self, context)

        return {'FINISHED'}

def add_object_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_object.bl_idname,
        text="Add Object",
        icon='PLUGIN')

def add_object_manual_map():
    url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
    url_manual_mapping = (
        ("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
    )
    return url_manual_prefix, url_manual_mapping


def register():
    bpy.utils.register_class(OBJECT_OT_add_object)
    bpy.utils.register_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_object)
    bpy.utils.unregister_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)


if __name__ == "__main__":
    register()
    bl_info = {
    "name": "Test object",
    "author": "DGRL",
    "version": (1, 0),
    "blender": (2, 80, 1),
    "location": "View3D > Add > Mesh > DGRL",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "wiki_url": "",
    "category": "Add Mesh",
}

import bpy
import bmesh
import mathutils
from mathutils import Vector
from bpy.types import Operator
from bpy_extras.object_utils import AddObjectHelper, object_data_add



def add_object(self, context):
    
    
    verts = [(1, 1, 0), (-1, 1, 0), (-1, 0.9, 0), (-0.25, 0.9, 0),] 

    
    mesh = bpy.data.meshes.new("mesh") 
    obj = bpy.data.objects.new("MyObject", mesh)

    scene = bpy.context.scene
    bpy.context.collection.objects.link(obj) 
    bpy.context.view_layer.objects.active = obj 
    bpy.context.active_object.select_set(state=True)

    mesh = bpy.context.object.data
    bm = bmesh.new()
    
    for v in verts:
        bm.verts.new(v) 
    
    bm.faces.new(bm.verts)
    bm.to_mesh(mesh)  
    bm.free()
    

    **object_data_add(context, mesh, operator=self)**  *<-- THIS LINE MAKES A SECOND BMESH BUT WITHOUT THERE IS NO ADJUST LAST OPERATION MENU!*
    


class OBJECT_OT_add_object(Operator, AddObjectHelper):
    """Create a new Mesh Object"""
    bl_idname = "mesh.add_object"
    bl_label = "Add Mesh Object"
    bl_options = {'REGISTER', 'UNDO'}


    def execute(self, context):

        add_object(self, context)

        return {'FINISHED'}

def add_object_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_object.bl_idname,
        text="Add Object",
        icon='PLUGIN')

def add_object_manual_map():
    url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
    url_manual_mapping = (
        ("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
    )
    return url_manual_prefix, url_manual_mapping


def register():
    bpy.utils.register_class(OBJECT_OT_add_object)
    bpy.utils.register_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_object)
    bpy.utils.unregister_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)


if __name__ == "__main__":
    register()
Post Undeleted by DGRL
Post Deleted by DGRL
Source Link
DGRL
  • 162
  • 9

Adjust last operarion menu does not work with newly made mesh and mesh is placed twice

I made a little script that makes a plane with thickness when i add the mesh to my model it places 2 meshes instead of 1

For 1 of the 2 meshes the ALO menu works but for the second one not Can someone help me and tell me what i did wrong?

This is the code i use

    bl_info = {
    "name": "Test object",
    "author": "DGRL",
    "version": (1, 0),
    "blender": (2, 80, 1),
    "location": "View3D > Add > Mesh > DGRL",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "wiki_url": "",
    "category": "Add Mesh",
}

import bpy
import bmesh
import mathutils
from mathutils import Vector
from bpy.types import Operator
from bpy_extras.object_utils import AddObjectHelper, object_data_add



def add_object(self, context):
    
    
    verts = [(1, 1, 0), (-1, 1, 0), (-1, 0.9, 0), (-0.25, 0.9, 0),] 

    
    mesh = bpy.data.meshes.new("mesh") 
    obj = bpy.data.objects.new("MyObject", mesh)

    scene = bpy.context.scene
    bpy.context.collection.objects.link(obj) 
    bpy.context.view_layer.objects.active = obj 
    bpy.context.active_object.select_set(state=True)

    mesh = bpy.context.object.data
    bm = bmesh.new()
    
    for v in verts:
        bm.verts.new(v) 
    
    dgrl = bm.faces.new(bm.verts)
    bottom = dgrl
    top = bmesh.ops.extrude_face_region(bm, geom=[bottom])
    bmesh.ops.translate(bm, vec=Vector((0,0,1)), verts=[v for v in top["geom"] if isinstance(v,bmesh.types.BMVert)])   
    bm.to_mesh(mesh)  
    bm.free()
    

    **object_data_add(context, mesh, operator=self)**  *<-- THIS LINE MAKES A SECOND BMESH BUT WITHOUT THERE IS NO ADJUST LAST OPERATION MENU!*
    


class OBJECT_OT_add_object(Operator, AddObjectHelper):
    """Create a new Mesh Object"""
    bl_idname = "mesh.add_object"
    bl_label = "Add Mesh Object"
    bl_options = {'REGISTER', 'UNDO'}


    def execute(self, context):

        add_object(self, context)

        return {'FINISHED'}

def add_object_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_object.bl_idname,
        text="Add Object",
        icon='PLUGIN')

def add_object_manual_map():
    url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
    url_manual_mapping = (
        ("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
    )
    return url_manual_prefix, url_manual_mapping


def register():
    bpy.utils.register_class(OBJECT_OT_add_object)
    bpy.utils.register_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_object)
    bpy.utils.unregister_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)


if __name__ == "__main__":
    register()