escape / un-escaping strings

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



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

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

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

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


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

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

    Thank you,


Log in to reply