Basic python question : replace a string by a variable

Hello,
I’m finishing a script but I have in small problem to replace a string with a variable that contains the same name string.

I have this line :
bpy.ops.node.add_node(use_transform=True, type=“CompositorNodeGroup”, settings=[{“name”:“node_tree”, “value”:“bpy.data.node_groups[‘my_node_group’]”}])

I’d like to replace the node group by a variable but I don’t know how to do without errors. I tried this:
my_var = “my_node_group”
bpy.ops.node.add_node(use_transform=True, type=“CompositorNodeGroup”, settings=[{“name”:“node_tree”, “value”:“bpy.data.node_groups[”+my_var+"]"}]) # and also …+str(my_var)+…

The error says that ‘my_node_group’ is not defined.

What did I wrong?
Thanks!

You can always use python exec, eval and or compile to convert a string to an executable statement.

But I think you may be having trouble with using double quotes. Try single quotes or something like this…


my_var = "my_node_group"
v = "bpy.data.node_groups[\"%s\"]" % my_var
bpy.ops.node.add_node(use_transform=True, type="CompositorNodeGroup", settings=[{"name":"node_tree", "value":v}])

Double quotes need to be ‘escaped’ via the \ character.

Ok!

Thanks.