Material for Plugin objects

Hi everyone,

I want to add material to my plugin objects to show colors also on the Rendered display mode. I simply tried below code;

public void SetAttributes(ObjectAttributes attributes)
{
     // Assign Object Color
     attributes.ObjectColor = System.Drawing.Color.Red;
     attributes.ColorSource = ObjectColorSource.ColorFromObject;

     // Introduce Material
     Material material = new Material();
     material.AmbientColor = System.Drawing.Color.Red;
     material.Name = "myBeautifulMaterial";

     // Add material to doc
     int indexOfMaterial = RhinoDoc.ActiveDoc.Materials.Add(material);

     // Assign Object Material
     attributes.MaterialIndex = indexOfMaterial;
     attributes.MaterialSource = ObjectMaterialSource.MaterialFromObject;
}

When I create my object with this Attributes, material is creating as Custom type. But I want to define as Paint type with RGB values as it is for ObjectColor. Is it possible with Material object because there is no any propery for Type under the Material object, or should I use something different?

Thanks in advance,
-Oğuzhan

No any idea? :confused: @dale @stevebaer

@nathanletwory - is this something you can help with?

You can create a specific type of material and assign it to objects

import System
import Rhino.Display
import Rhino.Render
import scriptcontext
import random

# get object selection
object_selection = [ob for ob in scriptcontext.doc.Objects if ob.IsSelected]

# create material
render_material = Rhino.Render.RenderContent.Create(
    Rhino.Render.RenderMaterial.PaintMaterialGuid,
    Rhino.Render.RenderContent.ShowContentChooserFlags.None,
    scriptcontext.doc)
render_material.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
render_material.Name = "PYSCRIPTCREATED " + System.Guid.NewGuid().ToString()
random_color = Rhino.Display.Color4f(
    random.random(),
    random.random(),
    random.random(),
    1.0)
render_material.SetParameter("color", random_color)
render_material.EndChange()

# assign
for ob in object_selection:
    print("Adding material", render_material.Name, "to", ob)
    ob.RenderMaterial = render_material
    ob.CommitChanges()

On a side note I wrote this using my literate programming extension. That tool is still work in progress, as is the page where I intend to host this type of script. Styling and such is still very much WIP.

The literate document is rhipy/create_specific_rendermaterial.literate at master · jesterKing/rhipy · GitHub . With my vscode extension from this were created the Python code above and the rendered HTML page: https://jesterking.github.io/rhipy/create_specific_rendermaterial.html.

2 Likes

@nathanletwory, Thanks for your suggestion, I will try to implement this with C# then will update topic about status.

-Best

@nathanletwory,

Thanks for your suggestion it worked! With C#, it needed to explicitly cast RenderContent to RenderMaterial before assinging to RhinoObject. Example code;

// Create render content
RenderContent renderContent = Rhino.Render.RenderContent.Create(
    Rhino.Render.RenderMaterial.PaintMaterialGuid, 
    RenderContent.ShowContentChooserFlags.None, 
    RhinoDoc.ActiveDoc);

// Begin changes..
renderContent.BeginChange(RenderContent.ChangeContexts.Program);

renderContent.Name = "sampleRenderContent";
System.Drawing.Color color = System.Drawing.Color.Red;
Rhino.Display.Color4f color = new Rhino.Display.Color4f(color);
renderContent .SetParameter("Color", color);

// ..End changes
renderContent .EndChange();

// Add any brep to RhinoDoc
Guid guid = RhinoDoc.ActiveDoc.Objects.AddBrep(Brep);

// Get RhinoObject that is created after to addition by guid
RhinoObject obj = RhinoDoc.ActiveDoc.Objects.FindId(guid);

// Cast explicitly renderContent to RenderMaterial object
obj.RenderMaterial = (RenderMaterial)renderContent;
obj.CommitChanges();

Best,
-Oğuzhan

1 Like