Jump to content

Generate Text File out of Houdini


bjs

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 4
Link to comment
Share on other sites

  • 2 years later...
  • 3 weeks later...

Hi All - 

I used the attribute_dump.hipnc file as a guide and it works great, but I would like to save out as just floats (not vectors)

Does anybody know what I need to do in the code to change that?

Right now I have:

for (int i = 0; i < @numpt; i++)
{
    vector pos = point(0, "P", i);
    float h = pos.y;
    append(v[]@point_colors, h);
}

But that writes a vector where all three values are h for each line. I'd like to just write a float to save space and time. Any ideas?

 

Thanks!

Link to comment
Share on other sites

15 hours ago, sherlockHound said:

Hi All - 

I used the attribute_dump.hipnc file as a guide and it works great, but I would like to save out as just floats (not vectors)

Does anybody know what I need to do in the code to change that?

Right now I have:


for (int i = 0; i < @numpt; i++)
{
    vector pos = point(0, "P", i);
    float h = pos.y;
    append(v[]@point_colors, h);
}

But that writes a vector where all three values are h for each line. I'd like to just write a float to save space and time. Any ideas?

 

Thanks!

Hello David

you are getting vectors because you are writing to v[]@point_colors which is a vector array. To write floats you need to replace the v[](vector array) with a f[](float array).

Link to comment
Share on other sites

6 hours ago, dleonhardt said:

Hello David

you are getting vectors because you are writing to v[]@point_colors which is a vector array. To write floats you need to replace the v[](vector array) with a f[](float array).

Thank you, Dennis! I looked into that on your recommendation, but I can not find a float array to use. Any ideas? I am very new to this, despite having used Houdini for many years! 

 

Link to comment
Share on other sites

18 minutes ago, sherlockHound said:

Thank you, Dennis! I looked into that on your recommendation, but I can not find a float array to use. Any ideas? I am very new to this, despite having used Houdini for many years! 

 

Change line 5 to

append(f[]@point_colors, h);

and you should get floats.

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...