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
    • Register
    • Login

    How to gracefully obtain the number of targets in a constraint tag?

    Cinema 4D SDK
    python 2024 windows
    2
    3
    315
    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.
    • O
      Oliver
      last edited by

      Hello all, I need to dynamically obtain the number of Targets in a Constraint tag. I couldn't find a 'Constraint tag' class, and the BaseTag class offers very limited functionality. I had to try retrieving the information from the container. Fortunately, the container contains the data I need. However, I'm curious if there's a more elegant solution beyond using the container?

      import c4d
      
      def getTargetCount(tag) -> int:
         tagData    = tag.GetDataInstance()
         count      = 0
         startIndex = 10002
      
         while True:
             if tagData.GetData(startIndex) is None:
                 break
             count      += 1
             startIndex += 10
      
         return count
      
      def main():
         tag = doc.GetSelection()[0]
         print(getTargetCount(tag))
         
      
      if __name__ == '__main__':
         main()
      

      253c5d95a5a61cbe08ddf2aa13e790e.png

      i_mazlovI 1 Reply Last reply Reply Quote 0
      • i_mazlovI
        i_mazlov @Oliver
        last edited by

        Hi @Oliver,

        No, Constraint tag creates its description dynamically, so there's no easy way to count targets. Your solution looks like pretty much it. The 10 increment is not exposed in our API, hence you need to define it yourself in your code. The startIndex you can retrieve from the tcaconstraint.h File Reference, namely
        ID_CA_CONSTRAINT_TAG_PSR_TARGET_COUNT = 10000 for Transform,
        ID_CA_CONSTRAINT_TAG_AIM_TARGET_COUNT = 20000 for Parent
        etc.

        There's no shortcut for it, your solution is good enough πŸ‘

        Cheers,
        Ilia

        MAXON SDK Specialist
        developers.maxon.net

        O 1 Reply Last reply Reply Quote 1
        • O
          Oliver @i_mazlov
          last edited by

          @i_mazlov This is very useful as it helps avoid manually inputting hard-coded index values! Thank you, and have a great day! πŸ™‚

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