Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Use the Variable Name as an Argument

    General Talk
    3
    4
    823
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      bentraje
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • CairynC
        Cairyn
        last edited by

        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.

        1 Reply Last reply Reply Quote 1
        • ManuelM
          Manuel
          last edited by

          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

          MAXON SDK Specialist

          MAXON Registered Developer

          1 Reply Last reply Reply Quote 1
          • B
            bentraje
            last edited by

            @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!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post