I would like to create a node, and center it in the node_tree view that is the "Shader Editor". Keeping in mind that this operator is only executed by a button in the context of the Shader Editor, so the context is correct.
I was wondering if it was possible to obtain the coordinates of the view, and find the center of it, to assign the coordinates to the new node created, so that it is placed exactly in the center of the view.
Below is an example of an operator creating a simple node, waiting to be positioned in the center of the Shader Editor view. (If possible)
I'd like to understand how you can get the position of the view, or something useful to position it correctly.
Simple test Creation of a node by operator:
class ADDNODE_OT_Test(bpy.types.operator):
bl_idname = "node.test"
bl_label = "Test"
def execute(self,context):
mat = bpy.data.materials["My_Material"]
nodes = mat.node_tree.nodes
bsdf = nodes.new(type='ShaderNodeBsdfPrincipled')
#bsdf.location = (?,?) <--- Is possible to get and set the center of the view location into the Shader editor?
return{'FINISHED'}
