spakin / SimpInkScr

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

crash loading new script #20

Closed peterennis closed 2 years ago

peterennis commented 2 years ago

captured in file:

simp_ink_err.pdf

Steps:

  1. Load and run 100 shapes sample (Inkscape 1.1.1)
  2. Delete code and paste another example
  3. Run new code

Error.

Needs a button to clear the existing script ???

spakin commented 2 years ago

I'm unable to reproduce this problem with Inkscape 1.1.1 (3bf5ae0, 2021-09-20) on Ubuntu 21.04. However, based on your PDF attachment, I have a guess as to the source of the error you're seeing. I suspect that

  1. The error still occurs without your steps 1 and 2. That is, your new code alone leads to the error.

  2. You upgraded Simple Inkscape Scripting since writing the code that's now failing (or at least since reading the API documentation).

Version 2.0.0 of Simple Inkscape Scripting introduced a number of breaking API changes. Among these, polyline now accepts a Python list of coordinates rather than individually specified coordinate arguments. The code peeking through in your screenshot's Python code box appears to use the pre-v2.0.0 API. Try wrapping the coordinate arguments to polyline in square brackets (e.g., polyline((60, 75), (100, 20), (140, 75))polyline([(60, 75), (100, 20), (140, 75)])).

Does that make the error go away?

peterennis commented 2 years ago

Your fix made the error go away.

Result in file:

simp_ink_err2.pdf

It does not remove the previous contents.

  1. Is there a command to add at the top of the script to start from scratch?
  2. Can a "Clear" button be added to the window?

I am new to python scripting and want to see how it works by copy-paste a lot of small examples such as you provide and iterate from there.

Congratulations and thanks for creating a very useful tool!

spakin commented 2 years ago

Your fix made the error go away.

Great!

It does not remove the previous contents.

  1. Is there a command to add at the top of the script to start from scratch?
  2. Can a "Clear" button be added to the window?

I believe the following code can be added to the top of a script to delete all existing content on the page:

for elt in svg_root.xpath('//*'):
    if isinstance(elt, inkex.ShapeElement) and not isinstance(elt, inkex.Layer):
        elt.getparent().remove(elt)
peterennis commented 2 years ago

I created a Clear script as a py file, loaded it and ran apply. The interface shows --and/or-- so I expected the program to run the file "and" follow up with the textbox contents.

However, the screen is cleared but the script in the box does not run.

Screenshot 2021-12-28 191203

The script file is here: https://github.com/peterennis/SimpInkScr/commit/9f4d26ceb357cb389c1f7b1f7395051a33c4c706 even if Apply is clicked multiple times.

peterennis commented 2 years ago

I continued and started loading your examples to see how they behave.

  1. Loaded the script animation.py, click apply and no text box code was visible.
  2. Zoom to show all the drawing.
  3. Load binary_tree.py, click apply, and now the text box content is drawn.

Screenshot 2021-12-28 200917

Maybe some problem with absolute vs. relative path, Clear sample not in examples folder, or Zoom drawing causing redraw to trigger ???

It appears my understanding of --and/or-- is correct but the sequence/setup of my testing is tickling a bug.

spakin commented 2 years ago

I created a Clear script as a py file, loaded it and ran apply. The interface shows --and/or-- so I expected the program to run the file "and" follow up with the textbox contents.

Yes, that's correct.

However, the screen is cleared but the script in the box does not run.

I was indeed able to reproduce this. My hypothesis is that Inkscape is creating a group in which to add the output of a "generate" extension (like Simple Inkscape Scripting) right before running the extension. The sample code then deletes this new group so the generated shapes have no place to go. The following modification of the clear-all code avoids deleting empty groups. Let me know if it works when you try it.

for elt in svg_root.xpath('//*'):
    if not isinstance(elt, inkex.ShapeElement):
        continue
    if isinstance(elt, inkex.Layer):
        continue
    if isinstance(elt, inkex.Group) and len(elt) == 0:
        continue
    elt.getparent().remove(elt)
  1. Loaded the script animation.py, click apply and no text box code was visible.
  2. Zoom to show all the drawing.
  3. Load binary_tree.py, click apply, and now the text box content is drawn.

I can't reproduce this behavior at all. However, animation.py is a bit odd in that it alters the page's width and height so it's believable that some difference between your setup and mine could affect this script's operation. My questions for you are as follows:

  1. Do you observe the same behavior if you use a script other than animation.py as the first script?
  2. Do you observe the same behavior if you edit animation.py to include the line svg_root.set('viewBox', '0 0 1024 768') right after svg_root.set('height', 768)?
peterennis commented 2 years ago
  • Do you observe the same behavior if you use a script other than animation.py as the first script?
  1. Load 001_SP100Diamonds and Apply
  2. Copy 002_Clear.py into the textbox
  3. Load 003_InkLineaMan.py
  4. Apply

Looks like it is fixed.

Screenshot 2021-12-30 193725

Code here: https://github.com/peterennis/SimpInkScr/commit/1c39c50c4be4ec5557169e6d00e1ae47464960d6

I will add another comment for the second question after more testing.

peterennis commented 2 years ago

2. Do you observe the same behavior if you edit animation.py to include the line svg_root.set('viewBox', '0 0 1024 768') right after svg_root.set('height', 768)?

  1. Load animation.py with sketch man in text box, Apply
  2. Zoom drawing
  3. Add viewbox code to animation.py, Apply

![Screenshot 2021-12-31 132420](https://user-images.githubusercontent.com /140737/147839254-eab36ac9-65ae-465d-8f90-8d6f9647b16f.png) Screenshot 2021-12-31 132718 Screenshot 2021-12-31 141552

If this is as expected, you can close this issue.

I have questions about the animation and will open a new issue.

Happy New Year 🎉

spakin commented 2 years ago

If this is as expected, you can close this issue.

Thanks for testing that. Before I close the issue, though, you had reported that the initial problem was that you "Loaded the script animation.py, click apply and no text box code was visible.". Just to confirm: if animation.py contains the viewBox line, then the text-box code is visible, while before it wasn't, right?

Happy New Year to you, too.

peterennis commented 2 years ago

No.

  • Load animation.py with sketch man in text box, Apply

No viewbox added. Code is still visible in textbox.

3. Add viewbox code to animation.py, Apply

Commit here: https://github.com/peterennis/SimpInkScr/commit/f69df22974fe1df3b20bd6ac06cc285fc1c7e2e6 Code is still visible in textbox.

The screenshots in the last message show this.

It may be some interplay with Clear.py and not repeatable in the scenario above.

I will keep a lookout going forward.

spakin commented 2 years ago

It may be some interplay with Clear.py and not repeatable in the scenario above.

Okay. So should I close the issue or leave it open?

peterennis commented 2 years ago

Okay. So should I close the issue or leave it open?

I am closing it.

Thanks