255 lines
8.1 KiB
Python
255 lines
8.1 KiB
Python
|
import bpy
|
||
|
import re
|
||
|
from mathutils import Vector
|
||
|
from pathlib import Path
|
||
|
|
||
|
from collections import defaultdict
|
||
|
|
||
|
|
||
|
def create_node(type, tree=None, **kargs):
|
||
|
'''Get a type, a tree to add in, and optionnaly multiple attribute to set
|
||
|
return created node
|
||
|
'''
|
||
|
tree = tree or bpy.context.scene.node_tree
|
||
|
|
||
|
node = tree.nodes.new(type)
|
||
|
for k,v in kargs.items():
|
||
|
setattr(node, k, v)
|
||
|
|
||
|
return node
|
||
|
|
||
|
def new_aa_node(tree):
|
||
|
'''create AA node'''
|
||
|
aa = create_node('CompositorNodeAntiAliasing', tree) # type = ANTIALIASING
|
||
|
aa.threshold = 0.5
|
||
|
aa.contrast_limit = 0.5
|
||
|
aa.corner_rounding = 0.25
|
||
|
aa.hide = True
|
||
|
return aa
|
||
|
|
||
|
|
||
|
def set_settings(scene=None):
|
||
|
if not scene:
|
||
|
scene = bpy.context.scene
|
||
|
# specify scene settings for these kind of render
|
||
|
scene = bpy.context.scene
|
||
|
scene.eevee.taa_render_samples = 1
|
||
|
scene.grease_pencil_settings.antialias_threshold = 0
|
||
|
|
||
|
def get_render_scene():
|
||
|
'''Get / Create a scene named Render'''
|
||
|
render = bpy.data.scenes.get('Render')
|
||
|
if not render:
|
||
|
render = bpy.data.scenes.new('Render')
|
||
|
set_settings(render) # setup specific settings
|
||
|
render.use_nodes = True
|
||
|
return render
|
||
|
|
||
|
def get_view_layer(name, scene=None):
|
||
|
'''get viewlayer name
|
||
|
return existing/created viewlayer
|
||
|
'''
|
||
|
if not scene:
|
||
|
scene = get_render_scene()
|
||
|
### pass double letter prefix as suffix
|
||
|
## pass_name = re.sub(r'^([A-Z]{2})(_)(.*)', r'\3\2\1', 'name')
|
||
|
## pass_name = f'{name}_{passe}'
|
||
|
pass_vl = scene.view_layers.get(name)
|
||
|
if not pass_vl:
|
||
|
pass_vl = scene.view_layers.new(name)
|
||
|
return pass_vl
|
||
|
|
||
|
### node location tweaks
|
||
|
|
||
|
def real_loc(n):
|
||
|
if not n.parent:
|
||
|
return n.location
|
||
|
return n.location + real_loc(n.parent)
|
||
|
|
||
|
def get_frame_transform(f, node_tree=None):
|
||
|
'''Return real transform location of a frame node
|
||
|
only works with one level of nesting (not recursive)
|
||
|
'''
|
||
|
if not node_tree:
|
||
|
node_tree = f.id_data
|
||
|
if f.type != 'FRAME':
|
||
|
return
|
||
|
# return real_loc(f), f.dimensions
|
||
|
|
||
|
childs = [n for n in node_tree.nodes if n.parent == f]
|
||
|
# real_locs = [f.location + n.location for n in childs]
|
||
|
|
||
|
xs = [n.location.x for n in childs] + [n.location.x + n.dimensions.x for n in childs]
|
||
|
ys = [n.location.y for n in childs] + [n.location.y - n.dimensions.y for n in childs]
|
||
|
xs.sort(key=lambda loc: loc) # x val : ascending
|
||
|
ys.sort(key=lambda loc: loc) # ascending # , reversed=True) # y val : descending
|
||
|
|
||
|
loc = Vector((min(xs), max(ys)))
|
||
|
dim = Vector((max(xs) - min(xs) + 60, max(ys) - min(ys) + 60))
|
||
|
|
||
|
return loc, dim
|
||
|
|
||
|
|
||
|
## get all frames with their real transform.
|
||
|
|
||
|
def bbox(f, frames):
|
||
|
xs=[]
|
||
|
ys=[]
|
||
|
for n in frames[f]: # nodes of passed frame
|
||
|
# Better as Vectors ?
|
||
|
if n.type == 'FRAME':
|
||
|
if n not in frames.keys():
|
||
|
# print(f'frame {n.name} not in frame list')
|
||
|
continue
|
||
|
all_xs, all_ys = bbox(n, frames) # frames[n]
|
||
|
xs += all_xs
|
||
|
ys += all_ys
|
||
|
|
||
|
else:
|
||
|
loc = real_loc(n)
|
||
|
xs += [loc.x, loc.x + n.dimensions.x] # + (n.dimensions.x/get_dpi_factor())
|
||
|
ys += [loc.y, loc.y - n.dimensions.y] # - (n.dimensions.y/get_dpi_factor())
|
||
|
|
||
|
|
||
|
# margin ~= 30
|
||
|
# return xs and ys
|
||
|
return [min(xs)-30, max(xs)+30], [min(ys)-30, max(ys)+30]
|
||
|
|
||
|
def get_frames_bbox(node_tree):
|
||
|
'''Return a dic with all frames
|
||
|
ex: {frame_node: (location, dimension), ...}
|
||
|
'''
|
||
|
|
||
|
# create dic of frame object with his direct child nodes nodes
|
||
|
frames = defaultdict(list)
|
||
|
frames_bbox = {}
|
||
|
for n in node_tree.nodes:
|
||
|
if not n.parent:
|
||
|
continue
|
||
|
# also contains frames
|
||
|
frames[n.parent].append(n)
|
||
|
|
||
|
# Dic for bbox coord
|
||
|
for f, nodes in frames.items():
|
||
|
if f.parent:
|
||
|
continue
|
||
|
|
||
|
xs, ys = bbox(f, frames)
|
||
|
# xs, ys = bbox(nodes, frames)
|
||
|
|
||
|
## returning: list of corner coords
|
||
|
# coords = [
|
||
|
# Vector((xs[0], ys[1])),
|
||
|
# Vector((xs[1], ys[1])),
|
||
|
# Vector((xs[1], ys[0])),
|
||
|
# Vector((xs[0], ys[0])),
|
||
|
# ]
|
||
|
# frames_bbox[f] = coords
|
||
|
|
||
|
## returning: (loc vector, dimensions vector)
|
||
|
frames_bbox[f] = Vector((xs[0], ys[1])), Vector((xs[1] - xs[0], ys[1] - ys[0]))
|
||
|
|
||
|
return frames_bbox
|
||
|
|
||
|
|
||
|
## nodes helper functions
|
||
|
|
||
|
def clear_nodegroup(name, full_clear=False):
|
||
|
'''remove duplication of a nodegroup (.???)
|
||
|
also remove the base one if full_clear True
|
||
|
'''
|
||
|
|
||
|
for ng in reversed(bpy.data.node_groups):
|
||
|
pattern = name + r'\.\d{3}'
|
||
|
if re.search(pattern, ng.name):
|
||
|
bpy.data.node_groups.remove(ng)
|
||
|
|
||
|
if full_clear and ng.name == name:
|
||
|
# if full clear
|
||
|
bpy.data.node_groups.remove(ng)
|
||
|
|
||
|
|
||
|
def rearrange_frames(node_tree):
|
||
|
frame_d = get_frames_bbox(node_tree) # dic : {frame_node:(loc vector, dimensions vector), ...}
|
||
|
if not frame_d:
|
||
|
print('no frame found')
|
||
|
return
|
||
|
|
||
|
print([f.name for f in frame_d.keys()])
|
||
|
## order the dict by frame.y location
|
||
|
frame_d = {key: value for key, value in sorted(frame_d.items(), key=lambda pair: pair[1][0].y - pair[1][1].y, reverse=True)}
|
||
|
frames = [[f, v[0], v[1].y] for f, v in frame_d.items()] # [frame_node, real_loc, real dimensions]
|
||
|
|
||
|
top = frames[0][1].y # upper node location.y
|
||
|
offset = 0
|
||
|
for f in frames:
|
||
|
## f[1] : real loc Vector
|
||
|
## f[0] : frame
|
||
|
|
||
|
## move frame by offset needed (delta between real_loc and "fake" loc , minus offset)
|
||
|
f[0].location.y = (f[1].y - f[0].location.y) - top - offset
|
||
|
# f[0].location.y = f[1].y - top - offset
|
||
|
offset += f[2] + 300 # gap
|
||
|
|
||
|
f[0].update()
|
||
|
|
||
|
def reorder_inputs(ng):
|
||
|
rl_nodes = [s.links[0].from_node for s in ng.inputs if s.is_linked and s.links and s.links[0].from_node.type == 'R_LAYERS']
|
||
|
rl_nodes.sort(key=lambda x: x.location.y, reverse=True)
|
||
|
names = [n.layer for n in rl_nodes]
|
||
|
inputs_names = [s.name for s in ng.inputs]
|
||
|
filtered_names = [n for n in names if n in inputs_names]
|
||
|
|
||
|
for dest, name in enumerate(filtered_names):
|
||
|
## rebuild list at each iteration so index are good
|
||
|
inputs_names = [s.name for s in ng.inputs]
|
||
|
src = inputs_names.index(name)
|
||
|
# reorder on node_tree not directly on node!
|
||
|
ng.node_tree.inputs.move(src, dest)
|
||
|
|
||
|
def reorder_outputs(ng):
|
||
|
ordered_out_name = [nis.name for nis in ng.inputs if nis.name in [o.name for o in ng.outputs]]
|
||
|
for s_name in ordered_out_name:
|
||
|
all_outnames = [o.name for o in ng.outputs]
|
||
|
# reorder on nodetree, not on node !
|
||
|
ng.node_tree.outputs.move(all_outnames.index(s_name), ordered_out_name.index(s_name))
|
||
|
|
||
|
def clear_disconnected(fo):
|
||
|
for inp in reversed(fo.inputs):
|
||
|
if not inp.is_linked:
|
||
|
print(f'Deleting unlinked fileout slot: {inp.name}')
|
||
|
fo.inputs.remove(inp)
|
||
|
|
||
|
def reorder_fileout(fo, ng=None):
|
||
|
if not ng: # get connected nodegroup
|
||
|
for s in fo.inputs:
|
||
|
if s.is_linked and s.links and s.links[0].from_node.type == 'GROUP':
|
||
|
ng = s.links[0].from_node
|
||
|
break
|
||
|
if not ng:
|
||
|
print(f'No nodegroup to refer to filter {fo.name}')
|
||
|
return
|
||
|
ordered = [o.links[0].to_socket.name for o in ng.outputs if o.is_linked and o.is_linked and o.links[0].to_node == fo]
|
||
|
for s_name in ordered:
|
||
|
all_outnames = [s.name for s in fo.inputs] # same as [fs.path for fs in fo.file_slots]
|
||
|
fo.inputs.move(all_outnames.index(s_name), ordered.index(s_name))
|
||
|
|
||
|
def connect_to_group_output(n):
|
||
|
for o in n.outputs:
|
||
|
if o.is_linked:
|
||
|
if o.links[0].to_node.type == 'GROUP_OUTPUT':
|
||
|
return o.links[0].to_socket
|
||
|
val = connect_to_group_output(o.links[0].to_node)
|
||
|
if val:
|
||
|
return val
|
||
|
return False
|
||
|
|
||
|
def connect_to_group_input(n):
|
||
|
for i in n.inputs:
|
||
|
if i.is_linked:
|
||
|
if i.links[0].from_node.type == 'GROUP_INPUT':
|
||
|
return i.links[0].from_socket
|
||
|
val = connect_to_group_input(i.links[0].from_node)
|
||
|
if val:
|
||
|
return val
|
||
|
return False
|