Melting Script - Just 4 particles every step???

Discussion about scripting with Python
thevode
Posts: 19
Joined: Sun May 30, 2010 12:35 am
Contact:

Melting Script - Just 4 particles every step???

Postby thevode » Thu Jun 03, 2010 6:23 pm

Hi!,

I'm developing a script to melt an ice text.

I'm using few particles and very low resolution to test the script. I have two problems with my script. it takes more particles from bottom of the freezed particles and it is very slowly taking particles to remove and then create new.

I'm using two square emitters, first to fill the object (I'm freezing it by code) and the second one to create the new liquid particles.

Can you help me to improve the script?. I've seen other threads for help with melting script and I hope you cant help me on this occasion.

My code:

Code: Select all

#--------------------------------------------------
# Function: onSimulationBegin
#--------------------------------------------------
def onSimulationBegin():

   emitter = scene.getEmitter("lleno2")
   letra = scene.getObject("T")
   particle = emitter.getFirstParticle()
   
   while (particle):
      particle.freeze()
      particle = particle.getNextParticle()

   letra.setParameter("Simulation", "Inactive")



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

def onSimulationStep():
   
   emitter=scene.getEmitter("lleno2")
   foam=scene.getEmitter("foam")
   
   removeList = []
   
   particles = emitter.getParticles()   

   for particle in particles:
      if ( particle.id == 0 ):
         break;
   neighbors = particle.getNeighbors(.025)
   id=particle.getId()
   pos=particle.getPosition()
   vel=particle.getVelocity()
   threshold = 5000
   if (len(neighbors) > threshold) and (len(neighbors)!=0) :
       pos = particle.getPosition()
       vel =  particle.getVelocity()
       removeList.append(particle.getId())
       foam.addParticle(pos,vel)

   if (len(removeList) > 0):
       for i in range(len(removeList)):
           lleno2.removeParticle(removeList[i])

      

#--------------------------------------------------
# Function: onSimulationFrame
#--------------------------------------------------
def onSimulationFrame():
   pass




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

def onSimulationEnd():
   pass




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

def onChangeToFrame():
   pass

Thanks for your help!!
Regards
www.thevode.com


thevode
Posts: 19
Joined: Sun May 30, 2010 12:35 am
Contact:

Melting Script - Just 4 particles every step???

Postby thevode » Fri Jun 04, 2010 5:07 pm

I think there are no people looking this forum in the spring, haha.

Thanks all :)
www.thevode.com

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

Melting Script - Just 4 particles every step???

Postby bolek » Sat Jun 05, 2010 8:30 am

Hello `thevode`

Try something like that:

Code: Select all

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

def onSimulationBegin():
   pass
#--------------------------------------------------
# Function: onSimulationStep
#--------------------------------------------------

def onSimulationStep():
   
   emitter1 = scene.getEmitter("Circle01")
   emitter2 = scene.getEmitter("melted")
   removeList = []
   particles = emitter1.getParticles()   
   for i in particles:
      neighbors = i.getNeighbors(.5)
      threshold = 300
      if(len(neighbors) > threshold):
         pass
      else:
         id = i.getId()
         pos = i.getPosition()
#         vel = i.getVelocity()
         vel = Vector.new(0.0,0.0,0.0)
         removeList.append(i.getId())
         emitter2.addParticle(pos,vel)

   if (removeList != 0):
      for i in range(len(removeList)):
         emitter1.removeParticle(removeList[i])




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

def onSimulationFrame():
   pass




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

def onSimulationEnd():
   pass




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

def onChangeToFrame():
   pass

Maybe I misunderstand your problem : ]
Don't forget insert your "onSimulationBegin" code.

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

thevode
Posts: 19
Joined: Sun May 30, 2010 12:35 am
Contact:

Melting Script - Just 4 particles every step???

Postby thevode » Sat Jun 05, 2010 12:23 pm

Bolek: I don't understand your code in my project. I think I've not explained well.

I change my explanation in this thread. I hope you can understand what I mean.

Basically I try to melt a 3d text but the script takes very few particles to remove and replace with the new particles from the "foam" emitter and from the bottom. I'm trying to fix it, but sometimes it takes particles from inside of frozen particles.

Can you download my text scene from here
www.thevode.com

thevode
Posts: 19
Joined: Sun May 30, 2010 12:35 am
Contact:

Melting Script - Just 4 particles every step???

Postby thevode » Sat Jun 05, 2010 6:04 pm

bolek wrote: Hello `thevode`

Try something like that:

Code: Select all

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

def onSimulationBegin():
   pass
#--------------------------------------------------
# Function: onSimulationStep
#--------------------------------------------------

def onSimulationStep():
   
   emitter1 = scene.getEmitter("Circle01")
   emitter2 = scene.getEmitter("melted")
   removeList = []
   particles = emitter1.getParticles()   
   for i in particles:
      neighbors = i.getNeighbors(.5)
      threshold = 300
      if(len(neighbors) > threshold):
         pass
      else:
         id = i.getId()
         pos = i.getPosition()
#         vel = i.getVelocity()
         vel = Vector.new(0.0,0.0,0.0)
         removeList.append(i.getId())
         emitter2.addParticle(pos,vel)

   if (removeList != 0):
      for i in range(len(removeList)):
         emitter1.removeParticle(removeList[i])




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

def onSimulationFrame():
   pass




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

def onSimulationEnd():
   pass




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

def onChangeToFrame():
   pass

Maybe I misunderstand your problem : ]
Don't forget insert your "onSimulationBegin" code.


Mmmhhh... interesting, thanks, I'm trying it
www.thevode.com

thevode
Posts: 19
Joined: Sun May 30, 2010 12:35 am
Contact:

Melting Script - Just 4 particles every step???

Postby thevode » Sun Jun 06, 2010 8:09 am

I'm restart from zero with a new try, first usinf pressure and after with neighbors one more time. This time with 500 of resolution and a text with 3 letters as object.

RF looks frozen long timeat the begining, Is it normal?.

Code: Select all

#--------------------------------------------------
# Function: onSimulationBegin
#--------------------------------------------------
def onSimulationBegin():

   emitter = scene.getEmitter("lleno2")
   letra = scene.getObject("HOT")
   particle = emitter.getFirstParticle()
   
   while (particle):
      particle.freeze()
      particle = particle.getNextParticle()

   letra.setParameter("Simulation", "Inactive")



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

def onSimulationStep():
   
   emitter = scene.getEmitter("lleno2")
   foam = scene.getEmitter("foam")
   
   threshold = 2000
   #total_mass = 0
   #mass_threshold = 2000
   particle = emitter.getFirstParticle()
   while (particle):   
         #mass = particle.getMass()
         #total_mass = total_mass + mass
         #pres = particle.getPressure()
         vel = particle.getVelocity()
         pos = particle.getPosition()
         id   = particle.getId()
         neighbors = particle.getNeighbors(0.25)
         if (len(neighbors) < threshold):
               foam.addParticle(pos,vel)
               emitter.removeParticle(id)
               
         particle = particle.getNextParticle()

#--------------------------------------------------
# Function: onSimulationFrame
#--------------------------------------------------
def onSimulationFrame():
   pass




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

def onSimulationEnd():
   pass




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

def onChangeToFrame():
   pass
www.thevode.com

thevode
Posts: 19
Joined: Sun May 30, 2010 12:35 am
Contact:

Melting Script - Just 4 particles every step???

Postby thevode » Mon Jun 07, 2010 7:05 am

This code works, but it's very slow to calculate. I have about 60.000 particles frozen and 100 on resolution in my first emitter (the frozen emitter) and 500 on the second one (the liquid emitter).

4hours... 11 frames calculated. Is it normal?, what can I do to reduce the time to calculate?.

Code: Select all

#--------------------------------------------------
# Function: onSimulationBegin
#--------------------------------------------------
def onSimulationBegin():

    emitter = scene.getEmitter("lleno2")
    letra = scene.getObject("HOT")
    particle = emitter.getFirstParticle()
   
    while (particle):
        particle.freeze()
        particle = particle.getNextParticle()

    letra.setParameter("Simulation", "Inactive")



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

def onSimulationStep():
   
    emitter = scene.getEmitter("lleno2")
    foam = scene.getEmitter("foam")
   
    threshold = 2000
    #total_mass = 0
    #mass_threshold = 2000
    particle = emitter.getFirstParticle()
    while (particle):   
            #mass = particle.getMass()
            #total_mass = total_mass + mass
            #pres = particle.getPressure()
            vel = particle.getVelocity()
            pos = particle.getPosition()
            id    = particle.getId()
            neighbors = particle.getNeighbors(0.28)
            if (len(neighbors) < threshold):
                    foam.addParticle(pos,vel)
                    emitter.removeParticle(id)
                   
            particle = particle.getNextParticle()

#--------------------------------------------------
# Function: onSimulationFrame
#--------------------------------------------------
def onSimulationFrame():
    pass




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

def onSimulationEnd():
    pass




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

def onChangeToFrame():
    pass
www.thevode.com

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

Melting Script - Just 4 particles every step???

Postby bolek » Mon Jun 07, 2010 8:34 pm

Hello again.

Little answer : ]

"RF looks frozen long timeat the begining, Is it normal?."

It seems to me that Realflow in the meantime freezing particles.
You do it in the onSimulationBegin definition.


"This code works, but it's very slow to calculate."

Help attached to RF, under Contents > Scripting Reference> Particle tells you that Particle[] getNeighbors( float ) function is high time consuming. Check it. : ]

Cheers
____________________________________________
http://vimeo.com/bolekcg
http://bolekcg.blogspot.com/

thevode
Posts: 19
Joined: Sun May 30, 2010 12:35 am
Contact:

Melting Script - Just 4 particles every step???

Postby thevode » Mon Jun 07, 2010 11:08 pm

hey!,

thanks for your help. I think I fix the speed on simulation using a removelist. I've opened a new thread with my last question. It's a new direct question and I've wrote it because maybe more people could help me in this new try ().

Phyton looks easy, but I need to get used, little differences from other lenguages.

Thanks a lot for your help bolek ; )
www.thevode.com

VERSACE1325
Posts: 2
Joined: Sat Dec 24, 2011 3:40 am

Melting Script - Just 4 particles every step???

Postby VERSACE1325 » Sat Dec 24, 2011 4:16 am

:)Thank you
Learn from you
Support it


Return to “Python-Scripting”

Who is online

Users browsing this forum: No registered users and 2 guests