# How to link an existing node group via Python script

I created a "voxel" data visualization system via DICOM files in Blender. However, as the DICOM changes from patient to patient, it is necessary to allow the user to change the transparency of the slices, for this it is necessary to link the parameters to a node group.

The problem is that I am unable to link an existing group in the scene during the lasso -for-, when creating the slices from DICOM.

I tried this command:

bpy.ops.node.add_node (type = "ShaderNodeGroup", use_transform = True, settings = [{"name": "node_tree", "value": "bpy.data.node_groups ['GroupVoxelShader']"}])


But it gives error of poll() failed.

So I tried to create the node group inside the material first and then make a link, so it creates a block, but with the following name: Missing Data-Block.

I tried to find a way to link with the group bpy.data.node_groups['GroupVoxelShader'] as shown in the image below:

But I can only do this manually, it was not possible to link automatically already in lasso, with Python.

Does anyone know how to do this? Thanks for reading!

import bpy

#you need to somehow specify the material(maybe loop through a few?..):
node_tree = bpy.data.materials['Material'].node_tree
# And a node group that you have in the file:
node_group_name = "NodeGroup"

nodes = node_tree.nodes

# Let's at least check if some image node and output node exists
# you might need a way to determine which ones to use if there are multiple
image_node = output_node = None
node_group_exists = False
for node in nodes:
if node.type == 'TEX_IMAGE':
image_node = node
elif node.type == 'OUTPUT_MATERIAL':
output_node = node
elif node.type == 'GROUP': # in case the script was used already
if node.node_tree.name == node_group_name:
node_group_exists = True
print('exists')
group_node = node

if output_node and image_node:
if node_group_name in bpy.data.node_groups and not node_group_exists: