THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 02/06/2011 at 08:24, xxxxxxxx wrote:
I just have a strange little problem. I already wrote a complete car-simulator in python, but the following really simple example just does not work.
What do I miss?
This code is in a Xpresso Node on a plane (can be any object), o is the Object-Link:
def main() :
m = o.GetMg()
m.off.y = m.off.y + 100
The two print-statements will output the same value for y ... and the position of the object will not change.
... is there a way to attach an example-scene ?
On 02/06/2011 at 09:03, xxxxxxxx wrote:
... creating my own matrix does not work either ??
m = o.GetMg()
mn = c4d.Matrix(m.off,m.v1,m.v2,m.v3)
mn.off.y = 500
The same output, y stays the same ...
On 02/06/2011 at 09:38, xxxxxxxx wrote:
In your example mn.off returns a new vector object where you change y. So the value is not assigned to the component of the internal vector of the Matrix.
Hope this helps.
On 02/06/2011 at 09:54, xxxxxxxx wrote:
mn.off is a method call ? I thought mn.off is just a reference to the c4d.Vector ?
On 02/06/2011 at 10:12, xxxxxxxx wrote:
Yes, it referres to the off component of a matrix.
But getting an attribute of an instance invokes the instances __getattr ibute __ method, so it returns a value.
Why is the returned Vector not the original instance ? If so, that would do it all:
mn.off.y = 20.
On 02/06/2011 at 10:22, xxxxxxxx wrote:
How can I know, that matrix.off will become a method call?
I can't see anything in the Matrix-API telling me that ?
So, for now, each time when I access a member, I will need to check if it returns me a copy or not?
On 02/06/2011 at 12:52, xxxxxxxx wrote:
No. Sorry, I didn't want to confuse you.
In Pyhton, most classes support a methode called "__getattribute__" which is invoked when you want to get an attribute. The returnvalue of this method is what you recieve from the attribute-acess.
The lower text was jsut for Sebastian.
No, you don't have to look it up. It's standart in Cinema 4D, and many other programs. I'm sure there is a sense, but I couldn't it figure out, yet.
If the original isntance would have been returned, and not a copy, changing the vectors value would take affect in the matrix, too.
That's how it's working:
m = c4d.Matrix(*[c4d.Vector(0) for i in xrange(4)])
v = m.off
v.x = 100
print m.off.x # 0
m.off.x = 100
print m.off.x # 0
m.off = v
print m.off.x # 100, what we wanted :)
More on Special Methods here.
PS: Yes it's wrong what i wrote in the previous post, not "getattr", instead "getattribute"
On 02/06/2011 at 13:36, xxxxxxxx wrote:
You didn't confuse me, it was the correct explanation
But assume the Vector class wouldn't consist of atomic values, instead of references to a simple value-container class.
Now, if I call v.x = 100 I again can't be sure, if I just set the value to a copy (of the simpe value-continer) or the actual container value of v (just if the API would state so). Who tells me, when there is a hidden call ??
This seems a pretty strange concept to me, as I can't be sure that a reference to an attribute contains (or, returns) a copy or not ...
On 02/06/2011 at 14:02, xxxxxxxx wrote:
You can be sure. It is as it is.
A matrix object does return a vector by calling i.e. m.off . But modifing this vector does not modify the original vector in the matrix.
A vector object does return an integer by calling i.e. v.x . But you can't modify this integer.
By setting a value to the vector, you set the value to the vector, not to a copy.
Omg, weird thing :S
btw, v.x = 100 invokes "v.__setattr__(100)", not "v.__getattribute__("x") = 100"
On 02/06/2011 at 14:45, xxxxxxxx wrote:
Ok. Now its really clear
Until now, I just got lucky, as I always created a vector first and then I set it on the matrix. This seems to be the first time, I tried to access a vector-component directly in this manner ...
On 02/06/2011 at 23:48, xxxxxxxx wrote:
It depends on the type and the level of access. If you call Matrix().off a copy (not the reference to the off vector) is returned. That yourmatrix.off.x=30 does not work is unfortunately a technical limitation. I will make a note for the docs.