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: 

I want to set the initial values for the palette display

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
kandennti
609 Views, 8 Replies

I want to set the initial values for the palette display

Hi all.

 

I want to create an add-in that uses palettes, and I want to send the initial values for the palettes to be displayed from the python side, but I don't know how to do this.

 

In python, this is how I do it.

class MyHTMLEventHandler(adsk.core.HTMLEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):
        try:
            htmlArgs = adsk.core.HTMLEventArgs.cast(args)
            data = json.loads(htmlArgs.data)

            global _app
            if htmlArgs.action == 'start':
                args.returnData = 10
            elif htmlArgs.action == 'sliderChange':
                _app.log(data['value'])

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

 

In html, I do it like this.

<!DOCTYPE html>
<html>
  <head>
  </head>

  <body>
    <div>
      <p>Slider</p>
      <input type="range" min="1" max="149" step="1" id="slider"
        onchange="sliderChange()">
    </div>
    <br />
  </body>

  <script>
    window.onload = function(){
      var args = {}
      var angle = adsk.fusionSendData("start", JSON.stringify(args))
      slider.value = angle
    }
    
    function sliderChange() {
      var args = {
        value: slider.value
      }
      adsk.fusionSendData("sliderChange", JSON.stringify(args))
    }
  </script>
</html>

 

This is due to my lack of knowledge in html + javascript.
I have confirmed that no HTML Event is generated in python when the palette is displayed.

 

If you know anything about this, please let me know.

 

I've attached all the code. When the add-in is run, the command is added to the following part

1.png

 

8 REPLIES 8
Message 2 of 9
thomasa88
in reply to: kandennti

With the old web view in Fusion, you could right click and show the development console, showing you any Javascript errors. I'm not sure how it works in the new web view (using Chrome). I think you can force the old web view when creating the palette.

 

I have not touched this code in a long while, but I'm fairly certain it managed to fill data when the palette loaded. I went a bit overboard on the communication and made it a bit complex, but maybe it can give some ideas: https://github.com/thomasa88/VerticalTimeline

Message 3 of 9
kandennti
in reply to: thomasa88

Thank you, @thomasa88 .

 

I don't know if you can understand it, but it helps me because I couldn't find a sample.

 

Message 4 of 9
JeromeBriot
in reply to: thomasa88


@thomasa88  a écrit :

With the old web view in Fusion, you could right click and show the development console, showing you any Javascript errors. I'm not sure how it works in the new web view (using Chrome). I think you can force the old web view when creating the palette.


First you have to activate the DevTools in the text commands window.

 

@kandennti, here is another example : Check Computer Specifications

See at the end of the file CheckComputerSpecs.js

 

@thomasa88 do you succeed in using the returnData property of the HTMLEventArgs object?

How the JS code should intercept this data?

 

 

Message 5 of 9
thomasa88
in reply to: JeromeBriot

@JeromeBriotGood info!

Well, it looks like my code did handle the returnData, but have not run it since mid 2020:

Python, VerticalTimeline.py:

    if html_commands:
        htmlArgs.returnData = json.dumps(html_commands)


if html_commands:
htmlArgs.returnData = json.dumps(html_commands)

Javascript, palette.html

    function query(action, data = {}) {
        let ret = adsk.fusionSendData(action, JSON.stringify(data));
        console.log("RET:", ret);
        return JSON.parse(ret);        
    }
Message 6 of 9
JeromeBriot
in reply to: thomasa88

Message 7 of 9
kandennti
in reply to: JeromeBriot

Thank you, @JeromeBriot .

It was complicated and I was about to cry.
I haven't looked at it in detail because I've been so busy and exhausted with work, but it's a clear sample.


@thomasa88 .
I felt again how powerful you are.

 


Thanks to both of you. I really appreciate it.

 

Message 8 of 9
kandennti
in reply to: kandennti

It's been a long time coming, but I've published an add-in that I created using the method you taught me.
It is in Japanese.

https://github.com/kantoku-code/Fusion360_PerspectiveAngleController 

 

In order to be able to change the Perspective Angle, I really wanted to use a modaless dialog, so I used a palette.

Message 9 of 9
kandennti
in reply to: kandennti

I found out how to pass initial values in the new browser (QT Web Browser) and have attached a sample script.

 

The QT Web Browser is described as asynchronous in the documentation.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-6C0C8148-98D0-4DBC-A4EC-D8E03A8A3B5B 

1.png

 

 

  <script>
    document.addEventListener("DOMContentLoaded", () => {
      let adskWaiter = setInterval(() => {
        if (window.adsk) {
          clearInterval(adskWaiter);

          let element = document.getElementById("xxx");
          adsk
            .fusionSendData("DOMContentLoaded", "{}")
            .then((data) =>
              element.insertAdjacentHTML("afterend", "<p>" + data + "</p>")
            );
        }
      }, 100);
    });
  </script>

 

 

The attached sample shows the name of the active document.
I have also confirmed that the same processing can be done with the palette, although it is displayed with the Browser Command Input.

 

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