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: 

Material issue faced when using Fusion API

6 REPLIES 6
Reply
Message 1 of 7
adn.developer
470 Views, 6 Replies

Material issue faced when using Fusion API

We are trying to export the material used in fusion to the output file.
When multiple color is applied on model in fusion in output file we get one random color applied to whole body.
Similarly we are trying to export texture, multiple texture applied on body is not exported in output file.
Please look into the issue and guide us through if we are doing something wrong.

6 REPLIES 6
Message 2 of 7
BrianEkins
in reply to: adn.developer

It is not clear to me what you are trying to accomplish.  Some of that is because of terminology.  Fusion has materials and appearances.  A material has physical properties like density and Poisson's ratio that define it's physical characteristics.  A material also has appearance information that defines how the object will look.  You mentioned materials but then it seems you're actually talking about appearances.  Are you exporting the model as a mesh or some other format?  What will you be using the data for?

 

A material can be assigned to a component and all of the bodies within that component will use the material.  A material can also be assigned to a body, which will override the material assigned to its parent component.

 

By default, a body will display using the appearance associated with the material assigned to the body of its parent component.  However, you can override the material appearance and assign another appearance.  Appearances can be assigned to components, bodies, and even individual faces.  The object at the lowest level that has an appearance assigned will override any appearance assignments at higher levels.  For example, an appearance assigned to a body will override the appearance assigned to the component and an appearance assigned to a face will override the body appearance.

 

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 7
adn.developer
in reply to: BrianEkins

We are exporting the mesh data to the output file, we want to get data of color and texture present in the model.
Using appearance the color/texture information is retrieved.
The issue faced is if model have multiple color/texture are present on the model we are getting value of only one color/texture from the list.
I hope i was able to clear the confusing about the issue faced.
Here is the link in which i have provided the small demo screen recorded video to illustrate the issue.
https://autode.sk/3aVOdP6
Looking forward to hear from you.
Thank you in advance for the help.

Message 4 of 7
BrianEkins
in reply to: adn.developer

I've attached a simple model with three different appearances applied.

 

Appearance01.png

The script below gets the appearances from the model and writes out information into the text window.

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

    try:
        des: adsk.fusion.Design = app.activeProduct
        root = des.rootComponent
        occ1: adsk.fusion.Occurrence = root.occurrences[0]
        occ2: adsk.fusion.Occurrence = root.occurrences[1]
        occ3: adsk.fusion.Occurrence = root.occurrences[2]
        body1: adsk.fusion.BRepBody = occ1.bRepBodies[0]
        body2: adsk.fusion.BRepBody = occ2.bRepBodies[0]
        body3: adsk.fusion.BRepBody = occ3.bRepBodies[0]
        
        WriteToTextWindow('1:')
        WriteToTextWindow('   body: ' + body1.name + ', ' + body1.appearance.name)
        WriteToTextWindow('   color: ' + AppearanceColor(body1.appearance))
        WriteToTextWindow('2:')
        WriteToTextWindow('   body: ' + body2.name + ', ' + body2.appearance.name)
        WriteToTextWindow('   color: ' + AppearanceColor(body2.appearance))
        WriteToTextWindow('3:')
        WriteToTextWindow('   body: ' + body3.name + ', ' + body3.appearance.name)
        WriteToTextWindow('   color: ' + AppearanceColor(body3.appearance))
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def AppearanceColor(appearance: adsk.core.Appearance):
    color: adsk.core.Color = appearance.appearanceProperties.itemById('wood_early_color').value
    return str(color.red) + ', ' + str(color.green) + ', ' + str(color.blue)  

 

Here are the results:

1:
   body: Green, 3D Walnut - Green
   color: 46, 167, 30
2:
   body: Blue, 3D Maple - Yellow
   color: 189, 167, 16
3:
   body: Red, 3D Pine - Red
   color: 159, 5, 5

 

Fusion uses an Autodesk specific renderer and materials are defined using a proprietary definition.  This makes it difficult to transfer materials from Fusion to other renderers.  In the example program, I'm getting the appearance associated with each body.  Appearances are defined by a set of properties.  In this case, the property that primarily defined the color is a property whose ID is "wood_early_color".  Depending on the settings of the properties, the primary color could be defined by a different property.

 

There are five different types of appearances; opaque, metal, transparent, layered, and 3D wood.  Each type of appearance will have a consistent set of properties.  The script below dumps out all of the appearances in the active design and shows the values of most of the properties associated with the appearance.  The first value of "Material Type" indicates which of the 5 types of appearances it is.

 

def run(context):
    app: adsk.core.Application = adsk.core.Application.get()
    ui = app.userInterface
    des: adsk.fusion.Design = app.activeProduct

    filename = 'C:/Temp/AppearanceInfo.txt'
    f = open(filename, 'w')

    appearance: adsk.core.Appearance = None
    for appearance in des.appearances:
        f.write('Appearance ' + appearance.name + ' (' + appearance.id + ')\n')

        prop: adsk.core.Property
        for prop in appearance.appearanceProperties:
            try:
                f.write('   Property ' + prop.name + ', ' + prop.id + ', ' + prop.objectType + '\n')
                f.write('       Value: ' + AppearanceValueAsString(prop) + '\n')
            except:
                f.write('   *** Failed')

        f.write('\n')

    f.close()

    ui.messageBox('Finished writing appearance info to C:/Temp/AppeaeranceInfo.txt')


def AppearanceValueAsString(appearanceProp):
    if appearanceProp.objectType == adsk.core.IntegerProperty.classType():
        intProp: adsk.core.IntegerProperty = appearanceProp
        return str(intProp.value)
    elif appearanceProp.objectType == adsk.core.StringProperty.classType():
        stringProp: adsk.core.StringProperty = appearanceProp
        return str(stringProp.value)
    elif appearanceProp.objectType == adsk.core.ColorProperty.classType():
        colorProp: adsk.core.ColorProperty = appearanceProp
        return str(colorProp.value.red) + ', ' + str(colorProp.value.green) + ', ' + str(colorProp.value.blue)
    elif appearanceProp.objectType == adsk.core.FloatProperty.classType():
        floatProp: adsk.core.FloatProperty = appearanceProp
        return str(floatProp.value) 
    elif appearanceProp.objectType == adsk.core.BooleanProperty.classType():
        boolProp: adsk.core.BooleanProperty = appearanceProp
        return str(boolProp.value) 
    else:
        return '???'
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 5 of 7

Hello,

Thank you for the response.
We have tried the sample code provided by you in our program.
The code did not yield the desired output for us.

Here I'm pasting some image will will give the clear understanding of what issue we are facing.

Here is the code snippet which get the list of appearance from the design and write the name of the appearance in the text file.
code_sample.png

 

Here is the image of design file, i have highlighted the material list used in the design.
image_materials1.png
Here is the output text file in which the list of material is written using the code snippet above.
As we can see there are repetition of same material in the loop.
From the list of appearance we are able to get only the first material present in the list

ProtoTechSolutions_0-1616680937463.png

Please look into the isuse and provide the solution for the same.
Thank you in advance

 

Message 6 of 7

Sorry, I'm so late to respond.  I somehow missed this.
Is it possible to post the f3d for that design here or you can send it to me directly using the email in my signature.  Was the file imported from another system?  At one time I believe there was an appearance issue with imported files.  Either way, I can do some testing and if I find what looks like a Fusion bug, pass it onto Autodesk.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 7 of 7
BrianEkins
in reply to: BrianEkins

I was given your sample file and wrote another little script to be able to visualize what's going on.  There is a bug in Fusion with how appearances are identified internally.  As a result, it can return the wrong appearance, which is what you're seeing.  It took me a bit to find it because the first thing I did was rename the appearances in your sample so my output was easier to read.  It turns out that making any edit to an appearance does something to correct the problem for that appearance.

 

I'm working with Autodesk to log the bug so they can prioritize it.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

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