Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python 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

    Get points within a given radius

    Cinema 4D SDK
    3
    4
    820
    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.
    • orestiskonO
      orestiskon
      last edited by m_adam

      Hi all,
      is there a python function to get the points of an object within a given radius of a defined position?
      I can iterate through all the points but this can get costly.

      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by m_adam

        Hi Orestis,

        Unfortunately, all helpers classes available in C++ such as KDTree, DistanceQueryInterface or either VoxelizationInterface are not available in Python. So the only reliable way is to iterates over all the points like so.

        import c4d
        import sys
        
        # Main function
        def main():
            start = time.time()
            if not op: return
            if not isinstance(op, c4d.PointObject): return
        
            # WorldPosition to Match
            posToMatch = c4d.Vector(25, -13, -88)
        
            # Retrieves points position in World Position
            opMat = op.GetMg()
            pts = [pt * opMat for pt in op.GetAllPoints()]
            if not pts: return
          
            # Iterates overs all list to find the closest point
            nearestDist = sys.float_info.max
            ptId = None
            for x, pt in enumerate(pts):
                dist = (pt - posToMatch).GetLength()
                if dist >= nearestDist: continue
                
                nearestDist = dist
                ptId = x
            
            print nearestDist, ptId
        
        
        # Execute main()
        if __name__=='__main__':
            main()
        

        I have also tested to do some threading stuff, but due to the GIL in python, this does not bring any performance gain (even worse).
        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 1
        • orestiskonO
          orestiskon
          last edited by

          Thanks Maxime, that clears it out.

          1 Reply Last reply Reply Quote 0
          • M
            mp5gosu
            last edited by mp5gosu

            You may want to consider to use scipy.
            You have to do indexing once, but can the efficiently iterate over the points within a desired radius with the functions that come with the module.

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