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

Could Py5Shape objects have a .width and a .height using @property? #352

Closed
villares opened this issue Sep 22, 2023 · 8 comments · Fixed by #358
Closed

Could Py5Shape objects have a .width and a .height using @property? #352

villares opened this issue Sep 22, 2023 · 8 comments · Fixed by #358

Comments

@villares
Copy link
Contributor

villares commented Sep 22, 2023

It is well documented that Py5Image and Py5Graphics have .width and Py5Shape has .get_width() instead...

I suppose this is a way of showing dimensions of Py5Shapes are mutable, but I was a bit surprised and decided to check how it looks in Processing Java.

image

PShape objects have .width.

Could the interface perhaps be unified for ergonomic reasons? Or is it a bad idea?

@hx2A
Copy link
Collaborator

hx2A commented Sep 22, 2023

Sure, let's add it, along with height and depth. I checked the Processing code and I don't see any reason why we shouldn't. The getWidth() method just returns the width variable.

When I first started making py5 I had to go through literally thousands of public methods and variables to determine what should be included in the API and what should be left out. As I've learned more about Processing I've found places where I didn't make the right decision. Which is fine, it is an easy thing to correct.

All of the include / exclude decisions are stored in CSV files. In some cases there are comments explaining why something is the way it is.

@villares
Copy link
Contributor Author

villares commented Sep 23, 2023

This looks like something related I 'd like to investigate. I can't reproduce this at will yet, so I'll drop this here as a reminder of my thoughts.

py5 encountered an error in your code:

File "/home/villares/GitHub/sketch-a-day/2023/sketch_2023_09_23/sketch_2023_09_23.py", line 40, in draw
    24   def draw():
 (...)
    36           elif isinstance(thumb, py5.Py5Image):
    37               rw = thumb.width
    38               func = py5.image
    39           elif isinstance(thumb, py5.Py5Shape):
--> 40               w, h = thumb.get_width(), thumb.get_height()
    41               ratio = w / h
    ..................................................
     w = 64.0
     h = 64.0
    ..................................................

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

Note how weird, I know thumb is a Py5shape instance and not None, maybe something is going on inside get_width(). I'm almost sure w, and h values here come from a previous round of the loop (complete code here), so get_width() did not finish executing.

@hx2A
Copy link
Collaborator

hx2A commented Sep 23, 2023

How was the Py5Shape instance thumb created? It looks like the load_shape() call on line 104 is somehow returning a degenerate Py5Shape instance. If you can pin down this problem, please open an issue for it.

Remember, the Python class Py5Shape is really a wrapper around the Java class PShape. Here, thumb._instance is supposed to be a Java PShape instance but instead it is None. That shouldn't happen.

In general, Processing isn't so good at reading SVG files it did not create itself. It does not know the full SVG specification so your approach here will not work for all SVG files. One approach is to use py5.convert_image(). If you pass it the path to an SVG file, it will use Cairo (assuming you have it installed) to convert the SVG file to a Py5Image object. With a Py5Image object you don't get the nice scaling features that PIL Image objects have though. However, you can use py5.scale() to shrink it down to the thumbnail size you are looking for, or better yet draw a quad with the Py5Image as a texture.

@villares
Copy link
Contributor Author

Ohhh, this will be handy! I hadn't realised (or maybe I forgot) convert_image() could tackle SVG via pycairo. I do remember reading on the py5 installation page that having it installed would extend py5 capabilities. You are quite right about load_shape and SVG, it can be a downer, I have a second open issue on Processing Java about SVG parsing problems, a previous one was solved.

@hx2A
Copy link
Collaborator

hx2A commented Sep 24, 2023

I hadn't realised (or maybe I forgot) convert_image() could tackle SVG via pycairo.

It really ought to be better documented. That's why I want to create an "Integrations" section in the documentation to specifically document py5's integrations with other libraries. It's one of the strengths of the library, in that it can help educators like yourself branch out into the Python ecosystem within in the context of creative coding.

I have a second open issue on Processing Java about SVG parsing problems

Did you open a new issue? I can't find it. In any case, py5 should never return a malformed Py5Shape object. Right now it specifically checks for Java exceptions from Processing's loadShape() but not for nulls. I'll open an issue address that. It's a small fix.

@villares
Copy link
Contributor Author

Thank you! Not that new, not related to this, so I'll break the URL: https://github.com/processing/processing4/issues/ 750

I think other people will be able to add more integrations in the future. imageio has a plug-ins system: https://imageio.readthedocs.io/en/stable/reference/index.html#plugins-backend-libraries

@hx2A
Copy link
Collaborator

hx2A commented Sep 24, 2023

I think other people will be able to add more integrations in the future. imageio has a plug-ins system: https://imageio.readthedocs.io/en/stable/reference/index.html#plugins-backend-libraries

Yes! py5's convert_image() method and the soon to be added convert_shape() method both allow users to create new integrations with the register_image_conversion() method. Also, I thought it would be an easy way for contributors to write some code that would get added to py5.

imageio looks useful! Thanks for pointing out. Certainly reading SWF files would be a great addition to py5.

@hx2A
Copy link
Collaborator

hx2A commented Oct 1, 2023

So the reason why I didn't create width or height or depth properties is because (for the opengl renderers at least) the values Processing returns are 0.0:

    py5.println(s.width, s.height, s.depth)
    py5.println(s.get_width(), s.get_height(), s.get_depth())
0.0 0.0 0.0
1.135807752609253 2.82108998298645 5.444699764251709

But I can re-address this by creating properties in Python and having them call the Java methods getWidth(), etc. Still a small change to add this.

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

Successfully merging a pull request may close this issue.

2 participants