Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show an error message when there is no selected object but a layer #33

Closed
OpenFoam-User opened this issue Jan 16, 2022 · 7 comments
Closed

Comments

@OpenFoam-User
Copy link

OpenFoam-User commented Jan 16, 2022

I am trying the recent API of SimpInkScr using Inkscape 1.2-dev:

objs = svg_root.selection

for obj in objs:
   obj = inkex_object(obj)
   obj.rotate(50, 'center')

The document is empty but I have selected the Layer 1 from "Object > Layer and objects" as shown below

image

But I am getting this confusing error message (I believe this could happen, if the layer is accidently selected intead of an object).

Is there an easy solution to get a meaninful error message to inform the user that the selection is a layer so it couldn't be transformed?

-------------------------------------------------------------------------------
simple_inkscape_scripting.py 1709 <module>
SimpleInkscapeScripting().run()

base.py 147 run
self.save_raw(self.effect())

simple_inkscape_scripting.py 1705 effect
exec(code, sis_globals)

<string> 4 <module>


simple_inkscape_scripting.py 1540 inkex_object
return SimpleObject(obj, merged_xform, conn_avoid, clip_path,

simple_inkscape_scripting.py 279 __init__
_simple_top.append_obj(self)

simple_inkscape_scripting.py 187 append_obj
self._svg_attach.append(obj._inkscape_obj)

etree.pyx 825 lxml.etree._Element.append


apihelpers.pxi 1341 lxml.etree._appendChild


ValueError:
cannot append parent to itself
@spakin
Copy link
Owner

spakin commented Jan 17, 2022

Interesting. FYI, Inkscape 1.1.1 doesn't select the contents of a layer when the layer itself is selected; this is new in Inkscape 1.2-dev.

You may be surprised to learn that it's in fact perfectly valid to apply transformations to a layer. A layer is just a group that Inkscape marks with an inkscape:groupmode="layer" attribute. Layers look like ordinary groups to SVG viewers other than Inkscape. Hence, I believe the correct approach is for Simple Inkscape Scripting simply not to attach objects created with inkex_object to anything but leave them where they are.

This should take just a few lines of code. Working on that now…

@spakin spakin closed this as completed in b874f89 Jan 17, 2022
@OpenFoam-User
Copy link
Author

OpenFoam-User commented Jan 17, 2022

Thank you for your reply.
Yes, I am aware of the fact that layers in Inkscape are special groups with a specific attribute (I read that in your Wiki page and somewhere else).

Now if a layer is selected nothing happens (this is better than a mysterious error message) and my point was to inform the user that he is selecting a layer instead and not its items because that's very unlikely that his/her intent is to apply the transformation on the layer itself (on the special group). and because explicit is better than implicit to quote the zen of Python.

However, there is a small issue regarding the anchor parameter:

obj.rotate(50) # it doesn't show an error when the layer is selected

but:

obj.rotate(50, 'center') # fails with the error message below
-------------------------------------------------------------------------------
simple_inkscape_scripting.py 1770 <module>
SimpleInkscapeScripting().run()

base.py 147 run
self.save_raw(self.effect())

simple_inkscape_scripting.py 1766 effect
exec(code, sis_globals)

<string> 5 <module>


simple_inkscape_scripting.py 463 rotate
around = bbox.center

AttributeError:
'NoneType' object has no attribute 'center'

@spakin
Copy link
Owner

spakin commented Jan 18, 2022

I'm unable to reproduce either of the above with dcf1941. Here's a sample image in Inkscape 1.2-dev in which I selected the layer:
before

I then ran the Simple Inkscape Scripting script.

objs = svg_root.selection
for obj in objs:
   obj = inkex_object(obj)
   obj.rotate(50)

This correctly rotated the layer around the origin:
after-50

I then ran

objs = svg_root.selection
for obj in objs:
   obj = inkex_object(obj)
   obj.rotate(50, 'center')

and Inkscape correctly rotated the layer around its center:
after-50-center

Do you observe different behavior from Simple Inkscape Scripting dcf1941?

@OpenFoam-User
Copy link
Author

Hi

Sorry I forget to mention in my last post that I am referring to an empty Layer without objects (as in my first screenshot, first post).

I believe you could reproduce that error message by selecting an empty layer. (Unfortunately, the current behaviour of Inkscape 1.2 is a little bit strange, in order to select a layer you need to click two times (not double click), and I've read somewhere that they will revert that behaviour: a small demonstration that is not related to this issue is to try to remove a layer, when you click the remove button it's likely that nothing will happen even if the layer appears to be selected, you need to click again in oder to select it ).

@spakin
Copy link
Owner

spakin commented Jan 18, 2022

Sorry I forget to mention in my last post that I am referring to an empty Layer without objects (as in my first screenshot, first post).

Ah, I had overlooked that fact. I can now reproduce your problem. Reopening the issue…

@spakin spakin reopened this Jan 18, 2022
@spakin spakin closed this as completed in b531e26 Jan 18, 2022
@spakin
Copy link
Owner

spakin commented Jan 18, 2022

Simple Inkscape Scripting should now be able to transform an empty layer. Please reopen the issue if you find a case in which that's still failing.

@OpenFoam-User
Copy link
Author

I've tested the latest commit. It works perfctly.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants