Merging all polygon selectiontags per material on an object
-
Hello,
I can loop over all the materials on an object and store their respective objecttags, but I can't find any example or information on how to merge a list of selectiontags together via python and then assign those to their respective materials again. It doesn't seem super hard to do but I just can't find an example of someone doing something similar.
My endgoal would be to basically simplify this so it merges all the selectiontags per material for each object so I don't have to do this all by hand for thousands of objects:
Any help would be greatly appreciated.
Thanks!
-
Hello @GillesKontrol,
Thank you for reaching out to us. Jesus Christ, that is a lot of tags! While we do have quite a few topics on selections here on the forum, we have no dedicated Python code examples for selections on GitHub (we probably should change that).
Selections are merged with
BaseSelect.Merge
. But there are some hoops to jump through here, which is why I have written a code example. Although my example is probably pretty close to what you want, I must remind you that we do not provide solutions in the SDK group. Any modification or improvement must be done by yourself.You should also show your code in the future, no matter how little your progress has been.
Cheers,
FerdinandScene: select.c4d
Before:
After:
Code:
"""Consolidates material assignments on all selected objects. Run with at least one object selected. The script will merge all texture tags using a restriction, i.e., a selection tag, that reference the same material. The operation will be wrapped into one undo item. Note: As always, the SDK group does not provide finished solutions. If you want things to be changed, that would be up to you. Here are some short-comings. * I did not deal with the case that there is a material M referenced on an object both as a texture tag with and without a restriction. Does not make too much sense but I ignored it. * I also ignored differences in projections. The script assumes that all material assignments are just unmodified UVW projections. * Finally, I determine the "sameness" of materials by identity and not by (quasi)-equality. When there are two absolutely identical materials in a scene, or two materials which are only differentiated by their name, they and their tags will not be merged. """ import c4d import mxutils import time doc: c4d.documents.BaseDocument # The active document. def ConsolidateMaterialsOnObject(op: c4d.BaseObject) -> None: """Consolidates the texture tags and selection tags used by them on #op by reference of material. Note: In the API material tags are called texture tags. """ # The first thing we do, is build a map where we associate materials assigned to #op with the # texture tags of #op assigning them. I.e., we collect the texture tags which should be # consolidated as they are assigning the same material. # # NOTE: We use here a 2023.2 feature, C4DAtom.__hash__, to insert the materials as keys into the # dict. In earlier versions we would have to use node UUIDs for that. groups: dict[c4d.BaseMaterial, list[c4d.TextureTag]] = {} # Iterate over all texture tags in #op and store them under the material they assign. for tag in [t for t in op.GetTags() if t.CheckType(c4d.Ttexture)]: material: c4d.BaseMaterial = tag[c4d.TEXTURETAG_MATERIAL] groups[material] = groups.get(material, []) + [tag] # Now we iterate over our build mappings as pairs of a material and the texture tags which # assign it. for material, textureTags in groups.items(): # Step over materials which are assigned by less than two tags. if len(textureTags) < 2: continue # Now we pull all the selection tags of #op which are referenced in the texture tags. selectionTags: list[c4d.SelectionTag] = [] for tag in textureTags: # Since selection tags are referenced a bit weirdly as a string in texture tags, # we cannot just grab their base link and instead must search for them. restriction: str = tag[c4d.TEXTURETAG_RESTRICTION] candidates: list[c4d.SelectionTag] = [t for t in op.GetTags() if t.CheckType(c4d.Tpolygonselection) and t.GetName() == restriction] if len(candidates) != 1: raise RuntimeError( f"Found texture tag {tag} with ambiguous restriction '{restriction}'.") selectionTags.append(candidates[0]) # Now we have all the input data we need. The first thing we do is establish a unique name # for the new selection tag, the 'restriction' referenced in the texture tag, and then # create the selection tag. restriction: str = f"{material.GetName()}_{time.perf_counter_ns()}" selectionTag: c4d.SelectionTag = op.MakeTag(c4d.Tpolygonselection) selectionTag.SetName(restriction) doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, selectionTag) # Now we get the BaseSelect for our new selection tag and copy over all the selection states # of the selection tags we have collected. selection: c4d.BaseSelect = selectionTag.GetBaseSelect() res: bool = all([selection.Merge(t.GetBaseSelect()) for t in selectionTags]) if not res: raise RuntimeError(f"Copying a selection tag for {material} failed.") # We do not need the old selection and texture tags anymore and therefore remove them. for t in (selectionTags + textureTags): doc.AddUndo(c4d.UNDOTYPE_DELETE, t) t.Remove() # We create a new texture tag and reference the current material from the loop and our new # selection tag in it. textureTag: c4d.TextureTag = op.MakeTag(c4d.Ttexture) textureTag[c4d.TEXTURETAG_MATERIAL] = material textureTag[c4d.TEXTURETAG_RESTRICTION] = restriction doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, textureTag) def main() -> None: """ """ # Get all directly selected objects in the scene, bail when there is no selection. selection: list[c4d.BaseObject] = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_NONE) if not selection: print ("no objects selected to consolidate selection tags for.") return # Wrapped in an undo, iterate over them and compact the material assignments on them. doc.StartUndo() for obj in doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN): ConsolidateMaterialsOnObject(obj) doc.EndUndo() # Push an update event. c4d.EventAdd() if __name__ == "__main__": main()