Python: How to assign attributes to existing objects?


sorry, me again. :wink: Can someone show me the way to assign attributes(with material) to existing objects in Python?

# materials are stored in the document's material table
index = scriptcontext.doc.Materials.Add()
mat = scriptcontext.doc.Materials[index]
mat.DiffuseColor = System.Drawing.Color.Chocolate

# set up object attributes to say they use a specific material
attr = Rhino.DocObjects.ObjectAttributes()
attr.MaterialIndex = index
attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject

objects = rhinoscriptsyntax.LastCreatedObjects();
# how to assign attr to objects?



Here is how you’d do this using rhinoscriptsyntax:

import rhinoscriptsyntax as rs
obj = rs.GetObject("Select object")
if obj:
    index = rs.ObjectMaterialIndex(obj)
    if index == -1:
        index = rs.AddMaterialToObject(obj)
    rs.MaterialColor(index, (127, 255, 191))
1 Like

Hi Dale,

thank you for this. My problem is still that the material is not saved in the 3dm-file and I don’t get the color under properties/material.

I think I also have to save the material in the document, but don’t know how to do it. Can you show me how to add the material with your index to the doc?



I guess I’m confused. After I run the script the select object now renders (kinda) green. And when I select the object and pick ObjectProperties > Materials, I can see the material. What am I missing?

Hi Dale,

I need AddMaterialToObject for a pictureFrame object.

I have tested now a normal surface, which works as expected. My pictureFrame gets the diffuse color with rs.AddMaterialToObject, but it’s not saved and seen in the properties/material window.

# works
rs.Command('_Plane 0 100 50')

# doesn't work
rs.Command('_PictureFrame "C:\\tmp\\picture.png" _AutoName=_No _AlphaTransparency=_Yes 0 100 100,0')

object = rs.LastCreatedObjects()[0];

if object:
    index = rs.ObjectMaterialIndex(object)
    if index == -1:
        index = rs.AddMaterialToObject(object)
    rs.MaterialColor(index, (255, 0, 0))

Is this a bug, or me? :wink:

Thank you


@andy, i’ve now invested one hour to find out how to disable the diffuse lightning toggle in a material with no result. Is this possible at all ?

I’ve created a rhino material, got its index, then created a basic render material using

rh_mat = scriptcontext.doc.Materials[index]
render_mat = Rhino.Render.RenderMaterial.CreateBasicMaterial(rh_mat)

next i found out this string:

some_string = render_mat.BasicMaterialParameterNames.DisableLighting

and tried to use it:

context = Rhino.Render.RenderContent.ChangeContexts.UI
rc = render_mat.SetParameter("diffuse-lighting", False, context)

however, rc is always False. I wonder if there is a way to access all these default UI elements in V6 and i am getting quite confused when dealing with materials, rendermaterials, child slots, child slot names, parameters, fields etc. Is there an easy way to add a material in Rhino via python which is visually equivalent to a picture frame, so it just has a texture and no diffuse lightning ? I guess this is what @Michael_Meyer is trying to do.



to me rs.AddMaterialToObject() doesn’t work for a pictureFrame object, so now I create alternatively a surface by my own, add rs.MaterialColor, mat.SetBitmapTexture, and mat.SetTransparencyTexture, which works nicely.

Hi @dale, also I’ve noticed that rs.AddMaterialToObject(object) deletes all UserTexts of my object, which I have added with rs.SetUserText(object, ‘marker’, ‘color’),… This looks buggy to me.

Hi @clement, thank you for your investigations. I also would like to know how to switch on/off these material options (Textures Color, Textures Transparency,…, Enable diffuse lighting (which will be set on, when I do rs.AddMaterialToObject()).

Thank you anyway for your great help and Rhinos wonderful script tools.

Have a sunny weekend


1 Like