AddMaterialToObject nukes usertext

G’day everyone,

A user has exposed my poor scripting skills. I’m just beginning the debugging and can’t see anything that looks to be responsible.

A surface has thickness usertext attributed to it, not attached to geometry so it should be resilient. A “property checker” confirms that the thickness (and other attributes) is assigned. Another script (below) changes the objects colour (rs.ObjectColor) based on the thickness attribute, but in the process nukes all the usertext attributes (thickness, name, etc…) of some of the surfaces.

While debugging, running this script once seems to work, nothing gets nuked and the colours get changed properly. But if I run it again straight away some of the parts are nuked, but not all of the ones with the same thickness. Actually, it looks like the usertext gets nuked on the first run, but is only apparent after the second run.

I have tried to avoid adding any usertext to geometry because I don’t want it to be volatile, but it may’ve slipped in over the intermittent years of development. But objectcolor change should not nuke geometry usertext.

It isn’t a particularly complex script.
GetObjects
Loop through the objects
GetUserText
AddMaterialToObject
ObjectColor
MaterialColor

Any thoughts?

import rhinoscriptsyntax as rs
import System
from System.Drawing import Color


def colourparts():
    
    strSurfIDs = rs.GetObjects("Please select surfaces to colour.", 24, True, True)
    if not strSurfIDs: return
    
    strAttribute = "Thickness"
    if not strAttribute: return
    
    
    for strSurfID in strSurfIDs:
        
        strValue = rs.GetUserText(strSurfID, strAttribute)      #Object Attribute Value
        
        index_Material = rs.AddMaterialToObject(strSurfID)	#In case viewport is rendered
        
        if strValue == "3":
            color = Color.LightCoral
        elif strValue == "4":
            color = Color.Green
        elif strValue == "5":
            color = Color.HotPink
        elif strValue == "6":
            color = Color.Olive
        elif strValue == "8":
            color = Color.Purple
        elif strValue == "10":
            color = Color.RoyalBlue
        elif strValue == "12":
            color = Color.DarkSeaGreen
        else:
            color = Color.Black
        
        objColour = rs.ObjectColor(strSurfID, color)
        matColour = rs.MaterialColor(index_Material, color)
        
    
if __name__ == '__main__':
    colourparts()

Coloured by layer

After first run of colour by thickness script (correct colour, but all usertext of affected parts is nuked).

After second run of colour by thickness script (incorrect, grey part has had all usertext nuked).

Why would the attached script nuke any usertext?

cheers,
Nick

With further investigation, it appears that the line…

index_Material = rs.AddMaterialToObject(strSurfID)

…is what nukes all the usertext from some objects…and I have no idea why. No idea why it nukes any usertext and no idea why it is only for some objects.

It appears this has been around awhile…

So I guess we have to deal with this ourselves…

I think this is something @andy might be able to help with.

Has this been resolved?