Rhino C++ Plugin : Weird issue when exporting materials?

Hi all !

So, I am making an exporter plugin to export a 3dm object into another file format.

And, I am exporting the materials using the material table and getting the material id, through the following code, which I discovered on this thread.

So, this is what I am doing to get the material index:

int GetMaterialIndex(CRhinoDoc& doc, const CRhinoObjectMesh& object_mesh)
{
	::RhinoApp().Print("\nGetting the material index");
	int material_index = -1;
	int layer_index = -1;

	switch (object_mesh.m_mesh_attributes.MaterialSource())
	{
	case ON::material_from_object:
		::RhinoApp().Print("\nMaterial is from - OBJECT");
		material_index = object_mesh.m_mesh_attributes.m_material_index;
		break;

	case ON::material_from_layer:
		::RhinoApp().Print("\nMaterial is from - LAYER");
		layer_index = object_mesh.m_mesh_attributes.m_layer_index;
		material_index = doc.m_layer_table[layer_index].m_material_index;
		break;

	case ON::material_from_parent:
	{
		::RhinoApp().Print("\nMaterial is from - PARENT");
		const CRhinoInstanceObject* iref_object = object_mesh.m_iref_object;
		if (iref_object)
		{
			switch (iref_object->Attributes().MaterialSource())
			{
			case ON::material_from_object:
				material_index = iref_object->Attributes().m_material_index;
				break;

			case ON::material_from_layer:
				layer_index = iref_object->Attributes().m_layer_index;
				material_index = doc.m_layer_table[layer_index].m_material_index;
				break;
			}
		}
	}
	break;
	
	default:
		::RhinoApp().Print("\n****NO MATERIAL IS FOUND FOR THIS OBJECT !!!!****");
		break;
	
	}

	return material_index;
}

What I’m having issues with is that, whenever an object has a material assigned to it by the layer (the Default material, to be precise), the material data doesn’t show up on my exported file and also I get the material index for it as -1.

This can be seen as,

This is an object in a rendered view in Rhino:

And, when I export the same object, its material is gone, like this:

So, this has raised some questions from me:

  1. Is the layer material not stored on the material table, especially the Default material, that has been assigned to this object, as seen in the screenshot?

  2. If the layer material is not in the material table, how do I get it?

  3. Also, an unrelated but a related question, how do I get the total count of materials that are present in the current document. (Is using the MaterialTable’s MaterialCount() the way to go?)

Thanks, and happy holidays to anyone who is reading this :slight_smile:

Instead of using the index of the material you should calculate a hash of the material properties that make up the material, then use the hash as the bringing factor.

The material index is problematic. To illustrate: create in Rhino material editor one material. Create five boxes. Assign the one material to each box.

You’ll find that the materials table has 5 entries. They all are actually the same material. But since the material is used 5 times it appears there five times.

This is where it is better to use the render materials. The class provides a function that calculates the hash for you. IIRC there is also no duplication as the Materials table in the RenderMaterials table.

1 Like

Oh, that explains all the duplicate material values I had been having in my exported file !

Thanks, @jesterking, this makes it clear to me about the differences between using material and render materials.

Thank you a lot ! :slight_smile:

Hi, thank you for enlightening me from your answer. I also have one more question left, if you have the time, could you please take a look at it?

Problems with normals in Rhino

Thank you, @jesterking

Answered (:

1 Like

Thank you for being so awesome!