On 29/03/2018 at 03:03, xxxxxxxx wrote:
Hi,
I need to store lots of references to objects in a list and later iterate through the document and see if the objects I find are in the list. Sounds simple enough, right?
Well, it isn't. Object references in Python are changing back and forth between two memory addresses, which makes it difficult to recognize that it's actually the same object.
To demonstrate, here's a short script:
> import c4d
>
>
>
>
> def main() :
>
> ** print('First GetActiveObject() and then iterate:')**
>
> ** obj = doc.GetActiveObject()**
>
> ** for i in range(10) :**
>
> ** print obj**
>
> ** **
>
> ** print(' ')**
>
> ** print('Call GetActiveObject() during iteration:')**
>
> ** for i in range(10) :**
>
> ** obj = doc.GetActiveObject()**
>
> ** print obj**
>
>
>
>
> if __name__=='__main__':
>
> ** main()**
Import the script into Script Manager, and create a simple scene with one active object:
Now run the script and see the output in the console:
So, obviously, every time I call doc.GetActiveObject() (and there are other occasions, too, when it happens. This is just the simplest one), the BaseObject has the other of two different memory addresses? I don't get it.
So, two questions:
- Why the heck is this happening? Is it on purpose? And what purpose might that be?
- How can I do a comparison that actually tells me reliably that two object references point to the same object?
Thanks for any heads-up!
Cheers,
Frank