Fusion API: Change Appearance of Body

<?xml encoding=”UTF-8″>By Xiaodong Liang

Various objects in Fusion 360 can be applied with material and appearance. Appearance is how the object looks like. While Material is the physical property of an object. It has a default appearance. e.g. an object with “Leather, weathered” would look like this.

clip_image001

However we can override its appearance with other type.

clip_image002

By this mean, the appearance of a material is not changed. Only the appearance of body is changed.

The corresponding APIs are:

  • BRepBody.material which is a Material object.

image

  • BrepBody.appearance which is an Appearance object.

image

So, if we test with the following JavaScript code before overriding the appearance, it will tell the material is “Leather, weathered” and material’s appearance and body’s appearance are both “Leather, weathered”. After overriding, material is “Leather, weathered”, material’s appearance are not either changed, but the body’s appearance is “Glass – Heavy Color (Blue)” now.

function run(context) {
    "use strict";
   
    if (adsk.debug === true) {
        /*jslint debug: true*/
        debugger;
        /*jslint debug: false*/
    }
    var ui;
    try {
        var app = adsk.core.Application.get();
        ui = app.userInterface;
        var product = app.activeProduct;
        var design = adsk.fusion.Design(product);
       
        //get the first body of the root component
        var rootComp = design.rootComponent;
        var body = rootComp.bRepBodies.item(0);
       
        //body material
        var bodyM = body.material;
        ui.messageBox('body material is: ' + bodyM.name);       
        //appearance with the material
        var MaterialA =bodyM.appearance;
        ui.messageBox('material appearance is: ' + MaterialA.name);
       
        //body appearance
        var BodyA = body.appearance;
        ui.messageBox('body appearance is: ' + BodyA.name);
    }
    catch (e) {
        if (ui) {
            ui.messageBox('Failed : ' + (e.description ? e.description : e));
        }
    }    
    adsk.terminate();
}


image
To change the material /appearance, you need to firstly get the material/appearance from the libraries, and set them to object.material or object.appearance.

image

 


function run(context) {
    "use strict";
   
    if (adsk.debug === true) {
        /*jslint debug: true*/
        debugger;
        /*jslint debug: false*/
    }
    var ui;
    try {
        var app = adsk.core.Application.get();
        ui = app.userInterface;
        var product = app.activeProduct;
        var design = adsk.fusion.Design(product);
       
        //get the first body of the root component
        var rootComp = design.rootComponent;
        var body = rootComp.bRepBodies.item(0);
       
        //body material
        var bodyM = body.material;
        ui.messageBox('body material is: ' + bodyM.name);       
        //appearance with the material
        var MaterialA =bodyM.appearance;
        ui.messageBox('material appearance is: ' + MaterialA.name);
       
        //body appearance
        var BodyA = body.appearance;
        ui.messageBox('body appearance is: ' + BodyA.name);        
       
        //get material lib
        var mLibs = app.materialLibraries;               
        var mLib = mLibs.itemByName("Fusion 360 Material Library");        
        //get one material from this lib
        var mOne = mLib.materials.itemByName("ABS Plastic");
        //change body material
        body.material = mOne;
        app.activeViewport.refresh();
        ui.messageBox('take a look at the screen after material is changed: ');       
       
        //get appearance lib
        var appearanceLib1 = mLibs.itemByName("Fusion 360 Appearance Library");           
         //get one material from this lib
        var aOne = appearanceLib1.appearances.itemByName("Fabric (Green)");
        //change body appearance
        body.appearance = aOne;
        app.activeViewport.refresh(); 
        ui.messageBox('take a look at the screen after appearance is changed: ');       
      
    }
    catch (e) {
        if (ui) {
            ui.messageBox('Failed : ' + (e.description ? e.description : e));
        }
    }   
  
    adsk.terminate();
}

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading