Suppose I have a Component called “Rock”. I want to know how many instances of “Rock” exist in the model. The only two available API methods are count_instances and count_used_instances.
They will return the correct counts if a Rock is on it’s own in the model, or if the Rock is inside a group.
Rock
Group > Rock
But if there is further nesting, the count is wrong. For instance, a Rock inside another Component inside a Group will not be counted.
The count_used_instances method is used to count the total number of component instances in a model using this component definition. This method takes into account the full hierarchy of the model.
Please post sample model and sample code if you are getting unexpected results.
Are you sure you used #count_used_instances (the newer, smart method that checks the full hierarchy) as is written in your example code and not #count_instances? (the old method that just checks the number of instance objects)?
Try placing any of those component instances within another component and then make a few copies of that parent component. The number of instances of the parent component doesn’t affect the number of “actual” instances of the child component (child_component.instances.size or child_component.count_instances). However it affects the number of instances you see in the model and would expect to be listed in a report (child_component.count_used_instances).
Counting is not trivial. As Christina said, we have to (recursively) multiply with the parent’s number of instances and check whether the parent is a used component (that means whether it’s most upper parent is the model). Same considerations if you want to consider visibility as well.
Hmm… that should not be the case. count_instance traverse the model hierarchy tree and count all instance - so that will be an accurate figure of how many instances are placed in the model.
I meant that instances.count is the same as count_instances.
Christina explained it right. count_used_instances counts the actual amount of instances in the model even if some of them are in a duplicate parent instance.