Converting layer colors to material colors

I’ve seen a couple of very old posts about converting layer colors to material colors and I’ve even found this script, but I don’t know how to apply it. I have tried using the “paste from clipboard” command to no avail. Here’s the script:

Option Explicit

Sub SetMaterialColorsFromObjectColors

’ Constants
Const rhColorByLayer = 0
Const rhColorByObject = 1

’ Variables
Dim aObjects, sObject
Dim nColor, nSource
Dim sLayer, nMaterial

’ Get all objects in the document
aObjects = Rhino.AllObjects
If Not IsArray(aObjects) Then Exit Sub

’ Process each object
For Each sObject In aObjects

' Get the object's color and color source
nColor = Rhino.ObjectColor(sObject)
nSource = Rhino.ObjectColorSource(sObject)
nMaterial = -1
' If the object's color source is "by layer"
' then get the layer's material index. If the
' layer does not have a material, add one.    
If (nSource = rhColorByLayer) Then
  sLayer = Rhino.ObjectLayer(sObject)
  nMaterial = Rhino.LayerMaterialIndex(sLayer)
  If( nMaterial < 0 ) Then
    nMaterial = Rhino.AddMaterialToLayer(sLayer)
  End If
' If the object's color source is "by object"
' then get the object's material index. If the
' object does not have a material, add one.    
ElseIf (nSource = rhColorByObject) Then
  nMaterial = Rhino.ObjectMaterialIndex(sObject)
  If( nMaterial < 0 ) Then
    nMaterial = Rhino.AddMaterialToObject(sObject)
  End If
End If
' Set the material color
If (nMaterial >= 0) Then
  If (nColor <> Rhino.MaterialColor(nMaterial)) Then
    Rhino.MaterialColor nMaterial, nColor
  End If
End If

Next

’ Redraw the document
Rhino.Redraw

End Sub

I’m brand new to Rhino. I’m trying to take very complex IGES files and convert them into smaller FBX files, but the embedded colors are assigned to layers instead of materials, so the FBX loses that data. Is there a simple way to convert the colors to materials?

Hi,

  • Create a new button (hold ctrl + dragging an other button)

Paste this:

!-_runscript (



    Option Explicit

    Sub SetMaterialColorsFromObjectColors

    ' Constants
    Const rhColorByLayer = 0
    Const rhColorByObject = 1

    ' Variables
    Dim aObjects, sObject
    Dim nColor, nSource
    Dim sLayer, nMaterial

    ' Get all objects in the document
    aObjects = Rhino.AllObjects
    If Not IsArray(aObjects) Then Exit Sub

    ' Process each object
    For Each sObject In aObjects

    ' Get the object's color and color source
    nColor = Rhino.ObjectColor(sObject)
    nSource = Rhino.ObjectColorSource(sObject)
    nMaterial = -1

    ' If the object's color source is "by layer"
    ' then get the layer's material index. If the
    ' layer does not have a material, add one.    
    If (nSource = rhColorByLayer) Then
      sLayer = Rhino.ObjectLayer(sObject)
      nMaterial = Rhino.LayerMaterialIndex(sLayer)
      If( nMaterial < 0 ) Then
        nMaterial = Rhino.AddMaterialToLayer(sLayer)
      End If

    ' If the object's color source is "by object"
    ' then get the object's material index. If the
    ' object does not have a material, add one.    
    ElseIf (nSource = rhColorByObject) Then
      nMaterial = Rhino.ObjectMaterialIndex(sObject)
      If( nMaterial < 0 ) Then
        nMaterial = Rhino.AddMaterialToObject(sObject)
      End If

    End If

    ' Set the material color
    If (nMaterial >= 0) Then
      If (nColor <> Rhino.MaterialColor(nMaterial)) Then
        Rhino.MaterialColor nMaterial, nColor
      End If
    End If

    Next

    ' Redraw the document
    Rhino.Redraw

    End Sub
SetMaterialColorsFromObjectColors
)

The problem is that this is a script and rhino doesnt recognize it if you copy paste it. So Runscript should be added :slight_smile:

2 Likes

Rhino for Mac does not have RhinoScript (it is based on a Microsoft technology that is not available on OSX). The command that you are pasting is RhinoScript, so it won’t work on Mac. Sorry.

Of course now that you have in front of you a clear outline of the methodology, it should be easier to write in Python/RhinoCommon than doing it from scratch.