I wrote a script that does a simple check up if the selected object is used in any Link or InExcludeData field trough the scene (objects, tags, nodes..), so you can debug a messy project and find out which objects are important or not.
I never updated it to include Field list, so I come here for help since I am lost.
The code to crawl trough is as follows
for value in bc:
if type(value) == c4d.InExcludeData:
for i in range(0, value.GetObjectCount()):
if value.ObjectFromIndex(doc, i) == op:
if value == op:
I figured out I have to check
if type(value) == c4d.FieldList:
But how do iterate trough it and check each objects like with inexcludedata?
Thanks for help!
FieldLists are despite their name actually trees. To walk a FieldList you have to get the associated GeListHead and then traverse the graph of that head. But there are multiple special cases and scenarios of how the objects in this graph can be organized. You should read the docs on that. Below is a simple example on how to traverse the graph of a field list.
Note that the returned objects of get_field_layers() are FieldLayers, not the BaseList2Ds you see in your object manager. You can get the associated objects with FieldLayer.GetLinkedObject(doc), but not all layers have an associated BaseList2D.
Also: please don't type check with type()
""" Returns all field layers that are referenced in a field list.
""" Listifies a GeListNode tree.
res = 
for child in node.GetChildren():
res += flatten_tree(child)
node = node.GetNext()
# get the GeListHead for the FieldList
root = op.GetLayersRoot()
if root is None:
# Get the first node under the GeListHead
first = root.GetFirst()
if first is None:
# traverse the graph
fieldlists = [value for id, value in op.GetData()
if isinstance(value, c4d.FieldList)]
if not fieldlists:
for layer in get_field_layers(fieldlists):
print layer.GetLinkedObject(doc), "\n\n"
First, for your next threads, please help us keeping things organised and clean. I know it's not your priority but it really simplify our work here.
As @zipit answer, i got nothing really to add here. Thanks for your answer.
For more information you can have a look at our FieldList Manual and you will find information for python on this page c4d.FieldList
thanks you so much for this, works like a charm.
It only fails on Text and MoText object, I think it is because "font" attribute, it is returning error "AttributeError: Parameter value not accessible (object unknown in Python)"
Any idea how to get around this?
Edit: Found solution here: https://plugincafe.maxon.net/topic/10248/13738_iterate-attributes/5
Manuel, thanks for pointing me to that, will follow it from now on np.
p.s. sorry about type() I usually copy paste code until it works and that works
I am glad that it works for you. But I want to point out again that my function is only an example and might not work on all scenarios of field list setups. And on the type() thing, I just saw it quite often recently and it is well known Python pitfall. The problem is that type() does not take polymorphism into account (by design). It is meant to test if an objects is exactly a type. isinstance() and issubclass() are more appropriate choices for type checks.
This is especially important in c4d due to its heavily object oriented nature and that you rarely deal with base types.
Here is an example:
from timeit import timeit
op = c4d.BaseList2D(c4d.Ocube)
cmp_type = lambda : type(op) == c4d.BaseList2D
cmp_isin = lambda : isinstance(op, c4d.BaseList2D)
print "op is of type BaseList2D:", cmp_type() # False: whoops!
print "op ininstance of BaseList2D:", cmp_isin() # True
# And it also peforms almost the same
print "Average execution time of type: ", timeit(cmp_type), "usec"
print "Average execution time of isinstance: ", timeit(cmp_isin), "usec"
op is of type BaseList2D: False
op isinstance of BaseList2D: True
Average execution time of type: 0.403712 usec
Average execution time of isinstance: 0.5232223 usec
I've forked the question to this thread