Counting Component Instances Returns Incorrect Number?

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.
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.

Group > ArbitraryComponent > Rock

Example code:

wanted_component = 'Rock'

Will I need to write my own recursive loop to find every instance of a Component? Or is there another method?

It seems to work OK for me. Can you post a model showing the problem?

Works as expected for me too. Could you have accidentally made your test component unique?

No, count_used_instances is the one you want.

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.

This also works.




This would only count an instance once even if its parent components has multiple instances.

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)?

Could you demonstrate this? According to my tests:

mycomponent.definition == Sketchup.active_model.definitions[mycomponent] = true

and mydefinition.instances returns an array of all the used instances.

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).


Ah. I see what you mean. The effect is the same with count_instances.

Resolved the issue, thanks everyone. An issue with the model I’d imported was the problem, the Ruby API is working correctly with count_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.

1 Like

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.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.