2023-03-14 13:38:04 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
Generic Blender functions
|
|
|
|
"""
|
2023-05-19 11:51:05 +02:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
from textwrap import dedent
|
2023-03-14 13:38:04 +01:00
|
|
|
|
|
|
|
import bpy
|
|
|
|
|
2023-05-19 11:51:05 +02:00
|
|
|
|
2024-03-14 17:44:05 +01:00
|
|
|
class attr_set():
|
|
|
|
"""Receive a list of tuple [(data_path:python_obj, "attribute":str, "wanted value":str)]
|
|
|
|
before with statement : Store existing values, assign wanted value
|
|
|
|
after with statement: Restore values to their old values
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, attrib_list):
|
|
|
|
"""Initialization
|
|
|
|
|
|
|
|
Args:
|
|
|
|
attrib_list (list[tuple]): a list of tuple with the
|
|
|
|
"""
|
|
|
|
self.store = []
|
|
|
|
|
|
|
|
for item in attrib_list:
|
|
|
|
prop, attr = item[:2]
|
|
|
|
self.store.append( (prop, attr, getattr(prop, attr)) )
|
|
|
|
|
|
|
|
for item in attrib_list:
|
|
|
|
prop, attr = item[:2]
|
|
|
|
if len(item) >= 3:
|
|
|
|
setattr(prop, attr, item[2])
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
|
|
|
for prop, attr, old_val in self.store:
|
|
|
|
setattr(prop, attr, old_val)
|
|
|
|
|
2023-05-19 11:51:05 +02:00
|
|
|
def abspath(path):
|
|
|
|
path = os.path.abspath(bpy.path.abspath(path))
|
|
|
|
return Path(path)
|
|
|
|
|
2023-03-21 18:33:29 +01:00
|
|
|
def get_scene_settings():
|
|
|
|
return bpy.context.scene.vsetb_settings
|
|
|
|
|
|
|
|
def get_strip_settings():
|
|
|
|
scn = bpy.context.scene
|
2023-04-25 18:43:04 +02:00
|
|
|
strip = bpy.context.active_sequence_strip
|
|
|
|
|
|
|
|
if not strip:
|
|
|
|
return
|
|
|
|
|
|
|
|
return strip.vsetb_strip_settings
|
2023-03-21 18:33:29 +01:00
|
|
|
|
2023-05-19 11:51:05 +02:00
|
|
|
def norm_arg(arg_name):
|
|
|
|
return "--" + arg_name.replace(' ', '-')
|
|
|
|
|
|
|
|
def norm_value(value):
|
|
|
|
if isinstance(value, (tuple, list)):
|
|
|
|
values = []
|
|
|
|
for v in value:
|
|
|
|
if not isinstance(v, str):
|
|
|
|
v = json.dumps(v)
|
|
|
|
values.append(v)
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
|
|
|
if isinstance(value, Path):
|
|
|
|
return str(value)
|
|
|
|
|
|
|
|
if not isinstance(value, str):
|
|
|
|
value = json.dumps(value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def get_bl_cmd(blender=None, background=False, focus=True, blendfile=None, script=None, output=None, **kargs):
|
2023-03-14 13:38:04 +01:00
|
|
|
cmd = [str(blender)] if blender else [bpy.app.binary_path]
|
|
|
|
|
|
|
|
if background:
|
|
|
|
cmd += ['--background']
|
|
|
|
|
|
|
|
if not focus and not background:
|
|
|
|
cmd += ['--no-window-focus']
|
|
|
|
cmd += ['--window-geometry', '5000', '0', '10', '10']
|
|
|
|
|
|
|
|
cmd += ['--python-use-system-env']
|
|
|
|
|
|
|
|
if blendfile:
|
|
|
|
cmd += [str(blendfile)]
|
2023-05-19 11:51:05 +02:00
|
|
|
|
|
|
|
if output:
|
|
|
|
cmd += ['-o', str(output)]
|
2023-03-14 13:38:04 +01:00
|
|
|
|
|
|
|
if script:
|
|
|
|
cmd += ['--python', str(script)]
|
|
|
|
|
|
|
|
if kargs:
|
|
|
|
cmd += ['--']
|
|
|
|
for k, v in kargs.items():
|
|
|
|
k = norm_arg(k)
|
|
|
|
v = norm_value(v)
|
|
|
|
|
|
|
|
cmd += [k]
|
|
|
|
if isinstance(v, (tuple, list)):
|
|
|
|
cmd += v
|
|
|
|
else:
|
|
|
|
cmd += [v]
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
2023-05-19 11:51:05 +02:00
|
|
|
|
|
|
|
def background_render(output=None):
|
|
|
|
#bpy.context.scene.render.filepath = '{output}'
|
2023-10-09 18:30:06 +02:00
|
|
|
|
|
|
|
output = os.path.abspath(bpy.path.abspath(output))
|
2023-05-19 11:51:05 +02:00
|
|
|
script_code = dedent(f"""
|
|
|
|
import bpy
|
|
|
|
bpy.context.scene.render.filepath = '{str(output)}'
|
|
|
|
bpy.ops.render.render(animation=True)
|
|
|
|
bpy.ops.wm.quit_blender()
|
|
|
|
""")
|
|
|
|
|
|
|
|
tmp_blend = Path(bpy.app.tempdir) / Path(bpy.data.filepath).name
|
2023-10-09 18:30:06 +02:00
|
|
|
bpy.ops.wm.save_as_mainfile(filepath=str(tmp_blend), copy=True)
|
2023-05-19 11:51:05 +02:00
|
|
|
|
|
|
|
script_path = Path(bpy.app.tempdir) / 'render_blender_background.py'
|
|
|
|
script_path.write_text(script_code)
|
|
|
|
|
|
|
|
cmd = get_bl_cmd(blendfile=tmp_blend, script=script_path, background=True)
|
|
|
|
|
|
|
|
print('Background Render...')
|
|
|
|
print(cmd)
|
|
|
|
subprocess.Popen(cmd)
|
|
|
|
|
2023-03-14 13:38:04 +01:00
|
|
|
def get_addon_prefs():
|
|
|
|
addon_name = __package__.split('.')[0]
|
|
|
|
return bpy.context.preferences.addons[addon_name].preferences
|
2023-05-04 18:45:17 +02:00
|
|
|
|