spakin / SimpInkScr

Simple Inkscape Scripting
https://inkscape.org/~pakin/%E2%98%85simple-inkscape-scripting
GNU General Public License v3.0
320 stars 31 forks source link

Error when using 'from simpinkscr import *' in Visual Studio Code #110

Closed WilliamTylerBradley closed 10 months ago

WilliamTylerBradley commented 11 months ago

I'm getting an error message with the following code and running it through the command line in Visual Studio Code:

from simpinkscr import *

wd, ht = float(user_args[0]), float(user_args[1])
clr = user_args[2]
rect((0, 0), (wd, ht), fill=clr)

This is the same example from Command-line execution with from simpinkscr import * added at the front to get IntelliSense working.

The error message is:

File "C:\Program Files\Inkscape\share\inkscape\extensions\simple_inkscape_scripting\simpinkscr\simple_inkscape_scripting.py", line 2760, in <module>
    main()
  File "C:\Program Files\Inkscape\share\inkscape\extensions\simple_inkscape_scripting\simpinkscr\simple_inkscape_scripting.py", line 2756, in main
    SimpleInkscapeScripting().run()
  File "C:\Users\wtbra\anaconda3\envs\envs\Lib\site-packages\inkex\base.py", line 250, in run
    self.save_raw(self.effect())
                  ^^^^^^^^^^^^^
  File "C:\Program Files\Inkscape\share\inkscape\extensions\simple_inkscape_scripting\simpinkscr\simple_inkscape_scripting.py", line 2749, in effect
    exec(code, sis_globals)
  File "<string>", line 10, in <module>
  File "C:\Users\wtbra\anaconda3\envs\envs\Lib\site-packages\simpinkscr\simple_inkscape_scripting.py", line 2664, in rect
    return SimpleObject(obj, transform, conn_avoid, clip_path, mask,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wtbra\anaconda3\envs\envs\Lib\site-packages\simpinkscr\simple_inkscape_scripting.py", line 427, in __init__
    _simple_top.append_obj(self)
    ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'append_obj'

I can't replicate the error if I change from simpinkscr import * to import simpinkscr, import numpy, or from numpy import *. The basic example without adding in any packages also works fine. So, I think there's something catching in the code when everything is imported from simpinkscr, but I'm not sure what exactly is happening.

How can I get this code working? Or is there a better method to use Visual Studio Code?

spakin commented 11 months ago

I'm not a regular Visual Studio Code user, but I did just install it, and I was able to reproduce this problem.

I believe I have a solution, but I'll need your help testing it. First, remove the

from simpinkscr import *

line from your script. Second, configure your project's launch.json file to run Simple Inkscape Scripting itself, passing it your script name and other arguments:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "/home/user/.config/inkscape/extensions/simple_inkscape_scripting/simpinkscr/simple_inkscape_scripting.py",
            "args": ["--py-source=${file}", "--output=/home/user/my-output.svg", "/home/user/.config/inkscape/templates/default.svg", "75", "50", "chartreuse"],
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

Does that work when you try it? If so, I'll update the wiki page accordingly.

WilliamTylerBradley commented 11 months ago

While I did get everything running, I wouldn't use this setup. I'm not getting the IntelliSense working on the Python file after taking out the from simpinkscr import *, and that's the main goal for this issue. Using launch.json is more work than using the terminal in Visual Studio Code. So, I think I'd use that instead. (I don't need IntelliSense working to use this extension, but it would be great to have.)

spakin commented 10 months ago

I'm not getting the IntelliSense working on the Python file after taking out the from simpinkscr import *, and that's the main goal for this issue.

My apologies. I was so focused on getting Visual Studio Code working with Simple Inkscape Scripting that I forgot that the whole point was to work with IntelliSense.

I believe I have a partial solution. Try importing simpinkscr only if its symbols are not already loaded:

try:
    rect
except NameError:
    from simpinkscr import *

This should prevent the AttributeError: 'NoneType' object has no attribute 'append_obj' error you were observing because Simple Inkscape Scripting's symbols no longer will be spread across multiple Python namespaces. However, IntelliSense seems to work in a degraded mode. By that, I mean that name completion works and Ctrl-Space correctly presents Simple Inskcape Scripting function prototypes, but Visual Studio Code no longer shows which parameter you're typing as you're typing it.

Is that an acceptable approach?

WilliamTylerBradley commented 10 months ago

This works great for me. I think Ctrl-Space provides enough information.

As a side note, if you use:

try:
    rect
except NameError:
    import simpinkscr

simpinkscr.rect(...

the parameters show. I don't know why this why works, but the other doesn't.

I can add this into the documentation if you want.

spakin commented 10 months ago

Although I too see the parameters when I use that construct, the AttributeError has returned as well. Do scripts using that approach work when you run them?

I can add this into the documentation if you want.

Thanks, but I just added some text: https://github.com/spakin/SimpInkScr/wiki/Running#visual-studio-code

Does that accurately cover the key parts of our discussion?

WilliamTylerBradley commented 10 months ago

I do get an error, but I think that was listed in the previous version of the documentation. So it was expected.

The current documentation lists the approach that works for me. I think this issue can be closed if you are good with that. Thanks, I really appreciate it!