Jump to content

Point Connecting Python OTL


jkunz07

Recommended Posts

Hey, I've been trying to get into python, this is the first python otl I've made so I know there are probably some issues with the code. I'm curious if anyone has any suggestions for optimizations?

Also, when iterating through both the for loops, I'd prefer to do it like this:

for a in pts:

for b in a+1:

or something similar to that, basically to avoid multiple lines being drawn in the same location, just not sure how to do that in python. I have it working properly in processing/java code that I can post if anyone has trouble understanding what I'm trying to describe.

Connect_Otl.zip

# This code is called when instances of this SOP cook.
geo = hou.pwd().geometry()

# Read the parameters of the node
connectionDist = hou.Node.evalParm(hou.pwd(), 'connectionDist')


# Create pts list to store points in
pts = []

#geo.addAttrib(hou.attribType.Point, "cdist", 0, False, True )

for pt in geo.points():
    pts.append(pt)


def createConnections():
    for a in pts:
        for b in reversed(pts):
            start = a.position()
            end = b.position()
            test = start.distanceTo(end)
            if test < connectionDist:
                poly = geo.createPolygon()
                poly.addVertex(a)
                poly.addVertex(
                #poly.setAttribValue("cdist", connectionDist)
                if hou.updateProgressAndCheckForInterrupt():
                    break

createConnections()

Edited by jkunz07
Link to comment
Share on other sites

In this case, my suggestion would be to use something like what I posted in this thread: . This would allow you to have a far more efficient method at connecting nearby points.

In regards to getting around duplicates, there's the option of creating some sort of unique value per line based on the point numbers used to build it. For example, you could take the two point numbers, sort them, and then generate a hash value from the string and store that somewhere. That way you can check to see if you've already build the line.

Link to comment
Share on other sites

In regards to getting around duplicates, there's the option of creating some sort of unique value per line based on the point numbers used to build it. For example, you could take the two point numbers, sort them, and then generate a hash value from the string and store that somewhere. That way you can check to see if you've already build the line.

Thanks for the quick reply!

I considered this method but was trying to avoid it by using the nested for loops.

I'd like to do something like this in python:


 for (int i=nodes.size()-1;i>=0;i--)  {
    node q = (node)nodes.elementAt(i);
    for (int j=0;j<=i-1;j++)  {    
      node p = (node)nodes.elementAt(j);
      line(q.x,q.y,q.z,p.x,p.y,p.z);
    }
 }

That's how I'm doing this in processing, it results in only unique lines.

basically for point 0 every point is connected, for point 1 every point except 0 is connected, for point 2 every point except points 0 & 1 are connected, for point 3 every point except 0, 1, and 2 are connected, and so on.

I was just curious if there was a similar way to iterate through the points in python syntax because this seemed to be the most efficient way to me

Link to comment
Share on other sites

Something like this then.

for i, a in zip(range(len(geo.iterPoints()), geo.points()):
    for b in geo.points()[i+1:]:
        start = a.position()
        end = b.position()
        test = start.distanceTo(end)
        if test < connectionDist:
            poly = geo.createPolygon()
            poly.addVertex(a)
            poly.addVertex(

Edited by graham
Link to comment
Share on other sites

Something like this then.

for i, a in zip(range(len(geo.iterPoints()), geo.points()):
    for b in geo.points()[i+1:]:
        start = a.position()
        end = b.position()
        test = start.distanceTo(end)
        if test < connectionDist:
            poly = geo.createPolygon()
            poly.addVertex(a)
            poly.addVertex(

Hey, thanks for your help! I changed a few things to get it working, and now it's giving me unique lines.

# This code is called when instances of this SOP cook.
geo = hou.pwd().geometry()

# Read the parameters of the node
connectionDist = hou.Node.evalParm(hou.pwd(), 'connectionDist')


# Create pts list to store points in
pts = []

for pt in geo.points():
    pts.append(pt)


def createConnections():
    for i, a in zip(range(len(pts)), geo.points()):
        for b in pts[i+1:]:
            start = a.position()
            end = b.position()
            test = start.distanceTo(end)
            if test < connectionDist:
                poly = geo.createPolygon()
                poly.addVertex(a)
                poly.addVertex(

createConnections()

Link to comment
Share on other sites

  • 1 year later...

Sorry to dig up an old thread, but would it be possible to:

1. Dynamically create an amount of lists based on a parameter value.

a. Send points with Cd of '0' go to list '0', Cd of '3' go to list '3' etc...

b. Connect the points only within each list.

Edited by samboosa
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...