Notifications
Clear all

[Closed] CustomAttributes Virtual Array Questions:

Hey all.

Has anyone tried using the .custAttributes node property before? The docs claim it’s the object’s customAttribute virtual array, but it does not seem to work as such. It’s rather strange really.

Using the custAttributes.add member function attaches the attribute to the supplied object. However, I would expect then for the .custAttributes virtual array property to show the attribute as an item. It does not. You can at this point copy the applied custAttribute to the object’s custAttributes vitual array using most standard array methods.

Here’s an example:


--Let's say we make a custAttribute definition:

----------------------------
def = attributes testDef
(
	parameters params 
	(
		test type:#float 
	)
)
----------------------------
 
theBox = box()					--Then, we create a box.

custAttributes.add theBox def			--Then add the CA to the box

print theBox.test 				--Now we query the "test" value in the box's testDef custAttribute
-->this returns 0.0

print theBox.custAttributes			--Now we query the custAttributes virtual array for the box

--this returns #CustAttribs(), an empty array.	
	
theAppliedDef = custAttributes.get theBox 1	--Now we grab the def that we applied to the box

append theBox.custAttributes theAppliedDef 	--And then add it to the box's custAttributes virtual array

print theBox.custAttributes			--Now we query the custAttributes virtual array for the box again

--this now returns #CustAttribs(testDef:testDef), showing that the def is now in the virtual array
						
custAttributes.delete theBox 1		            --Now we delete the def that we added using custAttributes.add

print theBox.test 				--Now we again query the "test" value in the box's testDef custAttribute
-->this still returns 0.0, which means maxscript can still "see" the attribute def that now exists in the virtual array

So all seems to work, though unexpectedly. The ramifications of using this vitual array seem dangerous. The custAttributes member functions for accessing an objects’s CAs won’t find them. For example, using custAttributes.getDefs theBox returns an empty array, and custAttributes.count theBox returns 0.

I was excited at first to learn of this property, as I’ve been looking for a way to access the defs applied to an object by index, rather than name, since my definitions are created programmatically based on user interaction, and since accessing the defs by name requires parsing and executing strings, which won’t work for the performance requirements of a system I’m working on, and feels very hacky.

Does anyone have experience using this virtual array, or perhaps a comment on what the best practices might be for using it. Maybe there is a way to access the customAttributes of an object by index that I am not aware of.

Thanks for the lengthy read, and look forward to your response(s).

-Dave Black

2 Replies

Well, it seems I’ve found the answer.

Say we just added a custAttribute to an box using the custAttributes.add function.

If we type $.custAttributes in the listener, the array that is returned will be empty.

This appears to be why:

Using the indexed method to reach the subAnimation properties of the object we do


$[4].custAttributes

or using the subAnimName method:


$[#Object__Box].custAttributes

Accessing this virtual array will return the attributes attached to the object in the way that makes sense.

EDIT: Also note that the above index value and subAnimName will vary from node to node

This is the true virtual array into which customAttributes are edited using the custAttributes member functions.

I’m posting this for posterity, lest some poor soul need the info.

-Dave

Thank you for posting this Dave. It’s really useful for something I’m working on.

Cheers

Dwida