4
$\begingroup$

After days of searching I have to ask now:

How can I get attributes out of a string list?

What I want is :

thing = bpy.context.scene.eevee.use_gtao
print(thing)

"thing" has now stored True.

But I have a string list. How can I get these strings to tell me their attributes?

Andi = [
        "bpy.context.scene.eevee.use_gtao",
        "bpy.context.scene.eevee.use_bloom",
        "bpy.context.scene.eevee.use_ssr",
        "bpy.context.scene.eevee.use_motion_blur",
]

I was going to try getattr, but there it needs an object first, not strings.

The reason I do this is so that I can save and recall render settings. First I worked with the preset system integrated in blender as explained here:

Python add drop down menu that stores data

But unfortunately the render settings change with each Blender version, which brings a lot of problems. That's why I'm looking for a way to have full control over everything myself, so that the render settings can be tested via Try Except statement and not just generate an error right away.

$\endgroup$
3
  • 2
    $\begingroup$ 99% sure it's XY Problem - tell us what you need it for, exactly. $\endgroup$ Commented Nov 19, 2021 at 9:16
  • $\begingroup$ All clear. Sorry for the confusion. I'm not a skilled programmer, but an artist first and foremost. I have edited the question. Hopefully it is better this way. $\endgroup$
    – Andi
    Commented Nov 19, 2021 at 9:28
  • 1
    $\begingroup$ So to summarize, you want to be able to save "presets" of your eevee render settings, and be able to change it at runtime using some kind of list in the interface ? Have you explored simply using different scenes with different settings ? $\endgroup$
    – Gorgious
    Commented Nov 19, 2021 at 10:00

1 Answer 1

5
$\begingroup$

Your Andi list stores a list of Python statements (assignments) in the form of strings. If you trust this list to not contain malevolent code (it could tell your computer to download a trojan virus, for example) you can tell Python to execute it:

import bpy

Andi = [
        "bpy.context.scene.eevee.use_gtao = False",
        "bpy.context.scene.eevee.use_bloom = False",
        "bpy.context.scene.eevee.use_ssr = True",
        "bpy.context.scene.eevee.use_motion_blur = True",
]

for s in Andi:
    exec(s)

You can also parse the strings in order to obtain the information that interests you, e.g.

import bpy

Andi = [
        "bpy.context.scene.eevee.use_gtao = False",
        "bpy.context.scene.eevee.use_bloom = False",
        "bpy.context.scene.eevee.use_ssr = True",
        "bpy.context.scene.eevee.use_motion_blur = True",
]

d = {s.split("=")[0].split(".")[-1]:(s[-4:]=="True") for s in Andi}
print(d)

Output:

{'use_gtao ': False, 'use_bloom ': False, 'use_ssr ': True, 'use_motion_blur ': True}

To get all possible values (security risk due to eval!):

d = {s.split("=")[0].split(".")[-1]:eval(s.split("=")[1]) for s in Andi}

To get only values (security risk due to eval!):

values = [eval(s.split("=")[1]) for s in Andi]
$\endgroup$
2
  • $\begingroup$ That's what I'm looking for, thank you! If it's not too much trouble, how can I just find the attribute? So only True or False? (I'm a little confused about attributes and values. "True" is the attribute, right?) $\endgroup$
    – Andi
    Commented Nov 19, 2021 at 10:07
  • 1
    $\begingroup$ True and False are Boolean values. d = {s.split("=")[0].split(".")[-1]:(s[-4:]=="True") for s in Andi} is a dictionary comprehension, resulting in a dictionary of an attribute e.g. use_gtao leading to a value e.g. False. To only get values: values = [s[-4:]=="True" for s in Andi] $\endgroup$ Commented Nov 19, 2021 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .