Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fusion 360 API: Editing Materials programmatically

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
denis.ch
1446 Views, 2 Replies

Fusion 360 API: Editing Materials programmatically

I am doing a script rendering a certain layered structure and I would like to assign each layer a computed color corresponding to its position.

There is a guide explaining how to edit appearances manually:  http://www.autodesk.com/products/fusion-360/blog/making-it-look-good-apply-and-edit-materials-in-fus...

 

My question is how to modify the appearance color programmatically?

 

I have been looking through every property of Ptr<Material> and Ptr<Appearance>  in the API documentation but could not identify any obvious method to access the color property.

 

2 REPLIES 2
Message 2 of 3
ekinsb
in reply to: denis.ch

Below is a Python example that copies an appearance from the "Fusion 360 Appearance" library into the open document, changes the color associated with the appearance, and assigns it to an occurrence.  Working with appearances is not easy because the various types of occurrences are defined in very different ways so not all appearances will have a property called "Color".  However, the concept is the same for any of the appearances properties and they can be edited through the API.

 

import adsk.core, adsk.fusion, adsk.cam, traceback
from random import randint

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

        # Create a new document.
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)        
        des = adsk.fusion.Design.cast(doc.products.itemByProductType('DesignProductType'))
        root = des.rootComponent

        # Get a reference to an appearance in the library.
        lib = app.materialLibraries.itemByName('Fusion 360 Appearance Library')
        libAppear = lib.appearances.itemByName('Plastic - Matte (Yellow)')

        numParts = 6
        for i in range(numParts):
            # Create an extrusion.
            sk = root.sketches.add(root.xYConstructionPlane)
            sk.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(i*3,0,0), 1)
            prof = sk.profiles.item(0)
            ext = root.features.extrudeFeatures.addSimple(prof, adsk.core.ValueInput.createByReal(4), adsk.fusion.FeatureOperations.NewComponentFeatureOperation)
            comp = ext.parentComponent
            
            # Get the single occurrence that references the component.
            occs = root.allOccurrencesByComponent(comp)
            occ = occs.item(0)
           
            # Create a copy of the existing appearance.
            newAppear = des.appearances.addByCopy(libAppear, 'Color ' + str(i+1))
            
            # Edit the "Color" property by setting it to a random color.
            colorProp = adsk.core.ColorProperty.cast(newAppear.appearanceProperties.itemByName('Color'))
            red = randint(0,255)
            green = randint(0,255)
            blue = randint(0,255)
            colorProp.value = adsk.core.Color.create(red, green, blue, 1)          
            
            # Assign the new color to the occurrence.
            occ.appearance = newAppear
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3
denis.ch
in reply to: ekinsb

Great!

The key was

     colorProp = adsk.core.ColorProperty.cast(newAppear.appearanceProperties.itemByName('Color'))

What is not documented in the API documentation is that an Appearance may (or may not) have a property called "Color" which I need to look for, and if found, cast it to Ptr<ColorProperty>.

 

But now I know that 🙂

 

Once the method is clear it is easy to explore and modify properties of an Appearance. Here is a working C++ snippet from my layer renderer based on your example:

{

    <...>

    Ptr<ExtrudeFeature> ef=<...>

 

    // appearance name for this layer

    char aname[64];
    sprintf(aname,"LayerColor_%s",line);

 

    // check if we have already created this appearance
    Ptr<Appearance> newAppear=globs.design->appearances()->itemByName(aname);
    if (newAppear)
        ef->bodies()->item(0)->appearance(newAppear);
    else
    { // custom appearance
        Ptr<Appearance> apc = globs.appearances->itemByName("Acrylic (Red)"); 
        newAppear = globs.design->appearances()->addByCopy(apc,aname);

        // Edit the "Color" property
        int nprop=(int)newAppear->appearanceProperties()->count();
        // in some materials there are two properties called "Color"; we want to set both since we cannot know which one is primary
        for(int i = 0; i<nprop;i++)
        {
            Ptr<Property> prop = newAppear->appearanceProperties()->item(i);
            if(strcmp(prop->name().c_str(),"Color")==0)
            {
                Ptr<ColorProperty> colorProp = (Ptr<ColorProperty>)prop;
                colorProp->value(Color::create(red,green,blue,1));
            }
        }

    // apply
    ef->bodies()->item(0)->appearance(newAppear);
}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report