Notifications
Clear all

[Closed] Layer name change callback / event

Is there any way to detect a layer name change?
I couldn’t seem to find any callbacks for it, and this didn’t seem to work either:


layer =(layermanager.getLayer 1).layerAsRefTarg;
when name layer changes id:#test theLayer do (
 print theLayer;
)

7 Replies

hrm… short of a timer… doesn’t look like it… when <parameters> etc. doesn’t catch the name change either (had to try).

What’s the purpose, though? Perhaps there’s another method to get what you want

I want to use it for the layer mode in the outliner tool I’m working on;
http://forums.cgsociety.org/showthread.php?f=6&t=567637&page=1

A timer could be a workaround, but should really be a last resort I think, it’s a seriously nasty one

might be a necessary evil… thought that maybe a node within a layer would trigger some event if you changed the name of the layer it’s in – but alas. Not that that would really help much

Yeah, even though it’s a rather ugly way when it comes to programming, it shouldn’t affect the performance of max too much… or at least so I hope.
Calling a layer name refresh method two or three times per second should be enough.

My next problem however, is how to identify layers. In the outliner I use the inode handle property of objects, but there doesn’t appear to be a constant identifier for layers…
I’d need this when renaming layers.

layers are required to have unique names so using their names should do fine.

The unique names only help up to a point…

Specifically with regards to renaming them – as there is no event, let’s say you have the layers: layer1, layer2, layer3
and somebody renames to: layer1, layerA, layerB, you can’t track which changed to what with -only- that information.
On the up side – most name changes are likely to be through the UI, and thus relatively slow, so within the interval of a timer.

But let’s say it’s not done through the UI, but via another script. Or, let’s say the layer name is changed outside of the workflow of the tool Pjanssen is creating; then you’re still stuck. Could tell the user “don’t do that”, but if there’s a way to get a unique ID from a layer somehow – that’d be good.

Sadly, it doesn’t have any unique IDs. However, you can give it one yourself. The .layerAsRefTarg variant will happily accept setAppData as well as custAttributes. Getting a unique ID would then merely be a question of just how unique you want it. GenClassID() is supposed to be fairly unique (given that Autodesk doesn’t want clashing plugin classIDs; although it has happened once iirc, but that may have been a copy/paste incident), so something like…


str = ""; for int in (join (genClassID returnvalue:true) (genClassID returnvalue:true)) do ( str += (bit.intAsHex int) + "-" ); subString str 1 (str.count - 1)
"3055b7f8-9580218f-5516d5fd-831f9c0"

…should be pretty unique.

Your tool should then implement setting up this ID when a new layer is created (this does have a callback – thank goodness), and when the tool is first fired up on any given scene (to check if there’s any layers without the ID data, and then set that up).

@Richard: Thanks a lot for your help, I greatly appreciate it!

I just implemented the timer to detect if any layers have been renamed. To keep the speed up, or rather, to not slow it down too much, I keep an array with the layer names, and compare the actual names of layers to that. So only when a layer name has changed a dotnet function will be called to rename the node accordingly.
When no changes occur, the method takes less than a milisecond, which is very acceptable. When a name change does occur it takes about 15 miliseconds. Not fast, but still acceptable.

As for the problems with the (changing) names of layers and the outliner tool, it’s mainly a problem of keeping track of the nodes. Keeping the connection between max and the nodes in the dotnet control. That’s why I use the inode handle for objects; I’ll always be able to find the right node with that integer.
However, I just noticed that the Name property of a node can actually be set. That, in combination with the no-duplicate names rule for layers, means I might not need to keep a custom ID and what not. It’d surely save me a lot of trouble.