Change subobjects' color through rhinocommon

Hi guys,
I’d like to change a brepface’s color to eg. red, in the meantime the other part’s color of this brep is remian the same (both the colorsource and the color). I can do it in rhino by selecting the brepface and then change the color at property panel, but in rhinocommon, I’ve found this but not working for now:

brepface.perfacecolor = red;

rhinoobject.attributes.colorsource = by obj;
rhinoobject.attributes.objectcolor = red;
rhinoobject.commitchanges();
redraw();

I didn’t found a particular method for changing a sub-object’s attributes I guess, I did see the method getsubobjects(), but it seems that this method generate the new objects instead of changing the old which I may not need to.

thx!

Hi @ygwang,

For Breps, the per-face color is stored on each Brep face.

For SubDs, the per-face color is stored on each SubD face.

– Dale

@dale thx for the advise!

As I understand,
brepface.perfacecolor = red;
After setting this, the model didn’t change. Like modify the rhinoobjcts attributes need a commit method, is there a special method I need to call to commit this change? Or just my code has some other parts wrong to cause the problem.

Hi @ygwang,

Try this:

import Rhino
import scriptcontext as sc
import System

def test_brep_perface_color():
    # Select a face
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select surface")
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Surface
    go.SubObjectSelect = True
    go.Get()
    if go.CommandResult() != Rhino.Commands.Result.Success:
        return
    
    objref = go.Object(0)
    # Get the brep face that was picked
    face = objref.Face()
    # Get the owning brep
    brep = objref.Brep()
    if face and brep:
        # Only make sense if there is more than 1 face
        if brep.Faces.Count > 1:
            new_brep = brep.DuplicateBrep()
            new_brep.Faces[face.FaceIndex].PerFaceColor = System.Drawing.Color.Red
            sc.doc.Objects.Replace(objref.ObjectId, new_brep)
            sc.doc.Views.Redraw()

if __name__ == "__main__":
    test_brep_perface_color() # Call the function defined above

– Dale

Thanks @dale !
Applying the replace method and the brepfaces’s color is changed!

Such a good sample, thanks so much!

I did the coloring with the script SubD Color the Faces and select them