Create a Template from a operation using Fusion 360 Python API

Create a Template from a operation using Fusion 360 Python API

enervino
Enthusiast Enthusiast
589 Views
5 Replies
Message 1 of 6

Create a Template from a operation using Fusion 360 Python API

enervino
Enthusiast
Enthusiast

I am not sure what I am doing wrong but I cannot seem to export Templates from operations for fusion using the API.

Here is the script I am trying:

 

 

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

def save_template():
    # Get the application
    app = adsk.core.Application.get()

    # Get the active document
    doc = app.activeDocument

    # From the Products collection on the active document, get the CAM Product
    cam :adsk.cam.CAM = doc.products.itemByProductType('CAMProductType')

    # Get the first setup.
    setup = cam.setups.item(0)

    # Get the first operation.
    operation = setup.operations.item(0)
    
    # generate the Cam Template
    returnValue = adsk.cam.CAMTemplate.createFromOperations(operation)
    
    # Save the template to a file
    returnValue.save('C:\\file.camtemplate')  # Change this to your desired file path

# Call the function
save_template()

 

 

I am trying to use the CAMTemplate Object to generate templates.

I was also trying to give it a operation list but then Fusion just fully cashed with no error.

0 Likes
Accepted solutions (1)
590 Views
5 Replies
Replies (5)
Message 2 of 6

jeff.pek
Community Manager
Community Manager
Accepted solution

Hi -

 

CAMTemplate.createFromOperations takes a list of operations, so you need to pass it as "[operation]".

 

Jeff

0 Likes
Message 3 of 6

enervino
Enthusiast
Enthusiast

OK here is the changes I made, Seems to work now thanks, I think I was having issues with separating setup.operations and a list of operation

 

        # Get the first operation.
        operation = setup.operations.item(0)

        #create a list of operations then add an operation to it
        operations = []
        operations.append(operation)

        # generate the Cam Template
        returnValue = adsk.cam.CAMTemplate.createFromOperations(operations)

        # Save the template to a file
        returnValue.save('C:\\c#\\#python\\1Test\\file.f3dhsm-template')  # Change this to your desired file path

 

0 Likes
Message 4 of 6

jeff.pek
Community Manager
Community Manager

Sure, that works. It would be even easier to just put square brackets around the "operation" argument in the call, but what you have does the equivalent.

 

Jeff

0 Likes
Message 5 of 6

enervino
Enthusiast
Enthusiast

I am not too familiar with Python syntax, it is all a bit strange to me. anyway, it seems if you put something the API does not like into returnValue.save('here') like an invalid directory or an existing file name, then fusion full crashes without warning, so sanitize that input (fusion should probably wanna look into this too because you would expect an exception throw there, not a full crash)

0 Likes
Message 6 of 6

jeff.pek
Community Manager
Community Manager

Agreed. Thanks for pointing that out.

  Jeff

0 Likes