Page MenuHome

[Blender Python Image API] Image Buffer duplicate
ClosedPublic

Authored by Kilon Alios (kilon) on Jun 15 2018, 11:50 AM.

Details

Summary

I added the ability to duplicate ImBuf object as request by ideasman in

Devtalk Python Image API thread

also see Manifest

Example code

py

>>> import imbuf
>>> import copy
>>> i1 = imbuf.new([12,12])
>>> i2 = i1.copy()
>>> i3 = i1.deepcopy()
>>> i4 = copy.copy(i1)
>>> i5 = copy.deepcopy(i1)
>>> i1
<imbuf: address=0x00000131224890B8, filename='', size=(12, 12)>
>>> i2
<imbuf: address=0x00000131224890B8, filename='', size=(12, 12)>
>>> i3
<imbuf: address=0x0000013122489AC8, filename='', size=(12, 12)>
>>> i4
<imbuf: address=0x00000131224890B8, filename='', size=(12, 12)>
>>> i5
<imbuf: address=0x000001312248A4D8, filename='', size=(12, 12)>
>>> i1.resize([24,24])
>>> i1
<imbuf: address=0x00000131224890B8, filename='', size=(24, 24)>
>>> i2
<imbuf: address=0x00000131224890B8, filename='', size=(24, 24)>
>>> i3
<imbuf: address=0x0000013122489AC8, filename='', size=(12, 12)>
>>> i4
<imbuf: address=0x00000131224890B8, filename='', size=(24, 24)>
>>> i5
<imbuf: address=0x000001312248A4D8, filename='', size=(12, 12)>

Diff Detail

Repository
rB Blender

Event Timeline

Kilon Alios (kilon) edited the summary of this revision. (Show Details)Jun 15 2018, 11:53 AM
Kilon Alios (kilon) edited the summary of this revision. (Show Details)
Kilon Alios (kilon) retitled this revision from Blender Python Image API to [Blender Python Image API] Image Buffer duplicate.Jun 15 2018, 12:26 PM

This should be implemented as a method to fit with Python's convention.

example with a Python list:

ls_a = [1, 2, 3]
ls_b = ls_a.copy()

The same method can be used for __copy__ and __deepcopy__ which Python uses https://docs.python.org/3/library/copy.html

See: https://developer.blender.org/diffusion/B/browse/master/source/blender/python/mathutils/mathutils_Vector.c$1325 for an example of this.

Campbell Barton (campbellbarton) requested changes to this revision.Jun 15 2018, 5:34 PM
This revision now requires changes to proceed.Jun 15 2018, 5:34 PM

This should be implemented as a method to fit with Python's convention.

example with a Python list:

ls_a = [1, 2, 3]
ls_b = ls_a.copy()

The same method can be used for __copy__ and __deepcopy__ which Python uses https://docs.python.org/3/library/copy.html

See: https://developer.blender.org/diffusion/B/browse/master/source/blender/python/mathutils/mathutils_Vector.c$1325 for an example of this.

I completely agree and it was my first choice but after some struggling, I decided to do it the more easy way. I will try to convert it to a method. Thanks for the example code, it really helps.

Kilon Alios (kilon) updated this revision to Diff 11252.EditedJun 15 2018, 10:24 PM

I think I got it right

I am not so sure about %%%deepcopy%%% , it seems to work as expected but because copy.deepcopy() expect memo as an argument, I have declare it but I have not dared to do anything with it because it suppose to affect the id and memory of python object and from what I read its easy to get bugs from it and I dont want to mess with CPython internals.

So ` ImBufInstance.copy() `, ` ImBufInstance.copy() ` , will reference the same image buffer which is located in the same adress , while ` ImBufInstance.deepcopy() ` and ` ImBufInstance.deepcopy(memo) ` will just duplicate the buffer, I think that is closer to the standard Python behavior than having duplication in both cases of copy and deepcopy.

and of course the same way it work for

copy.copy(ImBufInstance)
copy.deepcopy(ImBufInstance)

I tested with resize it seems to work as expected

and a minor modification to the header file, needed

Kilon Alios (kilon) edited the summary of this revision. (Show Details)Jun 15 2018, 10:38 PM
Kilon Alios (kilon) edited the summary of this revision. (Show Details)
Kilon Alios (kilon) edited the summary of this revision. (Show Details)

the diffs you send replace the previous ones completely, they don't add up, so now it's just a header change ,may i suggest switching to arc?

Kilon Alios (kilon) updated this revision to Diff 11255.EditedJun 15 2018, 10:53 PM

I am still trying to get the hang of TortoiseGit , is this one better ?

looks like an incremental from the last diff now.. still not good.

Kilon Alios (kilon) added a comment.EditedJun 15 2018, 11:00 PM

looks like an incremental from the last diff now.. still not good.

I cannot understand how you want it to look , its a diff from the first one, its not incremental, at least not to me. Unless you mean something different.

Diff4 removes the code added from Diff1 and adds the relevant code.Unfortunately I dont know how to delete Diffs here, or if it is even possible.

it always has to be a complete diff that can applied to master or 2.8 (or better use arc since it adds all contextual information)

Kilon Alios (kilon) abandoned this revision.EditedJun 16 2018, 1:29 AM

Cant make git or anything make me the damn diff, effort abandoned after 3 hours of suffering. Well at least I learned a lot about git and tortoise git 😆. Code works fine, but I don't want to waste any more time on such a silly thing like a patch, I have bigger fish to fry. If anyone wants to pick up the code as is, awesome , if not, awesome too.

It was fun.

I think I found a way to solve this, the easy way, now I have a clearer mind after a good sleep

Kilon Alios (kilon) updated this revision to Diff 11261.EditedJun 16 2018, 9:36 AM

ok this diff should work for you. I had to revert the files back and forth but its finally as it supposed to be, I hope . It seems after midnight I go to stupid mode. Thank you for your patience and understanding 😆

Campbell Barton (campbellbarton) requested changes to this revision.Jun 17 2018, 1:17 PM
Campbell Barton (campbellbarton) added inline comments.
source/blender/python/generic/imbuf_py_api.c
123

Instead of checking for NULL, you should error if this is ever the case, add `PY_IMBUF_CHECK_OBJ(self); at the top of the function.

The return value should always be an image.

154–184

There should not need to be an entirely duplicated function and docs.

See how Vector_deepcopy works.

This revision now requires changes to proceed.Jun 17 2018, 1:17 PM
Kilon Alios (kilon) updated this revision to Diff 11274.EditedJun 18 2018, 11:31 PM

Changed as instructed but still not sure for the implementation of deepcopy, it works well in python. I dont parse arguments to make it possible for both _ _ deepcopy _ _ (it takes 1 python argument) and deepcopy (it takes no python argument) to be implemented by the same c function. I have no idea how else to do it. Your vector example code implemented only _ _ deepcopy _ _ and not deepcopy. I thought it would be better to support both methods for compliance with the python standards.

Thanks, committed rBdf237b964b9c24c9ab315f6f31bf67990f1c2f7e

Simplified logic and added reusable PyC_CheckArgs_DeepCopy rBa69f985f40905b01cf542d370831e7ade138a4e3

This revision is now accepted and ready to land.Jun 26 2018, 9:33 AM
Kilon Alios (kilon) marked 2 inline comments as done.Jun 26 2018, 10:01 AM

Changed as instructed but still not sure for the implementation of deepcopy, it works well in python. I dont parse arguments to make it possible for both _ _ deepcopy _ _ (it takes 1 python argument) and deepcopy (it takes no python argument) to be implemented by the same c function. I have no idea how else to do it. Your vector example code implemented only _ _ deepcopy _ _ and not deepcopy. I thought it would be better to support both methods for compliance with the python standards.

Thank you too, for helping me understand :)