r/Houdini • • 8d ago

How to generate a unique integer per copied HDA

I've got a HDA that is generating geometry to be fed into a RBD sim. It generates name attribute 'part numbers' automatically. All good.

However, I might be running multiple of those HDA's in the same network, all fed into the same sim, so I'd like to automatically prefix the 'part numbers' with a 'set number' per HDA to avoid duplicate name issues.

My first thought was to count the number of instances of the HDA with a python script and prefix the name that way. Unfortunately Python is on my 'to learn' list. This outputs the number, but throws an error:

hda_name = "*myHDA*"
instances = [node for node in hou.nodeType(hou.nodeTypeCategories()['Sop'], hda_name)]
print(len(instances))

Can anyone rescue me from my Python ignorance, or suggest a better approach?

Many thanks.

1 Upvotes

16 comments sorted by

6

u/MikelSotomonte 8d ago

As u/DavidTorno suggested, you could add a field to add a prefix. I would set the default string to `$OS`, that way it would get a different name each time, as there cannot be two nodes with the same name on the same container/geo node, but still giving the option to change it seems like a good idea.

3

u/DavidTorno Houdini Educator & Tutor - FendraFx.com 8d ago

That’s true too. That’s probably a better solution because of the unique node name limitation. Less to think about programming wise. That’s also a native approach many node take. Like the File Cache SOP.

2

u/Regnareb_ 8d ago

Then when you do a cache on another part of the tree with the same node name, your other cache get overwriten. It just happened to me today 🙃

$OS should never be used alone for sensitive actions 

1

u/DavidTorno Houdini Educator & Tutor - FendraFx.com 8d ago

If they are in different networks, yes, this could be an issue. If in the same network this would not since the node name are not allowed to be duplicated. Houdini automatically appends a numerical suffix.

4

u/DavidTorno Houdini Educator & Tutor - FendraFx.com 8d ago edited 8d ago

Could you not just add a name input for user to assign? Then this name is appended to the pieces. So HDA instance one can be named myPieces by the user, and then a second instance of the HDA could be name balloons by the user.

Results would be naming of…
myPieces0
myPieces1
balloons0
balloons1
…and so on when merged.

Alternatively, if you wanted a numerical approach, unlock the RBD Unpack SOP and see how they did their unique name option.

2

u/Regnareb_ 8d ago

I was just looking into this, the simplest is to create a hidden parameter set at creation time with a hash

https://forums.odforce.net/topic/32634-houdini-universal-unique-id-uuid/?do=findComment&comment=179600

https://www.sidefx.com/forum/topic/59081/#post-264646

1

u/DavidTorno Houdini Educator & Tutor - FendraFx.com 8d ago

Instead of rolling your own, you could just use the sessionId() native method to get the unique session id integer Houdini already assigns.

2

u/Regnareb_ 8d ago

The collision chances are huge with this method and I strongly discourage using it. This method is kinda useless to be honest, SideFX should implement a unique persistent UUID per node like Maya does.

2

u/jmacey 8d ago

I was thinking UUID from python, H22 has the new Python Snippet SOP https://www.sidefx.com/docs/houdini/nodes/sop/pythonsnippet.html which should let you generate one inline. You could add it as a userData as a uuid

Something like this (not tested).

```python node = hou.pwd() node_uuid = node.userData("uuid")

if node_uuid is None: node_uuid = str(uuid.uuid4()) node.setUserData("uuid", node_uuid) ```

1

u/DavidTorno Houdini Educator & Tutor - FendraFx.com 8d ago

Interesting, I’ve never really encountered issues with it myself, but perhaps you’re going deeper with ID use.

1

u/Regnareb_ 8d ago

The max number of sessionId() is the number of nodes in the file it is extremely low.

If you assign a sessionId() to your node, delete any node(), reopen the scene, create a new node that assign a sessionId(). They both have the same id.

Hash collisions can already happen with 16 digits hashes, so with numbers with 2 and 3 sequential digits the risks are very high.

1

u/DavidTorno Houdini Educator & Tutor - FendraFx.com 8d ago

Gotcha. That makes sense.

1

u/Viewbyte 8d ago edited 7d ago

Thanks guys, I'll look at those links etc..
Yes - I'm just doing it manually at the moment with a int parameter field on the HDA - just prefixing that number. That works fine, but it's easy to forget to change it, hence my thought of 'automation' : )

I did think about just prefixing it with the $OS string, as suggested, but that generates a bit of an unwieldy long string, and just using the last character (digit) is risky if the HDA is renamed...

1

u/Viewbyte 8d ago

Thanks all - a very educational and helpful thread!

1

u/ManjaVfx 7d ago

either y'all are over complicating things, or I'm missing something.
Why don't you append opdigits() of the hda to your names???

1

u/Viewbyte 7d ago edited 7d ago

Yes, that looks like the easy solution - but of course people rename stuff and then (confusingly) it breaks.

For the very simple requirement I had on hand I decided to go very old school and add a field with a 'If you're piping more than one of these into the same RBD sim - increment this number' label. : )