import bpy from pathlib import Path from . import gen_vlayer, fn 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' # mode : bpy.props.StringProperty(options={'SKIP_SAVE'}) def execute(self, context): ''' ob = context.object layer = ob.data.layers.active if not layer: self.report({'ERROR'}, 'No active layer') return {"CANCELLED"} ct = 0 # send scene ? hided = 0 for l in ob.data.layers: if not l.select: if not l.viewlayer_render: l.viewlayer_render = fn.get_view_layer('exclude').name continue gen_vlayer.get_set_viewlayer_from_gp(ob, l) if l.hide: hided += 1 ct += 1 if hided: self.report({'WARNING'}, f'{hided}/{ct} layers are hided !') else: self.report({'INFO'}, f'{ct} layer(s) added to scene "Render"') ''' ## TODO: add colors to layers (specified in ENV or hardcoded for now...) ## Option: Maybe find a way to create a color from prefix hash ? (wlways give unique color with same prefix on other project!) render_scn = bpy.data.scenes.get('Render') if render_scn: self.report({'ERROR'}, 'A "Render" scene already exists') return {'CANCELLED'} ## Trigger rename lowercase bpy.ops.gp.lower_layers_name() ## Trigger renumber by distance bpy.ops.gp.auto_number_object() ## 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 bpy.ops.gp.add_object_to_render(mode="ALL") ## 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 ## Change to GP workspace (if needed) if context.window.workspace.name != 'GP Render': 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', 'GP', 'startup.blend') ret = bpy.ops.workspace.append_activate(idname='GP Render', filepath=str(render_wkspace_filepath)) 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)) ## Group all adjacent layer type ## Renumber File outputs ## Trigger check file before finishing ? ## note: After all these operation, a ctrl+Z might crash return {"FINISHED"} classes=( GPEXP_OT_render_auto_build, ) def register(): for cls in classes: bpy.utils.register_class(cls) def unregister(): for cls in reversed(classes): bpy.utils.unregister_class(cls)