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 Vertices. Show all posts
Showing posts with label Vertices. 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 11, 2012

randomize geometry

This snippet randomizes the z value of all vertices by a given amount. (be in object mode, with a mesh object selected)
import bpy
import random

verts = bpy.context.active_object.data.vertices

for i in verts:
    i.co.z *= random.uniform(0.7, 1.3) 
        
In combination with the separation script from a previous post you can get some neat effects, maybe this will plant some seeds

July 09, 2012

Make Unique Geometry (no shared vertices)

This is the basic approach to ripping all geometry apart. doesn't play nice with UV and ignores world matrix (ie, is stuff scaled rotated translated or not..) :)

Pretty soon afterwards I wanted a way to preserve UV information per face. The following script was born. link : todo

July 27, 2011

ready to generate geometry - edge fillet blender 2.5


the code is a little lengthy to post in its entirety, but you can download it here
def get_correct_verts(arc_centre, arc_start, arc_end, NUM_VERTS, context):

obj_centre = context.object.location
axis = mathutils.geometry.normal(arc_centre, arc_end, arc_start)

point1 = arc_start - arc_centre
point2 = arc_end - arc_centre
main_angle = point1.angle(point2)
main_angle_degrees = math.degrees(main_angle)

div_angle = main_angle / (NUM_VERTS - 1)

if DEBUG == True:
print("arc_centre =", arc_centre)
print("arc_start =", arc_start)
print("arc_end =", arc_end)
print("NUM_VERTS =", NUM_VERTS)

print("NormalAxis1 =", axis)
print("Main Angle (Rad)", main_angle, " > degrees", main_angle_degrees)
print("Division Angle (Radians)", div_angle)
print("AXIS:", axis)

trig_arc_verts = []

for i in range(NUM_VERTS):
rotation_matrix = mathutils.Matrix.Rotation(i*-div_angle, 3, axis)
# trig_point = (arc_start - obj_centre - arc_centre) * rotation_matrix # old
trig_point = rotation_matrix * (arc_start - obj_centre - arc_centre) # new
trig_point += obj_centre + arc_centre
trig_arc_verts.append(trig_point)

return trig_arc_verts
Matrix.Rotate() was the most complicated part, but now i've broken through that barrier it doesn't seem such utter voodoo anymore. Warning to user: you must apply scale/location/rotation transforms to your profile/object first before running the code or the vertices will appear somewhere else. Not sure if there is a cute way around that.

small update




blender 2.5 GL fillet v0.2 from zeffii stanton on Vimeo.

July 18, 2011

printing verts, face indices, and their normals

import bpy

current_obj = bpy.context.active_object

print("="*40) # printing marker
for polygon in current_obj.data.polygons:
    verts_in_face = polygon.vertices[:]
    print("face index", polygon.index)
    print("normal", polygon.normal)
    for vert in verts_in_face:
        print("vert", vert, " vert co", current_obj.data.vertices[vert].co)

July 05, 2011

printing verts and faces of object to a text file

July 04, 2011

Making a cube using from_pydata

Some primitives, generally geometry is built using triangles (tris) or quadrangles (quads)

Quads

import bpy

verts = [(1.0, 1.0, -1.0),
         (1.0, -1.0, -1.0),
        (-1.0, -1.0, -1.0),
        (-1.0, 1.0, -1.0),
         (1.0, 1.0, 1.0),
         (1.0, -1.0, 1.0),
        (-1.0, -1.0, 1.0),
        (-1.0, 1.0, 1.0)]

faces = [(0, 1, 2, 3),
         (4, 7, 6, 5),
         (0, 4, 5, 1),
         (1, 5, 6, 2),
         (2, 6, 7, 3),
         (4, 0, 3, 7)]

mesh_data = bpy.data.meshes.new("cube_mesh_data")
mesh_data.from_pydata(verts, [], faces)
mesh_data.update() # (calc_edges=True) not needed here

cube_object = bpy.data.objects.new("Cube_Object", mesh_data)

scene = bpy.context.scene  
scene.objects.link(cube_object)  
cube_object.select = True  

Tris

verts = [
(-0.285437,-0.744976,-0.471429),
(-0.285437,-0.744976,-2.471429),
(1.714563,-0.744976,-2.471429),
(1.714563,-0.744976,-0.471429),
(-0.285437,1.255024,-0.471429),
(-0.285437,1.255024,-2.471429),
(1.714563,1.255024,-2.471429),
(1.714563,1.255024,-0.471429)]

faces =  [
(4,5,1),
(5,6,2),
(6,7,3),
(4,0,7),
(0,1,2),
(7,6,5),
(0,4,1),
(1,5,2),
(2,6,3),
(7,0,3),
(3,0,2),
(4,7,5)]

import bpy  
  
mesh_data = bpy.data.meshes.new("cube_mesh_data")  
mesh_data.from_pydata(verts, [], faces)  
mesh_data.update() # (calc_edges=True) not needed here  
  
cube_object = bpy.data.objects.new("Cube_Object", mesh_data)  
  
scene = bpy.context.scene    
scene.objects.link(cube_object)    
cube_object.select = True    

July 01, 2011

adding faces to an existing mesh

update: use this, it uses BMesh:

this is old, deprecated code. with BMesh you don't have to enter object mode to update. be in objectmode before running this, or modify the code to do that for you.
import bpy

# start with adding a plane at 0,0,0  ,set 3d cursor to 0,0,0
bpy.ops.mesh.primitive_plane_add()
mplane = bpy.context.active_object

# in object mode 
if mplane.mode == 'EDIT':
    bpy.ops.object.mode_set(mode='OBJECT')

# add 4 verts
mplane.data.vertices.add(4)
mplane.data.vertices[-4].co = (1, -1, 1)
mplane.data.vertices[-3].co = (-1, -1, 1)
mplane.data.vertices[-2].co = (-1, 1, 1)
mplane.data.vertices[-1].co = (1, 1, 1)

for m in mplane.data.vertices:
    print(m.co)

print("mplane.data.faces - before")
for l in mplane.data.faces:
    print("face===")
    for em in l.vertices:
        print(em)
        
# add one face    
mplane.data.faces.add(1)
mplane.data.faces[-1].vertices_raw = [7,4,5,6]
mplane.data.update(calc_edges=True)

print("mplane.data.faces - after")
for l in mplane.data.faces:
    print("face===")
    for em in l.vertices:
        print(em)

print("num faces=", len(mplane.data.vertices))
print(mplane.data.faces[-1])

May 14, 2011

Blender 2.5 Python Find Connected Vertices

given a list of edges, how do you find what vertices are connected to a given vertex?
Edges = [   
                [0,1],[1,2],[2,3],[3,0],
                [4,5],[5,6],[6,7],[7,4],
                [0,4],[1,5],[2,6],[3,7]
            ]
numbered_vertex = 1   # where 1 is an arbitrary choice of vertex
for i in Edges:
    if numbered_vertex in i:
        print(i)

or this way..a little bit more confusing at first.. but ultimately not too unwieldy.
Edges = [   
                [0,1],[1,2],[2,3],[3,0],
                [4,5],[5,6],[6,7],[7,4],
                [0,4],[1,5],[2,6],[3,7]
            ]
n = 1   # where 1 is an arbitrary choice of vertex
print([x for x in Edges if x[0] == n or x[1] == n])
# or 
print([x for x in Edges if n in x])