Jump to content

Getting connected primitives in Python?


simonj

Recommended Posts

You can post your solution here if you got it.

Cheers!

steven

Yeah, sorry about that, hehe. I was going to but I had some quirks to work out.

Anyway, here is my solution, maybe there is a more effective one.

Since it doesn't seem like python (or more exactly "hython", right?) has a built in function, and I don't know how to use expressions inside of Python (any tips on that?) I created a Attribute Create-node

This node is set to create primitive attributes which will be a string. I checked "Write values" and put `primneighbours("../prim",$PR,2)` in the "string"-field. Note the ` before and after the expression, that makes sure that you don't put the expression as a string but the result of that expression instead.

This will set an attribute on each primitive which is a series of the connected primitives (aka the neighbours), for example, it can look like this for a primitive in the middle of the grid: 10 18 20 37.

Great, now all I have to do is to access these in python. Because the neighbours are in a string I need to fix that, python has a function called split that will do that for us.

so here is an example written in an IDLE.

>>> neighbours = "10 18 20 37"
>>> neighbours
'10 18 20 37'
>>> neighbours = neighbours.split()
>>> neighbours
['10', '18', '20', '37']

So you see that suddently we have these numbers in an array, and now they can be accessed through a loop:

in a python sop, this would be how you could do it:

geo = hou.pwd().geometry()

neighbours = geo.findPrimAttrib("neighbours")

for prim in geo.prims():
        nb = prim.attribValue(neighbours)
        for n in nb.split():
            num = int(n)
            nprim = geo.iterPrims()[num]

Let me explain it from the top.

geo = ... is a default to set in a sop node I think, makes access easier to write.

neighbours = ... is set to create a reference to an attribute instead of looking it up everytime in the loop, it's quicker this way.

for prim in geo.prims() iterates through all primitives

nb = ... gets the string containing the neighbours.

for n in nb.split(): put the strings numbers into an array and loops through them

num = int(n) converts the current number retrieved to an int instead of a string.

nprim = ... gets the primitive that has that id (geo.iterPrims()[what_id] is the prefered method for getting a specific primitive.)

So, that's it!

Any suggestions, or more efficient ways to do this are very welcome. :)

[edit, by the way, the reason I expained it so thoroughly is because it's a great way for me to remember it :) ]

Edited by simonj
Link to comment
Share on other sites

just wondering, could you supply a hip or hipnc file with that setup? I tried to recreate it but I think I'm missing some crucial steps in the workflow. Thanks!

Ofcourse I can, maybe I wasn't great at explaining either, a long explanation is not always a good one, hehe :)

I'm new at this .otl stuff, but I think this file should work "right out of the box".

This is a simple example and quite buggy, especially if you try lowering the "falloff" value below 2, you will see some wierd, and if you're lucky, cool, stuff.

A question for the pros though: How could I make this run "backwards" since now only the falloff value works "in front" of the primitives. and I would like it to work in all directions.

neighbours_python.zip

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