Layers checker

Hello guys! Is it real to check everything in layer (objects, materials, tags) and delete layer if its empty?

Hello @roman,

thank you for reaching out to us. I am assuming that your question is if it is possible to determine if a layer has 'content' attached to it or not.

Yes, that is possible. It is however more complicated than you might think. Layers are not a data structure that holds scene data but are an attribute that is assigned to scene elements via the ID_LAYER_LINK parameter. So, to determine if a layer "is empty" or not, you must traverse all elements of a scene and check if they are part of that layer not. This is computationally not ideal since you would traverse the scene repeatedly for each layer. You should therefore traverse it once and collect all the layers which are in use and then delete the unused ones. You cannot store simply the base link referenced for that in a ID_LAYER_LINK (at least when you want it to be very robust) but must store the MAXON_CREATOR_ID unique id of the linked layers.

There is also the problem that many things can be attached to layers, not only objects, materials, and tags, but also shaders and animation tracks for example (and there is probably some stuff I am overlooking). For that you will need a robust scene traversal method which is currently neither provided in the Python or C++ API. You must write your own one, by either just selecting a few cases or by doing it with GeListNode.GetBranchInfo() which is in both cases a non-trivial task, as you must avoid recursion-based stack overflows or in Python the recursion limit of 1000 preventing them.

There were some posts in the past which will help you, as for example this one which contains code for a simplistic NodeIterator which will yield all objects and tags of a scene when you pass the first object in a document to it, and this which talks about and contains code for retrieving the objects attached to a layer. But for the generic form you are requiring, there are no examples yet. We cannot write this for you, please produce your own code and we will help you along the way when you encounter problems.

Cheers,
Ferdinand

Beginner solution:
If you do not need a certain layer you could also implement the call comand "Delete unused layers"
Its not pretty but beats any convulated self written code if it is not called to often.

c4d.CallCommand(100004760) # Remove unused layers

cheers
mogh

@mogh I didn't know about this function 🤦
@ferdinand Thank you a lot for your detailed response. I will chek posts which you recommended