6
$\begingroup$

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):
    scene.node_tree.nodes.clear()


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

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

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}
                break
    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}
    if(regionType):
        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 
target_scene_name")

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: " + 
bpy.context.screen.scene.name)
        deleteNodeTree(scene, override)
        print("Pasting output options to scene: " + 
bpy.context.screen.scene.name)
        pasteNodeTree(scene, override)

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

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.