Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python 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

    escape / un-escaping strings

    Scheduled Pinned Locked Moved PYTHON Development
    5 Posts 0 Posters 418 Views
    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.
    • H Offline
      Helper
      last edited by

      On 12/03/2013 at 08:59, xxxxxxxx wrote:

      Hi, I'm not sure if my title is accurate enough.
      But, I'm thinking about having a User Data entry field where I can copy / paste the Python ID for certain object parameters (none specific), and then use that in a custom effector to alter that parameter.

      So, I'm not sure if there's a simple way to translate the string. I've been looking at other ways to escape / un-escape strings in Python, but everything I've seen so far is more complicated than calling something like eval('stringToEval')

      I will continue to search, but thought I'd post here in the mean time.

      Thanks,

      Jim

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 12/03/2013 at 09:44, xxxxxxxx wrote:

        simply use eval.

        print obj[eval('c4d.{0}'.format(obj[c4d.ID_USERDATA,1]))]

        edit : and before someone starts complaining, yeah it is pretty sloppy and unsafe, so you 
        might want to add some fallbacks and double checks.

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 12/03/2013 at 09:48, xxxxxxxx wrote:

          Thanks ferdinand.
          I was just about to post that I got it to work 😊
          My approach seems a bit different than yours though.

          p   = op[c4d.ID_USERDATA,4]
          obj[eval(p)] = <some value>
          
          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 12/03/2013 at 09:49, xxxxxxxx wrote:

            I do not recommend the eval function. See this excellent blog post why.
            Better use getattr:

            try:
                val = getattr(c4d, obj[c4d.ID_USERDATA, 1])
            except AttributeError:
                pass
            

            -Niklas

            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              On 12/03/2013 at 10:01, xxxxxxxx wrote:

              Niklas,
              Ouch. That article sure makes eval() scary.
              I will take your suggestions.

              Thank you,

              Jim

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