Overwrite Appearance of Brep Face

Overwrite Appearance of Brep Face

casey.rogers
Alumni Alumni
764 Views
3 Replies
Message 1 of 4

Overwrite Appearance of Brep Face

casey.rogers
Alumni
Alumni

I have added two named Appearances to my appearance library each with unique names. I have a program that is iterating through faces and passing them into a helper function that will change the appearance of the face. The desired result is to color different faces in a single body differently using differently colored appearance objects. However, when I run the add-in, none of the faces have visibly changed. However, one extra copy of the body's original appearance (in this case steel satin) now shows up in the appearances panel. Here is a stripped down version of my code:

 

 

def validate(face, color):
    try:
        app = adsk.core.Application.get();
        ui = app.userInterface;
        design = app.activeProduct
        paramList = design.userParameters

        apps = design.appearances
        badAlt = apps.itemByName("Short Altitude")
        badSide = apps.itemByName("Short Side")
        if not badAlt or not badSide:
            ui.messageBox("Appearance 'Short Altitude' or appearance 'Short Side' not found")

        if color:
            face.appearance = badSide
        else:
            face.appearance = badAlt
except:
ui.messageBox("Error: Validate Failed")

Any ideas what I am doing wrong? (Note: I am using the dev build of Fusion, in case that is relevant). Note: if I change "face.appearance" to junk like "face.asdf" the program executes with our any errors reported, including the messagebox in my except block. I also placed test message boxes before and after "face.apperance = badSide" and both message boxes were reached including when I replaced appearance with "asdf". really not sure what's happening here...

0 Likes
765 Views
3 Replies
Replies (3)
Message 2 of 4

neil.liu
Autodesk
Autodesk

Hi Casey,

 

I suppose you should get the metrial library first, then get the appearance from this library. You can refer to the sample script named "ApplyAppearanceToSelection" in Scripts and Add-ins dialog. 

 

Thanks, 

0 Likes
Message 3 of 4

casey.rogers
Alumni
Alumni

Specifically I am trying to apply to apperances that are duplicates of the steel satic appearance with its color modified. It appears my script was just applying a brand new duplicate of the typical steel satin apperance and was losing the color and name modifications I had made to my duplicates.

I simply switched to using unmodified but different appearances from the material library (without modifying my code at all) and it seems to be working. Not sure why I was losing the properties using my original methodolgy.

0 Likes
Message 4 of 4

ekinsb
Alumni
Alumni

I just tried reproducing the problem you reported but it's working ok for me.  Here's my full script that I'm using to test.

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        des = app.activeProduct
        root = des.rootComponent

        body = ui.selectEntity('Pick a body', 'Bodies').entity
        
        thing = True
        for face in body.faces:
            if thing:
                validate(face, 'junk')
                thing = False
            else:
                validate(face, None)
                thing = True
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def validate(face, color):
    try:
        app = adsk.core.Application.get();
        ui = app.userInterface;
        design = app.activeProduct
        paramList = design.userParameters

        apps = design.appearances
        badAlt = apps.itemByName("Short Altitude")
        badSide = apps.itemByName("Short Side")
        if not badAlt or not badSide:
            ui.messageBox("Appearance 'Short Altitude' or appearance 'Short Side' not found")

        if color:
            face.appearance = badSide
        else:
            face.appearance = badAlt
    except:
         ui.messageBox("Error: Validate Failed")

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes