Exporting particle data as txt

Discussion about scripting with Python
Post Reply
bmecohen
Posts: 28
Joined: Thu Sep 27, 2012 12:55 pm

Exporting particle data as txt

Post by bmecohen » Thu Oct 11, 2012 1:57 pm

Hello Everyone!

I am trying to make an exporter, to copy all the particle data to a txt file, for further student work.I use it as a batch script, maybe I should use it as an event Script.

Code: Select all

file = open("C:/Users/Gabor/scenes/test/particles_version02/001.txt", "w")
emitter_export = scene.getEmitter("Circle01")
particle = emitter_export.getFirstParticle()

while particle: 
         id = particle.getId()
         position = particle.getPosition() 
         velocity = particle.getVelocity()  
         density = particle.getDensity() 
         pressure = particle.getPressure() 
                   
         file.write("%s %s %s %s %s %s %s %s %s" % (id,position.getX(),position.getY(),position.getZ(),velocity.getX(),velocity.getY(),velocity.getZ(),density,pressure)) 
         particle = particle.getNextParticle()
The txt should like this in order: ID PosX PosY PosZ VelX VelY VelZ Den Pres

Also afterwards I don't see any other particle ID just the first one. Maybe I misunderstand the code entirely, but I want a code like this:

Code: Select all

VARIABLES = "id" "x" "y" "z" "vx" "vy" "vz" "density" "pressure"
ZONE F=POINT, STRANDID=1, SOLUTIONTIME=0.03
109616 0.786 -0.185 0.063 0.033 0.006 0.032 1049.749 304.752
109615 0.786 -0.177 0.063 0.001 0.005 0.003 1035.493 176.532
109614 0.786 -0.158 0.061 -0.005 -0.006 -0.007 1013.543 -1.258
I saw an example in the scripting reference, and there the code is working perfectly.

Code: Select all

# Iterate the list of particles and write out their ids.
currentParticle = emitter.getFirstParticle()
while currentParticle:
  scene.message( str( currentParticle.id ) )
  currentParticle = currentParticle.getNextParticle()
I don't know, maybe the particle = particle.getNextParticle() overwrite everything on my code, because, I only see the latest particle ID.
Please help me how to solve this.

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

Re: Exporting particle data as txt

Post by bolek » Fri Oct 12, 2012 1:37 pm

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

bmecohen
Posts: 28
Joined: Thu Sep 27, 2012 12:55 pm

Re: Exporting particle data as txt

Post by bmecohen » Fri Oct 12, 2012 3:35 pm

Hello bolek!

I used this code on a test file, where only one Circle01 emitter is working nothing else, and the code write me this output data:

Code: Select all

248  -0.0379498898983  0.935260474682  0.491737067699247  -0.136150225997  0.935216188431  0.474017232656246  -0.228775903583  0.935170412064  0.436915129423245  -0.312062859535  0.935169517994  0.381916612387244  -0.382550597191  0.935218274593  0.311275273561243  -0.437384337187  0.935256898403  0.227901443839242  -0.474289208651  0.935220718384  0.135194763541241  -0.491797238588  0.935177981853  0.0369662232697240  -0.489189207554  0.935169160366  -0.0628077462316239  -0.466516077518  0.935210943222  -0.160000011325238  -0.424762636423  0.93524813652  -0.250630706549237  -0.36561575532  0.935227930546  -0.330989062786236  -0.291515976191  0.935188949108  -0.397808164358235  -0.205465212464  0.935169577599  -0.448374599218234  -0.110983669758  0.935200572014  -0.480540007353233  -0.0119880000129  0.93523812294  -0.493041932583232  0.0874940380454  0.935237944126  -0.485363453627231  0.183398962021  0.935199022293  -0.457819998264230  0.271826177835  0.935168266296  -0.411539912224229  0.349099874496  0.935189127922  -0.34837153554228  0.412073165178  0.935230493546  -0.270974874496227  0.458188414574  0.935249626637  -0.182490319014226  0.485538750887  0.935207486153  -0.0865254402161225  0.493031024933  0.935167253017  0.0129917087033224  0.480304449797  0.935178458691  0.111983582377223  0.447934389114  0.935225605965  0.20636677742222  0.397237092257  0.935257911682  0.292313337326221  0.330257236958  0.935212790966  0.366282194853220  0.249770104885  0.935167312622  0.425270199776219  0.159034430981  0.935172021389  0.466845184565218  0.0618089623749  0.935221850872  0.489299863577217  -0.0307671818882  0.937455773354  0.399679630995216  -0.129203498363  0.937385082245  0.379534870386215  -0.219507560134  0.93738669157  0.335500270128214  -0.296004801989  0.9374563694  0.270284831524213  -0.353992551565  0.937436163425  0.188141897321212  -0.389705598354  0.937358677387  0.09431399405211  -0.400861531496  
My problem is, I checked the emitter, and in this stage, the latest particle is 248. I saw this ID on my txt but nothing else. I copied luke's solution, and I got this. I'm using realflow 2012. I don't know what is the problem.

lukeiamyourfather
Posts: 2880
Joined: Mon Oct 15, 2007 4:09 pm
Contact:

Re: Exporting particle data as txt

Post by lukeiamyourfather » Fri Oct 12, 2012 4:20 pm

bmecohen wrote:Hello bolek!

I used this code on a test file, where only one Circle01 emitter is working nothing else, and the code write me this output data:

Code: Select all

248  -0.0379498898983  0.935260474682  0.491737067699247  -0.136150225997  0.935216188431  0.474017232656246  -0.228775903583  0.935170412064  0.436915129423245  -0.312062859535  0.935169517994  0.381916612387244  -0.382550597191  0.935218274593  0.311275273561243  -0.437384337187  0.935256898403  0.227901443839242  -0.474289208651  0.935220718384  0.135194763541241  -0.491797238588  0.935177981853  0.0369662232697240  -0.489189207554  0.935169160366  -0.0628077462316239  -0.466516077518  0.935210943222  -0.160000011325238  -0.424762636423  0.93524813652  -0.250630706549237  -0.36561575532  0.935227930546  -0.330989062786236  -0.291515976191  0.935188949108  -0.397808164358235  -0.205465212464  0.935169577599  -0.448374599218234  -0.110983669758  0.935200572014  -0.480540007353233  -0.0119880000129  0.93523812294  -0.493041932583232  0.0874940380454  0.935237944126  -0.485363453627231  0.183398962021  0.935199022293  -0.457819998264230  0.271826177835  0.935168266296  -0.411539912224229  0.349099874496  0.935189127922  -0.34837153554228  0.412073165178  0.935230493546  -0.270974874496227  0.458188414574  0.935249626637  -0.182490319014226  0.485538750887  0.935207486153  -0.0865254402161225  0.493031024933  0.935167253017  0.0129917087033224  0.480304449797  0.935178458691  0.111983582377223  0.447934389114  0.935225605965  0.20636677742222  0.397237092257  0.935257911682  0.292313337326221  0.330257236958  0.935212790966  0.366282194853220  0.249770104885  0.935167312622  0.425270199776219  0.159034430981  0.935172021389  0.466845184565218  0.0618089623749  0.935221850872  0.489299863577217  -0.0307671818882  0.937455773354  0.399679630995216  -0.129203498363  0.937385082245  0.379534870386215  -0.219507560134  0.93738669157  0.335500270128214  -0.296004801989  0.9374563694  0.270284831524213  -0.353992551565  0.937436163425  0.188141897321212  -0.389705598354  0.937358677387  0.09431399405211  -0.400861531496  
My problem is, I checked the emitter, and in this stage, the latest particle is 248. I saw this ID on my txt but nothing else. I copied luke's solution, and I got this. I'm using realflow 2012. I don't know what is the problem.
I haven't used RealFlow in a while so that was written from memory and not tested. Try converting the particle.getId() to a string, like this.
id = str(particle.getId())
Same goes for any other variable that doesn't seem to be updating as it goes.
WhenPicsFly | Debian GNU/Linux

bmecohen
Posts: 28
Joined: Thu Sep 27, 2012 12:55 pm

Re: Exporting particle data as txt

Post by bmecohen » Sat Oct 13, 2012 12:17 pm

I tried this version, but I got the same result. I attach my result txt as picture. The left one is what I get from python, and the right one is what I made with export central, but manually inserted the headline. I counted my data structure, and I got all of my particle data, but just the first one have id. I tried with str and also as int but no difference. Please help me, where I made an error.

Code: Select all

currentFrame = scene.getCurrentFrame()
rootPath = scene.getRootPath()
emitter = scene.getEmitter("Circle01")
currentParticle = emitter.getFirstParticle()

# Creates a file object that Python can write to 
file = open(str(rootPath)+"/export/"+str(currentFrame)+".txt", "w") 


	# Loop through all particles and write out their ID and position 
while currentParticle: 
			id = str(currentParticle.getId())
			position = currentParticle.getPosition() 
			velocity = currentParticle.getVelocity()  
			density = str(currentParticle.getDensity())
			pressure = str(currentParticle.getPressure()) 
						 
			file.write( "%s %f %f %f %f %f %f %f %f" % (id,position.getX(),position.getY(),position.getZ(),velocity.getX(),velocity.getY(),velocity.getZ(),density,pressure)) 
			currentParticle = currentParticle.getNextParticle() 

	# Close the file object when done
file.close()
Attachments
txt comparison.PNG
txt comparison.PNG (221.3 KiB) Viewed 3581 times

Otuama
Posts: 1155
Joined: Fri Apr 02, 2010 12:56 am
Location: Swansea
Contact:

Re: Exporting particle data as txt

Post by Otuama » Sun Oct 14, 2012 11:26 am

Hi.

The code is doing everything you tell it to. What exactly is wrong with it?

Based on how you said you wanted the output's:
VARIABLES = "id" "x" "y" "z" "vx" "vy" "vz" "density" "pressure"
ZONE F=POINT, STRANDID=1, SOLUTIONTIME=0.03
109616 0.786 -0.185 0.063 0.033 0.006 0.032 1049.749 304.752
109615 0.786 -0.177 0.063 0.001 0.005 0.003 1035.493 176.532
109614 0.786 -0.158 0.061 -0.005 -0.006 -0.007 1013.543 -1.258
This code does the job:

Code: Select all

import os

currentFrame = scene.getCurrentFrame()
#scene.message(str(currentFrame))

rootPath = scene.getRootPath()
#scene.message(str(rootPath))

#If EXPORT dir doesn't exist, make it.
if not (os.path.isdir("%s\\Export" % rootPath)):
    os.makedirs("%s\\Export" % rootPath)

file = open(str(rootPath)+ "/export/frame_" + str(currentFrame)+".txt", "w") 

file.write("VARIABLES = \"id\" \"x\" \"y\" \"z\" \"vx\" \"vy\" \"vz\" \"density\" \"pressure\"\n")
file.write("ZONE F=POINT, STRANDID=1, SOLUTIONTIME=0.03\n")

emitter = scene.getEmitter("Circle01")
currentParticle = emitter.getFirstParticle()

#Loop through all particles and write out their ID and position 
while currentParticle: 
         id = currentParticle.getId()
         position = currentParticle.getPosition() 
         velocity = currentParticle.getVelocity()  
         density = currentParticle.getDensity()
         pressure = currentParticle.getPressure()

         file.write( "%s %f %f %f %f %f %f %f %f\n" % (id,position.getX(),position.getY(),position.getZ(),velocity.getX(),velocity.getY(),velocity.getZ(),density,pressure)) 


         currentParticle = currentParticle.getNextParticle() 

file.close()
It works in batch and also creates separate per frame files when used in simulation events.

Probably best to be used in FramesPost, not FramesPre in simulation events.

Image

Top values of image:

5828 (id)
0.479294 0.883606 -0.036983 (tXYZ)
-0.489681 -2.243395 0.037982 (vXYZ)
772.273987 (density)
-936.167542 (pressure)
iCreate Ltd
'Strength does not come from physical capacity. It comes from an indomitable will.' - Gandhi
'What Does Success Look Like?' - iCreate Ltd

bmecohen
Posts: 28
Joined: Thu Sep 27, 2012 12:55 pm

Re: Exporting particle data as txt

Post by bmecohen » Sun Oct 14, 2012 1:23 pm

Thanks a lot, it works now! I hope it could be useful for other rookies in python scripting as well!

Post Reply

Return to “Python-Scripting”