import bpy import re from pathlib import Path from . import gen_vlayer, fn def batch_setup_render_scene(context=None, render_scn=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 if render_scn is None: render_scn = bpy.data.scenes.get('Render') if not render_scn: print('"Render" scene not found in batch_setup_render_scene') return ## 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() if render_scn.objects.get('bg_cam'): render_scn.camera = render_scn.objects.get('bg_cam') fn.set_resolution_from_cam_prop(scene=render_scn) ## Go to camera view in visible viewports (! Need timer + Already done in workspace script!) # if not bpy.app.background: # 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': # print('3D viewport found, Go in Camera') # 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') print('batch setup render scene Done') 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, MA', 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') row = col.row() row.prop(self, 'prefix_filter') row.active = self.clean_name_and_visibility col.prop(self, 'clean_material_duplication') 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(',')] print('prefix_to_render: ', prefix_to_render) 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 ?? (maybe some layer were intentionally hidden...) if (res := re.search(r'^([A-Z]{2})_', l.info)): if res.group(1) in prefix_to_render and l.hide == True and not 'invisible' in l.info: 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) ## Hide "invisible" material mat_invisible = bpy.data.materials.get('invisible') if mat_invisible and mat_invisible.is_grease_pencil: mat_invisible.grease_pencil.hide = True 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 whatever the project!) fn.set_layer_colors(skip_if_colored=False) ## 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)) if ret != {'FINISHED'}: print(f'Fallback to addon stored workspaces: (No "GP Render" found in {render_wkspace_filepath})') # 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') ## extra retry after append activate ?... if ret == {'FINISHED'}: render_wkspace = bpy.data.workspaces.get('GP Render') if render_wkspace: context.window.workspace = render_wkspace # context.workspace.update_tag() # context.scene.update_tag() ## Batch setup render scene batch_setup_render_scene(render_scn=render_scn) ## No need for timer anymore ! # if batch_setup_render_scene: # if self.timer > 0: # print(f'batch_setup_render_scene: called with timer {self.timer}s') # # add timer otherwise render scene setup don't do anything # bpy.app.timers.register(batch_setup_render_scene, first_interval=self.timer) # else: # print('batch_setup_render_scene: Direct call') # batch_setup_render_scene(render_scn=render_scn) ## set at least one GP object active gp_ob = next((o for o in render_scn.objects if o.type == 'GPENCIL'), None) if gp_ob: context.view_layer.objects.active = gp_ob ## 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.\n') 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)