Add-in doesn't work (litteraly nothing happens)

jonassog
Contributor

Add-in doesn't work (litteraly nothing happens)

jonassog
Contributor
Contributor

Hi all,

 

I am used to creating Python add-ins to make F360 work, but this time something weird happens. After having saved my Python code, I try to run it from F360 but nothing happens. It's like the code doesn't run although there is no error either on my VS Code, or on Fusion. Anyone has already had this error before and/or knows how to solve it please ?

 

Thanks a lot

J

0 Likes
Reply
Accepted solutions (1)
1,543 Views
21 Replies
Replies (21)

jonassog
Contributor
Contributor

To add to what I just posted: sometimes the add-ins do work, but it's just that at some point, it stops working.

0 Likes

Jorge_Jaramillo
Collaborator
Collaborator

Does it happen with any add-in or just with the one you're testing?

Do you mind sharing the code of your add-in?

 

Regards,

Jorge Jaramillo

 

0 Likes

jonassog
Contributor
Contributor
It happens with any add-in I am testing, but just at some point. For some reason I cannot attach the code so here it is below. Sorry for the format.
#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback
import math as m
# import calcium_generator as cg



length_mm = 3
angle_deg = 180
thickness_mm = 1
depth_mm = 4
stenosis_percentage = 0.4
healthy_artery_r_mm = 1.5

stenosed_radius_mm = healthy_artery_r_mm*m.sqrt(1-stenosis_percentage)


length = 0.1 * length_mm
angle = angle_deg * 3.14/180
thickness = 0.1 * thickness_mm
depth = 0.1 * depth_mm
stenosed_radius = stenosed_radius_mm * 0.1
h = healthy_artery_r_mm * 0.1



def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('Hello script')

doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

product = app.activeProduct
design = adsk.fusion.Design.cast(product)


rootComp = design.rootComponent

sketches = rootComp.sketches
sketch = sketches.add(rootComp.xYConstructionPlane)
arcs = sketch.sketchCurves.sketchArcs

extrudes = rootComp.features.extrudeFeatures

centerPoint = adsk.core.Point3D.create(0,0,0)

beta = m.atan(1/(1+2*(h+depth)/thickness))
sweepAngle = (angle-2*beta)
startPoint_inner = adsk.core.Point3D.create((h+depth)*m.sin(sweepAngle/2),(h+depth)*m.cos(sweepAngle/2),0)
startPoint_outer = adsk.core.Point3D.create((h+depth+thickness)*m.sin(sweepAngle/2), (h+depth+thickness)*m.cos(sweepAngle/2),0)
inner = arcs.addByCenterStartSweep(centerPoint, startPoint_inner, sweepAngle)


outer = arcs.addByCenterStartSweep(centerPoint, startPoint_outer, sweepAngle)

middle_point_right = adsk.core.Point3D.create((h+depth+thickness/2)*m.sin(angle/2),(h+depth+thickness/2)*m.cos(angle/2),0)
middle_point_left = adsk.core.Point3D.create(-(h+depth+thickness/2)*m.sin(angle/2),(h+depth+thickness/2)*m.cos(angle/2),0)

end_arc_right = arcs.addByThreePoints(inner.startSketchPoint,middle_point_right, outer.startSketchPoint)
end_arc_left = arcs.addByThreePoints(inner.endSketchPoint,middle_point_left, outer.endSketchPoint)


constraints = sketch.geometricConstraints

constraints.addTangent(outer,end_arc_right)
constraints.addTangent(inner,end_arc_right)

constraints.addTangent(outer,end_arc_left)
constraints.addTangent(inner,end_arc_left)

inner.isFixed = True
outer.isFixed = True
end_arc_left.isFixed = True
end_arc_right.isFixed = True

prof = sketch.profiles.item(0)
distance = adsk.core.ValueInput.createByReal(length)
extrude1 = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

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

jonassog
Contributor
Contributor

My bad, I figured out how to attach the file. Here it is in.txt.

0 Likes

BrianEkins
Mentor
Mentor

I don't know what the problem is. I created a new script and replaced the code with yours. It ran without any edits and I get the model shown below.

ScriptResult.png

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

jonassog
Contributor
Contributor

Exactly, that is the weird thing. I just re-tried with some code added to what I posted here, and again, I cannot run it.

0 Likes

BrianEkins
Mentor
Mentor

Try creating a new script, delete the code, and paste your code in. Does that run? Also, you keep referring to an "Add-In," in your description but your code is for a script. Maybe that has something to do with it?

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

jonassog
Contributor
Contributor

I tried to copy and paste in another script, it still doesn't work. 

I refer to add-in because I thought that was the terminology used in F360 to talk about Python scripts (I have to go to the Add in tab in the GUI to create a python script from F360).

0 Likes

jonassog
Contributor
Contributor
My bad forget about me referring to add-in!
0 Likes

BrianEkins
Mentor
Mentor

I suspected it was just a naming issue, but I hoped it was a clue to the problem. Unfortunately, it isn't so I have no idea what the problem is. I've never encountered this before, and I haven't seen any reports of anything similar.

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

jonassog
Contributor
Contributor

That was just my bad for naming a script an "add in" but the issue is unfortunately still not solved. Is there another "clean" way to create a script from F360 other than pressing "SHIFT+S" and follow the instructions (hit "create", select python and reclick on create)?

 

 

0 Likes

jonassog
Contributor
Contributor

Also, does the last code I sent work for you?

0 Likes

BrianEkins
Mentor
Mentor

The last code doesn't work for me because I don't have numpy or matplotlib on my system.

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

jonassog
Contributor
Contributor

I wanted to comment out the two imports that you don't have for you to run the script, but just out of curiosity, I wanted to see if it worked on my end after I removed the two imports. Well, I guess I think I found something. It seems like the script does not run in F360 if either numpy or matplotlib is imported... This is really interesting, I have no clue about why these two in partciular, knowing that I do import random and math without any issue... Any guesses?

0 Likes

BrianEkins
Mentor
Mentor
Accepted solution

numpy and matplotlib are not part of the standard Python distribution, while random and math are. Fusion only installs the standard libraries. If you need other libraries, you need to install those on your own, and depending on the library, it can get very messy. I always try hard to only use the functionality available in the standard distribution.

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

jonassog
Contributor
Contributor

Can you explain what you mean when you say if I need those libraries I should install on your own ? Because I already installed them in Python, which is why I can import them and use them. Do I need to do other manipulations for fusion?

0 Likes

BrianEkins
Mentor
Mentor

Here are some previous threads where this is discussed. As I said before, it's a bit complicated.

 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/to-install-python-modules/m-p/5777176

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/import-data-from-excel-type-xlsx/m-p/11249...

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/recommended-way-of-installing-python-packa...

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

jonassog
Contributor
Contributor

Thanks for the links. I unfortunately think that my issue might be more than that. I re tried to run the code and it doesn't work this time...... I even removed the import of math.... See attached

0 Likes

BrianEkins
Mentor
Mentor

With your latest code, I had to comment out the line to import "calcium_generator" since I don't have that library, but once I did that, it ran fine.

 

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