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: 

Error in Python code

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Peter__B
1129 Views, 4 Replies

Error in Python code

Hi

 

The following python code is just for trying to learn the python script code for The Fusion 360 and does not produce any valuable output. However, does anybody know why the following code does not work and generates the error message below ?

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

text='string'
print(1,'=',text)

def run(context):

    ui = None

    try:
       global text
       app = adsk.core.Application.get()
       ui  = app.userInterface
#       ui.messageBox('Hello script')
              

       newProj = app.data.dataProjects.add('myNewProj')
       newFold = newProj.rootFolder.dataFolders.add('myNewFold')
       newDoc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
      
#       product = app.activeProduct
#       design = adsk.fusion.Design.cast(product)
       design = adsk.fusion.Design
       rootComp = design.rootComponent
       

       occ = rootComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
       occ.activate()
       newComp = occ.component
       
       newComp.name = 'Beam'
           
       
       sketches = newComp.sketches
       
       text='newstring'        

#       print(2,'=',text)
       xyPlane = newComp.xYConstructionPlane
       sketch = sketches.add(xyPlane)
       sketch.name = 'FirstSketch'
       
        
#       xzPlane = rootComp.xZConstructionPlane
#       sketch = sketches.add(xzPlane)
#       sketch.name = 'SecondComponent'

       lines = sketch.sketchCurves.sketchLines
       
       lines1 = lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0)\
       ,adsk.core.Point3D.create(3,1,0))       
       lines2 = lines.addByTwoPoints(lines1.endSketchPoint\
       ,adsk.core.Point3D.create(1,4,0))            
       lines.addByTwoPoints(lines2.endSketchPoint\
       ,lines1.startSketchPoint)
                
       prof = sketch.profiles.item(0)
       triExt = newComp.features.extrudeFeatures\
       .addSimple(prof,adsk.core.ValueInput.createByReal(50)\
       ,adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
       extBody = triExt.bodies.item(0)
       extBody.name = 'TriangleBeam'
       
       newDoc.saveAs('myDoc',newFold,'','')                      
       
       exportMgr = design.exportManager
       fusionExportOptions = exportMgr.\
       createFusionArchiveExportOptions('D:/FusionData/CadTest')        
       exportMgr.execute(fusionExportOptions)
       
       return text  
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
            

#print('B',run('dummy_arg'))
print(3,'=',text)

 

ErrorMsg.jpg

 

 

 

product = app.activeProduct

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

 

instead of

 

design = adsk.fusion.Design
(as in the current code)

 

the code works properly.

 

I thought that the .cast() just handles the "hints" and that the code would work properly without the .cast(), but perhaps my knowledge is still to poor.

 

Best Regards

Peter

 

4 REPLIES 4
Message 2 of 5
pludikar
in reply to: Peter__B

Hi,

 

design = adsk.fusion.Design

 

your variable (design) is referencing a Class (convention is class names begin with a capital letter).  The class is basically empty until it's initialized - fusion does that when it starts up - 

 

hence: 

product = app.activeProduct => references the existing and initialized fusion object

 

You are right, the .cast() is just a mechanism to get Spyder to recognise the type and link to Spyder's autofill/hints.  It's not involved in the logic.

 

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

 

could also been 

 

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

 

I've also seen it used as:

 

 

design = adsk.fusion.Design.cast(None)

design = app.activeProduct

 

However, I've found *.cast() doesn't always give reliable and complete hints, but that may be just me.

 

Peter

 

 

 

 

I'm not an expert, but I know enough to be very, very dangerous.

Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
Message 3 of 5
Peter__B
in reply to: pludikar

Hi

 

The code works fine with

 

design = app.activeProduct     (design = adsk.core.Application.get().activeProduct)

 

and also with

 

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

 

but .cast() seems to do more than just handle the hints. In manuals I have found that .cast() is not involved in the execusion.

 

Can anybody explain why ?

 

/Peter

Message 4 of 5
pludikar
in reply to: Peter__B

@Peter__B

hi,

 

.cast is designed to be a wrapper.  If it has any other side effects, it'll be a bug.  Use dir() in the spyder command console and you'll see .cast will have no useful methods or properties.

e.g. 

>>> dir(adsk.core.Application.cast)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

 

all the properties you see above, are, by convention meant to be internal to .cast (the __ before and after the name, signifies internal).  It's not that you can't access them, it's just that if you do, the result may be unknown.

 

.cast is like the plastic wrapper around a candy - it tells you what candy's inside, but it's not involved when you eat the candy.

 

In your example

 

>>dir(app.activeProduct) yields
['__class__', '__delattr__', '__delete__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__isabstractmethod__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'deleter', 'fdel', 'fget', 'fset', 'getter', 'setter']

 

and 

 

>>> dir(adsk.fusion.Design.cast(app.activeProduct))
['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

 

there are some differences between having the wrapper and not, but the differences are all internal - there are no added methods or properties.

 

I hope that helps

 

Peter

 

I'm not an expert, but I know enough to be very, very dangerous.

Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
Message 5 of 5
Peter__B
in reply to: pludikar

Thanks a lot, now I understand why it worked and did not work.

/Peter

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