I have a Blender file wih many scenes and I want to copy the node tree of one of them to the other scenes. From Blender interface it is a very simple task, in python I have tried using

bpy.ops.node.clipboard_copy() and bpy.ops.node.clipboard_paste()

with no success.

Is there any other method I can use?

Here is my code:

import sys
import bpy

def deleteNodeTree(scene, override):

def copyNodeTree(scene, override):
    bpy.ops.node.select_all(override, action = 'DESELECT')
    bpy.ops.node.select_all(override, action = 'SELECT')    

def pasteNodeTree(scene, override):
    bpy.ops.node.select_all(override, action = 'DESELECT')
    bpy.ops.node.select_all(override, action = 'SELECT')

def getOverride(type, regionType):
    Override = False
    for window in bpy.context.window_manager.windows:
        for area in window.screen.areas:
            if(area.type == type):
                Override = {"window": window, "screen": window.screen, "area": area, "scene" : bpy.context.scene}
    if(not Override):
        bpy.context.window_manager.windows[0].screen.areas[0].type = type
        Override = {"window": bpy.context.window_manager.windows[0], "screen": bpy.context.window_manager.windows[0].screen, "area": bpy.context.window_manager.windows[0].screen.areas[0], "scene" : bpy.context.scene, "region" : bpy.context.region}
        for region in Override["area"].regions:
            if(region.type == regionType):
            Override["region"] = region
    return Override

if len(sys.argv) < 6:
    raise Exception("Usage:  blender -b blend_file --python script.py 

blend_file        = sys.argv[2]
target_scene_name = sys.argv[5]

bpy.ops.wm.open_mainfile( filepath = blend_file )

#Copia su tutte le scene le impostazioni dei nodi di output prese dalla scena selezionata (target_scene_name)

bpy.context.screen.scene = bpy.data.scenes[target_scene_name]
override = getOverride('NODE_EDITOR', False)
copyNodeTree( bpy.context.screen.scene, override)

for scene in bpy.data.scenes:
    if scene.name != target_scene_name:     
        bpy.context.screen.scene = bpy.data.scenes[scene.name]
        scene = bpy.context.screen.scene
        print("Deleting output options from scene: " + 
        deleteNodeTree(scene, override)
        print("Pasting output options to scene: " + 
        pasteNodeTree(scene, override)

bpy.ops.wm.save_as_mainfile(filepath = blend_file, compress=False)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.