13
$\begingroup$

How can I add a Vertex from a script? I'd like to create a line from the extrusion of a vertex. This is my script now:

import bpy

location = bpy.context.scene.cursor_location    # Get Cursor location
bpy.ops.object.editmode_toggle()                # Enter Edit Mode

# Here goes the script I'm looking for!

bpy.ops.mesh.extrude_region_move()              # Extrude vertex
$\endgroup$
0

1 Answer 1

20
$\begingroup$

You can pass a list of vertex coordinates to Mesh.from_pydata(vertices, edges, faces) However, you also have to create a new mesh as well as a new object datablock and link them together properly:

Blender 2.8x

import bpy

def point_cloud(ob_name, coords, edges=[], faces=[]):
    """Create point cloud object based on given coordinates and name.

    Keyword arguments:
    ob_name -- new object name
    coords -- float triplets eg: [(-1.0, 1.0, 0.0), (-1.0, -1.0, 0.0)]
    """

    # Create new mesh and a new object
    me = bpy.data.meshes.new(ob_name + "Mesh")
    ob = bpy.data.objects.new(ob_name, me)

    # Make a mesh from a list of vertices/edges/faces
    me.from_pydata(coords, edges, faces)

    # Display name and update the mesh
    ob.show_name = True
    me.update()
    return ob

# Create the object
pc = point_cloud("point-cloud", [(0.0, 0.0, 0.0)])

# Link object to the active collection
bpy.context.collection.objects.link(pc)

# Alternatively Link object to scene collection
#bpy.context.scene.collection.objects.link(pc)

Blender 2.7x

import bpy

def point_cloud(ob_name, coords, edges=[], faces=[]):
    """Create point cloud object based on given coordinates and name.

    Keyword arguments:
    ob_name -- new object name
    coords -- float triplets eg: [(-1.0, 1.0, 0.0), (-1.0, -1.0, 0.0)]
    """

    # Create new mesh and a new object
    me = bpy.data.meshes.new(ob_name + "Mesh")
    ob = bpy.data.objects.new(ob_name, me)

    # Make a mesh from a list of vertices/edges/faces
    me.from_pydata(coords, edges, faces)

    # Display name and update the mesh
    ob.show_name = True
    me.update()
    return ob

# Create the object
pc = point_cloud("point-cloud", [(0.0, 0.0, 0.0)])

# Link object to the scene
bpy.context.scene.objects.link(pc)
$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.