Jump to content

Python write vector attribute to text file


philpappas

Recommended Posts

Hello,

i think this will be simple for someone more experienced.

i have two vector primitive attributes and i want to write them in a text file so-------> v1.x, v1.y v1.z, n1.x, n1.y ,n1.z , v2.x, v2.y v2.z, ....etc  for each primitive

 

this is how far i am now

 

#############  READ ATTRIBUTES  ######################

for prim in geo.prims() :
    pos = hou.Vector3(prim.attribValue('pos'))
    dir = hou.Vector3(prim.attribValue('dir'))
    data = str(pos) + "," + str(dir)    
    
#############  WRITE TO TEXT FILE ######################

myfile = '$HIP/lattice.txt'

with open(myfile, 'w') as f:
    for item in data:
        f.write("%s\n" % item)

spits out this nonsense, splits every character to a new line

[
-
1
7
5
2
,
 
-
1
1
9
.
0
3
,
 
1
4
6
4
]
,
[
0
,
 
3
0
.
9
0
1
4
,
 
0
]

 

 

Link to comment
Share on other sites

 

5 hours ago, philpappas said:

for prim in geo.prims() :
    pos = hou.Vector3(prim.attribValue('pos'))
    dir = hou.Vector3(prim.attribValue('dir'))
    data = str(pos) + "," + str(dir)    

Not sure if these lines are working, but it seems to me that the value of "data" would be a string. Therefore, when you loop through "data", "item" would be each characters in the string. Hope I could be misunderstanding..

Link to comment
Share on other sites

import hou
import numpy as np

geo = hou.node('/obj/geo1/torus').geometry()

# Read the attributes into numpy arrays
pos_array = np.frombuffer(geo.primFloatAttribValuesAsString('pos'), dtype=np.float32)
dir_array = np.frombuffer(geo.primFloatAttribValuesAsString('dir'), dtype=np.float32)

# Split the array by 3
pos_array = np.array_split(pos_array, pos_array.size/3)
dir_array = np.array_split(dir_array, dir_array.size/3)

# Stack the arrays (similar to python zip())
stack = np.column_stack((pos_array, dir_array))
# Make a single continuios array of form pos.x pos.y pos.z dir.x dir.y dir.z
final = stack.flatten()
# Save to a file with float precision = 3
np.savetxt('c:/temp/foo.txt', final, fmt='%.3f', newline=' ')

 

If you need speeeeeeeed :rolleyes: 

  • Like 3
Link to comment
Share on other sites

On 10/23/2019 at 7:04 AM, Stalkerx777 said:

If you need speeeeeeeed 

thanks a lot, it works (not so fast though if you have thousands of values :()

import hou
import numpy as np

node = hou.pwd() # get the current node
geo = node.geometry() #get geometry of node

# Read the attributes into numpy arrays
pos_array = np.frombuffer(geo.pointFloatAttribValuesAsString('pos'), dtype=np.float32)
dir_array = np.frombuffer(geo.pointFloatAttribValuesAsString('dir'), dtype=np.float32)

i read the point attributes directly from the python node, absolute paths wont help a lot because i'm working in an asset.

i also found this blog and the code takes about the same time to write the text file.

http://astroukoff.blogspot.com/2018/05/data-from-houdini-to-unity-via-text-file.html

this is the code i used (one attribute at a time though):

node = hou.pwd() # get the current node
geo = node.geometry() #get geometry of node

myTxt = file("$HIP/POSITION.txt", "w") #create file, w = write mode

for point in geo.points(): #iterate through each geo point
    pos = point.attribValue("pos") #save position in a pos var
    s = str(pos) #write position to s string
    myTxt.write(s) #write s string into a file

myTxt.close()

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...