1
$\begingroup$

I would like to set many render settings from a text file. The file looks like this:

# Comment
bpy.context.scene.eevee.use_gtao = True
@ bpy.context.scene.eevee.gtao_distance = 3

I read this file as a text file and split and process each line. (Ignore comments or do special functions on numeric values which are preceded with a @ sign).

I'm still not "fluent" in Python so I don't quite understand how to use the list what I get for each line. If I enter bpy.context.scene.eevee.gtao_distance = 3 in the script window in Blender it works but how do I use key and value (from the list) to set the actual rendering properties this way? bpy.context.scene.eevee.gtao_distance is only a string from my list, how do I make it an actual property which I can assign a value?

$\endgroup$
0

1 Answer 1

1
$\begingroup$

If you want a very fast way to execute your file, you can go something like

with open(filepath, "r") as f: 
    for l in f.readlines(): 
        exec(l)

However this is a security risk if you don't trust the contents of the text file. If you want a secure solution look for bpy.context.path_resolve(path) see https://docs.blender.org/api/current/bpy.types.Context.html#bpy.types.Context.path_resolve so in your case something like this :

with open(filepath, "r") as f: 
    for l in f.readlines(): 
        if l.startswith("bpy.context"):
            path, value = l.split("=")
            path = path.strip()
            parent_path, attr_name = path.rsplit('.', 1)
            parent_object = bpy.context.path_resolve(parent_path)
            value = value.strip()
            # Here you might want to write custom behaviour to cast to the correct data type :
            value = int(value)
            setattr(parent_object, attr_name, value)
$\endgroup$
2
  • $\begingroup$ Thank you very much, the first one is really super simple. By the second version I get an error: "Value error("Path could not be resolved: %r" % attr) - Path could not be resolved by bpy " It's regardless of the actual setting I try to change. I printed the parent_path variable and that is correct. (In the example: bpy.context.scene.eevee) $\endgroup$
    – Steve
    Commented Aug 23 at 12:36
  • $\begingroup$ Yeah I actually didn't test it thoroughly, I'll get around to it. First one is more than sufficient for a one-and-done script. Cheers $\endgroup$
    – Gorgious
    Commented Aug 26 at 16:17

You must log in to answer this question.

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