Created
October 29, 2023 17:17
-
-
Save tin2tin/dd17cbf956a15f5fc05e425e5bca9564 to your computer and use it in GitHub Desktop.
Install modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import subprocess | |
import os | |
import bpy | |
import pkgutil | |
# Get the path to the Python interpreter used by Blender | |
python_path = sys.executable | |
# Determine the script directory | |
script_directory = os.path.dirname(bpy.data.filepath) | |
# Create a folder for packages | |
packages_directory = os.path.join(script_directory, "packages") | |
os.makedirs(packages_directory, exist_ok=True) | |
# Print the path | |
print("Blender's Python path:", python_path) | |
# Ensure pip is installed | |
subprocess.run([python_path, "-m", "ensurepip"]) | |
# Check if the required packages are installed | |
required_packages = ["librosa", "pydub", "simpleaudio"] | |
installed_packages = {pkg.name for pkg in pkgutil.iter_modules()} | |
for package in required_packages: | |
if package not in installed_packages: | |
# Install the required package in the created folder | |
subprocess.run([python_path, "-m", "pip", "install", "--target=" + packages_directory, package]) | |
else: | |
print(f"{package} is already installed") | |
# Add the folder to the Python import path | |
sys.path.append(packages_directory) | |
# Import the installed packages | |
import numpy as np | |
import librosa | |
from pydub import AudioSegment | |
import simpleaudio as sa | |
# (Add the rest of your script here, including the custom nodes and operators) | |
def create_sine_wave(frequency=440, duration=1, amplitude=5000, sample_rate=44100): | |
t = np.linspace(0, duration, int(sample_rate * duration), dtype=np.float32) | |
wave = amplitude * np.sin(2 * np.pi * frequency * t) | |
return wave | |
def play_sound(wave, sample_rate=44100): | |
audio = sa.WaveObject(wave, 1, 2, sample_rate) | |
play_obj = audio.play() | |
play_obj.wait_done() | |
if __name__ == "__main__": | |
wave = create_sine_wave() | |
play_sound(wave) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment