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.
Solved! Go to Solution.
Solved by BrianEkins. Go to 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()))
@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?
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.
I'm sorry, I did not notice last part of your example... seems like you showed the answer there
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.
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
@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.
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)
Can't find what you're looking for? Ask the community or share your knowledge.