Euler bug??

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.

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[:]

fair enough!

Thanks for the help!

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]]


oh my!

That’s way confusing :-?

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]]
>>>


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'>

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