Jump to content
bjs

Generate Text File out of Houdini

Recommended Posts

bjs    0

Hey there –

I hope, this is the right forum. I am creating an HDA on SOP level that creates a detail attribute on a geo. What I would like to do now is create a textfile (on the desktop, the current directory or a directory chosen by the user – not too important right now) containing this detail attribute (a string) when the user clicks a button.

What I came about on https://tosinakinwoye.com/2014/05/04/houdini-python-snippets/ is this snippet:

# write bundle names, filter and pattern to file
import hou
namedumpfile = '/u/toa/Desktop/b_name.txt'
patterndumpfile = '/u/toa/Desktop/b_pattern.txt'
filterdumpfile = '/u/toa/Desktop/b_filter.txt'
a = file(namedumpfile, 'w')
b = file(patterndumpfile, 'w')
c = file(filterdumpfile, 'w')

# write bundle names, filter and pattern
for bdl in hou.nodeBundles():
   a.write(str(bdl.name()))
   a.write("\n")
   a.close

   b.write(str(bdl.pattern()))
   b.write("\n")
   b.close

   c.write(str(bdl.filter()))
   c.write("\n")
   c.close

Copied into a python node at the end of my setup, this lets me write a string to textfile – but several problems arise: I suspect that the code is run everytime the Python SOP is cooked (but I rather want to let the user decide when to write the file, not have Houdini do it automatically). And the resulting file is also locked (meaning that it can't be edited or deleted).

What I would like better would be the behaviour of a ROP Output Driver – meaning a node that generates the file only when the button is clicked.

So my suspicion is that the Python SOP is probably not the best choice?!

I would greatly appreciate your help. Thanks,

Bastian.

Share this post


Link to post
Share on other sites
bjs    0

I am currently looking into the Shell ROP inside a ROP Network, which looks promising. I now need to find a possibility to get that detail attribute from outside that ROP Network, but I think I saw something like this yesterday, I just have to find it again.

I'll keep you posted, thanks for your help!

Share this post


Link to post
Share on other sites
bjs    0

Alright, getting closer. This first snippet in the "Pre-Render Script" field is working:

myfile = '$HIP/myfile.txt'
a = file(myfile, 'w')
a.write("test abc")
a.close

Just like you would expect, every time you hit the "Run Command" button, it writes to the textfile in the current directory.

Now I would like to get a detail attribute from outside the ROP Network and write it into the file. Since I only have very basic knowledge of Python inside Houdini, this is what I tried:

myfile = '$HIP/myfile.txt'
a = file(myfile, 'w')
a.write(str(hou.node('../../OUT').findGlobalAttrib('myattrib')))
a.close

But I get an error message now, stating "'SopNode' object has no attribute 'findGlobalAttrib'" ...

If I leave out the "findGlobalAttrib" method, it writes "OUT" in the textfile, so Python seams to find the SOP, right?

myfile = '$HIP/myfile.txt'
a = file(myfile, 'w')
a.write(str(hou.node('../../OUT')))
a.close

What am I doing wrong?

Share this post


Link to post
Share on other sites
f1480187    454

1. Add new PythonModule to Scripts tab in Type Properties window.

2. Define your dump function.

def dump():
    # Retrieve values.
    geo = hou.pwd().geometry()
    data = geo.stringAttribValue('my_attrib')

    # Write on disk.
    path = '$HIP/attribute_dump.txt'
    path = hou.expandString(path)  # Expand Houdini variables.
    with open(path, 'w') as f:
        f.write(data)

3. Add new Button parameter and choose Python in the callback language. Then enter the callback like this:

kwargs['node'].hdaModule().dump()

 

Here is a bit more complex examples with vector arrays:

Edited by f1480187
  • Like 3

Share this post


Link to post
Share on other sites
bjs    0

Awesome, F1! Works beautifully – and thanks for the additional examples (how to link to a texfield and format those vector arrays)!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×