SOLVED How to get Redshift material assignments

Hello,
I'm trying to get the material assignments for Redshift materials but I'm getting inconsistent results.

The code is:

material = doc.GetFirstMaterial()
while material:
    assignment_list = material[c4d.ID_MATERIALASSIGNMENTS]
    print (material.GetName(), assignment_list.GetObjectCount())
    material = material.GetNext()

Initially the assignment list shows the correct number of objects. But as soon as you interact the material, e.g. select it or rename it, then it shows 1 extra number of objects:

c5a89d40-cb50-4584-9760-64480691f3a6-image.png

Not sure how should I get the correct assignment list consistently, does anyone have any ideas?

Here is an example file for convenience:
reshift_isolate.c4d

Hi Orestis,

This look like a bug that only happens with the old Redshift materials. Standard and the NodeMaterials are not affected by this issue. The problem is that the number come back to 1 or 0 if you save the file and re-open it so you can't always remove one form GetObjectCount.

The only workaround that i have for you is to check if there is a "None" object in the list.
I'm asking the devs if i missed anything and i will come back if i have more information.

import c4d
#Welcome to the world of Python


def main():
    material = doc.GetFirstMaterial()
    while material:
        assignment_list = material[c4d.ID_MATERIALASSIGNMENTS]
        nb = assignment_list.GetObjectCount()

        numberOfChild = 0
        for x in range(0, nb):
            obj = assignment_list.ObjectFromIndex(doc, x)
            if obj is not None:
                numberOfChild += 1
        print (material.GetName(), nb, numberOfChild)

        material = material.GetNext()
    print("")

Cheers,
Manuel

Thanks @m_magalhaes for looking it up, and writing a workaround, much appreciated!

hi,

I got some feedback from the devs. The function ObjectFromIndex have a parameter to define the document. That mean the list can contain material tags from a different document. The "problem" is that ObjectFromIndex return the number of tags in the list, and not only the number of tags in the list that are present in the current document. Old Redshift materials have a mechanism to optimized and avoid rendering the preview thumbnail on the attribut manager when it is not necessary. That mechanism renders the material on a different document. That is why the entry on the list is "None" because the tag is not present in the document passed to the function ObjectFromIndex.

Cheers,
Manuel