Hi all,
In a python script I’m trying to retrieve the renderMaterial of an object, but it seems I’m not doing it right. This returns an error:
import Rhino
objTypesFilter = Rhino.DocObjects.ObjectType.Brep & Rhino.DocObjects.ObjectType.Mesh
rc, objRef = Rhino.Input.RhinoGet.GetOneObject( "Select object", False, objTypesFilter )
obj = objRef.Object()
renderMaterial = obj.Attributes.RenderMaterial
In my defense the error is not super helpful:
I’m testing this script on the most simple object possible:
sample.3dm (65.6 KB)
My Rhino version:
Rhino 8 SR8 2024-6-11 (Rhino 8, 8.8.24163.12481, Git hash:master @ ded4d86ca901dbeaf13ee84f79b3e91b22da4880)
What is the right way to get the renderMaterial of a RhinoObject in RhinoCommon ?
pascal
(Pascal Golay)
June 20, 2024, 10:19pm
2
Hello- something like this perhaps
import Rhino
import scriptcontext as sc
objTypesFilter = Rhino.DocObjects.ObjectType.Brep & Rhino.DocObjects.ObjectType.Mesh
rc, objRef = Rhino.Input.RhinoGet.GetOneObject( "Select object", False, objTypesFilter )
mats = sc.doc.Materials
obj = objRef.Object()
attrs = obj.Attributes
idx = attrs.MaterialIndex
if idx != -1:
renderMaterial = mats[idx]
print renderMaterial.Name
-Pascal
Thank you @pascal
Your script above works nicely to get the material from the object in the 3dm file I sent in my first message.
If I try again on the same object in a scene where there are other objects, I get a new error:
Here is the scene: bla.3dm (415.7 KB)
The object that worked-but-now-doesn’t is this one in red:
doesn’t RhinoObject.Attributes.MaterialIndex
return the index of the Material
in sc.doc.Materials
rather than the RenderMaterial
in sc.doc.RenderMaterials
?
nathanletwory
(Nathan 'jesterKing' Letwory)
June 20, 2024, 10:50pm
7
@felix.mariotto RenderMaterial is directly on the RhinoObject ( https://developer.rhino3d.com/api/rhinocommon/rhino.docobjects.rhinoobject/rendermaterial?version=8.x# )
import Rhino
objTypesFilter = Rhino.DocObjects.ObjectType.Brep & Rhino.DocObjects.ObjectType.Mesh
rc, objRef = Rhino.Input.RhinoGet.GetOneObject( "Select object", False, objTypesFilter )
obj = objRef.Object()
renderMaterial = obj.RenderMaterial
print(renderMaterial)
2 Likes
Ah great, thank you both a lot !
nathanletwory
(Nathan 'jesterKing' Letwory)
June 20, 2024, 11:06pm
10
By the way, when obj.RenderMaterial
gives you None
then no material has been assigned, and you should use the default material. This is just an instance of the default plaster material. Here a small update with default material usage:
#! python3
import Rhino
import Rhino.Render
defaultRenderMaterial = Rhino.Render.RenderContentType.NewContentFromTypeId(Rhino.Render.ContentUuids.PlasterMaterialType)
objTypesFilter = Rhino.DocObjects.ObjectType.Brep & Rhino.DocObjects.ObjectType.Mesh
rc, objRef = Rhino.Input.RhinoGet.GetOneObject( "Select object", False, objTypesFilter )
obj = objRef.Object()
renderMaterial = obj.RenderMaterial
if not renderMaterial:
renderMaterial = defaultRenderMaterial
print(renderMaterial)
1 Like
@nathanletwory thank you for the precision !