Add -> Mesh -> Single Vertex

Hello, I am looking for simplest extension .py for add one vertex mesh. Thanks

possible but
just add a plane and remove 3 verts!

happy bl

1 Like

It either creates a vertex object at cursor location or adds a vertex to an editmesh if appropriate:

import bpy
import bmesh

class MESH_OT_primitive_vert(bpy.types.Operator):
    """Create object with single vertex or add vertex to editmesh"""
    bl_idname = "mesh.primitive_vert"
    bl_label = "Create Vertex"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        
        scene = context.scene
        ob = context.object
        
        if (ob is not None and
            ob.type == 'MESH' and
            ob.data.is_editmode):
            
            me = ob.data
            bm = bmesh.from_edit_mesh(me)
            co = scene.cursor_location - ob.matrix_world.translation
            v = bm.verts.new(co)
            bmesh.update_edit_mesh(me, False, True)
        
        else:    
        
            me = bpy.data.meshes.new("Vert")
            me.vertices.add(1)
            
            for ob in scene.objects:
                ob.select = False
                
            ob = bpy.data.objects.new("Vert", me)
            ob.location = scene.cursor_location
            ob.select = True
            
            scene.objects.link(ob)
            scene.objects.active = ob
            scene.update()
        
        return {'FINISHED'}

def draw_func(self, context):
    layout = self.layout
    layout.operator(MESH_OT_primitive_vert.bl_idname, text="Vertex")

def register():
    bpy.utils.register_class(MESH_OT_primitive_vert)
    bpy.types.INFO_MT_mesh_add.append(draw_func)


def unregister():
    bpy.utils.unregister_class(MESH_OT_primitive_vert)
    bpy.types.INFO_MT_mesh_add.remove(draw_func)


if __name__ == "__main__":
    register()


Thanks for reply. RickyBlender, I am adding plane, next merge all selected in center, and done. But if it possible to add to menu Add One Vertex why not do this. CoDEmanX, brilliant script! thanks! There is only one bug in line 17: need to change from ob.data.is_editmode): to ob.mode == ‘EDIT’): Thanks again.

I assign Single Vertex to Shift+Ctrl+A in Object and Edit mode, your script works great. When I am in Object Mode and I add single vertex Blender could be turn me to Edit Mode, and could be select new created vertex - it would be perfect :slight_smile: I add PLUGIN icon, is there possible to add own icon to my new Blender menu Add -> Mesh -> Single Vertex?

That’s not a bug, you need to upgrade your Blender, it works in 2.70. is_editmode is required to make it work in all situations, ob.mode conditions can fail if you work with linked duplicates.

You can only use existing icons

import bpy
import bmesh

class MESH_OT_primitive_vert(bpy.types.Operator):
    """Create object with single vertex or add vertex to editmesh"""
    bl_idname = "mesh.primitive_vert"
    bl_label = "Create Vertex"
    bl_options = {'REGISTER', 'UNDO'}
    
    @classmethod
    def poll(cls, context):
        return (context.object is not None and
                context.object.type == 'MESH')

    def execute(self, context):
        
        scene = context.scene
        ob = context.object

        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.select_all(action='DESELECT')
        
        me = ob.data
        bm = bmesh.from_edit_mesh(me)
        co = scene.cursor_location - ob.matrix_world.translation
        v = bm.verts.new(co)
        v.select_set(True)
        bm.select_history.add(v)
        bmesh.update_edit_mesh(me, False, True)
    
        return {'FINISHED'}

def draw_func(self, context):
    layout = self.layout
    layout.operator(MESH_OT_primitive_vert.bl_idname, text="Vertex", icon='LAYER_ACTIVE')

def register():
    bpy.utils.register_class(MESH_OT_primitive_vert)
    bpy.types.INFO_MT_mesh_add.append(draw_func)


def unregister():
    bpy.utils.unregister_class(MESH_OT_primitive_vert)
    bpy.types.INFO_MT_mesh_add.remove(draw_func)


if __name__ == "__main__":
    register()

It looks is_editmode = (ob.mode == ‘EDIT’) was not declared in this class for allow to use is_editmode condition, then Blender gives Error “Traceback (most recent call last): File …add_mesh_vertex.py, line 17, in execute ob.data.is_editmode): AttributeError: Mesh object has no attribute is_editmode location: :-1)”. For my Debian latest working compiled Blender with Freestyle from graphicall.org was 2.66.5. I tested higher versions but “error while loading shared libraries: libpython3.3m.so.1.0: cannot open shared object file: No such file or directory”. I must try to compile from source, but for Debian 7.5 latest python is 3.2

In last script in Object mode it turns to Edit Mode (OK) create and select new vertex (OK) in last selected Mesh instead creating new Mesh first. But both scripts are great and fully sufficient for me :slight_smile: Thank you.

how come i don’t see new primi in tool panel menu ?
got latest build on win !

thanks

Step by step how to add new option Add -> Mesh -> Single Vertex to Primitive panel. First I save CoDEmanX script as file /scripts/startup/bl_operators/add_mesh_vertex.py then edit file /scripts/startup/bl_operators/init.py I add

"add_mesh_vertex",

after line 26. Next In file /scripts/startup/bl_ui/space_view3d_toolbar.py I add line:

layout.operator("mesh.primitive_vert", text="Single Vertex", icon='PLUGIN')

and

layout.separator()

inside class VIEW3D_PT_tools_add_object (after line 135). After restart blender you should see new primitive object.

My last script adds a new entry to the mesh menu, there’s no need to edit Blender’s layout scripts.

Hello, I tested it again on 2.70a release, when I run script from build id Text Editor both scripts adds new entry, but when I put script to file …/scripts/startup/bl_operators/add_mesh_vertex.py new function Add Single Vertex want not display in menus. Maybe I do something wrong. Anyway I wish to have adding single verticle function on top of Mesh menus. I do simple changes in first script. Now when I am in Object Mode and want to add new vertex function creates new object Vert with one vertex, as you wrote and additionally turn me to Edit Mode and select created vertex. When I am in Edit mode and want to add vertex it deselect selected first, and adds new vertex and additionally select created. In both cases I am ready to start Extrude for create / modify Mesh. In attachement there is Single Vertex function add_mesh_vertex.py.zip for unzip and put to /scripts/startup/bl_operators/. In file /scripts/startup/bl_operators/init.py in _modules you need to add “add_mesh_vertex”, line. For add function on top of menus in file /scripts/startup/bl_ui/space_view3d.py in INFO_MT_mesh_add class need to add layout.operator(“mesh.primitive_vert”, icon=‘LAYER_ACTIVE’, text=“Single Vertex”) and in file /scripts/startup/bl_ui/space_view3d_toolbar.py in classes VIEW3D_PT_tools_add_mesh_edit and VIEW3D_PT_tools_add_mesh need to add col.operator(“mesh.primitive_vert”, text=“Single Vertex”, icon=“LAYER_ACTIVE”) and restart blender. I have question how can I turn HTML code ON?

Attachments


In updated script, now there is automatically turning to Edit Mode, and in Edit Mode it automatically turns to Vertex Select… in my opinion it is comfortable. I hope that Add Single Vertes will be useful for from every that want to convert 2D drawings to 3D models, especially with assigned SHIFT+CTRL+A shortcut key.

Attachments

add_mesh_vertex.py.zip (697 Bytes)


would be interesting to also add a simple line and possible to control if vertical or horizontal!

thanks for sharing

I create simple line by Add -> Mesh -> Single Vertex (or SHIFT+CTRL+A if assigned), next press E, next move mouse vertical or horizontal holding CTRL, and for end LMB - and line is ready. There is also possible to adding verticles by holding CTRL and press LMB, but in this way you can not add vertex too close of manipulator. With E there is not important where the manipulator is, you can draw precisely. Thanks for CoDEmanX, I only add simple changes. There are tutorials for 2.70: http://youtu.be/Ca_4AyTkAR8 and easier for 2.71 http://youtu.be/8LRfiBN8H5U. For my account “HTML code is Off” - how can I turn On on forum?

Or just run this in the text editor

import bpy
bpy.ops.mesh.primitive_vert()