[ create a new paste ] login | about

Link: http://codepad.org/DpEBjT7O    [ raw code | fork ]

Python, pasted on Feb 21:
import c4d

# --- connect & delete all objects of the current selection, which have the same material
# --- assigned to them. objects which have multiple materials assigend to them will be skipped.
# --- objects with multiple material tags holding the same material will be connected.
# ------------------------------------------------------------------------------------------------
def main():
    selection = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
    dic = buildDict(selection)
    for key in dic:
        connect(dic[key]) 

# --- connect & delete a object list
# ------------------------------------------------------------------------------------------------
def connect(objlist):
    if len(objlist) > 1:
        for i in xrange(len(objlist)):
            if i == 0:
                doc.SetActiveObject(objlist[i], c4d.SELECTION_NEW)
            else:
                doc.SetActiveObject(objlist[i], c4d.SELECTION_ADD)
        c4d.CallCommand(16768)

# --- get material used by this baseobjct. returns none for multiple materials.
# --> BaseMaterial | None
# ------------------------------------------------------------------------------------------------
def getMaterial(obj):
    material = None
    if isinstance(obj, c4d.BaseObject):
        taglist  = obj.GetTags()
        for tag in taglist:
            if tag.GetType() == c4d.Ttexture:
                # single tag
                if material == None:
                    material = tag.GetMaterial()
                # multiple tags of the same material
                elif material == tag.GetMaterial():
                    pass
                # multiple tags
                else: return None
    return material

# --- build a dict of all materials in the given selection. each key contains a list
# --- with the objects which carry only this material in their texturetag(s).
# --> dict
# ------------------------------------------------------------------------------------------------
def buildDict(selection):
    result = {}
    for objA in selection:
        matA = getMaterial(objA)
        for objB in selection:
            if objA is not objB:
                matB = getMaterial(objB)
                if matA == matB and matA and matB:
                    mkey = matA.GetName()
                    if mkey in result:
                        if objA not in result[mkey]:
                            result[mkey].append(objA)
                        if objB not in result[mkey]:
                            result[mkey].append(objB)
                    else:
                        result[mkey] = []
                        result[mkey].append(objA)
                        result[mkey].append(objB)
    return result

if __name__=='__main__':
    main()


Create a new paste based on this one


Comments: