Bug in object attributesin RhinoCommon

When I use the script it Ungroups the grouped objects and deletes all Usertexts? Is this a bug?

def ChangeMaterial(matIndex, ObjectID):
    attr = Rhino.DocObjects.ObjectAttributes()
    attr.MaterialIndex = matIndex
    attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject
    attrChange = scriptcontext.doc.Objects.ModifyAttributes(ObjectID, attr, True)
    scriptcontext.doc.Views.Redraw();

Hi

This is a nasty one:

attr = rhino_object.Attributes

should be

attr = rhino_object.Attributes.Duplicate()
1 Like

Hi Willem,

My script woks without Dipplicate().

def ChangeMaterial(materialIndex, objectTo):
    rhino_object = rs.coercerhinoobject(objectTo, True, True)
    attr = rhino_object.Attributes
    attr.MaterialIndex = materialIndex
    attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject
    attrChange = scriptcontext.doc.Objects.ModifyAttributes(objectTo, attr, True)

It does not work without coerce:

def ChangeMaterial(matIndex, ObjectID):
    attr = Rhino.DocObjects.ObjectAttributes()
    attr.MaterialIndex = matIndex
    attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject
    attrChange = scriptcontext.doc.Objects.ModifyAttributes(ObjectID, attr, True)

Hi

I only now see that in this second example you do not take the attributes from the input object but rather create new “default” attributes. That will cause any non defaults such as layer grouping and usertext to be oberwritten.

Does that make sense?
-Willem

Sure it is, thank you.