Willem
(Willem Derks)
June 3, 2019, 12:05pm
1
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 rhinoscript method fails to create a Duplicate of the attributes:
attr = rhino_object.Attributes
should be
attr = rhino_object.Attributes.Duplicate()
obj = rs.GetObject()
if obj:
index = rs.ObjectMaterialIndex(obj)
if index==-1: index = rs.AddMaterialToObject(obj)
See Also:
IsMaterialDefault
ObjectMaterialIndex
ObjectMaterialSource
"""
rhino_object = rhutil.coercerhinoobject(object_id, True, True)
attr = rhino_object.Attributes
if attr.MaterialSource!=Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject:
attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject
scriptcontext.doc.Objects.ModifyAttributes(rhino_object, attr, True)
attr = rhino_object.Attributes
material_index = attr.MaterialIndex
if material_index>-1: return material_index
material_index = scriptcontext.doc.Materials.Add()
attr.MaterialIndex = material_index
scriptcontext.doc.Objects.ModifyAttributes(rhino_object, attr, True)
return material_index
For then the code works as expected
-Willem
1 Like
Hi @Willem , thats a strange one. I think that the object itself gets not properly updated. If i use the exact same code as rs.AddMaterialToObject(id)
in a seperate function, it works.
def AddMaterialToObjectEx(object_id):
rhino_object = rs.coercerhinoobject(object_id, True, True)
attr = rhino_object.Attributes.Duplicate()
if attr.MaterialSource != Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject:
attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject
scriptcontext.doc.Objects.ModifyAttributes(rhino_object, attr, True)
attr = rhino_object.Attributes
material_index = attr.MaterialIndex
if material_index>-1: return material_index
material_index = scriptcontext.doc.Materials.Add()
attr.MaterialIndex = material_index
scriptcontext.doc.Objects.ModifyAttributes(rhino_object, attr, True)
return material_index
prints this:
foo value : bar
foo value : bar
_
c.
3 Likes
I also tried this without Duplucates();
def AddMaterialToObjectEx(object_id):
rhino_object = rs.coercerhinoobject(object_id, True, True)
attr = rhino_object.Attributes
if attr.MaterialSource != Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject:
attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject
# scriptcontext.doc.Objects.ModifyAttributes(rhino_object, attr, True)
# attr = rhino_object.Attributes
material_index = attr.MaterialIndex
if material_index>-1: return material_index
material_index = scriptcontext.doc.Materials.Add()
attr.MaterialIndex = material_index
scriptcontext.doc.Objects.ModifyAttributes(rhino_object, attr, True)
return material_index