Check normals?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 11:26, xxxxxxxx wrote:

    I am trying to find anything in the Python C4DSDK about getting and checking the normals of a given polygon face. Is this possible? Would it then be possible to then assign that face to a polygon selection tag based on the direction it's normals are facing? 
    -A



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 11:34, xxxxxxxx wrote:

    You must calculate the normals yourself. Search in google for it, I don't know exactly how to do it.
    I.e. this: Surface normal - Wikipedia

    You can then check each normal if it is within a given "volume", well, if it is in the range you want it to be.

    Cheers,



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 12:04, xxxxxxxx wrote:

    the problem i am having is that i am sucessful in getting a list of CPolygon Objects, and iterating through them, but i cant seem to extract any information from them. in the SDK all it says i:

    c4d.CPolygon - a polygon class

    Structure which represents a polygon.
    See also
    Polygon Objects in Detail
    _ <_h2_>_Defini_<_h2_>_/h2> <_<_dt id="c4d.cpolygon"_>_111;n"> classc4d.CPolygon

    _/di_<_h2_>_h2>Members
    \__init\_\_( t_a , t_b , t_c [, t_d ])
    Parameters:|

    • t_a ( int ) – First point in the polygon.
    • t_b ( int ) – Second point in the polygon.
    • t_c ( int ) – Third point in the polygon.
    • t_d ( int ) – Optional fourth point in the polygon._/ul>
      ---|---

    and that's it... so how exactly does one get the matrix of a polygon face? thanks for all the help nux... again. i know your probably loosing patience with my noob ass, but the help is greatly appreciated.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 12:15, xxxxxxxx wrote:

    for i in dir(op.GetAllPoygons()[0]) :  
      print i
    

    and watch out for attributes like a,b,c or d. ;-)

    print op.GetPoint(polygon.a)
    

    Prints out the position of the point "a" of "polygon".

    maybe this gives you enough of a hint.
    Cheers,



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 12:18, xxxxxxxx wrote:

    ahhh, ok, so I have to access the point information. and from that get the normals. But then why does the SDK say:

    "Polygons in CINEMA 4D have their own coordinate system. You cannot see this system in the viewports, but it is important to be aware of it when using modeling tools.
    The origin of the polygon coordinate system is located at the centre of the polygons. The X axis is along the line between A and B. The Z axis is the normal. The Y axis is perpendicular to the XZ plane."
    I'll try to get the info I need from the point information, but it seems to be hinting that there is a way to access the Matrix coordinates of individual polygon faces.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 12:34, xxxxxxxx wrote:

    hmmmm... getting this error:

    AttributeError: 'c4d.CPolygon' object has no attribute 'GetPoint'

    this is the snippet of code i am using.

    def GetNormalFacing(op) :
      polys = op.GetAllPolygons()#get a list of polygons
      for poly in polys:
          print poly

    gives me:

    (a:0, b:1, c:2, d:3)

    but when i try to access the "a" for example:

    def GetNormalFacing(op) :
      polys = op.GetAllPolygons()#get a list of polygons
        for poly in polys:
            return poly.GetPoint(polygon.a)

    and then i try ti run it, i get an error saying that:

    AttributeError: 'c4d.CPolygon' object has no attribute 'GetPoint'

    This is essentially the problem i am having. CPolygon obejcts have no functions except __init__.

    GetPoint only seems to be for Point Objects. not CPolygon Objects.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 12:35, xxxxxxxx wrote:

    No, GetPoint is a Method of the PointObject - class.

    print op.GetPoint(polygon.a)
    
    You see ? op is a PointObject, 'polygon' is a CPolygon.  
      
    So, here is a better explanation:  
      
    CPolygon-objects hold indicies of Points in their attributes a,b,c and d.  
    Theese indieces represent the points of a PointObject (PolygonObject inherits from PointObject).  
    
    myPointIndexTakenFromCPolygon = myPolygonTakenFromPointOrPolygonObject.a  
        myPointCoordinate = myPointOrPolygonObject.GetPoint(myPointIndexTakenFromCPolygon)
    
    Ergo, a Polygon is defined by the indieces of points of an object.  
    It would be absolutely redundant saving the Vectors of the Points twice.  
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 12:47, xxxxxxxx wrote:

    f**king brilliant. it works like a charm. i just needed to replace your "polygon" with the name of my polygon. i didn't understand why i would have to access "op"  at first, but now i see that i HAVE to pass the GetPoint Function on my base object in order to use the fucntion and then use the CPolygon object to tell Python what points to get. awesome.  thanks again man. the working code is:

    def SelectWalls(op) :
      polys = op.GetAllPolygons()#Get a list of all available polygons
      for poly in polys:
          return op.GetPoint(poly.a)

    and it returns:

    Vector(-325, -331, -99)

    when i print. brilliant.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 13:18, xxxxxxxx wrote:

    Np. :)
    Just for final correction of your comment:

    Originally posted by xxxxxxxx

    polys = op.GetAllPolygons()#How many polygons does the object have in total?

    Does not (not only actually) return the number of polygons, it returns a list containing all polygons. And the length of the list ( len(polys) ) is the number of Polygons, logically.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/06/2011 at 13:34, xxxxxxxx wrote:

    haha, yeah sorry forgot to change the comment to say #Get All Polygons., I totally understand and will fix it.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/06/2011 at 02:55, xxxxxxxx wrote:

    In case you do search for an algorithm of how to calculate the Normal of a Polygon, here is a very good and easy to reproduce in Python - Pseudocode:

    http://www.opengl.org/wiki/Calculating_a_Surface_Normal

    Cheers,



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/06/2011 at 05:45, xxxxxxxx wrote:

    Btw. the cross-product is already implemented. So just use:

    normal = vec1.Cross(vec2)

    Be aware that switching the vectors, will switch the direction of the normal.

    Cheers,
    maxx



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/06/2011 at 06:06, xxxxxxxx wrote:

    But the normal of 2 vectors is not what he searched for. ;)
    Anyway, very useful ! Didn't know about that Method. Thanks



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/06/2011 at 08:08, xxxxxxxx wrote:

    Well, actually, it is.

    You just take the three points, create two vectors by substraction, calculate the normal by cross-product ... The result is the normal on the polygon-surface (defined by the three points).

    Cheers,
    maxx



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/06/2011 at 10:29, xxxxxxxx wrote:

    wow! thanks guys and gals! that openGL wiki is exactly what i was looking for. and the little snippet on how to get the cross product of two vectors is super helpful as well. thanks so much!



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/06/2011 at 11:38, xxxxxxxx wrote:

    ok, so thanks to everybody's help i was successfully able to calculate the face normals and find out which way the polygon is facing. It seems to be working so I Thought this might help some people out!

    def FindNormals(op) : #function to find direction of walls and assign to seperate selection tags
        polys = op.GetAllPolygons() #get list of polygons in op
        doc = c4d.documents.BaseDocument()
        i=0
        for poly in polys: #find normals

    pt_a = op.GetPoint(poly.a)
            pt_b = op.GetPoint(poly.b)
            pt_c = op.GetPoint(poly.c)
            pt_d = op.GetPoint(poly.d)
            vector1 = pt_a-pt_b
            vector2 = pt_b-pt_c
            normal = vector1.Cross(vector2)
            normal.Normalize()
            nx = normal.x
            ny = normal.y
            nz = normal.z
            if(nx!=0) :                                            #check x normal
                print "poly",i,poly,"is facing x to a degree of:",nx
                i+=1
            elif(ny!=0) :                                          #check y normal
                print "poly",i,poly,"is facing y to a degree of:",ny
                i+=1
            elif(nz!=0) :                                          #check z normal
                print "poly",i,poly,"is facing z to a degree of:",nz
                i+=1
            else:
                print "something went wrong"

    update: fixed some minor code output issues.


Log in to reply