Kill particles inside Object

Discussion about scripting with Python
wigal123
Posts: 6
Joined: Tue Mar 25, 2008 5:33 pm

Kill particles inside Object

Postby wigal123 » Tue Mar 25, 2008 5:35 pm

Hi,

I am totally new when it comes to scripting.
I am trying to get the script from the user manual to work, but with no success.
Could somebody post it here, and tell me exactly where to put it?!

any help is very appreciated.

thanks


User avatar
tsn
Posts: 890
Joined: Fri Oct 27, 2006 10:36 am

Kill particles inside Object

Postby tsn » Tue Mar 25, 2008 5:46 pm

Could you please copy and paste the script to this thread and tell me, where you've encountered problems? Alternatively, just point me to the manual's page, where you've found the script. Is it a general problem or do you receive syntax errors?

Regards,
Thomas

wigal123
Posts: 6
Joined: Tue Mar 25, 2008 5:33 pm

Kill particles inside Object

Postby wigal123 » Tue Mar 25, 2008 6:04 pm

Hi,

trhats what i am using

def removeParticles( emitter ):
pass

dotprod = facenormalvec * norm2particlevec

em = scene.getEmitter("splash")
particle = em.getFirstParticle()
while particle:
nextparticle = particle.getNextParticle()
# for each particle, loop through the killer object?s vertices and determine if the dot product is positive or negative
# if any one dot product is positive, the particle is outside and we do nothing to it.
# otherwise, we remove the particle
particle = nextparticle

em = scene.getEmitter("splash")
obj = scene.getObject("Boat")
vertlist = obj.getVertices()
particle = em.getFirstParticle()
while particle:
nextparticle = particle.getNextParticle()
# for each particle, loop through the killer object?s vertices and determine if the dot product is positive or negative
# set the flag value: a value of 1 will mean the particle is inside.
flag = 1
for v in vertlist:
# calculate the vector from the vertex to the particle
v2pvec = particle.getPosition() - v.getPosition()
# get the vertex normal:
vnorm = v.getNormal()
# get the dot product and check if positive or negative
dotprod = vnorm * v2pvec
if dotprod > 0.0: flag = 0
# if any one dot product is positive, the particle is outside and we do nothing to it. otherwise, we remove the particle if flag:
em.removeParticle(particle.getId())
particle = nextparticle

I am getting a "Script error: 'facenormalvec' is not defined at number 35

In the user guide the tutorial starts at page 168 and is called custom object killer

cheers

User avatar
tsn
Posts: 890
Joined: Fri Oct 27, 2006 10:36 am

Kill particles inside Object

Postby tsn » Wed Mar 26, 2008 9:03 am

You're mixing up multiple parts of the script. No matter what you're trying to do, the given script will never run. Just to mention the first line, you've received an error:

You are trying to receive a result from undefined values. In any programming language, you have to define your variables, before you can calculate with them.

The pass statement tells Python to do nothing. Additionally, you're using a scripted daemon, but this script should be used with an event:

Layout : Events Script (F11)

Search for def onSimulationStep() and replace the pass statement with this code:

Code: Select all

def onSimulationStep():

   em       = scene.getEmitter("myemitter")
   obj      = scene.getObject("killerobject")
   vertlist = obj.getVertices()
   particle = em.getFirstParticle()

   while particle:
      nextparticle = particle.getNextParticle()
      flag = 1

      for v in vertlist:
         v2pvec  = particle.getPosition() - v.getPosition()
         vnorm   = v.getNormal()
         dotprod = vnorm * v2pvec

         if dotprod > 0.0:
            flag = 0

         if flag:
            em.removeParticle(particle.getId())

      particle = nextparticle

Beware of the leading spaces and indents. They're essential or you'll get syntax errors. Use the tab key to create the leading spaces. Replace myemitter and killerobject with the names of the objects in your scene. Finally, check Active from the scripting window and simulate. Don't forget to deactivate/delete the scripted daemon, before simulating.

I didn't check the script extensively and was only looking for syntax errors. I know it's running, but I don't know, if you'll get the expected results. Normally, I use more vector transformations when I have to manipulate particles inside an object.

If you still can't get along with this script, just PM me your mail address and I'll send you the scene file. Also I strongly recommend to learn the basic principles of scripting, before using Python.

wigal123
Posts: 6
Joined: Tue Mar 25, 2008 5:33 pm

Kill particles inside Object

Postby wigal123 » Wed Mar 26, 2008 9:22 am

thanks you so much tsn,

I do realise that i have a lot to learn about python but i was just not giventhe time to learn it on that one.
The script seems ro work fine. thank you!!!

User avatar
tsn
Posts: 890
Joined: Fri Oct 27, 2006 10:36 am

Kill particles inside Object

Postby tsn » Wed Mar 26, 2008 9:41 am

Glad to hear, it's working. I understand your situation, of course. Sometimes a certain script is needed, because you have to solve a particular task and don't have the time to learn a new scripting language.

:)

wigal123
Posts: 6
Joined: Tue Mar 25, 2008 5:33 pm

Kill particles inside Object

Postby wigal123 » Wed Mar 26, 2008 10:36 am

cheered too early, it snot behaving right :( but I have to go with it for now.. i just clip the particles on the inside when meshing...

maybe i come back to it later, thanks again though

User avatar
bitSeq
Posts: 1044
Joined: Thu Jul 10, 2008 9:48 am
Location: UK - Stuart Armiger
Contact:

Kill particles inside Object

Postby bitSeq » Thu May 27, 2010 12:41 pm

Hi, Sorry I know this is an old post but does anyone know how to make this script work so that particles outside of the object are killed?

Cheers

chapsandmutton
Posts: 9
Joined: Mon Jul 12, 2010 6:06 pm

Kill particles inside Object

Postby chapsandmutton » Tue Jul 13, 2010 10:01 pm

Heads up to future users:

This script works, but it just needs a little adjusting. Basically, the script runs through and sees is the dot product of the vertex normal and the vector distance is positive or negative. If it's negative we know the particle is inside the geometry.

This script works in reverse, though - it starts by assuming everything is inside the geometry and works on finding what's outside of it. Since everything is flagged to be killed at first, running the if loop that does the killing before having checked all vertices pre-emptively kills most of the particles.

Just bump that final if statement back one tab and you're good to go. Bumped into this when I was looking for a similar solution - thanks to tmdag for pointing me in the right direction.

User avatar
tmdag
Posts: 1023
Joined: Thu Jun 28, 2007 2:22 pm
Location: New Zealand
Contact:

Kill particles inside Object

Postby tmdag » Wed Jul 14, 2010 1:33 am

http://tmdag.com/blog/?p=615


different approach. I'll post it later
"Do not feed the trolls"
Albert 'tmdag' Szostkiewicz
FX Technical Director
Weta Digital


Return to “Python-Scripting”

Who is online

Users browsing this forum: No registered users and 1 guest