Object references changing back and forth



  • 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:

    1. Why the heck is this happening? Is it on purpose? And what purpose might that be?
    2. 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



  • On 30/03/2018 at 01:19, xxxxxxxx wrote:

    Hi Frank,

    It's how python work, actually in python pretty much everything is a reference to a python Object which itself point to a c4d Object.
    Which means each time you create a new variable you are creating a new reference to this python object, so it's why memory address of each variable is different because you actually storing a reference and not the object itself.

    You can use GetGUID() or even use FindUniqueID(c4d.MAXON_CREATOR_ID) wich is the same as getting the internal GeMarker.
    For a more in-depth comprehension of the need of two, I suggest you to read this thread and the GeMarker Manual.

    Hope it's help and let me know if you have any problems,

    Cheers,
    Maxime


Log in to reply