-
-
Save ideasman42/29849d7a18ca737f63c625e731bc2676 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import os | |
from bpy import data as D, context as C | |
if not C.scene.use_nodes: | |
C.scene.use_nodes = True | |
compo_nodes = bpy.context.scene.node_tree.nodes | |
compo_links = bpy.context.scene.node_tree.links | |
bpy.context.scene.render.use_border = False | |
bpy.context.scene.render.use_compositing = True | |
bpy.context.scene.cycles.samples = 1 | |
def create_node(type, name, x, y, hide): | |
node = compo_nodes.new(type=type) | |
node.name = name | |
node.hide = hide | |
node.location.x = x | |
node.location.y = y | |
return node | |
def find_output(node, name): | |
for output in node.outputs: | |
if name == output.name: | |
return output | |
print('Cant find {0}'.format(name)) | |
def switch_image(pass_name, prefix, tiles_paths, layers_node, mode): | |
tiles = len(tiles_paths) | |
for tile, tile_path in enumerate(tiles_paths): | |
node_name = "{0}imput_tile_{1}".format(prefix, tile) | |
node_image = create_node('CompositorNodeImage', node_name, 0, 100 * tile, True) | |
imagepath = tile_path | |
node_image.image = D.images.load(filepath=imagepath) | |
image_output = find_output(node_image, pass_name) | |
if tile > 0: | |
# Mix | |
if mode in {'IMAGE', 'IMAGE+ALPHA'}: | |
node_name = "{0}mix_tile_{1}_{2}".format(prefix, tile - 1, tile) | |
node = create_node('CompositorNodeMixRGB', node_name, 50 + (50 * tile), 15 + (100 * tile), True) | |
node.blend_type = 'SCREEN' | |
compo_links.new(image_output, node.inputs[1]) | |
try: | |
compo_links.new(last_mix.outputs[0], node.inputs[2]) | |
except: # TODO, exception type | |
compo_links.new(find_output(last_image, pass_name), node.inputs[2]) | |
pass | |
last_mix = node | |
# Mix Alpha | |
node_name = "{0}mix_alpha_{1}_{2}".format(prefix, tile - 1, tile) | |
node = create_node('CompositorNodeMixRGB', node_name, 300 + (60 * tile), 10 + (100 * tile), True) | |
node.blend_type = 'ADD' | |
last_alpha_mix = node | |
node_name = "{0}get_alpha_{1}".format(prefix, tile) | |
node = create_node('CompositorNodeSepRGBA', node_name, 150 + (60 * tile), 15 + (100 * tile), True) | |
compo_links.new(image_output, node.inputs[0]) | |
if tile == 1: | |
compo_links.new(compo_nodes['{0}get_alpha_{1}'.format(prefix, tile - 1)].outputs[3], | |
compo_nodes['{0}mix_alpha_{1}_{2}'.format(prefix, tile - 1, tile)].inputs[2]) | |
compo_links.new(compo_nodes['{0}get_alpha_{1}'.format(prefix, tile)].outputs[3], | |
compo_nodes['{0}mix_alpha_{1}_{2}'.format(prefix, tile - 1, tile)].inputs[1]) | |
if tile > 1: | |
compo_links.new(compo_nodes['{0}get_alpha_{1}'.format(prefix, tile)].outputs[3], | |
compo_nodes['{0}mix_alpha_{1}_{2}'.format(prefix, tile - 1, tile)].inputs[1]) | |
compo_links.new(compo_nodes['{0}mix_alpha_{1}_{2}'.format(prefix, tile - 2, tile - 1)].outputs[0], | |
compo_nodes['{0}mix_alpha_{1}_{2}'.format(prefix, tile - 1, tile)].inputs[2]) | |
last_image = node_image | |
if mode in {'ALPHA', 'IMAGE+ALPHA'}: | |
node_name = "{0}set_alpha".format(prefix) | |
node = create_node('CompositorNodeSetAlpha', node_name, 15 + (100 * tiles), 150 + (60 * tiles), True) | |
compo_links.new(last_alpha_mix.outputs[0], node.inputs[1]) | |
compo_links.new(last_mix.outputs[0], node.inputs[0]) | |
return node.outputs[0] | |
elif mode in {'IMAGE'}: | |
return last_mix.outputs[0] | |
else: | |
return last_alpha_mix.outputs[0] | |
def switch_z(pass_name, prefix, tiles_path, layers_node): | |
tiles = len(tiles_paths) | |
for tile, tile_path in enumerate(tiles_paths): | |
node_name = "{0}imput_tile_z_{1}".format(prefix, tile) | |
node_image = create_node('CompositorNodeImage', node_name, 0, 100 * tile, True) | |
imagepath = tile_path | |
node_image.image = D.images.load(filepath=imagepath) | |
z_output = find_output(node_image, pass_name) | |
if tile > 0: | |
# Mix | |
node_name = "{0}mix_z_{1}_{2}".format(prefix, tile - 1, tile) | |
node = create_node('CompositorNodeMath', node_name, 50 + (50 * tile), 15 + (100 * tile), True) | |
node.operation = 'MINIMUM' | |
compo_links.new(z_output, node.inputs[0]) | |
try: | |
compo_links.new(last_mix.outputs[0], node.inputs[1]) | |
except: # TODO, use exception type | |
compo_links.new(last_image.outputs[1], node.inputs[1]) | |
last_mix = node | |
last_image = node_image | |
return last_mix.outputs[0] | |
def relink(out, new_out): | |
link_list = [] | |
for link in out.links: | |
if link.to_socket == out: | |
link_list.append(link.from_socket) | |
else: | |
link_list.append(link.to_socket) | |
for link in link_list: | |
compo_links.new(new_out, link) | |
# MAIN | |
tiles_paths = [{% for tile in tiles -%} | |
'{{tile}}', | |
{%- endfor %}] | |
##VARS_INSERTED_HERE## | |
translation = { | |
'Image': 'Combined', | |
'Alpha': 'Combined', | |
'Z': 'Depth', | |
'Normal': 'Normal', | |
'UV': 'UV', | |
'Speed': 'Vector', | |
'Color': 'Color', | |
'Diffuse': 'Diffuse', | |
'Specular': 'Specular', | |
'Shadow': 'Shadow', | |
'AO': 'AO', | |
'Reflect': 'Reflect', | |
'Refract': 'Refract', | |
'Indirect': 'Indirect', | |
'IndexOB': 'IndexOB', | |
'IndexMA': 'IndexMA', | |
'Mist': 'Mist', | |
'Emit': 'Emit', | |
'Environment': 'Env', | |
'Diffuse Direct': 'DiffDir', | |
'Diffuse Indirect': 'DiffInd', | |
'Diffuse Color': 'DiffCol', | |
'Glossy Direct': 'GlossDir', | |
'Glossy Indirect': 'GlossInd', | |
'Glossy Color': 'GlossCol', | |
'Transmission Direct': 'TransDir', | |
'Transmission Indirect': 'TransInd', | |
'Transmission Color': 'TransCol', | |
'Subsurface Direct': 'SubsurfaceDir', | |
'Subsurface Indirect': 'SubsurfaceInd', | |
'Subsurface Color': 'SubsurfaceCol', | |
} | |
switching = True | |
layer_num = 0 | |
while switching: | |
switching = False | |
for layers_node in compo_nodes: | |
if layers_node.type == 'R_LAYERS': | |
switching = True | |
break | |
if not switching: | |
break | |
output_node = None | |
layer_num += 1 | |
print('a') | |
for out in layers_node.outputs: | |
if not out.is_linked: | |
continue | |
print(out.name) | |
prefix = '{0}_{1}_'.format(layer_num, out.name) | |
pass_name = translation.get(out.name) | |
if pass_name in {'Combined'}: | |
new_out = switch_image(pass_name, prefix, tiles_paths, layers_node, 'IMAGE+ALPHA') | |
relink(out, new_out) | |
elif pass_name in {'Alpha'}: | |
new_out = switch_image(pass_name, prefix, tiles_paths, layers_node, 'ALPHA') | |
relink(out, new_out) | |
elif pass_name in {'Depth'}: | |
new_out = switch_z(pass_name, prefix, tiles_paths, layers_node) | |
relink(out, new_out) | |
else: | |
new_out = switch_image(pass_name, prefix, tiles_paths, layers_node, 'IMAGE') | |
relink(out, new_out) | |
compo_nodes.remove(layers_node) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment