How to gracefully obtain the number of targets in a constraint tag?
-
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()
-
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 -
@i_mazlov This is very useful as it helps avoid manually inputting hard-coded index values! Thank you, and have a great day!