4
$\begingroup$

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?

$\endgroup$
4
  • $\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$ – CodeManX Jul 4 '14 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$ – sambler Jul 5 '14 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$ – user31150 Oct 5 '16 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 Bob Oct 5 '16 at 14:54
5
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ i think it's possible to update UI. obj.data.update() $\endgroup$ – mifth Jan 14 '15 at 14:50
1
$\begingroup$

Possibly, the root can be moved too using ParticleHairKey.co.

particle.hair_keys[k].co = (x,y,z)
$\endgroup$
1
  • $\begingroup$ Is this stable if the mesh is adjusted (if vertices are moved in space)? Are the child hairs computed properly? $\endgroup$ – Mutant Bob Jan 14 '15 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.