hole filling splash script

Discussion about scripting with Python
Karl Richter
Posts: 19
Joined: Fri Feb 01, 2008 5:34 pm

hole filling splash script

Postby Karl Richter » Tue Mar 06, 2012 7:55 pm

I posted this in the WIP section, but realized this is probably a more appropriate place.

example videos:
http://vimeo.com/37829671
http://vimeo.com/37829671

code:

Code: Select all

#
# Hole Filling script by Karl Richter
#
# This script has only been tested with RF5. 
#
# Basically, this script keeps track of the distances
# between particles from frame to frame and adds new
# particles if they get too far apart.
#


# Make an empty dictionary
particleDictionary = {}

# This is the emitter the script will affect
fluidEmitter = "Circle01"

# This procedure fills the dictionary full of particles to
# keep track of for the current frame.
def getNeighbor():
   particleDictionary.clear()
   emitter = scene.getEmitter(fluidEmitter)
   emitter.createVoxelization(True)
   particle = emitter.getFirstParticle()
   while particle:
      # This part sets the search radius and sets a speed
      # threshold so we don't include particles that arn't moving much.
      # For higher resolution fluids, you will want to make this a
      # smaller number. This is an expensive procedure, so try and
      # keep this number as small as possible while still getting the effect you want.
      # .2 seems to be a good start for a fluid with a 1 resolution.
      searchRadius = .2
      velocityVector = particle.getVelocity()
      velocityMag = velocityVector.module()
      # Set the velocity multiplyer to a smaller number to only add faster
      # moving particles.
      velocityMag = velocityMag * .1
      if velocityMag > searchRadius: velocityMag = searchRadius
      id = particle.getId()
      neighbors = particle.getNeighbors(velocityMag)
      # Only include particles with more than this many neighbors.
      # You could also add stuff here to limit the effect in other ways.
      if len(neighbors) > 6:
         particleDictionary[id] = neighbors
      particle = particle.getNextParticle()
   # print the number of particles we are keeping track of in each frame
   scene.message(str(len(particleDictionary)))


# This is the procedure that adds the particles
def fillHoles():
   emitter = scene.getEmitter(fluidEmitter)
   for id,neighbors in particleDictionary.iteritems():
      particle = emitter.getParticle(id)
      if str(particle) == "None" : continue
      particleVel = particle.getVelocity()
      particlePos = particle.getPosition()
      numNeighbor = len(neighbors)
      count = 0
      # The divisor here is the number of neighbor samples we will iterate through
      # for each particle. You want to keep this number as low as possible
      # while still getting the effect you want, as this can really slow down the
      # simulation. 3 seems to be a good starting point.
      sample = numNeighbor/3
      if sample < 1: sample = 1
      while count < numNeighbor:
         # Find the mid point of the current particle and its neighbor
         neighborPos = neighbors[count].getPosition()
         midPoint = particlePos + neighborPos
         midPoint.scale(.5)
         # Test to see if we can put a particle here. The number at the
         # end sets how relaxed this test is. A value of 0  will
         # make it less likely to add a particle here, while a value of 1
         # will make it very likely. If you set this number too large, the
         # amount of particles could grow uncontrollably. 0.2 seems
         # like a good starting point.
         if emitter.testPositionForParticleInsertion(midPoint, 0.2):
            # add the new particle
            newParticle = emitter.addParticle(midPoint,particleVel)
            # rebuild the voxelization so the Particle Insertion function
            # will include the newly added particle
            emitter.createVoxelization(True)
         count =count + sample
   

#--------------------------------------------------
# Function: onSimulationBegin
#--------------------------------------------------


def onSimulationBegin():
   # make neighbor dictionary when you begin the sim, or
   # when you pause and continue
   getNeighbor()
   pass




#--------------------------------------------------
# Function: onSimulationStep
#--------------------------------------------------

def onSimulationStep():

   pass


#--------------------------------------------------
# Function: onSimulationFrame
#--------------------------------------------------

def onSimulationFrame():
   # fill the holes
   fillHoles()
   # make a new dictionary
   getNeighbor()

   pass




#--------------------------------------------------
# Function: onSimulationEnd
#--------------------------------------------------

def onSimulationEnd():
   pass




#--------------------------------------------------
# Function: onChangeToFrame
#--------------------------------------------------

def onChangeToFrame():
   pass


User avatar
bolek
Posts: 121
Joined: Thu Nov 27, 2008 10:24 am
Contact:

hole filling splash script

Postby bolek » Tue Mar 06, 2012 8:52 pm

Nicely done Karl. :)

Best!
____________________________________________
http://vimeo.com/bolekcg
http://bolekcg.blogspot.com/

User avatar
scubedio
Posts: 97
Joined: Thu Jan 11, 2007 5:17 pm
Location: mumbai

hole filling splash script

Postby scubedio » Wed Mar 07, 2012 1:22 pm

Hi Karl ....... thnx for the script......Good job.....:)
Rgds
Scubedio

https://vimeo.com/scubedio

savat
Posts: 91
Joined: Thu Oct 08, 2009 1:53 pm

hole filling splash script

Postby savat » Wed Mar 07, 2012 3:27 pm

really apreciated

arda3d
Posts: 8
Joined: Mon Nov 14, 2011 12:59 pm

hole filling splash script

Postby arda3d » Tue Mar 13, 2012 12:34 pm

Karl that rocks! Thanks :)

User avatar
nuwan_rr
Posts: 130
Joined: Thu Oct 18, 2007 7:22 am
Location: Colombo- Sri lanka

hole filling splash script

Postby nuwan_rr » Sun Mar 18, 2012 5:33 pm

this is great tnx
speco


http://www.vimeo.com/nuwan

pushkar
Posts: 14
Joined: Wed Apr 18, 2012 11:45 am

Re: hole filling splash script

Postby pushkar » Thu Jun 21, 2012 8:36 am

hi, i am new in realflow,
will you please help me out. i am using this script, its working. but its unable to create splash like your test simulation.

please help me out.
i hope you will reply:)


Return to “Python-Scripting”

Who is online

Users browsing this forum: No registered users and 3 guests