Use the Variable Name as an Argument



  • Hi,

    I have the following code that declares a variable on existing objects:

        scapula_proxy       = doc.SearchObject("scapula_proxy")
        shoulder_proxy      = doc.SearchObject("shoulder_proxy")
        elbow_proxy         = doc.SearchObject("elbow_proxy")
        wrist_proxy         = doc.SearchObject("wrist_proxy")
    

    As you can see, the scapula_proxy is redundant. Is there a way to streamline it like that one below (hypothetical):

    scapula_proxy       = doc.SearchObject(*Variable Name*)
    

    Not sure if this is possible, but would be happy if it is as I have several lines with this format.
    I also tried with the stackoverflow but the examples they have is a bit complicated and I ended up in a rabbit hole.

    Is there a way around this?

    Thank you for looking at my problem.



  • In C++ I would point you at macros, but Python has none (except for some libraries which I am not knowledgeable in). A macro could do a textual replacement allowing you to write something like
    searchobject(scapula_proxy)
    which would then be transformed into that desired line.

    However, I suppose that is not that easy to achieve in Python, as it is mixing up two levels: the souce code level and the application data level. (Not redundant either.)

    IMO it is not a good idea anyway to obfuscate the clear lines above by introducing some abbreviated codification. If you'd really need that you can write a preprocessor, but in the long term I don't think it's advisable.



  • hello,

    my suggestion is to use Dictionary.

        #Creates an list of object with value None
        myObjList = {"Cube.3": None , "Cube.2" : None , "Cube.1" : None, "Cube" : None }
        
        #Search the object in the document
        for key in myObjList.keys():
            myObjList[key] = doc.SearchObject(key)
        
        #Interates trough the list and do something
        for key, value in myObjList.items():
            if value is not None:
                print key, value
    

    You can also use exec to execute python (didn't tested it)

    While this is interesting, it's not related to Cinema4D but more to Python.

    Cheers
    Manuel



  • @Cairyn and @m_magalhaes

    Thanks for the responses. The dictionary route seems like a good workaround.
    I think I'm going with it.

    Have a great day ahead!