@ideasman42 Thanks. That’s not exactly what I wanted, but you gave me the answer on IRC, so I got the solution. Thank you.
As soon as I get it all working I’ll give the full info here.
—edit— 2012-11-14
Solution given by ideasman42 is as follows:
bpy.ops.node.output_file_add_socket() needs the proper context.
Two issues:
- It can be executed only when we are in node editor, i.e: mouse is hovered over node editor panel.
- We need to specify the file output node as a part of a context.
Ad. 1
When we use this operator in the script and want to execute it directly from text editor window by hitting atl-P or “Run Script” button - it simply won’t work. To solve this issue - we need to “wrap” it in operator that can be executed when we are in node editor:
class NodeFileOutputSocket(bpy.types.Operator):
bl_idname = "node.add_socket"
bl_label = "Add File Output Socket"
@classmethod
def poll(cls, context):
space = context.space_data
return space.type == 'NODE_EDITOR'
def execute(self, context):
main(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(NodeFileOutputSocket)
def unregister():
bpy.utils.unregister_class(NodeFileOutputSocket)
if __name__ == "__main__":
register()
So those will be last lines of our code. Before that we need to define “main” function where we will address the second issue, i.e - passing our node to the context.
def main(context):
scn = context.scene
tree = svn.node_tree
nodes = tree.nodes
node = nodes['File Output'] # Here we specify the file output node that we want to add input socket to.
C = context.copy() # Create copy of context to be used for bpy.ops.node.output_file_add_socket()
C["node"] = node # We pass our node into the context
bpy.ops.node.output_file_add_socket(C) # here's where the magic happens
The whole code looks like this:
import bpy
def main(context):
scn = context.scene
tree = svn.node_tree
nodes = tree.nodes
node = nodes['File Output'] # Here we specify the file output node that we want to add input socket to.
C = context.copy() # Create copy of context to be used for bpy.ops.node.output_file_add_socket()
C["node"] = node # We pass our node into the context
bpy.ops.node.output_file_add_socket(C) # here's where the magic happens
class NodeFileOutput(bpy.types.Operator):
bl_idname = "node.add_socket"
bl_label = "Add File Output Socket"
@classmethod
def poll(cls, context):
space = context.space_data
return space.type == 'NODE_EDITOR'
def execute(self, context):
main(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(NodeFileOutput)
def unregister():
bpy.utils.unregister_class(NodeFileOutput)
if __name__ == "__main__":
register()
Hover the mouse over this text, alt-P, then hover the mouse over node editor, space -> “Add File Output Socket”, BAM.
By the way: It’s really annoying that we need to write so many lines just to add a socket.
IMHO it would be great if we could say something like:
# pseudo code
node = node = nodes['File Output'] # provided we have earlier specified that nodes stands for node tree nodes of our scene
node.inputs.new() # there's no such method, but it would be great if it existed