How to get Faces Aligned with a Direction and Located at the "Top" of the Body

Mike.Thompson
Mike.Thompson Member, Employee Posts: 327
25 Answers First Anniversary 100 Comments 25 Likes
✭✭✭✭
edited November 2023 in 3D Design

This example is how to get faces aligned with a direction and also located on the "top" of the body in that direction. You can see the type of faces in the image, aligned with the Z Direction. Notice one of the 3 blocks does not have a face selected because it it not oriented in line with the Z axis.
This is useful often when wanting to select these faces as cutters or to pull for some reason.
An example SC geometry file with script group is attached.

Tagged:

Comments

  • Shunya Hashimoto
    Shunya Hashimoto Member Posts: 1
    First Comment
    **

    Hi
    I want to select faces by specific directions.So, I thinked i can use your method. But when i open and run your、 I had an error "attribute 'V20' of 'namespace#' object is read-only".

    Do you have any solution?

    Also, I read your script. I think your write about materials. How did you select "Top" face? If you can, Could you teach the method?

    BR

  • Takahiro Yamaguchi
    Takahiro Yamaguchi Member, Employee Posts: 12
    First Comment Name Dropper
    ✭✭✭

    @Mike.Thompson Can you taka a look into Hashimoro-san's question?

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 327
    25 Answers First Anniversary 100 Comments 25 Likes
    ✭✭✭✭
    edited November 2023

    @Takahiro Yamaguchi , I have updated the original post to correct the attachment. The code is also posted below:

    # Python Script, API Version = V20
    
    """
    Get the top face in the Z Direction
    """
    
    UserDirection = Direction.DirZ
    DotTolerance = 0.9999
    
    TopFaces=[]
    for B in GetRootPart().GetAllBodies():
        ZFaces = []
        for F in B.Faces:
            Norm = F.GetFaceNormal(0,0)
            Dot = Vector.Dot(Norm.UnitVector, UserDirection.UnitVector) 
            if Dot > DotTolerance:
                Z = F.GetFacePoint(0,0)[2]
                ZFaces.append([F,Z])
        if len(ZFaces)>0:
            ZFaces.sort(key=lambda V: V[1])
            TopFaces.append(ZFaces[-1][0])
    Selection.Create(TopFaces).SetActive()
    
    
  • Takahiro Yamaguchi
    Takahiro Yamaguchi Member, Employee Posts: 12
    First Comment Name Dropper
    ✭✭✭

    @Mike.Thompson Thank you! It was works fine to me.

  • Takahiro Yamaguchi
    Takahiro Yamaguchi Member, Employee Posts: 12
    First Comment Name Dropper
    ✭✭✭

    @Mike.Thompson If I want to select the X direction, what should I change?

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 327
    25 Answers First Anniversary 100 Comments 25 Likes
    ✭✭✭✭

    At the top of the script is: UserDirection = Direction.DirZ

    You can change this to UserDirection = Direction.DirX, or you can specify any arbitrary direction with XYZ like Direction.Create(0.5,0.8,1.596)