Vertex ordering when createPolygon()

   3174   2   0
User Avatar
Member
875 posts
Joined: Oct. 2008
Offline
How do I control the way vertices are ordered when I create new geometry with Python.

For example, the following code makes a copy of the input geo shifted by 0.2 units in y, but it mixes the ordering (see attached image).


geo = hou.pwd().geometry()
points = geo.points()

#—-poly—————————————————-
def drawpoly(path):
poly = geo.createPolygon()
for position in path:
p = geo.createPoint()
p.setPosition(position)
poly.addVertex(p)

#—-MAIN—————————————————-
def main():
path =
for point in points:
pos = point.position() + hou.Vector3( 0, 0.2, 0 )
path.append(pos)

drawpoly(path)
#————————————————————

main()

Attachments:
mix.jpg (18.2 KB)

--
Jobless
User Avatar
Member
1926 posts
Joined: Nov. 2006
Offline
If you are going to need to ensure vertex order like in this case then it is best to deal with things on a primitive by primitive basis.

Also, in your case, you aren't actually making an accurate copy of the geometry. Your input geo has 4 primitives while you are only creating one and attempting to create vertices at all the points on it.

Here's my version of what you are trying to do. First I create a new point for each point and translate it up 1 unit. I then just add the point to a list. At the end of the loop the new point list is exactly the same as the original point list but with the translated points. Next we need to handle the primitives. So for each primitive we create a new one that will be translated up. We then look at the vertices of that primitive. For each vertex we get the number of the point it is attached to. Since that point number corresponds to the index in the new points list of the original point but translated up, we just index into the new points list and set the new primitive to have a vertex at the translated point. This ensures vertex order is the same and the output geometry will be exactly what we want.

new_points =

# Create the translated points.
for point in points:
new_point = geo.createPoint()
new_pos = point.position() + hou.Vector3(0,1,0)
new_point.setPosition(new_pos)
new_points.append(new_point)

# Create new prims for each existing prim.
for prim in geo.prims():
new_prim = geo.createPolygon()
# For each vertex in the original prim, get the point number
# and then add a vertex to the new poly at that point in the new
# point list.
for vert in prim.vertices():
ptnum = vert.point().number()
new_prim.addVertex(new_points)
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
875 posts
Joined: Oct. 2008
Offline
Brilliant, ta.
--
Jobless
  • Quick Links