18

I would like to create a mesh, then interactively add vertices (afterwards, possibly through a loop) to it through python.

Is that possible?

I can't find enough info from other answers to really get this going - either I find out how to create a mesh, or how to add a vertex, but can't seem to put them together in to a script.

Jono
  • 183
  • 1
  • 1
  • 5
  • Nowadays you'll want to use bmesh...I don't have time to write a full answer, but I will later if no one else has. – JakeD Aug 29 '16 at 12:32
  • 3
    related: http://blender.stackexchange.com/questions/414/how-to-use-bmesh-to-add-verts-faces-and-edges-to-existing-geometry?rq=1 – zeffii Aug 29 '16 at 12:42
  • 1
    related: http://blender.stackexchange.com/questions/2407/how-to-create-a-mesh-programmatically-without-bmesh/2416#2416 – zeffii Aug 29 '16 at 12:44
  • 2
    if those two links aren't helpful, please your edit question to include more details about what exactly you don't understand. – zeffii Aug 29 '16 at 12:45

2 Answers2

29

This is how to create a new object and add the vertices in the verts list:

import bpy
import bmesh

verts = [(1, 1, 1), (0, 0, 0)]  # 2 verts made with XYZ coords
mesh = bpy.data.meshes.new("mesh")  # add a new mesh
obj = bpy.data.objects.new("MyObject", mesh)  # add a new object using the mesh

scene = bpy.context.scene
scene.objects.link(obj)  # put the object into the scene (link)
scene.objects.active = obj  # set as the active object in the scene
obj.select = True  # select object

mesh = bpy.context.object.data
bm = bmesh.new()

for v in verts:
    bm.verts.new(v)  # add a new vert

# make the bmesh the object's mesh
bm.to_mesh(mesh)  
bm.free()  # always do this when finished

This is how to alter an existing mesh:

import bpy
import bmesh

verts = [(1, 1, 1), (0, 0, 0)]  # 2 verts made with XYZ coords
mesh = bpy.context.object.data
bm = bmesh.new()

# convert the current mesh to a bmesh (must be in edit mode)
bpy.ops.object.mode_set(mode='EDIT')
bm.from_mesh(mesh)
bpy.ops.object.mode_set(mode='OBJECT')  # return to object mode

for v in verts:
    bm.verts.new(v)  # add a new vert

# make the bmesh the object's mesh
bm.to_mesh(mesh)  
bm.free()  # always do this when finished
JakeD
  • 8,467
  • 2
  • 30
  • 73
18

In Blender 2.80 onward:

import bpy

mesh = bpy.data.meshes.new("myBeautifulMesh") # add the new mesh obj = bpy.data.objects.new(mesh.name, mesh) col = bpy.data.collections["Collection"] col.objects.link(obj) bpy.context.view_layer.objects.active = obj

verts = [( 1.0, 1.0, 0.0), ( 1.0, -1.0, 0.0), (-1.0, -1.0, 0.0), (-1.0, 1.0, 0.0), ] # 4 verts made with XYZ coords edges = [] faces = [[0, 1, 2, 3]]

mesh.from_pydata(verts, edges, faces)

We can make a function, too.

import bpy

def add_mesh(name, verts, faces, edges=None, col_name="Collection"):
if edges is None: edges = [] mesh = bpy.data.meshes.new(name) obj = bpy.data.objects.new(mesh.name, mesh) col = bpy.data.collections[col_name] col.objects.link(obj) bpy.context.view_layer.objects.active = obj mesh.from_pydata(verts, edges, faces)

verts = [( 1.0, 1.0, 0.0), ( 1.0, -1.0, 0.0), (-1.0, -1.0, 0.0), (-1.0, 1.0, 0.0), ] faces = [[0, 1, 2, 3]] add_mesh("myBeautifulMesh_1", verts, faces)

verts = [( 3.0, 1.0, 0.0), ( 3.0, -1.0, 0.0), ( 2.0, -1.0, 0.0), ( 2.0, 1.0, 0.0), ] add_mesh("myBeautifulMesh_2", verts, faces)

RPaladin
  • 1,235
  • 1
  • 5
  • 17
ludiccc
  • 281
  • 2
  • 4