How to use RenderMaterial in Python script

I have never worked with RenderMaterials before. And am totally lost. So please help me if you can. I have worked with old-style materials and get those to show up fine on the pieces trimmed from a larger mesh.

For this case with a Render Material the mesh, in Rendered view, looks like:


It uses a RenderMaterials I believe:

I am trimming the mesh into smaller pieces being careful to retain the texture coordinates for all the mesh vertices. After trimming, the number of vertices and texture coordinates are equal just like when I was working with textures referencing old-style materials. But no texture is displayed on the trimmed mesh.

So somehow I need to apply the material to this trimmed mesh? I got the material used by the starting mesh with:

from scriptcontext import doc
rms = [x for x in doc.RenderMaterials]
if len(rms) > 0:
rm = rms[0]
source_mat = rm

If I print out source_mat I get:

<Rhino.Render.NativeRenderMaterial object at 0x0000000000000089 [Rhino.Render.NativeRenderMaterial]>

If I print out len(rms) I get 1, so there is only 1 RenderMaterials in the starting mesh (actually doc).

I tried to follow a suggestion on the forum to apply this material using:

	    dmat = DisplayMaterial(RenderMaterial.SimulatedMaterial(source_mat,True))
		doc.RenderMaterials.Add(dmat)

but this fails with the message:

Message: ‘type’ object has no attribute ‘SimulatedMaterial’

The logic of these steps makes no sense to me. Taking a material from the doc and then putting it back in the doc cannot make any difference to the trimmed mesh created from the larger starting mesh. So I am obviously lost.

When I work with old Materials I can get this to work with a difference set of commands. But for RenderMaterials I am lost as to how to get the textures in the trimmed mesh to show the RenderMaterials. Help!

Regards,
Terry.

The answer is to do none of the above. Once the mesh has been split with its texture coordinates intact, I can just go to the layer I put the split mesh on and assign its material to the one shown above. This brings all of the textures back making for a perfect looking result.

1 Like