Ferdinand, thanks for the reply.
In my case I need to programmatically select particular polygons to apply different materials to, I cannot rely on an interactive selection.
So as to not involve my custom code, which relies on numpy, etc, the example code below tries to set the material for the one half of the polygons in a mesh that is brought in as 'Default' object using File->Merge Project.
I drag the object from the Objects panel into the python console, and assign it to the variable 'op' to match your examples.
The code where I select the first 50% polygons clearly works, I see one half of the mesh selected if I view in 'select polygon' mode. (The code for programmatic selection was suggested by this thread : https://developers.maxon.net/forum/topic/13194/polygon-islands-convenience-method)
The final result - apparently nothing happens. If I leave my programmatic selection active, right-click on the material I inserted, and choose to 'Apply', the action takes place and half of the mesh has the new color.
What am I still missing?
Thanks, Randy
# Ferdinand, the following line is created by dragging the imported object into the python session so I get a reference to
# it; I assign it to the variable 'op' to match your code
op = Default
kwargs = {"command": c4d.MCOMMAND_SELECTALL,"list": [op],"mode": c4d.MODELINGCOMMANDMODE_POLYGONSELECTION,"bc": c4d.BaseContainer(),"doc": doc}
c4d.utils.SendModelingCommand(**kwargs)
polySelection = op.GetPolygonS()
polySelection.DeselectAll()
count = op.GetPolygonCount()
for i in range(count) :
if i < count/2 :
polySelection.Select(i)
## the above 'works' in the sense that I see half of the polygons selected if I'm in face selection mode
if not c4d.utils.SendModelingCommand(c4d.MCOMMAND_GENERATESELECTION, [op], c4d.MODELINGCOMMANDMODE_POLYGONSELECTION, c4d.BaseContainer(), op.GetDocument(), c4d.MODELINGCOMMANDFLAGS_NONE):
raise RuntimeError("Could not create polygon selection.")
selectionTagCollection: list[c4d.BaseTag] = [t for t in op.GetTags() if t.CheckType(c4d.Tpolygonselection)]
selectionTag: c4d.BaseTag = selectionTagCollection[-1]
bs = selectionTag.GetBaseSelect()
print('# selected = %d of %d' % (bs.GetCount(),count))
# selected = 24998 of 49996
material: c4d.Material = c4d.BaseMaterial(c4d.Mmaterial)
textureTag: c4d.BaseTag = op.MakeTag(c4d.Ttexture)
# Insert the material into the document of #op, set its color to a random color, and reference
# both the material and the selection in the texture tags. Selections references in texture tags
# are a bit weird as in that they work over strings and not BaseLinks. So we pass the name of
# our selection tag.
op.GetDocument().InsertMaterial(material)
# try to make half the mesh blue
material[c4d.MATERIAL_COLOR_COLOR] = c4d.Vector(0., 0., 1.)
textureTag[c4d.TEXTURETAG_MATERIAL] = material
textureTag[c4d.TEXTURETAG_RESTRICTION] = selectionTag.GetName()
c4d.EventAdd()
# nothing happens