Closed derekeden closed 2 years ago
Hi,
I'm having an issue when trying to create a node tree from headless python console If I start blender from scratch and try to make the node group it does not work: so then I tried creating the blend file manually first, and creating an empty node group in the file: this time, when I open the blend file in the Blender GUI, the node tree has been updated. Can you please explain why the first is not working or how I could achieve this?
Yes, @thomgrand came to the same conclusion when building the test suite. You need to have a template blend file with an empty BVTK Node Tree to start with, then adding nodes in a script works. I don't know why.
doing
bpy.data.node_groups[0].user_fake_user=True
did work, but adds an 'F' next to each group. it seems that if I use the fake user approach, all my problems go away but I'm not sure what this even means or if there will be downstream consequences.
Fake (data) user is Blender's way of tagging data blocks in memory, which need to be saved in a blend file, even if no Blender objects are using the data. It should be OK to use it if it seems to work otherwise.
BR, Tuomo
Fake user seems to work fine so far
Hello,
I'm having an issue when trying to create a node tree from headless python console
the way I do this is by running blender in the background and opening up the python console.
If I start blender from scratch and try to make the node group it does not work:
launch using:
blender -b --python-console
for some reason, after I've saved the file and opened in Blender GUI there are no node groups present. I have tested that I was able to do non-BVTKNodes operations (i.e. creating a cube works)
so then I tried creating the blend file manually first, and creating an empty node group in the file:
launch using:
blender 'testout.blend' -b --python-console
this time, when I open the blend file in the Blender GUI, the node tree has been updated.
Can you please explain why the first is not working or how I could achieve this?
I want to be able to develop a file and node groups from scratch, without having to manually instantiate each node group in the GUI.
I would also note that when I create node groups using python from within the Blender scripting environment, it works fine. For some reason just creating node groups from a headless python session does not work.
Much appreciated!
EDIT - I also tried running a python expression through the command line:
blender -b --python-expr "import bpy; bpy.data.node_groups.new('test', type='BVTK_NodeTreeType'); print(bpy.data.node_groups[0]); bpy.ops.wm.save_as_mainfile(filepath='testout.blend')"
putting it into a script doesnt work either:
blender -b --python try.py
it seems I can only create a node group when the GUI is open??
also, if I take away the -b flag in these last 2 approaches, it works fine.. but i have to kill blender manually
EDIT - after digging more into it, I've found that it's related to the "users" each node group has:
this can be accessed through:
bpy.data.node_groups[0].users
I guess they all start as 0, but if you open them in the GUI they change to 1.
I tried all of the methods here to try to set it to 1 for the newly instatiated node groups:
but could not do it,
doing
bpy.data.node_groups[0].user_fake_user=True
did work, but adds an 'F' next to each group.it seems that if I use the fake user approach, all my problems go away but I'm not sure what this even means or if there will be downstream consequences.