Get object by active tag in Python
-
Hi,
I am able to retrieve a list of all selected tags in scene via doc.GetActiveTags(), but now I need to get a list of objects linked to those active tags and because objects are not selected themselves, doc.GetActiveObject() gives you Null. Is it somehow possible in Python? I guess the must be some BaseLink relation between selected tag and its object, just could not find any real world example.
Thanks for any help!
-
BaseTag.GetObject(self)
-
Cool, thanks!
-
Hello @stanDM,
thank you for reaching out to us. And thank you @Cairyn for providing an answer. There is not much to add for us here, except for making the answer a bit more formal.
Polyhierarchical scene graph information is being expressed as branches in Cinema 4D and accessible through GeListNode.GetBranchInfo(). To 'move up one level' in such polyhierarchical relation, there is the general purpose method BaseList2D.GetMain() which will return the object for a tag, the material for a shader, etc. There is also the convenience method BaseTag.GetObject() of
BaseTag
which returns the object the tag is attached to. It will return the same entity asBaseList2D.GetMain()
. Find below a short example for a Python Programming Tag.Cheers,
FerdinandThe output:
op = <c4d.BaseTag object called Python/Python with ID 1022749 at 2768059772160> obj = <c4d.BaseObject object called Cube/Cube with ID 5159 at 2768059772416> branchedFrom =<c4d.BaseObject object called Cube/Cube with ID 5159 at 2768059771904> (obj == branchedFrom) = True >>>
The code:
"""Example for retrieving the object a tag is attached to. This example is designed for a Python Programming Tag. The module attribute `op` is predefined in this context as the `BaseTag` that is the Programming Tag calling this Python module. """ import c4d def main(): """Called on tag evaluation. """ print (f"{op = }") # Get the attached object. obj = op.GetObject() if obj is None: raise RuntimeError(f"Illegal scene state for: {op}") print (f"{obj = }") # The more general method to retrieve the node another node did branch # from. The referenced object is exactly the same as for `op.GetObject()`. branchedFrom = op.GetMain() if branchedFrom is None: raise RuntimeError(f"Illegal scene state for: {op}") print (f"{branchedFrom =}") print (f"{(obj == branchedFrom) = }")
-
Hello @stanDM,
without any further questions or postings, we will consider this thread as solved by Friday the 4th, February 2022.
Thank you for your understanding,
Ferdinand