Saturday, May 19, 2018

Data from Houdini To Unity via text file

For my current project, I needed a way to get some position and vector from houdini to unity.
I just wanted to store points position + normals in two Vector3 Array in unity for different usage

This may be something really simple for python users, but it was kind a new for me, because I don't  really do a lot of Python and Text parsing at all !




Here are the basics steps :

  1. Generate data in houdini
  2. Save data in text file with Python sop
  3. Parse that data and convert it to something unity can read 




This is the geometry I'm working on, I'm going to scatter some points on it


I have the data I want to transfer from houdini.  Here are some randomly scattered points, each points has a position ("P" attribute, and a normal "N" attribute)



Now for the interesting part : Using a Python SOP, with some code I can have houdini write everything in a text file for me. Here is that code snippet with comments


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

myTxt = file("C:/Houdini/MyTextFile.txt", "w") #create file, w = write mode

for point in geo.points(): #iterate through each geo point
    pos = point.position() #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()


It's really simple code, and it will output something like this (change "C:/Houdini/MyTextFile.txt" to what you want of course)


Each points position is now written in the file, if you check in your geometry spreadsheet, you will see that the position indeed match in the same order. I duplicate the node and did the same thing for the Normal. the only change is  :

nm = point.attribValue("N")  instead of pos = point.position() 

Just import that Text file in Unity (drag and drop in your project, or just save it directly in your project folder by changing the path in the Python code)


In unity I them created a (pretty crappy) code to parse this file, this was actually a bit painful for me, parsing text is really not fun.  you can see the whole code here, I did my best to comment it, but it's honestly bad code...I did all of this in a scriptable object, that way I can have this data sitting nicely in my project


in association to this code that convert the textfile into two vector3 lists of Position and normals, I did a quick custom inspector to have a button to refresh those values  that trigger the main parse function


this tiny bit of code gives me acces to this button, when I create a new object of type SamplingPointlist, by Right Clicking my project > Create > Sampling Point List



After the button is pressed...Done ! My data is now ready to be used ! 
To visualize it quickly, I have another script with declared variable of type  

public SamplingPointsList myPointListAsset;

And I can just iterate through every single myPointListAsset.Positions[x] and myPointListAsset.Normals[x] and draw a quick sphere, and line using the normal (also drive the line color from normal, just for fun).




Its not the most optmized code, but this is all offline generation and parsing, so it works for me :)


At the end here is the files I have in unity : 
  • pointNormals.txt  (file from houdini)
  • pointPosition.txt  (file from houdini) 
  • SamplingPointList.cs (Scriptable Object Class with the main code for parsing )
  • LevelPointList01.asset (this is a scriptable object create with the RightClick>Create menu, derived from the above class. I'll have one for each level in my case)
  • GenerateSamplingPoints.cs (Editor class to add a button to the scriptable object inspector)
  • +Code to visualize the gizmo



I hope it help someone ! 
Also feel free to share the proper way to parse the data to directly get the data as vector ! I tried some regex, but I won't touch that thing ever again unless I need too 😫


edit : added/updated code link/ typos

2 comments:

  1. Thanks for sharing!
    I'm not python fluent and needed to be able to do this sort of thing today, so it was a help!

    ReplyDelete
  2. This was super helpful! How can i write the points position for every frame in one text file?

    ReplyDelete

Pinned

"This year I'll learn Learning Blender" he said for the 5th year in a row. + BforArtists

- What a fool   Context: I have 10+ years of 3DS Max experience, I initially started learning it by myself, then went to 3D school, and spen...