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...

June 27, 2011

randomizing geometry

this code randomizes all internal vertices on a surface of faces, it takes into account the shortest edge connected to each vertex. What it doesn't do is store the initial vertex position, so any consecutive randomize will random atop of the newly randomized coordinate

# must be in object mode
# with no verts selected on the object.

import bpy
import random
from mathutils import Vector

index_list = []
tracker_list = []

for i in bpy.context.active_object.data.edges:
index_list.append(i.vertices[0])
index_list.append(i.vertices[1])
tracker_list.append([i.vertices[0], i.vertices[1]])

# rudimentary numeric sorting, came in handy for debugging, but
# not essential for operational code.
sorted_list = sorted(index_list)

# use a set to eliminate multiples fast
sorted_set = set(sorted_list)

# track all verts with 4 or more other verts attached, store as list_internals
list_internals = []
for num in sorted_set:
if sorted_list.count(num) >= 4: # quad mesh only.
list_internals.append(num)

'''
# this selects all internal geometry.
for i in list_internals:
bpy.context.active_object.data.vertices[i].select = True
'''
print("="*20)

# store the length of the shortest connected edge, for each internal vertex
min_length_list = []
meshdata = bpy.context.active_object.data.vertices
for vert in list_internals:

# find each edge that uses this vert
# print(vert, "used in")
temp_storage = []
for pair in tracker_list:
if vert in pair:
vec1 = meshdata[pair[0]].co
vec2 = meshdata[pair[1]].co
dist = (vec1-vec2).length
temp_storage.append(dist)
# print(pair, "length is", dist)

shortest_attached_edge = min(temp_storage)
# print("shorted length is: ", shortest_attached_edge)

# store [vertnum, shortest_attached_edge]
min_length_list.append([vert, shortest_attached_edge])

for i in min_length_list:
print(i)

# randomize to .4 or shortest edge.
def randomize_vector(vector_in, amount):
amount = amount * 0.2
# random.uniform(0.7, 1.3)
vecx = random.uniform(vector_in.x-amount, vector_in.x+amount)
vecy = random.uniform(vector_in.y-amount, vector_in.y+amount)
vecz = random.uniform(vector_in.z-amount, vector_in.z+amount)

return Vector((vecx, vecy, vecz))

for i in min_length_list:
print(meshdata[i[0]].co, "randomize by ", 0.4*i[1] )
newvec = randomize_vector(meshdata[i[0]].co, 0.4*i[1])
print(newvec)
meshdata[i[0]].co = newvec


becomes