jkunz07 Posted November 10, 2011 Share Posted November 10, 2011 (edited) 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 November 10, 2011 by jkunz07 Quote Link to comment Share on other sites More sharing options...
graham Posted November 10, 2011 Share Posted November 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
jkunz07 Posted November 10, 2011 Author Share Posted November 10, 2011 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 Quote Link to comment Share on other sites More sharing options...
graham Posted November 10, 2011 Share Posted November 10, 2011 (edited) 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 November 10, 2011 by graham Quote Link to comment Share on other sites More sharing options...
jkunz07 Posted November 11, 2011 Author Share Posted November 11, 2011 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() Quote Link to comment Share on other sites More sharing options...
samboosa Posted February 18, 2013 Share Posted February 18, 2013 (edited) 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 February 18, 2013 by samboosa Quote Link to comment Share on other sites More sharing options...
edward Posted February 19, 2013 Share Posted February 19, 2013 Partition SOP to create each of your "list" as a "group". You can then use an Add SOP and supply it with these groups. Quote Link to comment Share on other sites More sharing options...
samboosa Posted February 19, 2013 Share Posted February 19, 2013 Partition SOP to create each of your "list" as a "group". You can then use an Add SOP and supply it with these groups. I currently have that now, I was just hoping it could all be avoided by doing it in a Python sop... Quote Link to comment Share on other sites More sharing options...
graham Posted February 19, 2013 Share Posted February 19, 2013 It's a speed vs convenience thing; Partition is going to be much faster than Python. Quote Link to comment Share on other sites More sharing options...
edward Posted February 20, 2013 Share Posted February 20, 2013 The Partition is a "convenience" to me because I don't need to code it. 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.