hi marcin,
ya thank you for the lead... ya sure i'd appreciate any help..
heres the code for you guys to check and let me know the mistakes. Pls
note that this is a mix of two scripts fused to make the exchange of
data work together...Now the issue is the tool does produce bin files
but when i try to load these file sequence in RF, it hangs !
-ashish
# PDC to BIN export tool
'''
Script : pdc_to_bin.py
Desc : Read pdc files and write out bin files
Author : Ashish Dantu -
ashis...@gmail.com
Date : 1/10/2007
Notes : Based on scripts by :
# Name: export_custom_normal.rfs - Author: Gregory
Ecker
# Name: pdc_import.rfs - Author: Jorge Medina
Usage : When the script is run, it will open a 'filedialog'
window. Choose a .pdc file (start file in a pdc sequence).
The script will write out .bin files in the same
directory.
Script flow :
main() [get folder path ]
-> fires importFile() [open each pdc file in
the path and store the POS and vel values]
-> fires binmain() [for each pdc,
write a bin file in the same directory using the pass pos and vel
values]
-> fires
writeBinParticleFileHeader() [write header info]
-> fires
writeParticleFileFrame() [write the per particle attributes]
'''
import Tkinter,tkFileDialog,tkMessageBox,struct,array,os,sys,dircache
######################################
## binMain
## ITEMS to be PASSED :
## endFrame = len(pdcfiles in the folder)
## particleNode = particleshape node name
## fullbinDir = particle bin directory
## numOfParts = rfId (particles for the current frame)
## posiPP = particle pos array
## velPP = particle vel array
def
binMain(endFrame,particleNode,fullbinDir,numOfParts,posiPP,velPP,currFrame):
# default frame parameters
global sceneFps
sceneFps = 25
start_frame = 0
end_frame = endFrame
step_frame = 1
# get directory to write data to:
fullPath = fullbinDir
# step through scene and write out bin files:
#print 'exporting frame :' , str(currFrame), ' Total parts ',
numOfParts #, ' \n the pos is \n ',posiPP
# create file for this frame, write out header info:
str_number = '%(#)05d' % {"#":currFrame}
filename = particleNode + "_custom" + str_number + ".bin"
fullfilename = fullPath + filename
filehandle = open(fullfilename, "wb")
# writeBinParticleFileHeader(filehandle, particleNode,
currFrame,numOfParts)
writeBinParticleFileHeader(filehandle,
particleNode,currFrame,numOfParts)
#print '\n',len(posiPP)
for i in range (0,len(posiPP)):
#print ' > wrtFileFrame for ', i
#print posiPP[i]
writeParticleFileFrame (filehandle,posiPP[i],velPP[i],i)
filehandle.close()
######################################
# writeParticleFileFrame
# write a frame of data to the open filehandle for the given particle
node.
def writeParticleFileFrame(filehandle, posiPP, velPP,partID):
filehandle.write(struct.pack('3f',
posiPP[0],posiPP[1],posiPP[2])) # 3f position
filehandle.write(struct.pack('3f',
velPP[0],velPP[1],velPP[2])) # 3f velocity
filehandle.write(struct.pack('3f', 0.0, 0.0,
0.0)) # 3f force
filehandle.write(struct.pack('3f', 0.0, 0.0,
0.0)) # 3f vorticity
filehandle.write(struct.pack('3f', 0.0, 0.0,
0.0)) # 3f normal
filehandle.write(struct.pack('i',
1)) # i number of neighbors $$
NOT_IMPLEMENTED
filehandle.write(struct.pack('3f', 0, 0,
1)) # 3f texture vector
infoBits = 7
filehandle.write(struct.pack('h', infoBits)) # h infobits $$ WHAT
IS THIS ?
filehandle.write(struct.pack('f', 1000)) # f age
filehandle.write(struct.pack('f', 1)) # f isolation time
filehandle.write(struct.pack('f', 3)) # f viscosity
filehandle.write(struct.pack('f', 1)) # f density
filehandle.write(struct.pack('f', 1)) # f pressure
filehandle.write(struct.pack('f', 1)) # f mass
filehandle.write(struct.pack('f', 1)) # f temperature
filehandle.write(struct.pack('i', 1)) # i particle id
######################################
# writeBinParticleFileHeader(filehandle, emitterNode)
# write header for bin particle file
# filehandle: must be open
# particleNode: node of emitter to write out
def writeBinParticleFileHeader(filehandle, particleNode,
currFrame,numOfParts):
#print filehandle, particleNode, currFrame,numOfParts
#global sceneFps
sceneFps =25
filehandle.write(struct.pack('l',
0xFABADA)) # 'l' magic
filehandle.write(struct.pack('250s',
particleNode)) # '250s' fluid name
filehandle.write(struct.pack('h',
9)) # 'h' version 9
# globalscale = scene.getGlobalgetGlobalVariableValue("scale")
globalscale = 1.0
filehandle.write(struct.pack('f',
globalscale)) # 'f' scene scale
iType = 1
filehandle.write(struct.pack('i',
iType)) # 'i' fluid type
curtime = currFrame * 1/sceneFps
filehandle.write(struct.pack('f',
curtime)) # 'f' elapsed sim time
filehandle.write(struct.pack('i',
currFrame)) # 'i' cur frame number
filehandle.write(struct.pack('i',
sceneFps)) # 'i' frames per second
filehandle.write(struct.pack('l',
numOfParts)) # 'l' number of particles
# radius = emitterNode.getParameter("radius")
radius = 0.1
filehandle.write(struct.pack('f', radius)) #
'f' radius $$ NOT_IMPLEMENTED - NOT SURE WHERE THIS IS
filehandle.write(struct.pack('3f', 1, 1, 1)) #
'3f' $$NOT IMPLEMENTED pressure max,min,avg
filehandle.write(struct.pack('3f', 3, 2, 1)) #
'3f' $$NOT IMPLEMENTED speed max, min, average
filehandle.write(struct.pack('3f', 3, 2, 1)) #
'3f' $$NOT_IMPLEMENTED temperature:, max,min, average
# ePos = emitterNode.getParameter("position")
filehandle.write(struct.pack('3f', 0.0,0.0,0.0)) #
emitter position
# eRot = emitterNode.getParameter("rotation")
filehandle.write(struct.pack('3f', 0.0,0.0,0.0)) #
emitter rotation
# eScale = emitterNode.getParameter("scale")
filehandle.write(struct.pack('3f', 0.0,0.0,0.0)) #
emitter scale
######################################
# importFile
def importFile( fileName,fullbinDir,nFiles,rootName,currFrame ):
file = open( str(fileName), "rb" )
magic = str( file.read( 4 ) ) # pdc or not
if ( magic != "PDC " ):
#print( "The file " + fileName + " is not a PDC format." )
file.close()
return ( False )
listrfIds = []
posiPP = []
veloPP = []
switch = 0
rfId=0.0
# 1 Integer indicating the file format version number.
version = struct.unpack( ">i", file.read( 4 ) )[0]
#1 Integer holding bit information about whether the values stored
in the file are BIG_ENDIAN or LITTLE_ENDIAN.
endianess = struct.unpack( ">i", file.read( 4 ) )[0]
# 2 Integers holding extra bit information that various file
format version might decide to use.
file.read( 8 )
# 1 Integer indicating the number of particles represented in this
file.
cParticles = struct.unpack( ">i", file.read( 4 ) )[0]
# 1 Integer indicating the number of attributes that have values
stored in this file.
cAttributes = struct.unpack( ">i", file.read( 4 ) )[0]
for i in range( 1, cAttributes ):
#posiPP = []
#veloPP = []
# 1 Integer indicating the length of the attribute's name
nameLength = struct.unpack( ">i", file.read( 4 ) )[0]
# M Characters indicating the name of the attribute, where M
is the length of the name.
attributeName = struct.unpack( "=" + str( nameLength ) + "s",
\
file.read( nameLength ) )[0]
# 1 Integer indicating the type of data for the current
attribute.
attributeType = struct.unpack( ">i", file.read( 4 ) )[0]
if ( attributeType == 3 ): # 3 ---> Double Array
if ( attributeName == "particleId" ):
for j in range (0,cParticles): #
cParticles --> num of particles
mayapId = struct.unpack( ">d",
file.read( 8 ) )[0]
#fill the particle Ids
listrfIds.append
( mayapId )
else:
file.read( 8 * cParticles )
elif ( attributeType == 5 ): # 5 ---> Vector Array
switch = 1
if ( attributeName == "position" ):
# Update particles' position.
for rfId in listrfIds:
posadd=[0.0,0.0,0.0]
posadd[0] = struct.unpack( ">d",
file.read( 8 ) )[0]
posadd[1] = struct.unpack( ">d",
file.read( 8 ) )[0]
posadd[2] = struct.unpack( ">d",
file.read( 8 ) )[0]
posiPP.append(posadd)
if ( attributeName == "velocity" ):
for rfId in listrfIds:
veladd=[0.0,0.0,0.0]
veladd[0] = struct.unpack( ">d",
file.read( 8 ) )[0]
veladd[1] = struct.unpack( ">d",
file.read( 8 ) )[0]
veladd[2] = struct.unpack( ">d",
file.read( 8 ) )[0]
veloPP.append(veladd)
#print '\n after append vpp > ',veloPP[0]
else:
file.read( 8 * 3 * cParticles )
if (switch ==1):
#print '\n vel PP >',veloPP
binMain(nFiles,rootName,fullbinDir,cParticles,posiPP,veloPP,currFrame)
file.close()
return ( True )
# -------------------- MAIN ------------------- #
def main():
## Show file picker dialog and get the selected file.
root = Tkinter.Tk()
root.withdraw()
filePath =
tkFileDialog.askopenfilename(parent=root,defaultextension='pdc',title='Choose
a folder with PDC')
if filePath == None:
print "Choose a file"
else:
print 'The chosen path is > ',filePath
## Find path and file root name.
path = os.path.split( filePath )
filename=path[1]
if ( filename.count( "." ) != 2 ):
return ( 1 )
( rootName, dummyNumber, dummyExt ) = filename.split( "." )
## Build the list of files to load in order using their sequence
number.
listFiles = dircache.listdir( path[0] )
## all the files in the current chosen path
listFiles.sort()
nFiles = len (listFiles)
stepFile = 0
newListFile = []
for n in range ( 0, nFiles ):
if ( listFiles[n].count( "." ) != 2 ):
# the pdc filename should have two dots (.) >
particleShape1.1000.pdc
continue
( rootNameAux, number, ext ) = listFiles[n].split( "." )
if ( ( ext.lower() == "pdc" ) and ( rootName ==
rootNameAux ) ):
newListFile.append( int( number ) )
if ( len( newListFile ) < 2 ):
return ( 1 )
newListFile.sort()
#print 'Current sequence of files from chosen pdc are :\n %s and
total files are %d' % (newListFile,nFiles),'\n'
# ---- read each file ---- #
nFiles = len ( newListFile )
for i in range ( 0, (nFiles) ):
# Build file name to load.
number = str (newListFile[i])
fileName = path[0] + "/" + rootName + "." + number + ".pdc"
#print '\n Reading file : ',fileName
# for each of the file, fire the import def and print out the
values
# importFile( fileName,fullbinDir,nFiles,rootName )
if ( not importFile( fileName,(path[0] +
"/"),nFiles,rootName,i ) ):
return ( 1 )
# Return success
return ( 0 )
if __name__ == "__main__":
result = main()
if ( result == 1 ):
tkMessageBox.showinfo('Retry','Reading failed !')
else:
tkMessageBox.showinfo('Done','Reading success :)')