I wan’t to make some sort of undo for my script but I can’t figure out how to make a copy of some object that will hold all data of originall object. In the same time, changes that I make on original object must not reflect in that copied object and that copyed object must not be linked to any scene.
in Blender 2.43 Iv added a copy function to as many datatypes as possible.
ob_new = ob.copy()
This links to the same object data, but will not be in any scenes.
to copy object data also do.
ob_new.link(ob.getData(mesh=1).copy())
Make sure the object data supports copy- of course empty object wont.
Wow, this was fast Thanks ideasman, I’ll try that right now
A nother, more general question… will delete(somevar) or somevar=None actually free memory that was reserved for that var? I have noticed that blender uses more and more memory when I’m using some scripts that I have wroted and I would like to know how to free that memory. Sorry if this is question for some general Python forum…
del var1, var2
will remove a reference to these variables, and if there not stored somewhere else (in a list) for instance, they will be free’d
Python however can be a bit greedy and not free the ram thats not used (it will keep it to use later on)
a good way to avoid hogging memory is to avoid making large lists where possible, use xrange instead of range dict.iterkeys() rather then .keys(), use Mesh instead of NMesh
Thanks for the tips ideasman. I’ll see what I can do for my scripts
ah nice that copy is there now, cheers ideasman again.
i think the common way to use it is via the copy() function in the copy module:
In [1]: import copy
In [4]: class foo:
...: def __init__(self, bar):
...: self.bar = bar
...:
In [5]: f1 = foo('1')
In [6]: f2 = copy.copy(f1)
In [7]: f2.bar
Out[7]: '1'
i guess calling copy yourself works fine too, but afaik usually those things are called with functions … like you usually see str(thing) and not thing.str() but dunno if it matters really(?)
if you ever encounter probs with references to same things that the copies have being still shared, you need deepcopy(), which is in the standard copy module too.
~Toni
antont, agree, it was never decieded weather .copy and copy() should coexist
You can import the copy module
from copy import copy
ob_new =copy(ob)