RhinoCommon - Programmatically added material is duplicated when added to another object


I work with RhinoCommon and C#.

I want to add some default materials to a document when I open it, such that they appear in the materials list and can be used. I got it working with this code from here http://developer.rhino3d.com/samples/rhinocommon/addmaterial/ and the materials appear in the list as expected.

However when I drag the material onto any object in the scene the material is duplicated (It’s duplicated once, independent from how many objects I drag it onto). This happens with every material I added programmatically.

I saw this post about duplicated materials but closing and reopening Rhino didn’t work for me: Material Editor creates duplicates of my materials

My questions are:

  • Is this expected behaviour? How can I prevent it?
  • Are there other ways to add default materials on opening a document. Maybe loading from a file or as a “global” material (as I understand all materials are bound to a certain document).

Thank you!

Edit: Some more information:

The material (a mirror material with black diffuse color and 100% reflectivity) produces incorrect rendering results. The reflected scene appears like a big glitch. This is corrected when I manually set the reflectivity to another value. However, the material is duplicated again after I changed it.

Hi Chris,

Are you using Rhino for Windows or Rhino for Mac?

– Dale

Hi Dale!

I’m using Rhino for Windows. My goal is to develop a plugin that adds some default materials when a file is loaded. A user should be able to assign these materials to objects in the scene.

For this I implemented the code from this example http://developer.rhino3d.com/samples/rhinocommon/addmaterial/ and created a hidden sphere for each material.

I’m new to Rhino so maybe I don’t quite understand how materials work in here. From my current understanding a material is part of the RhinoDoc and can only ‘exist’ when its assigned to something. But why does it duplicate when I assign a programmatically added material to another object?

A solution would be to track the material IDs I added and remove materials that got duplicated, but I rather not implement some workaround when there’s a proper solution to this problem.

Hi Chris,

There are couple of better approaches to adding default “stuff” to a document.

First, you can create template files. These are what you see when you run the New command. Template files can hold pretty much anything you want (layers, linetypes, layouts, geometry, etc.). To create a Template file, just save your document to the Templates folder.

For materials specifically, a better approach is to make sure the materials you like and use frequently are part of your material library, which is external to Rhino. This way, your materials can be quickly dragged into your current document. The advantage of this is that you can share your materials with your colleagues, plus you don’t pollute your documents with stuff that you aren’t using. To see what’s available in your material library, click Panels -> Libraries.

Yes, this is expected. If you don’t want every object to have a material, then assign materials “by layer” instead of “by object.”

Hope this helps.

– Dale

Thank you! This helps a lot.

For the materials I will definitely follow the approach with the material library. This is exactly what I need. And I will look into templates to see if I can use them for my project.