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();
Willem
(Willem Derks)
May 8, 2021, 10:18pm
2
Hi
This is a nasty one:
attr = rhino_object.Attributes
should be
attr = rhino_object.Attributes.Duplicate()
Hi,
I found that after running rhinoscriptcontext.AddMaterialToObject(object_id) to add a material to an object, all it’s Usertexts are purged:
import rhinoscriptsyntax as rs
id = rs.AddLine([0,0,0] , [1,1,1])
rs.SetUserText(id, 'foo', 'bar')
print 'foo value : {}'.format(rs.GetUserText(id, 'foo'))
rs.AddMaterialToObject(id)
print 'foo value : {}'.format(rs.GetUserText(id, 'foo'))
printout:
foo value : bar
foo value : None
Edit @Dale I found the cause in the reply by @clement
the r…
2 Likes
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)
Willem
(Willem Derks)
May 11, 2021, 6:10am
4
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
Willem Derks:
.Duplicate()
Just had to deal with this one. Thanks a bunch!