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
251 stars 9 forks source link

Fix non English filepath error in windows #21

Closed atticus-lv closed 3 years ago

atticus-lv commented 3 years ago

add suppport for different encoding system(For Example,'GBK') in Windows using local.getdefaultlocale() Now can import image like 测试图像.png and not raise Index Error again.

Proposed changes

Screenshots/Screencasts

https://user-images.githubusercontent.com/64118610/135513404-26a2c9a0-c2bd-4df4-a3ca-6fd98dcb11d1.mp4

Further comments

thanhph111 commented 3 years ago

And one more thing we should consider. On my machine, if we don't check this (in fact, users don't usually use it):

image

the Blender will get an error:

Error: Python: Traceback (most recent call last):
  File "C:\Users\thanh\AppData\Roaming\Blender Foundation\Blender\2.93\scripts\addons\ImagePaste\imagepaste\operators.py", line 188, in execute
    bpy.ops.object.load_reference_image(filepath=image.filepath)
  File "C:\Users\thanh\Documents\Blender\2.93\scripts\modules\bpy\ops.py", line 132, in __call__      
    ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Error: Error: Cannot read 'C:\Users\thanh\Desktop\????.jpg': Invalid argument

because the subprocess decode wrong from 测试图像.jpg to ????.jpg.

import locale
locale.getdefaultlocale()
# ('en_GB', 'cp1252')

Do you have any clue we can also solve that?

atticus-lv commented 3 years ago

I think the cp1512 will not contain the Chinese char in its coding.So it works bad. Chinese char is default set in coding cp936

locale.getdefaultlocale()
# ('zh_CN', 'cp936')
locale.getdefaultlocale()
# after toggle,support non Chinese ('zh_CN', 'cp65001')

May be we should set chcp 65001 to the powershell to change its coding before it runs. So we can default run 'utf-8'

thanhph111 commented 3 years ago

Yeah, you're right. But we should force it to utf-8 even from PowerShell and then subprocess can read it also with utf-8, no matter what the system's encoding is. I'm on it now.

thanhph111 commented 3 years ago

Can you check that is OK on your side?

atticus-lv commented 3 years ago

Can you check that is OK on your side?

Good, it works find.

thanhph111 commented 3 years ago

Great! I think we can have the merge now. The patch will be release this weekend. Thanks for your contributions!