I am creating particle systems using python. I'd like to be able to comb the hair using python (iterate through the control hairs and set their control points). What are the APIs for doing this?
-
$\begingroup$ I'm pretty sure you can't, the operator depends on 3D View and mouse input and I haven't seen a low-level way yet (in contrast to grease pencil strokes). $\endgroup$– CodeManXJul 4, 2014 at 19:16
-
$\begingroup$ While it probably won't give the control you want you can setup curves in python and give them a curve guide force field With the max distance you can have several curves controlling 'clumps' of hair. $\endgroup$– samblerJul 5, 2014 at 8:14
-
$\begingroup$ in the latest Blender on the footer, where you can find the different views, you now can find the "particle Edit" view, They have added an interactive "comb" for blender... Have fun! $\endgroup$– user31150Oct 5, 2016 at 6:50
-
$\begingroup$ The interactive combing of hair is old news. That is why I specified "using python". Also because I want to comb the hair according to mathematical formulas, not frobbing about imprecisely with the mouse. $\endgroup$– Mutant BobOct 5, 2016 at 14:54
2 Answers
The python API for combing hair is not very complete. You can adjust the control points for individual hairs using code like this:
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)
for k in range(dz):
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)
That is an intermediate result from my research. It successfully sets the control points for the parent hairs.
- It does not update the UI. You have to toggle between Particle Edit and Object modes for the UI to refresh.
- You can not really adjust the position of the root using python. While there is a "location" property on the python particle object, it is just a computed value. The real value is a barycentric coordinate "fuv" for the face indexed by "num", and both are only accessible from C (or a version of blender with hacked dna). Using "displaced" values in co_local works for the parent hairs, but child hairs will not be interpolated correctly.
As of blender 2.71 the python API for editing particle systems has some shortcomings. A chat with some folks in #blendercoders came up with a reference to some past work on editing the point cache, although the previous work was with the cloth sim and significant work would have to be done to adapt it for the particle cache (and it requires that you enable Hair Dynamics).
-
$\begingroup$ i think it's possible to update UI. obj.data.update() $\endgroup$– mifthJan 14, 2015 at 14:50
Possibly, the root can be moved too using ParticleHairKey.co
.
particle.hair_keys[k].co = (x,y,z)
-
$\begingroup$ Is this stable if the mesh is adjusted (if vertices are moved in space)? Are the child hairs computed properly? $\endgroup$ Jan 14, 2015 at 17:11