Material Assignments doesn't update when changing TextureTag Material

Python
Cinema 4D R23.110
Windows 10 latest

I have written a simple back-converter to change Octane Materials back to Cinema 4D materials, for export or for members of our team who don't have Octane.
It basically works by creating a new Cinema 4D Material, setting it up & then changing the link in the existing Texture Tag from the Octane material to the new Cinema 4D material.
It all works fine, except that after the script has run, the 'Assign' tab of the new Cinema 4D material doesn't show the objects & texture tags to which it is assigned. This makes me worry that I'm missing some kind of message or initialisation step.
Here's the bit of code that's relevant:

def ReAssign(doc, oct_mat, c4d_mat):                                              #Assigns the new Cinema 4D material to the texture tags
    obj_link = oct_mat[c4d.ID_MATERIALASSIGNMENTS]                                #Get the link list for the Octane Material's assignment
    link_count = obj_link.GetObjectCount()                                        #Get how many objects are in the link list
    for i in range(link_count):                                                   #For each of them...
        tex_tag = obj_link.ObjectFromIndex(doc, i)                                #Get the texture tag
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, tex_tag)                                 #Add an undo for the tex tag change
        tex_tag[c4d.TEXTURETAG_MATERIAL] = c4d_mat                                #Replace the Octane Material with the Cinema 4D material
        tex_tag.Message(c4d.MSG_CHANGE)                                           #update the tex tag
        c4d_mat.Message(c4d.MSG_CHANGE)                                           #update the c4d material

Any pointers on what I'm missing/ best practice ?

That's weird.
Maybe your Cinema 4D material isn't inserted into the document before running ReAssign?
Here, in R20 it works. I used this dummy code:

import c4d

def ReAssign(doc, oct_mat, c4d_mat):                                              #Assigns the new Cinema 4D material to the texture tags
    obj_link = oct_mat[c4d.ID_MATERIALASSIGNMENTS]                                #Get the link list for the Octane Material's assignment
    link_count = obj_link.GetObjectCount()                                        #Get how many objects are in the link list
    for i in range(link_count):                                                   #For each of them...
        tex_tag = obj_link.ObjectFromIndex(doc, i)                                #Get the texture tag
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, tex_tag)                                 #Add an undo for the tex tag change
        tex_tag[c4d.TEXTURETAG_MATERIAL] = c4d_mat                                #Replace the Octane Material with the Cinema 4D material
        tex_tag.Message(c4d.MSG_CHANGE)                                           #update the tex tag
        c4d_mat.Message(c4d.MSG_CHANGE)                                           #update the c4d material

def main():
    mats = doc.GetMaterials()
    for mat in mats:
        mat_new = c4d.BaseMaterial(c4d.Mmaterial)
        doc.InsertMaterial(mat_new, None, False)
        ReAssign(doc, mat, mat_new)

# Execute main()
if __name__=='__main__':
    main()

Thanks for your reply. You're right I was inserting it afterwards.
Is it best to insert the material to the document immediately after creation before I start to change anything ?
It's working correctly now.

@Graeme said in Material Assignments doesn't update when changing TextureTag Material:

Is it best to insert the material to the document immediately after creation before I start to change anything

You don't have to, but it usually is a good idea, before setting anything document-related stuff, like Tag assignments.
The reason here is that, if you assign a material to a tag, they must be part of the same document. Since you can have multiple documents open (in editor or memory), Cinema must know on which one to operate on.

hi,

thanks @mp5gosu for the answer.

About inserting material before making some changes, there's no real right way of doing it. It just that if something go wrong about the change you are doing, you have to remove the material from the document, while if you don't insert it, you don't have to.

As @mp5gosu said it's better to insert the object in the document before linking them. Even if should work if you insert the material after.

Cheers,
Manuel