Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

Showing posts with label add. Show all posts
Showing posts with label add. Show all posts

May 29, 2013

Adding geometry to existing geometry using BMesh

I resist asking questions until that feeling starts to kick where there has to be a fast but illusive answer. I want to see blender.stackexchange progress out of beta, it has been pretty useful already!
import bpy, bmesh

obj = bpy.context.object
bm = bmesh.from_edit_mesh(obj.data)

bm.verts.new((2.0, 2.0, 2.0))
bm.verts.new((-2.0, 2.0, 2.0))
bm.verts.new((-2.0, -2.0, 2.0))

bm.faces.new((bm.verts[i] for i in range(-3,0)))

bm.verts.new((2.0, 2.0, -2.0))
bm.verts.new((-2.0, 2.0, -2.0))
bm.verts.new((-2.0, -2.0, -2.0))
bm.verts.new((2.0, -2.0, -2.0))

bm.faces.new((bm.verts[i] for i in range(-4,0)))

# update, while in edit mode
# thanks to ideasman42 for making this clear
# http://blender.stackexchange.com/questions/414/
bmesh.update_edit_mesh(obj.data)
Adding edges is also similar
# let's add two verts and connect them as an edge
bm.verts.new((0.0, 1.0, 1.0))
bm.verts.new((0.0, 0.0, 1.0))
bm.edges.new((bm.verts[-2], bm.verts[-1]))

# remember to do a    bmesh.update_edit_mesh(obj.data)

July 23, 2011

Place empty while in Edit Mode

Sometimes we need to place a marker (Empty) at a given point on our mesh. I use this often to define a pivot point. But manually hopping in and out of edit mode to add an Empty soon becomes lame. This is my solution. Run this in edit mode, select as many verts as you wish.

May 14, 2011

Blender 2.5 Python Vector Arithmetic

Blender has a sweet library of maths functions / classes, one of them is Vector. (from mathutils import Vector)
because coordinates (co) are Vector datatypes

# default cube in edit mode
>>> bpy.context.object.data.vertices[0].co
Vector((1.0, 0.9999999403953552, -1.0))
you can simply do

# let's pretend vec1 and vec2 are already defined.
>>> vec1 = Vector((2.0,2.0,2.0))
>>> vec2 = Vector((3.0,3.0,3.0))

>>> vec1+vec2
Vector((5.0, 5.0, 5.0))
that beats having to do

>>> Vector((vec1[0] + vec2[0], vec1[1] + vec2[1], vec1[2] + vec2[2]))
Vector((5.0, 5.0, 5.0))
...etc