gp_render/OP_add_layer.py

126 lines
4.0 KiB
Python
Raw Normal View History

2021-09-07 23:11:42 +02:00
import bpy
2021-09-18 18:07:44 +02:00
from . import gen_vlayer, fn
2021-09-07 23:11:42 +02:00
class GPEXP_OT_add_layer_to_render(bpy.types.Operator):
bl_idname = "gp.add_layer_to_render"
bl_label = "Add Gp Layer as render nodes"
bl_description = "Create a view_layer for GP > add node > connect to output"
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 ?
2021-09-18 18:07:44 +02:00
hided = 0
2021-09-07 23:11:42 +02:00
for l in ob.data.layers:
if not l.select:
2021-09-18 18:07:44 +02:00
if not l.viewlayer_render:
# TODO : need to link, can raise error if object is not linked in Render scene yet
l.viewlayer_render = fn.get_view_layer('exclude').name
2021-09-07 23:11:42 +02:00
continue
gen_vlayer.get_set_viewlayer_from_gp(ob, l)
2021-09-18 18:07:44 +02:00
if l.hide:
hided += 1
2021-09-07 23:11:42 +02:00
ct += 1
2021-09-18 18:07:44 +02:00
if hided:
self.report({'WARNING'}, f'{hided}/{ct} layers are hided !')
else:
2021-09-07 23:11:42 +02:00
self.report({'INFO'}, f'{ct} layer(s) added to scene "Render"')
2021-09-18 18:07:44 +02:00
2021-09-07 23:11:42 +02:00
return {"FINISHED"}
2021-09-18 18:07:44 +02:00
def export_gp_objects(oblist, exclude_list=[]):
2021-09-07 23:11:42 +02:00
# Skip layer containing element in excluyde list
if not isinstance(oblist, list):
oblist = [oblist]
for ob in oblist:
for l in ob.data.layers:
2021-09-18 18:07:44 +02:00
# if l.hide:
# continue
if l.hide or any(x + '_' in l.info for x in exclude_list): # exclude hided ?
l.viewlayer_render = fn.get_view_layer('exclude').name # assign "exclude"
2021-09-07 23:11:42 +02:00
continue
2021-09-18 18:07:44 +02:00
_vl, _cp = gen_vlayer.get_set_viewlayer_from_gp(ob, l) # scene=fn.get_render_scene())
2021-09-07 23:11:42 +02:00
## send operator with mode ALL or SELECTED to batch build
class GPEXP_OT_add_objects_to_render(bpy.types.Operator):
bl_idname = "gp.add_object_to_render"
bl_label = "Add all Gp Layer of active object as render nodes"
bl_description = "Setup GP object in render scene\nNote: 'send all' mode skip hided objects"
2021-09-07 23:11:42 +02:00
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GPENCIL'
mode : bpy.props.StringProperty(default='ALL', options={'SKIP_SAVE'})
def execute(self, context):
2021-09-18 18:07:44 +02:00
# create render scene
fn.get_render_scene()
excludes = [] # ['MA', 'IN'] # Get list dynamically
2021-09-07 23:11:42 +02:00
if self.mode == 'SELECTED':
2021-09-18 18:07:44 +02:00
export_gp_objects([o for o in context.selected_objects if o.type == 'GPENCIL'], exclude_list=excludes) # excludes
2021-09-07 23:11:42 +02:00
elif self.mode == 'ALL':
scn = bpy.data.scenes.get('Scene')
if not scn:
self.report({'ERROR'}, 'Could not found default scene')
return {"CANCELLED"}
2021-09-18 18:07:44 +02:00
export_gp_objects([o for o in scn.objects if o.type == 'GPENCIL' and not o.hide_get()], exclude_list=excludes) # excludes
2021-09-07 23:11:42 +02:00
return {"FINISHED"}
class GPEXP_OT_split_to_scene(bpy.types.Operator):
bl_idname = "gp.split_to_scene"
bl_label = "Split Objects To Scene"
bl_description = "Take selected objects and send them to separate scene\n(new scene is named after active object)"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GPENCIL'
mode : bpy.props.StringProperty(default='ALL', options={'SKIP_SAVE'})
def execute(self, context):
err = fn.split_object_to_scene()
if err:
self.report({'ERROR'}, err)
return {"CANCELLED"}
return {"FINISHED"}
2021-09-07 23:11:42 +02:00
classes=(
GPEXP_OT_add_layer_to_render,
GPEXP_OT_add_objects_to_render,
GPEXP_OT_split_to_scene,
2021-09-07 23:11:42 +02:00
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)