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
|
|
|
|
2024-04-16 18:01:10 +02:00
|
|
|
|
|
|
|
def add_layer_to_render(ob, node_scene=None):
|
|
|
|
'''Send GP object to render layer
|
|
|
|
return a tuple with report message'''
|
|
|
|
|
|
|
|
# ob = ob or bpy.context.object
|
|
|
|
layer = ob.data.layers.active
|
|
|
|
if not layer:
|
|
|
|
return ('ERROR', 'No active layer')
|
|
|
|
|
|
|
|
node_scene = fn.get_compo_scene(scene_name=node_scene, create=True)
|
|
|
|
|
|
|
|
ct = 0
|
|
|
|
# send scene ?
|
|
|
|
hidden = 0
|
|
|
|
for l in ob.data.layers:
|
|
|
|
if not l.select:
|
|
|
|
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
|
|
|
|
continue
|
|
|
|
|
|
|
|
gen_vlayer.get_set_viewlayer_from_gp(ob, l, node_scene=node_scene)
|
|
|
|
|
|
|
|
if l.hide:
|
|
|
|
hidden += 1
|
|
|
|
ct += 1
|
|
|
|
|
|
|
|
if hidden:
|
|
|
|
return ('WARNING', f'{hidden}/{ct} layers are hidden!')
|
|
|
|
|
|
|
|
else:
|
|
|
|
return ('INFO', f'{ct} layer(s) added')
|
|
|
|
|
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'})
|
2024-04-16 18:01:10 +02:00
|
|
|
node_scene : bpy.props.StringProperty(default='',
|
|
|
|
description='Scene where to add nodes, Abort if not found', options={'SKIP_SAVE'})
|
2021-09-07 23:11:42 +02:00
|
|
|
|
|
|
|
def execute(self, context):
|
2024-04-16 18:01:10 +02:00
|
|
|
ret = add_layer_to_render(context.object, node_scene=self.node_scene)
|
|
|
|
if isinstance(ret, tuple):
|
|
|
|
self.report({ret[0]}, ret[1])
|
|
|
|
if ret[0] == 'ERROR':
|
|
|
|
return {'CANCELLED'}
|
2021-09-07 23:11:42 +02:00
|
|
|
return {"FINISHED"}
|
|
|
|
|
2024-04-16 18:01:10 +02:00
|
|
|
|
|
|
|
def add_object_to_render(mode='ALL', scene='', node_scene=''):
|
|
|
|
context = bpy.context
|
|
|
|
|
|
|
|
if scene:
|
|
|
|
scn = fn.get_render_scene(scene)
|
|
|
|
else:
|
|
|
|
scn = fn.get_render_scene()
|
|
|
|
|
|
|
|
if node_scene:
|
|
|
|
node_scn = fn.get_compo_scene(scene_name=node_scene, create=True)
|
|
|
|
if not node_scn:
|
|
|
|
return ('ERROR', f'/!\ Node Scene "{node_scene}" not found ! Abort "Add object to Render" !')
|
|
|
|
else:
|
|
|
|
# if not passed add in render scene
|
|
|
|
node_scn = scn
|
|
|
|
|
|
|
|
excludes = [] # ['MA', 'IN'] # Get list dynamically
|
|
|
|
if mode == 'SELECTED':
|
|
|
|
gen_vlayer.export_gp_objects([o for o in context.selected_objects if o.type == 'GPENCIL'], exclude_list=excludes, scene=scn, node_scene=node_scn)
|
|
|
|
|
|
|
|
elif mode == 'ALL':
|
|
|
|
gen_vlayer.export_gp_objects([o for o in context.scene.objects if o.type == 'GPENCIL' and not o.hide_get() and fn.is_valid_name(o.name)], exclude_list=excludes, scene=scn, node_scene=node_scn)
|
|
|
|
|
|
|
|
|
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"
|
2023-01-05 16:03:25 +01:00
|
|
|
bl_description = "Setup GP object in render scene\
|
2024-04-16 18:01:10 +02:00
|
|
|
\nNote: 'send all' mode skip hidden 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'
|
|
|
|
|
2024-04-16 18:01:10 +02:00
|
|
|
mode : bpy.props.EnumProperty(
|
|
|
|
items=(
|
|
|
|
('ALL', 'All', 'All objects', 0),
|
|
|
|
('SELECTED', 'Selected', 'Selected objects', 0),
|
|
|
|
),
|
|
|
|
default='ALL', options={'SKIP_SAVE'},
|
|
|
|
description='Choice to send all or only selected objects')
|
2023-01-18 14:28:27 +01:00
|
|
|
|
2024-04-16 18:01:10 +02:00
|
|
|
scene : bpy.props.StringProperty(default='',
|
|
|
|
description='Scene where to link object and create viewlayer (create if not exists)', options={'SKIP_SAVE'})
|
|
|
|
|
|
|
|
node_scene : bpy.props.StringProperty(default='',
|
|
|
|
description='Scene where to add nodes, Abort if not found', options={'SKIP_SAVE'})
|
2021-09-07 23:11:42 +02:00
|
|
|
|
2024-04-16 18:01:10 +02:00
|
|
|
def execute(self, context):
|
|
|
|
ret = add_object_to_render(mode=self.mode, scene=self.scene, node_scene=self.node_scene)
|
|
|
|
if isinstance(ret, tuple):
|
|
|
|
self.report({ret[0]}, ret[1])
|
|
|
|
if ret[0] == 'ERROR':
|
|
|
|
return {'CANCELLED'}
|
2021-09-07 23:11:42 +02:00
|
|
|
return {"FINISHED"}
|
|
|
|
|
2021-09-30 18:51:49 +02:00
|
|
|
class GPEXP_OT_split_to_scene(bpy.types.Operator):
|
|
|
|
bl_idname = "gp.split_to_scene"
|
|
|
|
bl_label = "Split Objects To Scene"
|
2021-10-13 19:00:15 +02:00
|
|
|
bl_description = "Take selected objects and send them to separate scene\n(new scene is named after active object)"
|
2021-09-30 18:51:49 +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):
|
2023-06-26 18:11:17 +02:00
|
|
|
if self.mode == 'ALL':
|
|
|
|
print('split selected object in separated scene')
|
|
|
|
err = fn.split_object_to_scene()
|
|
|
|
if err:
|
|
|
|
self.report({'ERROR'}, err)
|
|
|
|
return {"CANCELLED"}
|
2024-04-17 17:03:34 +02:00
|
|
|
|
2023-06-26 18:11:17 +02:00
|
|
|
elif self.mode == 'INDIVIDUAL':
|
|
|
|
print('split selected object individually in separated scenes')
|
|
|
|
errors = []
|
|
|
|
selected_objs = [o for o in context.selected_objects]
|
|
|
|
scn = context.scene
|
|
|
|
for o in reversed(selected_objs):
|
|
|
|
err = fn.split_object_to_scene(objs=[o], scene_name=o.name)
|
|
|
|
if err:
|
|
|
|
errors.append(err)
|
|
|
|
|
|
|
|
## Reset to current scene (function use ops and go to new scene)
|
|
|
|
context.window.scene = scn
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
fn.show_message_box(errors, 'Error Log')
|
|
|
|
return {"CANCELLED"}
|
2021-09-30 18:51:49 +02:00
|
|
|
|
2023-06-26 18:11:17 +02:00
|
|
|
return {"FINISHED"}
|
2021-09-30 18:51:49 +02:00
|
|
|
|
2021-09-07 23:11:42 +02:00
|
|
|
classes=(
|
|
|
|
GPEXP_OT_add_layer_to_render,
|
|
|
|
GPEXP_OT_add_objects_to_render,
|
2021-09-30 18:51:49 +02:00
|
|
|
GPEXP_OT_split_to_scene,
|
2021-09-07 23:11:42 +02:00
|
|
|
)
|
|
|
|
|
2023-01-18 14:28:27 +01:00
|
|
|
def register():
|
2021-09-07 23:11:42 +02:00
|
|
|
for cls in classes:
|
|
|
|
bpy.utils.register_class(cls)
|
|
|
|
|
|
|
|
def unregister():
|
|
|
|
for cls in reversed(classes):
|
|
|
|
bpy.utils.unregister_class(cls)
|