Notifications
Clear all

[Closed] Fastest way to check for Custom Attribrute

I’ve been using isProperty for forever to check for the existence of a specific custom attribute, but I was wondering if there was a faster way.

In a scene with 1,000 objects, looping through them using isProperty takes .25 seconds, and using hasProperty only takes .1 seconds. The problem, though, is that hasProperty doesn’t check for custom attributes, just the default object properties.

If calling theObject.theCA returned undefined or false instead of throwing an error, that seems like it would be faster. But having to wrap it in a try/catch is only slightly slower than isProperty (.28 seconds).

I’m trying to check hundreds of objects in a scene and keep some sort of real time feedback as a user moves another object around in it.

6 Replies

try/catch? or check to see which CAs do exist on an object using the CA management functions?

Perhaps adding a ‘hasProp’ boolean attribute (set to ‘false’) to the objects that are missing the property is an option? You can evaluate the “hasProp” directly, because you know that it exists on all of the objects.

theCAtts = attributes CAtts
    (
    	parameters main
    	(
    		hasProp type:#boolean default:true
    		prop type:#string
    	)
    )
    
    theCAttsNoProp = attributes CAttsNoProp
    (
    	parameters main
    	(
    		hasProp type:#boolean default:false
    	)
    )
    
    -- Create a set of boxes
    boxes = for i = 1 to 1000 collect (box())
    
    -- Half of them have the property
    for i = 1 to 1000 by 2 do 
    (
    	custAttributes.add boxes[i] theCAtts
    )
    
    -- Half do not
    for b in boxes where not isProperty b "valid" do 
    (
    	custAttributes.add b theCAttsNoProp
    )
    
    start = timeStamp()
    objs1 = for i in boxes where isProperty i "prop" collect i
    end = timeStamp()
    format "Processing checking for property took % seconds
" ((end - start) / 1000.0)
    
    start = timeStamp()
    objs2 = for i in boxes where i.hasProp collect i
    end = timeStamp()
    format "Processing testing 'hasProp' took % seconds
" ((end - start) / 1000.0)
    
    

My Results:
Processing checking for property took 0.062 seconds
Processing testing ‘hasProp’ took 0.016 seconds

 PEN

Something like this?

for d in (custAttributes.getSceneDefs()) do print d.name

Thanks. Creating a function along the lines of

for i = 1 to custAttributes.count obj do
(
	if (custAttributes.get obj i).name == "testCA" then true else false
)

is as fast as using hasProperty.

Thanks for the info, James.

 PEN

I have one that loops through all nodes in a scene and it doesn’t take that long considering. It even looks on each controller. So just doing objects shouldn’t be bad at all.