Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Extraction of indexes out of range of safeframe

    Cinema 4D SDK
    windows python 2024
    2
    3
    577
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      seora
      last edited by seora

      Hi, I'm looking for codes and trying to combine them because I want to extract out-of-frame indexes for objects that are within the range of the safeframe. However, there's an issue where all indexes that are not out of range are extracted. How can I get an out-of-range index? I want a script that ultimately removes out-of-range vertices.
      Below is the code that is currently being written

      test.py

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @seora
        last edited by ferdinand

        Hey @seora,

        Thank you for reaching out to us. I get that you want to determine if a vertex lies within the safe frame, but I do not understand what your concrete question is. There is no such built-in function, you would have to write that yourself (if that is the question).

        Just cast a ray from the origin of the scene camera towards your vertex (in world coordinates) and check if it intersects with the safe frame. In BaseView you can find the necessary functions to convert points out of screen into world space. How to write a ray-rectangle intersection is out of scope of support as this is a basic math question.

        Cheers,
        Ferdinand

        Code

        """Warning: This is AI generated and documented code.
        """
        from c4d import Vector
        
        def ray_rectangle_intersection(ray_origin: Vector, ray_direction: Vector, rectangle_vertices: list[Vector]) -> tuple[bool, Vector]:
            """
            Determines if a ray intersects with a rectangle in 3D.
        
            Parameters:
            ray_origin (Vector): The origin of the ray.
            ray_direction (Vector): The direction of the ray.
            rectangle_vertices (list[Vector]): The vertices of the rectangle, ordered either clockwise or counter-clockwise.
        
            Returns:
            tuple[bool, Vector]: A tuple where the first element is a boolean indicating whether the ray intersects the rectangle,
                                 and the second element is the point of intersection if it does, or None otherwise.
            """
            # Compute the normal of the plane that the rectangle lies in
            edge1 = rectangle_vertices[1] - rectangle_vertices[0]
            edge2 = rectangle_vertices[3] - rectangle_vertices[0]
            rectangle_normal = edge1.Cross(edge2)
        
            # Use the ray-plane intersection formula to find the point where the ray intersects the plane
            t = (rectangle_vertices[0] - ray_origin).Dot(rectangle_normal) / ray_direction.Dot(rectangle_normal)
            intersection_point = ray_origin + t * ray_direction
        
            # Check if this point lies within the rectangle
            edge3 = rectangle_vertices[2] - rectangle_vertices[1]
            edge4 = rectangle_vertices[2] - rectangle_vertices[3]
            within_rectangle = (edge1.Cross(intersection_point - rectangle_vertices[0]).Dot(rectangle_normal) >= 0 and
                                edge2.Cross(intersection_point - rectangle_vertices[0]).Dot(rectangle_normal) >= 0 and
                                edge3.Cross(intersection_point - rectangle_vertices[1]).Dot(rectangle_normal) >= 0 and
                                edge4.Cross(intersection_point - rectangle_vertices[3]).Dot(rectangle_normal) >= 0)
        
            return within_rectangle, intersection_point if within_rectangle else None
        

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • S
          seora
          last edited by seora

          @ferdinand Thank you, I got the script I wanted using the answer code!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post