schUsseln
(schUsseln)
September 15, 2005, 5:32pm
1
Hi All,
Any explanation / rationale for why the following happens would be greatly appreciated!!!
import Blender
from Blender import Mathutils
from Blender.Mathutils import *
X = Euler([0, 0, 0])
Y = X
print X, Y
######################
##gives
#[0.0000, 0.0000, 0.0000]
# [0.0000, 0.0000, 0.0000]
######################
Y[1] = 27
print X, Y
######################
##gives
#[0.0000, 27.0000, 0.0000]
# [0.0000, 27.0000, 0.0000]
######################
why are these two vars linked?!? Recursively, at that!
I have to do some sloppy code when attempting to snap an object to one rotation and back again. Sort of annoying, but may be sensical…I dunno.
z3r0_d
(z3r0 d)
September 15, 2005, 7:52pm
2
BECAUSE THAT IS THE WAY PYTHON WORKS!!
it is not a problem with blender at all. When you assign objects [non-primitives … pretty much everything but int, float] they both point to the same object
some other fun things
a = range(2)
a[0] = a
print a
print a[0]
prints:
[[…], 1]
[[…], 1]
so a list can contain itself… good stuff
well anyway, the way you copy a list [but not the objects inside] is called a slice copy. Instead of Y = X do Y=X[:]
sornen
(sornen)
September 16, 2005, 3:50am
4
If you want a copy of the object than you have to slice it.
>>> x = [[1,2,3],[4,5,6]]
>>> y= x[:] #slice to obtain copy
>>> print y,x
[[1, 2, 3], [4, 5, 6]] [[1, 2, 3], [4, 5, 6]]
>>> y[0] = 2
>>> print y,x
[2, [4, 5, 6]] [[1, 2, 3], [4, 5, 6]]
>>> y[1][1] = 9 #but note that this is the same object as x[1][1]
>>> print y,x
[2, [4, 9, 6]] [[1, 2, 3], [4, 9, 6]]
sornen
(sornen)
September 16, 2005, 11:40pm
6
Not too confusing if you understand that the list [4,5,6] is the same object in x and y. So that changing elements of the list will change both x,y.
If you want a copy of everything in the list then python provides a deepcopy function. This will make a copy of everything.
>>> import copy
>>> x = [[1,2,3],[4,5,6]]
>>> y = copy.deepcopy(x)
>>> y[1][1] = 9
>>> print y,x
[[1, 2, 3], [4, 9, 6]] [[1, 2, 3], [4, 5, 6]]
>>>
schUsseln
(schUsseln)
September 19, 2005, 3:49pm
7
starting to feel like an idiot…
but it appears that slicing and deepcopy can’t be applied to Eulers.
>>x = Euler([0,0,0])
>>y = x[:]
>>print x, y
[0.0000, 0.0000, 0.0000]
[0.0, 0.0, 0.0]
>>y = copy.deepcopy(x)
Traceback (most recent call last):
File "Text", line 14, in ?
File "C:\PYTHON23\LIB\copy.py", line 220, in deepcopy
raise Error(
copy.Error: un(deep)copyable object of type <type 'euler'>
stiv
(stiv)
September 19, 2005, 5:47pm
8
I did not check this in 2.37a, but in cvs blender:
You can use the copy constructor to make a deep copy of an Euler:
x = Euler()
y = Euler( x )
Slicing an Euler yields a list, not an Euler.
x = Euler()
y = x[:]
print type(y), y