Add and edit custom material trough API

Add and edit custom material trough API

oyvindTMNSU
Advocate Advocate
3,279 Views
16 Replies
Message 1 of 17

Add and edit custom material trough API

oyvindTMNSU
Advocate
Advocate

Is it possible to change the density of a material via the API in fusion? I want to create a custom material and edit its density so I can set a specific weight on a part. 

0 Likes
Accepted solutions (1)
3,280 Views
16 Replies
Replies (16)
Message 2 of 17

BrianEkins
Mentor
Mentor
Accepted solution

Here's a sample script that creates a new material.  Materials are created by copying an existing material and then modifying it to be what you want.  There appears to be a small bug that after running this script there are two new materials in the design; the original material and the new one.  It doesn't impact anything and can be ignored or you could delete it.  Autodesk is aware so hopefully, it will be fixed soon.  Because of that, I would just leave it as-is and in the future, when it's fixed, there will only be one.

 

import adsk.core, adsk.fusion, traceback

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

        # Get the library that contains a material that is similar
        #  to the desired new material.
        matLib = app.materialLibraries.itemByName('Fusion 360 Material Library')

        # Get an existing material that is close the the desired material.
        existingMaterial = matLib.materials.itemByName('ABS Plastic')

        # Copy the existing material into 
        newMaterial = des.materials.addByCopy(existingMaterial, 'My ABS Plastic')
        densityProp: adsk.core.FloatProperty = newMaterial.materialProperties.itemByName('Density')
        densityProp.value = densityProp.value / 2
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 17

oyvindTMNSU
Advocate
Advocate

@BrianEkins is it not possible to change the density of the new material from the API? Do you have a smart work around if not?

 

0 Likes
Message 4 of 17

oyvindTMNSU
Advocate
Advocate

Here is what I want to write an add-in for.

Just to give a image of why I need it. Maybe its easier to suggest a workaround, if needed.

 

Case: 

I model up a component that will be a part of my assembly. This component is made of several different materials in the real world, and I am only interested in adding the actual mass/weight of the component. Without placing correct materialt to all bodies of the component.  The weight is either given by the supplier of the product, or I just put it on a scale. 

So I want to add a custom weight to the component I just modeled 

 

Add-in:

 

1. type the real world total weight  of the product into a dialog box.

 

2. Get the volume of the component in fusion 360 (physical properties in API)

 

3.  Create a new physical material

 

4. Edit the existing density in the new material... result of the math. (New_density= real world weight/volume )

 

5. Assign the new material to the component.

0 Likes
Message 5 of 17

oyvindTMNSU
Advocate
Advocate

I'm sorry, I did not notice last part of your example... seems like you showed the answer there

0 Likes
Message 6 of 17

BrianEkins
Mentor
Mentor

Sorry that I'm a little slow to get to the forum but I'm glad you've found the solution in my earlier post.  Good luck.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 7 of 17

oyvindTMNSU
Advocate
Advocate

Question related to this.
How can I use a user input to set the value of the density? I want the user to write the input in kg/mm^3. But I can se that the default unit in the density box is something else. And also, as far as I have understood, the input from the user is a string.

 

what Im asking is how I can "write" into the input box for density of a material, via an user input where the user writes "0,007 kg/mm^3". The same way I would type it in the UI when change a material density

0 Likes
Message 8 of 17

oyvindTMNSU
Advocate
Advocate
0 Likes
Message 9 of 17

jordan783ZS
Advocate
Advocate

@oyvindTMNSUFunnily enough a little while ago I made a little Addon to accomplish this exact task.

Feel free to have a look at it and adapt it to your needs.

There is some real weirdness I found with how fusion seems to deal with density in the design and material density (they use different units but this isn't really documented anywhere).

I feel like fusion should have this feature built-in though, like many other CAD programs do.

0 Likes
Message 10 of 17

jordan783ZS
Advocate
Advocate

I've only just seen that you're to have the user enter density. You can make the user input a "value input", which will allow them to type the density in any unit and will then convert it into the internal default units.

For example, in my app I use the following like to add a value input that takes mass as input (defaulting to "g" or grams).

inputs.addValueInput('weight_input', 'Desired Weight', "g", weightVal)

 

0 Likes
Message 11 of 17

mfreedVY4CK
Explorer
Explorer

Thanks for the Weight_Corrector.zip utility above! Super useful. I feel like this should be a native Fusion feature because it saves SO much time when you need to adjust the mass of a part. Particularly when parts are 3D printed - there isn't a standard density material that just works for any part. Each part is different depending on how you slice it.

 

I updated your utility to modify the mass of individual bodies, instead of components. I also fixed up a few UI things here and there. See attached.

0 Likes
Message 12 of 17

jordan783ZS
Advocate
Advocate

No problem, glad you found it useful! I also hope they make a way to set it more elegantly in fusion.
I'll have a look at your script and see if I can incorporate the improvements into my local version. 
One issue I fixed a while back was caused by the dreaded US vs UK spellings of Aluminium. You might want to check you don't have the same issue.

0 Likes
Message 13 of 17

mfreedVY4CK
Explorer
Explorer

Thanks for the heads-up about US vs UK Alumin(i)um!

 

Also, I was able to strip out a ton of stuff that wasn't needed for the add-in - see the version attached. 52 kB vs 22 MB.

0 Likes
Message 14 of 17

oyvindTMNSU
Advocate
Advocate

This is exactly what I need! Thank you so much! The only thing is that is would be nice to be able to set the weight for a whole assembly/main component. Forexample a PCb with a lot of bodies. I just want to set a complete weight, not every body. And also be able to set the name of the new material

 

0 Likes
Message 15 of 17

jordan783ZS
Advocate
Advocate

I haven't looked at @mrfreedVY4CK 's version yet, but that's exactly what my original addin did, worked out the density for the component and set all the bodies within to that density to make the whole component the correct mass

0 Likes
Message 16 of 17

mfreedVY4CK
Explorer
Explorer

Here's another version that allows selecting bodies OR components, and I also added a setting for whether you want the specified mass to apply to each selection, or all of them combined. 

Message 17 of 17

oyvindTMNSU
Advocate
Advocate

thank you!

0 Likes