string format



  • how come that my string formatting is not working in C4D.

    exposure = lamp[c4d.REDSHIFT_LIGHT_PHYSICAL_EXPOSURE]
    

    this works like it should and give's me the exposure

    x = 'REDSHIFT_LIGHT_PHYSICAL_EXPOSURE'
    exposure = lamp[c4d.'{}'.format(x)]
    

    but this give's an invalid syntax.

    I want to format because I need a dict comprehension where I get each attribute from a list. like this

    attributes = ['REDSHIFT_LIGHT_PHYSICAL_INTENSITY', 'REDSHIFT_LIGHT_PHYSICAL_EXPOSURE']
    lamps = doc.GetActiveObjects(0)
    dict_variable = [{attr: lamp[c4d.'{}'.format(attr)] for attr in attributes} for lamp in lamps]
    

    is there maybe like a C4D slack? got always question, but also quick answers for other people. a forum is sometimes a bit slow.
    My thanks!



  • You trying to create name of the variable with formatting.
    But format is only for strings.
    Maybe 'eval()' can help you.



  • @mikeudin seems to work. maybe because it needs to have c4d.REDSHIFT_LIGHT_PHYSICAL_EXPOSURE as an expression and not as a string.

    thanks a lot man!


  • Global Moderator

    Hi @Jvos,

    First of all, (no worry at all) I would like to remind you to read How to Post Questions especially the part about categories.
    Since your topic is related to Cinema 4D please next time create a topic within cinema 4d development. (I've moved it).

    Regarding your issue, pretty much all parameters in Cinema 4D are stored in a BaseContainer.
    A BaseContainer is a dictionary which links an ID (Int) to a Data.
    When you are doing Object[Something] you access the value within this BaseContainer.

    If you write in the console c4d.REDSHIFT_LIGHT_PHYSICAL_INTENSITY, it will print you the ID of this parameter.
    Then to get the value of a parameter which is stored in the object's BaseContainer, you don't have to pass a string, but the ID, which is registered with a SYMBOL NAME because it's easier to remember than an arbitrary number (In some case there is no SYMBOL equivalent for an ID).

    So as @mikeudin suggested the eval function is the right way to go, or you can directly store the ID in your list.

    attributes = [c4d.REDSHIFT_LIGHT_PHYSICAL_INTENSITY, c4d.REDSHIFT_LIGHT_PHYSICAL_EXPOSURE]
    

    Finally, there is no official C4D slack/discord or chat.
    Cheers,
    Maxime.