gp_render/OP_auto_build.py

395 lines
16 KiB
Python

import bpy
import re
from pathlib import Path
from . import gen_vlayer, fn
def batch_setup_render_scene(context=None):
'''A series of setup actions for Render scene:
- renumber fileout
- Clean compo Tree
- Go to camera view in visible viewports
- Swap to bg cam
'''
if context is None:
context = bpy.context
render_scn = context.scene
## Renumber File outputs
print('Renumber File outputs')
for fo in render_scn.node_tree.nodes:
if fo.type == 'OUTPUT_FILE':
fn.renumber_keep_existing(fo)
## Swap to bg_cam (if any)
if render_scn.objects.get('bg_cam') and (not render_scn.camera or render_scn.camera.name != 'bg_cam'):
print('Swap to bg cam')
bpy.ops.gp.swap_render_cams()
## Go to camera view in visible viewports
print('Go to camera view in visible viewports')
if render_scn.camera:
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == 'VIEW_3D':
area.spaces.active.region_3d.view_perspective = 'CAMERA'
## Clean compo Tree
print('Clean compo Tree')
bpy.ops.gp.clean_compo_tree('EXEC_DEFAULT', use_render_scene=True)
# bpy.ops.gp.clean_compo_tree('INVOKE_DEFAULT', use_render_scene=True)
## Trigger check file before finishing ?
# bpy.ops.gp.check_render_scene('INVOKE_DEFAULT')
class GPEXP_OT_render_auto_build(bpy.types.Operator):
bl_idname = "gp_export.render_auto_build"
bl_label = "Auto-Build"
bl_description = "Trigger all operation to make build render scene with default settings"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GPENCIL'
timer : bpy.props.FloatProperty(default=0.1, options={'SKIP_SAVE'})
excluded_prefix : bpy.props.StringProperty(
name='Excluded Layer By Prefix', default='GP, RG, PO',
description='Exclude layer to send to render by prefix (comma separated list)')
clean_name_and_visibility : bpy.props.BoolProperty(name='Clean Name And Visibility', default=True,
description='Add object name to layer name when there is only prefix (ex: "CO_")\
\nEnable visibility for layer with prefix included in Prefix Filter')
clean_material_duplication : bpy.props.BoolProperty(name='Clean Material Duplication', default=True,
description='Clean material stack. i.e: Replace "mat.001" in material stack if "mat" exists and has same color')
prefix_filter : bpy.props.StringProperty(name='Prefix Filter', default='CO, CU, FX, TO', # , MA # exclude MA if mask are applied
description='Comma separated prefix to render. Set the other prefix and non-prefixed layer to exluded viewlayer')
set_layers_colors : bpy.props.BoolProperty(name='Set Layers Colors', default=True,
description='')
trigger_rename_lowercase : bpy.props.BoolProperty(name='Trigger Rename Lowercase', default=True,
description='')
trigger_renumber_by_distance : bpy.props.BoolProperty(name='Trigger Renumber By Distance', default=True,
description='')
export_layer_infos : bpy.props.BoolProperty(name='Export Layer Infos', default=True,
description='')
group_all_adjacent_layer_type : bpy.props.BoolProperty(name='Group All Adjacent Layer Type', default=True,
description='')
change_to_gp_workspace : bpy.props.BoolProperty(name='Change To Gp Workspace', default=True,
description='')
batch_setup_render_scene : bpy.props.BoolProperty(name='Batch Setup Render Scene', default=True,
description='')
def invoke(self, context, event):
# return self.execute(context)
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
col = layout.column()
col.prop(self, 'clean_name_and_visibility')
col.prop(self, 'clean_material_duplication')
row = col.row()
row.prop(self, 'prefix_filter')
row.active = self.clean_name_and_visibility
col.prop(self, 'set_layers_colors')
col.prop(self, 'trigger_rename_lowercase')
col.prop(self, 'trigger_renumber_by_distance')
col.prop(self, 'export_layer_infos')
col.label(text='Send prefixed layer to render scene (except excluded)')
col.prop(self, 'excluded_prefix', text='Excluded')
col.prop(self, 'group_all_adjacent_layer_type')
col.prop(self, 'change_to_gp_workspace')
col.prop(self, 'batch_setup_render_scene')
# layout.prop(self, 'clear_unused_view_layers')
def execute(self, context):
print('-- Auto-build Render scene --\n')
## Prefix Filter
# TODO : add to preferences / environment var
# prefix_to_render = ['CO', 'CU', 'FX', 'TO', 'MA']
prefix_to_render = [p.strip() for p in self.prefix_filter.split(',')]
render_scn = bpy.data.scenes.get('Render')
if render_scn:
self.report({'ERROR'}, 'A "Render" scene already exists')
return {'CANCELLED'}
all_gp_objects = [o for o in context.scene.objects if o.type == 'GPENCIL']
## clean name and visibility
if self.clean_name_and_visibility:
for o in all_gp_objects:
if o.hide_render:
print(f'skip: {o.name} hide render')
continue
for l in o.data.layers:
## Clean name when layer has no name after prefix
if re.match(r'^[A-Z]{2}_$', l.info):
l.info = l.info + o.name.lower()
## Make used prefix visible ?
if (res := re.search(r'^([A-Z]{2})_', l.info)):
if res.group(1) in prefix_to_render and l.hide == True:
print(f'{o.name} -> {l.info} : Switch visibility On')
l.hide = False
if self.clean_material_duplication:
print('Clean material duplicates')
for ob in all_gp_objects:
fn.clean_mats_duplication(ob)
ob_list = [o for o in all_gp_objects if not o.hide_get() and fn.is_valid_name(o.name)]
if not ob_list:
self.report({'ERROR'}, 'No GP object to render found')
return {'CANCELLED'}
print('GP objects to send:')
for o in ob_list:
print(f' - {o.name}')
## Set layers colors (skip if colors were already set ?)
if self.set_layers_colors:
# Option: Maybe find a way to create a color from prefix hash ? (always give unique color with same prefix on other project!)
fn.set_layer_colors(skip_if_colored=True)
## Trigger rename lowercase
if self.trigger_rename_lowercase:
print('Trigger rename lowercase')
bpy.ops.gp.lower_layers_name('EXEC_DEFAULT')
# bpy.ops.gp.lower_layers_name('INVOKE_DEFAULT')
## Trigger renumber by distance
if self.trigger_renumber_by_distance:
print('Trigger renumber by distance')
bpy.ops.gp.auto_number_object('EXEC_DEFAULT')
# bpy.ops.gp.auto_number_object('INVOKE_DEFAULT')
## Export layer infos ? (skip if json already exists)
if self.export_layer_infos:
print('Export layer infos (skip if json already exists)')
bpy.ops.gp.export_infos_for_compo('INVOKE_DEFAULT', skip_check=True)
## Send all GP to render scene
print('Send all GP to render scene')
# bpy.ops.gp.add_object_to_render(mode="ALL") # Ops to send all
gen_vlayer.export_gp_objects(ob_list, exclude_list=self.excluded_prefix) # Create render scene OTF
## Switch to new Render Scene
print('Switch to new Render Scene')
render_scn = bpy.data.scenes.get('Render')
if not render_scn:
self.report({'ERROR'}, 'No render scene found')
return {'CANCELLED'}
context.window.scene = render_scn
## Group all adjacent layer type
if self.group_all_adjacent_layer_type:
print('Group all adjacent layer type')
for ob in ob_list:
fn.group_adjacent_layer_prefix_rlayer(ob, excluded_prefix=self.excluded_prefix, first_name=True)
# bpy.ops.gp_export.render_scene_setup() # next render scene setup at once
## attempt to refresh scene
# render_scn.node_tree.nodes.update()
# context.view_layer.update()
# context.scene.update_tag()
## Change to GP workspace (if needed)
if self.change_to_gp_workspace:
if context.window.workspace.name != 'GP Render':
print('Change to GP workspace')
if (render_wkspace := bpy.data.workspaces.get('GP Render')):
context.window.workspace = render_wkspace
else:
render_wkspace_filepath = Path(bpy.utils.user_resource('SCRIPTS'), 'startup', 'bl_app_templates_user', 'GP', 'startup.blend')
ret = bpy.ops.workspace.append_activate(idname='GP Render', filepath=str(render_wkspace_filepath))
print('ret: ', ret)
if ret != {'FINISHED'}:
# Fallback to workspace template shipped with addon (TODO : add template blend file in addon)
render_wkspace_filepath = Path(__file__).parent / 'workspaces' / 'startup.blend'
ret = bpy.ops.workspace.append_activate(idname='GP Render', filepath=str(render_wkspace_filepath))
if ret != {'FINISHED'}:
print('No GP render workspace available')
## Batch setup render scene
if batch_setup_render_scene:
bpy.app.timers.register(batch_setup_render_scene, first_interval=self.timer)
## Trigger check file before finishing ?
# bpy.ops.gp.check_render_scene('INVOKE_DEFAULT')
## Note: After all these operation, a ctrl+Z might crash
print('\nDone.')
return {"FINISHED"}
'''
class GPEXP_OT_render_auto_build(bpy.types.Operator):
bl_idname = "gp_export.render_auto_build"
bl_label = "Auto-Build"
bl_description = "Trigger all operation to make build render scene with default settings"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GPENCIL'
excluded_prefix : bpy.props.StringProperty(
name='Excluded Layer By Prefix', default='GP,RG,PO',
description='Exclude layer to send to render by prefix (comma separated list)')
timer : bpy.props.FloatProperty(default=0.01, options={'SKIP_SAVE'})
def execute(self, context):
print('-- Auto-build Render scene --\n')
## Prefix Filter
## TODO : add to preferences / environment var
prefix_to_render = ['CO', 'CU', 'FX', 'TO', 'MA']
render_scn = bpy.data.scenes.get('Render')
if render_scn:
self.report({'ERROR'}, 'A "Render" scene already exists')
return {'CANCELLED'}
## clean name and visibility
for o in [o for o in context.scene.objects if o.type == 'GPENCIL']:
if o.hide_render:
print(f'skip: {o.name} hide render')
continue
for l in o.data.layers:
## Clean name when layer has no name after prefix
if re.match(r'^[A-Z]{2}_$', l.info):
l.info = l.info + o.name.lower()
## Make used prefix visible ?
if (res := re.search(r'^([A-Z]{2})_', l.info)):
if res.group(1) in prefix_to_render and l.hide == True:
print(f'{o.name} -> {l.info} : Switch visibility On')
l.hide = False
ob_list = [o for o in context.scene.objects if o.type == 'GPENCIL' and not o.hide_get() and fn.is_valid_name(o.name)]
if not ob_list:
self.report({'ERROR'}, 'No GP object to render found')
return {'CANCELLED'}
print('GP objects to send:')
for o in ob_list:
print(f' - {o.name}')
## Set layers colors (skip if colors were already set ?)
## Option: Maybe find a way to create a color from prefix hash ? (always give unique color with same prefix on other project!)
fn.set_layer_colors(skip_if_colored=True)
## Trigger rename lowercase
print('Trigger rename lowercase')
bpy.ops.gp.lower_layers_name('EXEC_DEFAULT')
# bpy.ops.gp.lower_layers_name('INVOKE_DEFAULT')
## Trigger renumber by distance
print('Trigger renumber by distance')
bpy.ops.gp.auto_number_object('EXEC_DEFAULT')
# bpy.ops.gp.auto_number_object('INVOKE_DEFAULT')
## Export layer infos ? (skip if json already exists)
print('Export layer infos (skip if json already exists)')
bpy.ops.gp.export_infos_for_compo('INVOKE_DEFAULT', skip_check=True)
## Send all GP to render scene
print('Send all GP to render scene')
# bpy.ops.gp.add_object_to_render(mode="ALL") # Ops to send all
gen_vlayer.export_gp_objects(ob_list, exclude_list=self.excluded_prefix) # Create render scene OTF
## Switch to new Render Scene
print('Switch to new Render Scene')
render_scn = bpy.data.scenes.get('Render')
if not render_scn:
self.report({'ERROR'}, 'No render scene found')
return {'CANCELLED'}
context.window.scene = render_scn
## Group all adjacent layer type
print('Group all adjacent layer type')
for ob in ob_list:
fn.group_adjacent_layer_prefix_rlayer(ob, excluded_prefix=['GP', 'PO', 'RG'], first_name=True)
bpy.ops.gp_export.render_scene_setup() # next render scene setup at once
## attempt to refresh scene
# render_scn.node_tree.nodes.update()
# context.view_layer.update()
# context.scene.update_tag()
## Change to GP workspace (if needed)
if context.window.workspace.name != 'GP Render':
print('Change to GP workspace')
if (render_wkspace := bpy.data.workspaces.get('GP Render')):
context.window.workspace = render_wkspace
else:
render_wkspace_filepath = Path(bpy.utils.user_resource('SCRIPTS'), 'startup', 'bl_app_templates_user', 'GP', 'startup.blend')
ret = bpy.ops.workspace.append_activate(idname='GP Render', filepath=str(render_wkspace_filepath))
print('ret: ', ret)
if ret != {'FINISHED'}:
# Fallback to workspace template shipped with addon (TODO : add template blend file in addon)
render_wkspace_filepath = Path(__file__).parent / 'workspaces' / 'startup.blend'
ret = bpy.ops.workspace.append_activate(idname='GP Render', filepath=str(render_wkspace_filepath))
if ret != {'FINISHED'}:
print('No GP render workspace available')
bpy.app.timers.register(batch_setup_render_scene, first_interval=self.timer)
## Trigger check file before finishing ?
# bpy.ops.gp.check_render_scene('INVOKE_DEFAULT')
## Note: After all these operation, a ctrl+Z might crash
print('\nDone.')
return {"FINISHED"}
'''
class GPEXP_OT_render_scene_setup(bpy.types.Operator):
bl_idname = "gp_export.render_scene_setup"
bl_label = "Batch Setup Render Scene"
bl_description = "Batch some actions to setup render scene:\
\n- renumber file output nodes\
\n- Clean compo Tree\
\n- Go to camera view in visible viewports\
\n- Swap to bg cam"
bl_options = {"REGISTER"}
def execute(self, context):
print('-- Auto-setup Render scene --\n')
batch_setup_render_scene(context=context)
return {"FINISHED"}
classes=(
GPEXP_OT_render_auto_build,
GPEXP_OT_render_scene_setup,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)