Jump to content

Sign in to follow this  
Anthony Cavel

PYTHON_merge all duplicated materials

Recommended Posts

Hello everyone

I'm trying to write a script that selects and merges the shaders wich have the same "MATERIAL_COLOR_SHADER"

For the moment I have this, and it allows me to merge only manually selected shaders:

 

import c4d

def ():
        mats = doc.GetActiveMaterials ()
        list_tags = list ()


        for mat in mats:
            obj_link = mat [c4d.ID_MATERIALASSIGNMENTS]


            for i in xrange (obj_link.GetObjectCount ()):
                list_tags.append (obj_link.ObjectFromIndex (doc, i))


        doc.StartUndo ()
        for tag in list_tags:
            doc.AddUndo (c4d.UNDOTYPE_CHANGE, tag)
            tag [c4d.TEXTURETAG_MATERIAL] = mats [0]


        for mat in mats [1:]:
            doc.AddUndo (c4d.UNDOTYPE_DELETE, matt)
            mat.Remove ()


        doc.EndUndo ()
        c4d.EventAdd ()


if __name __ == '__ main__':
    main ()

Now I have no idea of  what I could write more to find and select the duplicates materials. This scripts would be very useful when I import fbx characters from DAZ. There are always 1000 duplicated shaders to merge, and it takes time. Maybe a script already exists, but I have not found anything yet.

Thanks,
Anthony
 

Share this post


Link to post
Share on other sites

Which is the part your'e having trouble with?

Just being able to go through all the materials in the document rather than a selection?

mat = doc.GetFirstMaterial()
while (mat != None):
    do something to mat
    mat = mat.GetNext()



Or being able to automatically determine which materials are the same?  In which case you need to determine what you mean by the same, if it's everything i.e. color, settings etc, then you should be able to collect all of them and compare the contents of their BaseContainers.  If it's just something like the name must be the same then you can compare the individual parameters.  In each case push the matches into an array/list then apply the function you've already written to eliminate the duplicates.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...