generated from stilobique/BlenderTemplate
65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
import bpy
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
class ConfigLighting(bpy.types.Operator):
|
|
"""Add or conform a lighting build"""
|
|
bl_idname = 'graou.lighting'
|
|
bl_label = 'Config or update a lighting'
|
|
|
|
def execute(self, context):
|
|
# Look if a light object are in the scene to clear
|
|
self.clear_light_on_scene()
|
|
|
|
r = self.get_blend_resources_file()
|
|
|
|
world = bpy.data.worlds.get('WorldIconRendering')
|
|
if not world:
|
|
with bpy.data.libraries.load(r, link=True) as (data_from, data_to):
|
|
data_to.worlds = data_from.worlds
|
|
|
|
world = bpy.data.worlds.get('WorldIconRendering')
|
|
|
|
context.scene.world = world
|
|
|
|
if not bpy.data.collections.get('Lighting'):
|
|
print(f'[Pipeline] Need to import the lighting collection')
|
|
with bpy.data.libraries.load(r, link=False) as (data_from, data_to):
|
|
for collection in data_from.collections:
|
|
print(f'[Pipeline] Collection can be import "{collection}"')
|
|
if 'Lighting' in collection:
|
|
print(f'[Pipeline] Append {collection}.')
|
|
data_to.collections.append(collection)
|
|
|
|
print('[Pipeline] Link lighting collection with the scene collection')
|
|
bpy.data.collections['Scene'].children.link(bpy.data.collections.get('Lighting'))
|
|
|
|
return {'FINISHED'}
|
|
|
|
@staticmethod
|
|
def get_blend_resources_file():
|
|
"""Get the blend file shared with the addon"""
|
|
addon_folder = Path(bpy.utils.user_resource('SCRIPTS'))
|
|
addon_name = __name__.split(".")[0]
|
|
|
|
return addon_folder.joinpath("addons", addon_name, "resources", "Lighting.blend").as_posix()
|
|
|
|
def clear_light_on_scene(self):
|
|
bpy.ops.object.select_all(action='DESELECT')
|
|
|
|
for ob in bpy.data.objects:
|
|
if ob.type == 'LIGHT' and not self.find_collection(ob):
|
|
ob.select_set(True)
|
|
bpy.ops.object.delete()
|
|
|
|
else:
|
|
ob.select_set(False)
|
|
|
|
@staticmethod
|
|
def find_collection(ob, name='Lighting'):
|
|
"""Check if an object are attached with the Lighting collection"""
|
|
for collection in ob.users_collection:
|
|
if collection.name == name:
|
|
return True
|