Programmatically Change Material Properties in Process Material Library

Programmatically Change Material Properties in Process Material Library

jon6CUA5
Explorer Explorer
423 Views
2 Replies
Message 1 of 3

Programmatically Change Material Properties in Process Material Library

jon6CUA5
Explorer
Explorer

I know I can modify basic properties like Density, etc. by retrieving the material from app.materialProperties, but these material objects do not seem to have nesting properties like Length, Width, Frame width, Item separation. Is there another way to access those properties?

0 Likes
424 Views
2 Replies
Replies (2)
Message 2 of 3

sophiadoan
Contributor
Contributor

@jon6CUA5 

I don't have a solution for you. It is not obvious for me from the Fusion API how to get properties. So, how do you access various properties from materialProperties and appearanceProperties. The only property I know how to get is appearanceProperties.itemByName('Image'). Are all property names in the from Fusion material dialog as seen by the user? For example, I got the "Image" name from the "Material Editor" dialog. If so, how would one handle names that appear in sub dialog? I hope all prop names are in a flat data structure. 

0 Likes
Message 3 of 3

kandennti
Mentor
Mentor

Hi @jon6CUA5 .

 

We have created a sample that dumps the materialProperties of a selected body.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

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

        msg: str = 'Select'
        selFilter: str = 'Bodies'
        sel: adsk.core.Selection = selectEnt(msg, selFilter)
        if not sel:
            return

        body: adsk.fusion.BRepBody = sel.entity
        material: adsk.core.Material = body.material
        dump(f'-- {material.name} : {material.id} --')
        props: adsk.core.Properties = material.materialProperties
        dump('(Property.name : Property.id : Property.value : Property.isReadOnly)')
        for idx in range(props.count):
            prop: adsk.core.Property = props.item(idx)
            dump(f'{prop.name} : {prop.id} : {prop.value} : {prop.isReadOnly}')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def dump(s):
    adsk.core.Application.get().log(f'{s}')
    print(s)


def selectEnt(
        msg: str,
        filterStr: str) -> adsk.core.Selection:

    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui: adsk.core.UserInterface = app.userInterface
        sel = ui.selectEntity(msg, filterStr)
        return sel
    except:
        return None

 

It seems that each material has different properties that it owns, so I felt that I had to provide an itemById method and an itemByName method.

0 Likes