tkeskita / BVtkNodes

Create and execute VTK pipelines in Blender Node Editor
GNU General Public License v3.0
111 stars 19 forks source link

cannot create new node group in headless mode #67

Closed derekeden closed 2 years ago

derekeden commented 2 years ago

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

##does not work
import bpy
bpy.ops.mesh.primitive_cube_add()
group = bpy.data.node_groups.new('test', type='BVTK_NodeTreeType')
group.nodes.new(type='VTKPolyDataReaderType')
print(bpy.data.node_groups[0]) #<bpy_struct, BVTK_NodeTreeType("test") at 0x000001BEE0E9EC08>
print(list(bpy.data.node_groups[0].nodes)) #[bpy.data.node_groups['test'].nodes["vtkPolyDataReader"]]
bpy.ops.wm.save_mainfile(filepath='testout.blend')
bpy.ops.wm.quit_blender()

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

##works -- assuming file created before hand with node group
import bpy
print(bpy.data.node_groups[0]) #<bpy_struct, BVTK_NodeTreeType("test") at 0x000002AB5ADDF008>
group = bpy.data.node_groups[0]
group.nodes.new(type='VTKPolyDataReaderType')
print(list(bpy.data.node_groups[0].nodes)) #[bpy.data.node_groups['test'].nodes["vtkPolyDataReader"]]
bpy.ops.wm.save_mainfile()
bpy.ops.wm.quit_blender()

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:

image

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.

image

I tried all of the methods here to try to set it to 1 for the newly instatiated node groups:

image

but could not do it,

doing bpy.data.node_groups[0].user_fake_user=Truedid work, but adds an 'F' next to each group.

image

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.

tkeskita commented 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=Truedid 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

derekeden commented 2 years ago

Fake user seems to work fine so far