2

How to create polyline and then poly loop with python. Bmesh fastest way.

Here is an example:

enter image description here

Thanks.

p2or
  • 15,860
  • 10
  • 83
  • 143
mifth
  • 2,341
  • 28
  • 36

1 Answers1

4

Create a polyline. (btw, imposing pure bmesh requirements on this is probably arbitrary, and using from_pydata is just as good)

import bpy
import bmesh
from mathutils import Vector

scene = bpy.context.scene 
meshes = bpy.data.meshes
objects = bpy.data.objects

# generate polyline 

bm = bmesh.new()
verts = [bm.verts.new((0, 0, z)) for z in range(5)]

for i in range(len(verts)-1):
    bm.edges.new([verts[i], verts[i+1]])

me = meshes.new('placeholder_mesh')
mesh_obj = objects.new('polyline', me)

scene.objects.link(mesh_obj)

bm.to_mesh(me)
bm.free()

Run this on a polyline:

import bpy
import bmesh
from mathutils import Vector

# Get the active mesh
obj = bpy.context.edit_object
me = obj.data

# Get a BMesh representation
bm = bmesh.from_edit_mesh(me)

bmesh.ops.extrude_edge_only(bm, edges=bm.edges[:])
bmesh.ops.translate(bm, vec=Vector((0,1.2,0)), verts=[v for v in bm.verts if v.select])

bmesh.update_edit_mesh(me, True)

See TextEditor -> Templates -> Python -> Simple Bmesh (edit mode) for the templates.


It might just be simpler to construct the polygon loop from scratch using from_pydata. It really depends on the final use-case

zeffii
  • 39,634
  • 9
  • 103
  • 186
  • How to make polygon loop from scratch using from_pydata? – mifth May 30 '15 at 19:56
  • see first sentence in my answer. ( http://blender.stackexchange.com/questions/2407/how-to-create-a-mesh-programmatically-without-bmesh/2408#2408 ) all you need are the vertex coordinates and the face index loops, building from scratch you are responsible for that. – zeffii May 30 '15 at 20:10
  • i think this is not what i need. I use editmode only. Could you check this issue too? http://blender.stackexchange.com/questions/31733/python-fix-normal-of-a-new-polygon – mifth May 30 '15 at 21:30
  • yeah, from_pydata isn't designed for interactive / edit mode mesh updates. I already looked at your other post, interesting problem. – zeffii May 30 '15 at 21:34