From 6f73b7fc297d3174360916e7e3e24b616dd326e2 Mon Sep 17 00:00:00 2001 From: Swann Date: Sat, 19 Sep 2020 00:47:46 +0200 Subject: [PATCH] feat: ground work for sound sync --- multi_user/bl_types/__init__.py | 3 +- multi_user/bl_types/bl_font.py | 6 --- multi_user/bl_types/bl_sound.py | 73 ++++++++++++++++++++++++++++ multi_user/bl_types/bl_speaker.py | 12 ++++- multi_user/bl_types/dump_anything.py | 2 + 5 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 multi_user/bl_types/bl_sound.py diff --git a/multi_user/bl_types/__init__.py b/multi_user/bl_types/__init__.py index a094329..ad35fe8 100644 --- a/multi_user/bl_types/__init__.py +++ b/multi_user/bl_types/__init__.py @@ -35,7 +35,8 @@ __all__ = [ 'bl_lattice', 'bl_lightprobe', 'bl_speaker', - 'bl_font' + 'bl_font', + 'bl_sound' ] # Order here defines execution order from . import * diff --git a/multi_user/bl_types/bl_font.py b/multi_user/bl_types/bl_font.py index f219074..599f3ad 100644 --- a/multi_user/bl_types/bl_font.py +++ b/multi_user/bl_types/bl_font.py @@ -51,12 +51,6 @@ class BlFont(BlDatablock): logging.info(f'loading {font_path}') return bpy.data.fonts.load(font_path) - return bpy.data.images.new( - name=data['name'], - width=data['size'][0], - height=data['size'][1] - ) - def _load(self, data, target): pass diff --git a/multi_user/bl_types/bl_sound.py b/multi_user/bl_types/bl_sound.py new file mode 100644 index 0000000..0db46f8 --- /dev/null +++ b/multi_user/bl_types/bl_sound.py @@ -0,0 +1,73 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# ##### END GPL LICENSE BLOCK ##### + + +import bpy +import mathutils +import os +import logging +import pathlib +from .. import utils +from .dump_anything import Loader, Dumper +from .bl_datablock import BlDatablock + +class BlSound(BlDatablock): + bl_id = "sounds" + bl_class = bpy.types.Sound + bl_delay_refresh = 1 + bl_delay_apply = 1 + bl_automatic_push = True + bl_check_common = False + bl_icon = 'SOUND' + + def _construct(self, data): + if 'file' in data.keys(): + prefs = utils.get_preferences() + ext = data['filepath'].split(".")[-1] + sound_name = f"{self.uuid}.{ext}" + sound_path = os.path.join(prefs.cache_directory, sound_name) + + os.makedirs(prefs.cache_directory, exist_ok=True) + file = open(sound_path, 'wb') + file.write(data["file"]) + file.close() + + logging.info(f'loading {sound_path}') + return bpy.data.sounds.load(sound_path) + + def _load(self, data, target): + loader = Loader() + loader.load(target, data) + + def _dump(self, instance=None): + if not instance.packed_file: + # prefs = utils.get_preferences() + # ext = pathlib.Path(instance.filepath).suffix + # sound_name = f"{self.uuid}{ext}" + # sound_path = os.path.join(prefs.cache_directory, sound_name) + # instance.filepath = sound_path + instance.pack() + + return { + 'filepath':instance.filepath, + 'name':instance.name, + 'file': instance.packed_file.data + } + + + def diff(self): + return False diff --git a/multi_user/bl_types/bl_speaker.py b/multi_user/bl_types/bl_speaker.py index 265f045..af2ef28 100644 --- a/multi_user/bl_types/bl_speaker.py +++ b/multi_user/bl_types/bl_speaker.py @@ -49,6 +49,7 @@ class BlSpeaker(BlDatablock): 'volume', 'name', 'pitch', + 'sound', 'volume_min', 'volume_max', 'attenuation', @@ -61,6 +62,15 @@ class BlSpeaker(BlDatablock): return dumper.dump(instance) - + def _resolve_deps_implementation(self): + # TODO: resolve material + deps = [] + + sound = self.instance.sound + + if sound: + deps.append(sound) + + return deps diff --git a/multi_user/bl_types/dump_anything.py b/multi_user/bl_types/dump_anything.py index c5828de..30ba151 100644 --- a/multi_user/bl_types/dump_anything.py +++ b/multi_user/bl_types/dump_anything.py @@ -595,6 +595,8 @@ class Loader: instance.write(bpy.data.collections.get(dump)) elif isinstance(rna_property_type, T.VectorFont): instance.write(bpy.data.fonts.get(dump)) + elif isinstance(rna_property_type, T.Sound): + instance.write(bpy.data.sounds.get(dump)) def _load_matrix(self, matrix, dump): matrix.write(mathutils.Matrix(dump))