import bpy
import math
from mathutils import Vector
def debug_dump(k, particle):
hk = particle.hair_keys[k]
return "%s\t%s\t%s\t%s\n%s" % (hk.co_local, hk.co,
particle.prev_location, particle.location,
(hk.co - particle.prev_location))
def comb1(obj):
ps = obj.particle_systems[0]
dx =10
dy = 10
for i in range(dx):
for j in range(dy):
idx = i*10+j
particle = ps.particles[idx]
dz = len(particle.hair_keys)
loc = ( (i/dx-0.5)* 7, (j/dy-0.5)*7, 0)
particle.hair_keys[0].co_local = (0,0,0)
loc = particle.hair_keys[0].co
particle.location = loc
particle.prev_location = loc
for k in range(dz):
print(particle.hair_keys[k].co_local)
x = k*0.1*i
r = math.pi*dy/(1+j+1)
theta = k /r
y = (1-math.cos(theta))*r
z = math.sin(theta)*r
particle.hair_keys[k].co_local = (x,y,z)
print(debug_dump(k, particle))
# particle.hair_keys[k].co = particle.location + Vector( [x,y,z] )
# print(debug_dump(k, particle))
#particle.hair_keys[k].co_local = (x,y,z)
#print(debug_dump(k, particle))
def bary(verts, w):
rval = verts[0]*w[0]
for i in range(1,len(verts)):
rval = rval + verts[i]*w[i]
return rval
def bary2(obj, particle):
mesh = obj.data
fuv = particle.fuv
return bary([mesh.vertices[i].co for i in mesh.polygons[0].vertices], fuv)
def comb2(obj):
ps = obj.particle_systems[0]
for p in ps.particles:
p.fuv = [0.1, 0.9,0,0]
p.location = bary2(obj, p)
dx =5
dy = 5
for i in range(dx):
for j in range(dy):
idx = i*dx+j
particle = ps.particles[idx]
dz = len(particle.hair_keys)
# this next bit does not work unless your blender has hacked DNA. The fuv property is not normally available
uv = [ i / (dx-1) , j/(dy-1)]
particle.fuv = (uv[0]*uv[1], (1-uv[0])*uv[1], (1-uv[0])*(1-uv[1]), uv[0]*(1-uv[1]))
loc = bary2(obj, particle)
print(particle.fuv, loc)
particle.location = loc
particle.prev_location = loc
slant = 0.5*(i-2)
for k in range(dz):
print(particle.hair_keys[k].co_local)
x = k*slant
curvature = 3*(j-2)/dy
if (curvature!=0):
r = math.pi/(curvature)
theta = k /r
y = (1-math.cos(theta))*r
z = math.sin(theta)*r
else:
y = 0
z=k
particle.hair_keys[k].co_local = (x,y,z)
print(debug_dump(k, particle))
# particle.hair_keys[k].co = particle.location + Vector( [x,y,z] )
# print(debug_dump(k, particle))
#particle.hair_keys[k].co_local = (x,y,z)
#print(debug_dump(k, particle))
def investigate1(obj):
ps = obj.particle_systems[0]
particle = ps.particles[-1]
hk = particle.hair_keys[1]
debug_dump(1, particle)
hk.co_local = (0,0,0)
print(debug_dump(1, particle))
hk.co_local = (1,0,0)
print(debug_dump(1, particle))
hk.co_local = (0,1,0)
print(debug_dump(1, particle))
hk.co_local = (0,0,1)
print(debug_dump(1, particle))
obj = bpy.data.objects['Plane']
print(obj)
comb2(obj)
#investigate1(obj)
|
Blender python API quick-start
Syntax highlighting by Pygments.