Primitive Normal Orientation

Ever wondered what “Orient Polygons” does? It tells me that it winds all the polygons in the same ‘direction’. What direction is that?
And how does it know (determine) what polygons to reverse or flip. I find that desription utterly useless and needed more control over my prim normals in Houdini (defining direction using a set of shared points and their surface orientaiton) for a project i’m working on.

Based on those requirementrs I made a simple orient prim normal operator that uses a vector point attribute to test the primitive orientation. If the op determines the prim normal needs to be flipped, it’s added to a primitive normal group that can be reversed using the reverse SOP.

import math

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


# Evaluate the parameters
prim_group = hou.pwd().evalParm("primitive_group")          #primitive group to sample from
normal_name = hou.pwd().evalParm("sample_attribute")        #vector attribute used for averaging and comparison
export_group = hou.pwd().evalParm("export_group")           #primitive group wrong faced normals get exported to
angle_threshold = hou.pwd().evalParm("angle_offset")     #maximum allowed angle

# Finds the primitive group
def findPrimGroup(inName):

    for group in geo.primGroups():
        if group.name() == inName:
            return group.prims()
            break

    return None


'''
Sample Information------------------------------------------------------------------------------------------
'''



# Fetch the primitives and sample input information
if len(prim_group) is not 0:
    prims = findPrimGroup(prim_group)
    if prims is None:
        raise hou.NodeError("Can't find primitive group: %s" % prim_group)

else:
    prims = geo.prims()

if len(prims) < 1:
    raise hou.NodeError("Not enough primitives provided! Need at least one..")

if len(export_group) < 1:
    raise hou.NodeError("No export group found")

if export_group in geo.primGroups():
    raise hou.NodeError("Primitive group '%s' already exists" % export_group)

export_group = geo.createPrimGroup(export_group)

normal_attrib = geo.findPointAttrib(normal_name)
if normal_attrib is None:
    raise hou.NodeError("Lookup attribute '%s' does not exist" % normal_name)


'''
Compute---------------------------------------------------------------------------------------------------
'''


# Now sample the normals
for prim in prims:
   
    # get the prim verts
    verts = prim.vertices()

    # from every vert get the associated point normals
    # note that every point normal is normalized
    prim_point_normals = []
    for v in verts:
        vector = hou.Vector3(v.point().attribValue(normal_attrib))
        vector.normalized()
        prim_point_normals.append(vector)

    # accumulate point normals
    accum = hou.Vector3(0,0,0)
    for n in prim_point_normals:
        for i in range(3):
            accum[i]+=n[i]

    # get the average
    count = len(prim_point_normals)
    for i in xrange(len(accum)):
        accum[i] /= count

    # get primitive normal
    prim_normal = hou.Vector3(prim.normal())
    inv_prim_normal = -1*prim_normal

    # get the angle
    current = accum.angleTo(prim_normal)
    inversed = accum.angleTo(inv_prim_normal)

    if inversed < (current+angle_threshold):
        export_group.add(prim)

32 thoughts on “Primitive Normal Orientation

  1. Hello my friend! I want to say that this article is awesome, great written and include approximately all significant infos. I would like to look more posts like this .

  2. Hello my loved one! I wish to say that this article is awesome, nice written and include approximately all significant infos. I¡¦d like to see more posts like this .

  3. I simply could not go away your website prior to suggesting that I extremely enjoyed the usual info an individual supply on your guests? Is going to be back regularly to investigate cross-check new posts

  4. Simply desire to say your article is as amazing. The clearness in your post is simply excellent and i could assume you are an expert on this subject. Well with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please continue the rewarding work.

  5. I’m just commenting to make you know of the impressive discovery my wife’s girl developed going through your site. She learned too many issues, including what it’s like to have an ideal coaching nature to get folks very easily comprehend specific very confusing issues. You undoubtedly exceeded her desires. Thanks for supplying the warm and helpful, trusted, revealing and fun thoughts on the topic to Evelyn.

  6. Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I will certainly comeback.

  7. Very efficiently written story. It will be beneficial to anybody who employess it, as well as me. Keep doing what you are doing – can’r wait to read more posts.

  8. I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my view, if all website owners and bloggers made good content as you did, the web will be much more useful than ever before.

  9. I¡¦ll right away clutch your rss as I can not in finding your email subscription hyperlink or newsletter service. Do you have any? Please permit me recognize in order that I may subscribe. Thanks.

  10. I intended to put you this tiny note so as to say thanks a lot once again relating to the stunning tricks you’ve discussed on this site. It was really extremely open-handed of you to convey freely all that a few people could have distributed as an electronic book to end up making some dough for themselves, even more so considering that you could possibly have done it if you ever desired. The ideas additionally served to become a easy way to fully grasp the rest have a similar interest much like mine to learn good deal more regarding this condition. I believe there are thousands of more fun sessions up front for individuals that find out your blog.

  11. Unquestionably believe that which you stated. Your favorite justification appeared to be on the internet the simplest thing to be aware of. I say to you, I certainly get irked while people consider worries that they plainly don’t know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people could take a signal. Will probably be back to get more. Thanks

  12. Thanks for sharing superb informations. Your site is so cool. I’m impressed by the details that you¡¦ve on this web site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for extra articles. You, my friend, ROCK! I found just the info I already searched everywhere and just couldn’t come across. What a great site.

  13. Hey There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and return to read more of your useful info. Thanks for the post. I will certainly return.

  14. Wow! This can be one particular of the most useful blogs We have ever arrive across on this subject. Basically Fantastic. I’m also a specialist in this topic therefore I can understand your effort.

  15. I would like to give thanks a lot for the job you have made in writing this blog post. I am hoping the same effective job from you later on also.

  16. Greetings I located your blog by mistake when i was searching Bing for this subject, I have to tell you your webpage is truly helpful I also seriously like the theme, it is good!

Leave a Reply

Your email address will not be published. Required fields are marked *