Render Material events

HI all,
to which event should I suscribe to handle “additions” and “deletions” to RenderMaterial table (materials not applied to document objects) and also, which event for handling RenderMaterial modifications (color changes, name changes…) ? I cant figure this out… Tried with RenderMaterialTableEvent, with MaterialTabeEvent and with RenderContent events (ContentAdded ) but no luck…

thanks

Hi @aitorleceta, are you looking for this?

https://developer.rhino3d.com/api/RhinoCommon/html/E_Rhino_RhinoDoc_MaterialTableEvent.htm

Of course, you need to check event args for that:

https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_DocObjects_Tables_MaterialTableEventArgs.htm

So you need to get this Material using args like MaterialTable[args.Index] and check equality with OldSettings but you need to define own rules for that.

RenderMaterial is derivative of RenderContent and both are abstract what means that you need to implement those on your own (i guess you don’t want) to have own render materials.

Would you mind telling what you want to achieve? Maybe I will be able to help :wink:

1 Like

FYI, the material table is about materials assigned to objects. It does not contain materials not assigned.Furthermore the MaterialTable lists all assignments. As such the same material appears as many times as it has been assigned.

This should work:

  public class TestRenderContentEvents : Command
  {
    public TestRenderContentEvents()
    {
    }
    protected static bool registered = false;

    public override string EnglishName => "TestRenderContentEvents";

    protected override Result RunCommand(RhinoDoc doc, Rhino.Commands.RunMode mode)
    {
      if (!registered)
      {
        RenderContent.ContentAdded += ContentAdded;
      }
      else {
        RenderContent.ContentAdded -= ContentAdded;
      }
      registered = !registered;

      return Result.Success;
    }
    
    private void ContentAdded(object src, Rhino.Render.RenderContentEventArgs args)
    {
      RhinoApp.WriteLine($"Content added {args.Content.Name}");
    }
  }
2 Likes

Oh boy, I was typing the wrong namespace for “ContentAdded”!!! too much hours in front of the screen i guess…
I’m developing on ghpython here and caching event error is particularly tricky, as the they pass along silently. Do you guy has any trick to catch exception on events?
Anyway, thank you very much both @nathanletwory and @D-W !!

this is for a “kinda” plugin written in GhPython. I need the user to be able to select materials not assigned to document elements (renderMaterials), that the reason MaterialTableEvent was not the event I was looking for…

If you just need a list of material names you can use

import Rhino as r

rmmats = [rm.Name for rm in r.RhinoDoc.ActiveDoc.RenderMaterials]

materialNames = rmmats

(there is probably a better way to get at the associated rhino doc)

@nathanletwory
yes, that is how I reach them in the first term. Now my plugin need to maintain that list, so the need of events. I’m using mainly names and colors. The colors are to emulate complex render materials (with complex i mean, with reflection maps, bumps…) with display conduit, because… there is no way to use directly a RenderMaterial / Rhino.RhinoDoc.Material as “display material” that I need to pass to the Display Pipelin method DrawMeshShaded(Mesh, DisplayMaterial) , right?
I mean, right now I’m using Simulate() to get a docObject.Material from RenderMaterial, and then extracting the diffuse color from that “simulated” material to , finally, generate a DisplayMaterial, a poor version of my awsome renderMaterial in order to apply them to my geometry (no doc object) on screen. The idea is that the render material will be applied when my geometry is finally “baked” to the document.
Is it there a better method to accomplish this material representation/preview trick?
Hope I explained myself…

If you want to have good preview of your geometry in a viewport then I’d suggest using the Rendered mode and putting your geometry in the Custom Preview component and assign the material by name to it.

See for instance How to render/set tinted glass material in Grasshopper?

That way you don’t have to do your own geometry drawing and you can just use Rendered mode to view your geometry using the materials in the Rhino document. This works also for Raytraced.

Not sure how it would work with other display modes, though.

1 Like

@nathanletwory Thanks for being on top of things. Docs aren’t mentioning about it - actualy MaterialTable is missing the description.

@aitorleceta just was trying to help :wink:

1 Like