b-init / ImagePaste

A simple Blender addon to grab images from your clipboard and paste as a reference image in viewport or onto the image editor.
GNU General Public License v3.0
246 stars 9 forks source link

Improve clipboard operation speed on Windows #32

Open b-init opened 2 years ago

b-init commented 2 years ago

I'm working on a python utility to interact with the windows clipboard. It doesn't depend on 3rd party utilities and should be much faster and more flexible than our current approach. The module (clipette) only supports RGB bitmap images now but I'm slowly working on transparency and supporting more formats. After it's good enough, we can work on integrating it with ImagePaste. What do you think?

thanhph111 commented 2 years ago

Amazing, that's a new approach that I haven't thought about.

The script basically uses a shared library of Windows to access to OS APIs and then works with clipboard using them. I wonder if there's a way to do the same on macOS, a quick search brings me to nowhere.

b-init commented 2 years ago

For MacOS, there must be some official documentation about how a software is supposed to exchange clipboard data? (like there is for windows) And if so, there might also be a way to port that to a python script, given the tool to make that possible exists? (like ctypes for directly accessing windows' clipboard utility)

williamchange commented 2 years ago

On macOS this is done through the NSPasteboard https://developer.apple.com/documentation/appkit/nspasteboard which is what the pasteboard module uses, though I haven't looked further into whether this can be achieved like you have done through Windows using a shared library that's native on the platform

atticus-lv commented 2 years ago

In my import addon , I use ctype to improve import speed. It is much faster(about 1ms-) than calling the shell (0.7s). But there is still some encode problem with it https://github.com/atticus-lv/super_io/blob/master/clipboard/clipboard.py#L293

b-init commented 2 years ago

Update: Transparency works now. I've added support for Bitmaps and PNG (both copying and pasting) which is most of what we'll need.

The current hurdles are integrating it with blender. The way I've used ctypes doesn't seem to work through blender, will need to figure that out. Another problem is that blender doesn't support bitmaps with alpha. That's a problem because lot ot the times transparent images are only available on clipboard as bitmaps and I can only save that as a bitmap. But since blender can't read or convert this transparent bitmap, it's stuck.

b-init commented 2 years ago

It's done! Check out the clipette branch and let me know what you think @thanhph111

Sorry if I've not followed any formatting conventions or for the poor structure of the clipette module. I just don't think I properly understand those things yet.

thanhph111 commented 2 years ago

Wow, amazing! This will be a significant upgrade. I'll take a look this weekend. Don't worry about the minor format stuff, you've done most of the work.