gp_render/OP_auto_build.py

194 lines
7.6 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'
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')
## TODO: Add colors to layers (specified in ENV or hardcoded for now...)
## Option: Maybe find a way to create a color from prefix hash ? (always give unique color with same prefix on other project!)
prefix_to_render = ['CO', 'CU', 'FX', 'TO', 'MA'] # TODO : add to preferences / environment var
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 ?)
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)