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: 

ChatGPT Script Issue

3 REPLIES 3
Reply
Message 1 of 4
aaronkcap
262 Views, 3 Replies

ChatGPT Script Issue

Let me preface this with I am pretty bad with python coding. I was messing around with ChatGPT to make a script in fusion that creates  an exfruded profile of text based off of user input of the text and font size, and can't seem to figure out what the error is or why it is not working. Any help is greatly appreciated.

import adsk.core, adsk.fusion

# Function to create a profile
def create_profile(name, font_size):
app = adsk.core.Application.get()
ui = app.userInterface
design = app.activeProduct

# Get the active component
root_comp = design.activeComponent
sketches = root_comp.sketches
extrudes = root_comp.features.extrudeFeatures

# Create a sketch on the XY plane
sketch = sketches.add(root_comp.xYConstructionPlane)

# Prompt the user for input
name_input = ui.inputBox("Enter a name for the profile:", "Name", name)
if name_input[0]:
name = name_input[0]

font_size_input = ui.inputBox("Enter the font size:", "Font Size", font_size)
if font_size_input[0]:
font_size = float(font_size_input[0])

# Create text
text_point = adsk.core.Point3D.create(0, 0, 0)
text_entity = sketch.sketchTexts.add(name, text_point, False)

# Set text style
text_entity.fontName = "Lucida Calligraphy Italic"
text_entity.textHeight = font_size

# Extrude the profile
prof = sketch.profiles.item(0)
ext_input = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
ext_input.setDistanceExtent(adsk.core.ValueInput.createByString("1 cm"))
ext = extrudes.add(ext_input)

ui.messageBox("Profile created successfully.")

# Call the create_profile function
create_profile("", 12)

 

 

3 REPLIES 3
Message 2 of 4
felix85SA3
in reply to: aaronkcap

I suggest posting code in code sections, so others can easily read what you are trying to do.

Put that script inside a script template and run it. The default script boilerplate includes "error logging" via ui.messageBox. After doing that, You'll get an error 'TypeError: Wrong number or type of arguments for overloaded function' for your second ui.inputBox because your default value is an integer, but the default value is expected to be string.

Message 3 of 4
kandennti
in reply to: aaronkcap

Hi @aaronkcap -San.

 

I have tried to fix it so that there are no errors, but I am not sure if I am doing the process you want.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        # Call the create_profile function
        create_profile("", 12)

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


# Function to create a profile
def create_profile(name, font_size):
    app = core.Application.get()
    ui = app.userInterface
    design = app.activeProduct

    # Get the active component
    root_comp = design.activeComponent
    sketches = root_comp.sketches
    extrudes: fusion.ExtrudeFeatures = root_comp.features.extrudeFeatures

    # Create a sketch on the XY plane
    sketch: fusion.Sketch = sketches.add(root_comp.xYConstructionPlane)

    # Prompt the user for input
    name_input = ui.inputBox(
        "Enter a name for the profile:",
        "Name",
        name
    )
    if name_input[0]:
        name = name_input[0]

    font_size_input = ui.inputBox(
        "Enter the font size:",
        "Font Size",
        str(font_size)
    )
    if font_size_input[0]:
        font_size = float(font_size_input[0])

    # Create text
    sktTxts: fusion.SketchTexts = sketch.sketchTexts
    txtIpt: fusion.SketchTextInput = sktTxts.createInput2(
        name,
        font_size,
    )
    txtIpt.setAsMultiLine(
        core.Point3D.create(0, 0, 0),
        core.Point3D.create(10, 5, 0),
        core.HorizontalAlignments.LeftHorizontalAlignment,
        core.VerticalAlignments.TopVerticalAlignment, 0
    )
    text_entity: fusion.SketchText = sktTxts.add(txtIpt)

    # Extrude the profile
    extrudes.addSimple(
        text_entity,
        core.ValueInput.createByString("1 cm"),
        fusion.FeatureOperations.NewBodyFeatureOperation,
    )

    ui.messageBox("Profile created successfully.")


The Fusion360 scripts that chatGPT writes out are often incomplete.

Message 4 of 4
judith_casta
in reply to: aaronkcap

  

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