Script : Harden edges of all UV borders (break Phong shading)



  • Hi, I'd like to present you a simple script.
    It let you automatically select all UV borders and break their Phong shading, that might be useful for game modeling, especially with hardsurface props.
    More info about hard edges - http://www.polycount.com/forum/showthread.php?t=107196

    Unfortunately, the 'Texture View' window must be opened at least once for properly work :(
    Just close 'Texture View' and continue work.

    Video: https://vimeo.com/317072443

    Download link: https://github.com/rocketjump4d/UV2PhongEdges



  • Thanks for that script. I wrote something similar some years ago but never finished it.
    As you commented in your source, there's a huge bottleneck in performance somewhere so it needs definitely some more work. Also, there are are some polys selected on processing end, the selection should be restored after the process.

    Plus, please add Undo - absolutely neccessary!

    But nice work so far. :)

    I'm pretty certain, if you work out a more low level variant, without the need to open texture view, it will become a huge tinmesaver.



  • Hi @mp5gosu, thank you for your feedback!
    Yep, it has Undo problem. I assume, it's because the script uses CallCommand. And there are 2 places where I still don't understand how to avoid this :(

    the selection should be restored after the process
    Thank you for this remark. I'm gonna fix it asap.
    Cheers.

    EDIT: I've fixed the script. Now it restores the selection after the process.



  • Hi,

    CallCommand() is exactly the same as a user choosing a command from the menu or clicking a palette button. So, indeed using CallCommand() most likely involves the creation of an undo step, and there no means to avoid. So, as soon as your code creates additional custom undo steps or makes use of CallCommand() multiple times, multiple undo steps are inevitable.

    The only workaround would be to do the work done in a command manually. For certain commands it may be easily achievable as the API provides functions toi achieve the same directly, for other (and unfortunately most) commands it will involve more work, up to the point of re-engineering the internals of one of our commands.

    Cheers,
    Andreas



  • Hello guys,
    I rewritten script UV2PhongEdges (weird name, I know :D) from scratch. Now it should works much faster. Also UNDO finally works, yyyeeeey!

    You can download it here
    https://github.com/rocketjump4d/UV2PhongEdges

    And here is an example
    https://vimeo.com/447775062

    Also, if you just need to get edges indexes of all UV borders you can use this code:

    import c4d
    from c4d import utils
    
    def GetUVBorders(obj):
        edgesIndexes = set()
        abcd = tuple("abcd")
    
        tuvw = obj.GetTag(c4d.Tuvw)
        nbr = utils.Neighbor()
        nbr.Init(obj)
    
        # Create empty set for `edgesVV`
        # In this case `edgeVV` means `Edge between Vertex0 and Vertex1 (edgeVertexVertex)`
        # edgeVV is just a tuple(v0, v1), where v0 is index of the first vertex
        # and v1 is the second one
        allEdgesVV = set()
    
        for i in xrange(obj.GetPointCount()):
            # Find neighbor vertex for this one
            neighborIndexes = nbr.GetPointOneRingPoints(i)
    
            for ni in neighborIndexes:
                edgeTuple = (i, ni)
                allEdgesVV.add(edgeTuple)
    
        # At this point I've got a set of all `edgesVV` of the object
        # Something like this:
        # (0, 3)
        # (0, 5)
        # (5, 1)
    
        for edgeVV in allEdgesVV:
            # Find neighbour polygons for this edge
            # I called them polyA and polyB
            polyAIndex, polyBIndex = nbr.GetEdgePolys(edgeVV[0], edgeVV[1])
            polyA = obj.GetPolygon(polyAIndex)
    
            if polyBIndex is c4d.NOTOK:
                # There is no polyB. It means that this edge is border of the object
    
                # eiA stands for `Edge Index in polyA for current edgeVV`
                eiA = polyAIndex * 4 + polyA.FindEdge(edgeVV[0], edgeVV[1])
                edgesIndexes.add(eiA)
                continue
    
            polyB = obj.GetPolygon(polyBIndex)
    
            # piA0 stands for `Point Index in polyA for vertex edgeVV[0]`
            # the same for others
            piA0 = polyA.Find(edgeVV[0])
            piA1 = polyA.Find(edgeVV[1])
            piB0 = polyB.Find(edgeVV[0])
            piB1 = polyB.Find(edgeVV[1])
    
            # Replace "d" (3) to "c" (2) if polygon is triangle
            if polyA.IsTriangle() and piA0 == 3:
                piA0 = 2
            if polyA.IsTriangle() and piA1 == 3:
                piA1 = 2
            if polyB.IsTriangle() and piB0 == 3:
                piB0 = 2
            if polyB.IsTriangle() and piB1 == 3:
                piB1 = 2
    
            # Get UV coordinates for each point in each polygon
            uvCoordA0 = tuvw.GetSlow(polyAIndex)[abcd[piA0]]
            uvCoordA1 = tuvw.GetSlow(polyAIndex)[abcd[piA1]]
            uvCoordB0 = tuvw.GetSlow(polyBIndex)[abcd[piB0]]
            uvCoordB1 = tuvw.GetSlow(polyBIndex)[abcd[piB1]]
    
            if uvCoordA0 != uvCoordB0 or uvCoordA1 != uvCoordB1:
                eiA = polyAIndex * 4 + polyA.FindEdge(edgeVV[0], edgeVV[1])
                eiB = polyBIndex * 4 + polyB.FindEdge(edgeVV[0], edgeVV[1])
                edgesIndexes.add(eiA)
                edgesIndexes.add(eiB)
    
        return edgesIndexes
    

    Let me know if you find bugs.
    Btw, sorry any mistakes. My English is... meh



  • Wow, very nice! There's a huge progress, it now works pretty fast.
    Thanks for adding the wishlist items. :)