Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

ChatGPT Script Issue

aaronkcap
Advocate

ChatGPT Script Issue

aaronkcap
Advocate
Advocate

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)

 

 

0 Likes
Reply
640 Views
3 Replies
Replies (3)

felix85SA3
Participant
Participant

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.

1 Like

kandennti
Mentor
Mentor

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.

0 Likes

judith_casta
Community Visitor
Community Visitor

  

0 Likes