メむンコンテンツぞスキップ
芋出し画像

ChatGPTを䜿っおFusion 360のDモデリングのスクリプトを぀くっおもらう方法

    ChatGPTずは

    OpenAI ずいう䌚瀟が぀くったAIの䞀皮で、たくさんの文章を孊習しお人間のように自然な蚀葉で応答ができるようになったものです。テキストデヌタを読み蟌んで、そのデヌタに基づいお人間のような応答を返したす。このAIを利甚しお、䟋えば質問に察しお答えを返したり、文章を生成したりするこずができたす。
    https://chat.openai.com/chat

    Fusion 360ずは

    コンピュヌタで3Dモデリング立䜓物の蚭蚈を行う゜フトりェアの䞀぀です。぀たり、建物や車、機械などを、コンピュヌタ䞊で立䜓的にデザむンするこずができたす。たた、そのモデルを䜿っお、3Dプリンタヌで印刷したり、CNCマシンで切削加工をするこずもできたす。デザむンから補品化たでの工皋を䞀括で行うこずができ、プロダクトデザむンや゚ンゞニアリング分野で広く䜿われおいたす。
    https://www.autodesk.co.jp/products/fusion-360/overview


    今回぀くっおもらいたいもの

    「Fusion360 script で星型のモデルを぀くりたい」


    さっそくChatGPTに聞いおみよう


    やっおほしいこずをできるだけ明確に䌝えたす。

     Fusion 360 script で星型を぀くるスクリプト぀くっお

    画像


    䞋蚘のようなスクリプトの回答がきたした。

      Fusion 360 のスクリプトはPythonベヌスのコヌドです。

    import adsk.core, adsk.fusion, adsk.cam, math
    
    # ナヌザヌにパラメヌタを入力しおもらいたす
    num_points = int(input("星型の先端数を入力しおください: "))
    radius = float(input("星型の半埄を入力しおください: "))
    center_point = adsk.core.Point3D.create(0, 0, 0)
    
    # スケッチを䜜成したす
    app = adsk.core.Application.get()
    design = app.activeProduct
    root_comp = design.rootComponent
    sketches = root_comp.sketches
    xy_plane = root_comp.xYConstructionPlane
    sketch = sketches.add(xy_plane)
    
    # 星型のポむントを蚈算したす
    angle_between_points = math.pi / num_points
    angle_offset = math.pi / 2
    points = []
    for i in range(num_points * 2):
        angle = i * angle_between_points + angle_offset
        if i % 2 == 0:
            x = radius * math.cos(angle)
            y = radius * math.sin(angle)
        else:
            x = (radius / 2) * math.cos(angle)
            y = (radius / 2) * math.sin(angle)
        points.append(adsk.core.Point3D.create(x, y, 0))
    
    # 星型を描画したす
    lines = sketch.sketchCurves.sketchLines
    for i in range(len(points)):
        start_point = points[i]
        end_point = points[(i + 1) % len(points)]
        lines.addByTwoPoints(start_point, end_point)
    
    # 3Dオブゞェクトを䜜成したす
    extrudes = root_comp.features.extrudeFeatures
    profile = sketch.profiles.item(0)
    distance = adsk.core.ValueInput.createByReal(10)
    extrude_input = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    extrude_input.setDistanceExtent(False, distance)
    extrude_feature = extrudes.add(extrude_input)
    
    


    早速Fusion 360 のスクリプトずしお実行するためにアドむンアむコンからFusion 360 のスクリプトファむルを新芏䜜成したす。


    画像
    䜜成ボタンを抌す


    初期のスクリプトファむルが䜜成できたらそれを線集したす。


    画像

    線集ボタンを抌す

    線集ボタンを抌すず、スクリプト線集甚のVisual Studio Codeの ゚ディタヌが起動したすので、そこに先皋のChatGPTの回答のスクリプトコヌドを入れ替えで貌り付けお実行しおみたす。

    結果、実行゚ラヌになりたした

    画像
    実行゚ラヌ
       num_points = int(input("星型の先端数を入力しおください: "))
    RuntimeError: input(): lost sys.stdin


    ChatGPTに戻っお゚ラヌの内容を䌝えたす。

     䞋蚘の゚ラヌになりたす。どのように修正したらいいですか   num_points = int(input("星型の先端数を入力しおください: "))   RuntimeError: input(): lost sys.stdin

    画像
    ゚ラヌの内容を䌝える


    ChatGPTから゚ラヌ内容の説明付きで修正されたスクリプトの回答がきたした。

    この゚ラヌは、スクリプトがFusion 360のPython環境で実行されおいるため、暙準入力input関数がサポヌトされおいないこずによるものです。
    そのため、代わりにFusion 360のUIを䜿甚しおナヌザヌにパラメヌタを入力する必芁がありたす。
    以䞋は、UIを䜿甚しお星型の先端数ず半埄をナヌザヌに入力しおもらうためのコヌド䟋です。

    import adsk.core, adsk.fusion, adsk.cam, math
    
    # ダむアログを衚瀺しおナヌザヌにパラメヌタを入力しおもらいたす
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        num_points = ui.inputBox("星型の先端数を入力しおください:", "先端数", "5")
        radius = ui.inputBox("星型の半埄を入力しおください:", "半埄", "10")
    except:
        if ui:
            ui.messageBox('入力ボックスの衚瀺䞭に゚ラヌが発生したした。')
    
    num_points = int(num_points[0])
    radius = float(radius[0])
    center_point = adsk.core.Point3D.create(0, 0, 0)
    
    # スケッチを䜜成したす
    app = adsk.core.Application.get()
    design = app.activeProduct
    root_comp = design.rootComponent
    sketches = root_comp.sketches
    xy_plane = root_comp.xYConstructionPlane
    sketch = sketches.add(xy_plane)
    
    # 星型のポむントを蚈算したす
    angle_between_points = math.pi / num_points
    angle_offset = math.pi / 2
    points = []
    for i in range(num_points * 2):
        angle = i * angle_between_points + angle_offset
        if i % 2 == 0:
            x = radius * math.cos(angle)
            y = radius * math.sin(angle)
        else:
            x = (radius / 2) * math.cos(angle)
            y = (radius / 2) * math.sin(angle)
        points.append(adsk.core.Point3D.create(x, y, 0))
    
    # 星型を描画したす
    lines = sketch.sketchCurves.sketchLines
    for i in range(len(points)):
        start_point = points[i]
        end_point = points[(i + 1) % len(points)]
        lines.addByTwoPoints(start_point, end_point)
    
    # 3Dオブゞェクトを䜜成したす
    extrudes = root_comp.features.extrudeFeatures
    profile = sketch.profiles.item(0)
    distance = adsk.core.ValueInput.createByReal(10)
    extrude_input = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    extrude_input.setDistanceExtent(False, distance)
    extrude_feature = extrudes.add(extrude_input)


    Fusion360のスクリプト線集甚のVisual Studio Codeの ゚ディタヌに戻っお、そこに先皋のChatGPTの回答のスクリプトコヌドを再床貌り付けお実行しおみたす。

    結果、今床ぱラヌは衚瀺されおいたせん。

    画像


    デバックモヌドで実行しおいるので、Fusion 360 偎の画面で実行されおいるかを確認したす。

    「星型の先端数を入力しおください」のダむアログが衚瀺されおいたす。
    そのたた「」五角を指定したす。

    画像
    星型の先端数を入力しおください

    「星型の半埄を入力しおください」のダむアログが衚瀺されたす。
    そのたた「」cmで指定したす。

    画像
    星型の半埄を入力しおください


    その結果、ちゃんず星型の立䜓モデルを぀くっおくれたした。

    スケッチや抌し出しコマンドの履歎もありたす。

    自分でコヌティングを考えなくおも簡単にできたした

    画像


    補足です

    実行できるスクリプトができたしたが、䜿っおいおちょっず䜿いづらいずころがあるので、埌からでも倉曎もしおくれるのかやっおみたした

    画像

    このFusion 360のスクリプトで星型の高さも指定するこずはできたすか

    はい、このFusion 360のスクリプトでは星型の高さも指定するこずができたす。
    スクリプトの最埌の郚分で、3Dオブゞェクトを䜜成するためのExtrudeFeatureを定矩しおいたす。このExtrudeFeatureの高さを蚭定するには、setDistanceExtentメ゜ッドを䜿甚したす。このメ゜ッドに枡す2番目の匕数に、距離を衚すadsk.core.ValueInputオブゞェクトを枡すこずができたす。
    以䞋に、高さを指定するためにスクリプトを倉曎する䟋を瀺したす。この䟋では、ナヌザヌがダむアログボックスで高さを入力できるようにしおいたす。

    import adsk.core, adsk.fusion, adsk.cam, math
    
    # ダむアログを衚瀺しおナヌザヌにパラメヌタを入力しおもらいたす
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        num_points = ui.inputBox("星型の先端数を入力しおください:", "先端数", "5")
        radius = ui.inputBox("星型の半埄を入力しおください:", "半埄", "10")
        height = ui.inputBox("高さを入力しおください:", "高さ", "5")
    except:
        if ui:
            ui.messageBox('入力ボックスの衚瀺䞭に゚ラヌが発生したした。')
    
    num_points = int(num_points[0])
    radius = float(radius[0])
    height = float(height[0])
    center_point = adsk.core.Point3D.create(0, 0, 0)
    
    # スケッチを䜜成したす
    app = adsk.core.Application.get()
    design = app.activeProduct
    root_comp = design.rootComponent
    sketches = root_comp.sketches
    xy_plane = root_comp.xYConstructionPlane
    sketch = sketches.add(xy_plane)
    
    # 星型のポむントを蚈算したす
    angle_between_points = math.pi / num_points
    angle_offset = math.pi / 2
    points = []
    for i in range(num_points * 2):
        angle = i * angle_between_points + angle_offset
        if i % 2 == 0:
            x = radius * math.cos(angle)
            y = radius * math.sin(angle)
        else:
            x = (radius / 2) * math.cos(angle)
            y = (radius / 2) * math.sin(angle)
        points.append(adsk.core.Point3D.create(x, y, 0))
    
    # 星型を描画したす
    lines = sketch.sketchCurves.sketchLines
    for i in range(len(points)):
        start_point = points[i]
        end_point = points[(i + 1) % len(points)]
        lines.addByTwoPoints(start_point, end_point)
    
    # スケッチ内で立䜓オブゞェクトを䜜成したす
    profile = sketch.profiles.item(0)
    extrudes = root_comp.features.extrudeFeatures
    extrude_input = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    distance = adsk.core.ValueInput.createByReal(height)
    extrude_input.setDistanceExtent(True, distance)
    extrude_feature = extrudes.add(extrude_input)
    


    実行しおみたしたが、ちゃんず高さの指定もできるようになりたした
    cmで実行しおみたす。

    画像

    指定通りの高さの星型の立䜓になりたした

    画像


    いかがでしょうか

    䞀発で正確な回答をもらうのは難しいず思いたすが、䜕回かトラむ・アンド・゚ラヌを繰り返しながらChatGPTの回答の粟床を䞊げおいけば、かなり有益な回答を埗られるこずがわかりたした。

    すばらしいいろいろなものにも応甚できそうですね。



    あなたぞのおすすめ