diff --git a/system_demo_mode/__init__.py b/system_demo_mode/__init__.py index 18b8b676c53c1ec4b971c719ae7f01331cca8748..e70da0ee3f00b99eaff7bdea3b544730c7a02c18 100644 --- a/system_demo_mode/__init__.py +++ b/system_demo_mode/__init__.py @@ -40,14 +40,14 @@ if "bpy" in locals(): import bpy -from bpy.props import StringProperty, BoolProperty, FloatProperty, EnumProperty +from bpy.props import StringProperty, BoolProperty, IntProperty, FloatProperty, EnumProperty from io_utils import ImportHelper class DemoModeSetup(bpy.types.Operator): '''Creates a demo script and optionally executes''' bl_idname = "wm.demo_mode_setup" - bl_label = "Demo Mode" + bl_label = "Demo Mode (Setup)" bl_options = {'PRESET'} # List of operator properties, the attributes will be assigned @@ -57,7 +57,7 @@ class DemoModeSetup(bpy.types.Operator): filepath = StringProperty(name="File Path", description="Filepath used for importing the file", maxlen=1024, default="", subtype='FILE_PATH') random_order = BoolProperty(name="Random Order", description="Select files randomly", default=False) mode = EnumProperty(items=( - ('AUTO', "Automatic", ""), + ('AUTO', "Auto", ""), ('PLAY', "Play", ""), ('RENDER', "Render", ""), ), @@ -69,9 +69,9 @@ class DemoModeSetup(bpy.types.Operator): # # anim # ==== - anim_cycles = FloatProperty(name="Cycles", description="Number of times to play the animation", min=0.1, max=1000.0, soft_min=1.0, soft_max=1000.0, default=1.0) + anim_cycles = IntProperty(name="Cycles", description="Number of times to play the animation", min=1, max=1000, default=2) anim_time_min = FloatProperty(name="Time Min", description="Minimum number of seconds to show the animation for (for small loops)", min=0.0, max=1000.0, soft_min=1.0, soft_max=1000.0, default=4.0) - anim_time_max = FloatProperty(name="Time Max", description="Maximum number of seconds to show the animation for (incase the end frame is very high for no reason)", min=0.0, max=100000000.0, soft_min=1.0, soft_max=100000000.0, default=60.0) + anim_time_max = FloatProperty(name="Time Max", description="Maximum number of seconds to show the animation for (incase the end frame is very high for no reason)", min=0.0, max=100000000.0, soft_min=1.0, soft_max=100000000.0, default=8.0) anim_screen_switch = FloatProperty(name="Screen Switch", description="Time between switching screens (in seconds) or 0 to disable", min=0.0, max=100000000.0, soft_min=1.0, soft_max=60.0, default=0.0) # # render @@ -91,6 +91,10 @@ class DemoModeSetup(bpy.types.Operator): text = bpy.data.texts.new("demo.py") text.from_string(cfg_str) + + if self.run: + extern_demo_mode_run() + return {'FINISHED'} def invoke(self, context, event): @@ -102,44 +106,100 @@ class DemoModeSetup(bpy.types.Operator): def draw(self, context): layout = self.layout + + box = layout.box() + box.label("Search *.blend recursively") + box.label("Writes: demo.py config text.") + col = layout.column() col.prop(self, "run") col.label("Generate Settings:") - col.prop(self, "mode") + row = col.row() + row.prop(self, "mode", expand=True) col.prop(self, "random_order") - + mode = self.mode - + col.separator() colsub = col.column() - colsub.active = (mode in ('AUTO', 'ANIMATE')) + colsub.active = (mode in ('AUTO', 'PLAY')) colsub.label("Animate Settings:") colsub.prop(self, "anim_cycles") colsub.prop(self, "anim_time_min") colsub.prop(self, "anim_time_max") + colsub.prop(self, "anim_screen_switch") col.separator() colsub = col.column() colsub.active = (mode in ('AUTO', 'RENDER')) colsub.label("Render Settings:") - colsub.prop(self, "render_anim") + colsub.prop(self, "display_render") + + +class DemoModeRun(bpy.types.Operator): + bl_idname = "wm.demo_mode_run" + bl_label = "Demo Mode (Start)" + + def execute(self, context): + if extern_demo_mode_run(): + return {'FINISHED'} + else: + self.report({'ERROR'}, "Cant load demo.py config, run: File -> Demo Mode (Setup)") + return {'CANCELLED'} + + +# --- call demo_mode.py funcs +def extern_demo_mode_run(): + # this accesses demo_mode.py which is kept standalone + # and can be run direct. + from . import demo_mode + if demo_mode.load_config(): + demo_mode.demo_mode_load_file() # kick starts the modal operator + return True + else: + return False + + +def extern_demo_mode_register(): + # this accesses demo_mode.py which is kept standalone + # and can be run direct. + from . import demo_mode + demo_mode.register() + + +def extern_demo_mode_unregister(): + # this accesses demo_mode.py which is kept standalone + # and can be run direct. + from . import demo_mode + demo_mode.unregister() + +# --- intergration def menu_func(self, context): - self.layout.operator(DemoModeSetup.bl_idname) + layout = self.layout + layout.operator(DemoModeSetup.bl_idname, icon='PREFERENCES') + layout.operator(DemoModeRun.bl_idname, icon='PLAY') + layout.separator() def register(): bpy.utils.register_class(DemoModeSetup) + bpy.utils.register_class(DemoModeRun) - bpy.types.INFO_MT_file_import.append(menu_func) + bpy.types.INFO_MT_file.prepend(menu_func) + + extern_demo_mode_register() def unregister(): bpy.utils.unregister_class(DemoModeSetup) + bpy.utils.unregister_class(DemoModeRun) + + bpy.types.INFO_MT_file.remove(menu_func) - bpy.types.INFO_MT_file_import.remove(menu_func) + extern_demo_mode_unregister() if __name__ == "__main__": register() diff --git a/system_demo_mode/config.py b/system_demo_mode/config.py index 6ef731fc9cf6fe31b82934d5f92d83661948c6a7..6dcc4d9cde2f096f7b6baffa3d02769f6e42ef55 100644 --- a/system_demo_mode/config.py +++ b/system_demo_mode/config.py @@ -1,5 +1,6 @@ import os + def blend_list(path): for dirpath, dirnames, filenames in os.walk(path): @@ -36,8 +37,10 @@ def generate(dirpath, random_order, **kwargs): def as_string(dirpath, random_order, **kwargs): + """ Config loader is in demo_mode.py + """ cfg, dirpath = generate(dirpath, random_order, **kwargs) - + # hint for reader, can be used if files are not found. cfg_str = [] cfg_str += ["# generated file\n"] @@ -48,9 +51,8 @@ def as_string(dirpath, random_order, **kwargs): cfg_str += ["search_path = %r\n" % dirpath] cfg_str += ["\n"] - # All these work but use nicest formatting! - if 0: # works but not nice to edit. + if 0: # works but not nice to edit. cfg_str += ["config = %r" % cfg] elif 0: import pprint @@ -59,6 +61,7 @@ def as_string(dirpath, random_order, **kwargs): cfg_str += [("config = %r" % cfg).replace("{", "\n {")] else: import pprint + def dict_as_kw(d): return "dict(%s)" % ", ".join(("%s=%s" % (k, pprint.pformat(v))) for k, v in sorted(d.items())) ident = " " @@ -66,6 +69,5 @@ def as_string(dirpath, random_order, **kwargs): for cfg_item in cfg: cfg_str += ["%s%s,\n" % (ident, dict_as_kw(cfg_item))] cfg_str += ["%s]\n\n" % ident] - - + return "".join(cfg_str), dirpath diff --git a/system_demo_mode/demo_mode.py b/system_demo_mode/demo_mode.py index 028ecfda05a44ad7f176fb19c7eb97be01567b64..8ea0fb3d3f0db81b710ef25f8f106f7932083b07 100644 --- a/system_demo_mode/demo_mode.py +++ b/system_demo_mode/demo_mode.py @@ -27,10 +27,11 @@ blender --python release/scripts/addons/system_demo_mode/demo_mode.py looks for demo.py textblock or file in the same path as the blend: # --- example config = [ - dict(anim_cycles=1.0, anim_render=False, anim_screen_switch=0.0, anim_time_max=10.0, anim_time_min=4.0, mode='AUTO', display_render=4.0, file='/l/19534_simplest_mesh_2.blend'), - dict(anim_cycles=1.0, anim_render=False, anim_screen_switch=0.0, anim_time_max=10.0, anim_time_min=4.0, mode='AUTO', display_render=4.0, file='/l/252_pivotConstraint_01.blend'), + dict(anim_cycles=1, anim_render=False, anim_screen_switch=0.0, anim_time_max=10.0, anim_time_min=4.0, mode='AUTO', display_render=4.0, file='/l/19534_simplest_mesh_2.blend'), + dict(anim_cycles=1, anim_render=False, anim_screen_switch=0.0, anim_time_max=10.0, anim_time_min=4.0, mode='AUTO', display_render=4.0, file='/l/252_pivotConstraint_01.blend'), ] # --- +/data/src/blender/lib/tests/rendering/ ''' import bpy @@ -39,11 +40,13 @@ import time import tempfile import os +DEMO_CFG = "demo.py" + # populate from script global_config_files = [] -global_config = dict(anim_cycles=1.0, +global_config = dict(anim_cycles=1, anim_render=False, anim_screen_switch=0.0, anim_time_max=60.0, @@ -52,7 +55,7 @@ global_config = dict(anim_cycles=1.0, display_render=4.0) # switch to the next file in 2 sec. -global_config_fallback = dict(anim_cycles=1.0, +global_config_fallback = dict(anim_cycles=1, anim_render=False, anim_screen_switch=0.0, anim_time_max=60.0, @@ -61,13 +64,12 @@ global_config_fallback = dict(anim_cycles=1.0, display_render=4.0) - global_state = { "init_time": 0.0, "last_switch": 0.0, "reset_anim": False, - "anim_cycles": 0.0, # count how many times we played the anim - "last_frame": 0, + "anim_cycles": 0, # count how many times we played the anim + "last_frame": -1, "render_out": "", "render_time": "", # time render was finished. "timer": None, @@ -75,22 +77,38 @@ global_state = { "demo_index": 0, } + def demo_mode_auto_select(): - + play_area = 0 render_area = 0 - + + totimg = 0 + for area in bpy.context.window.screen.areas: size = area.width * area.height if area.type in {'VIEW_3D', 'GRAPH_EDITOR', 'DOPESHEET_EDITOR', 'NLA_EDITOR', 'TIMELINE'}: play_area += size elif area.type in {'IMAGE_EDITOR', 'SEQUENCE_EDITOR', 'NODE_EDITOR'}: render_area += size + + if area.type == 'IMAGE_EDITOR': + totimg += 1 + + # since our test files have this as defacto standard + if totimg >= 2: + mode = 'RENDER' + else: + if play_area >= render_area: + mode = 'PLAY' + else: + mode = 'RENDER' + + if 0: + return 'PLAY' + + return mode - mode = 'PLAY' if play_area >= render_area else 'RENDER' - print(mode, play_area, render_area) - return 'PLAY' - def demo_mode_next_file(): global_state["demo_index"] += 1 @@ -139,21 +157,30 @@ def demo_mode_init(): global_config["mode"] = demo_mode_auto_select() if global_config["mode"] == 'PLAY': + global_state["last_frame"] = -1 + global_state["anim_cycles"] = 0 bpy.ops.screen.animation_play() elif global_config["mode"] == 'RENDER': print(" render") + + # setup tempfile global_state["render_out"] = tempfile.mkstemp()[1] - - bpy.context.scene.render.filepath = global_state["render_out"] - bpy.context.scene.render.file_format = 'PNG' # animation will fail! - bpy.context.scene.render.use_file_extension = False - bpy.context.scene.render.use_placeholder = False if os.path.exists(global_state["render_out"]): print(" render!!!") os.remove(global_state["render_out"]) - bpy.ops.render.render('INVOKE_DEFAULT', write_still=True) + # setup scene. + scene = bpy.context.scene + scene.render.filepath = global_state["render_out"] + scene.render.file_format = 'AVI_JPEG' if global_config["anim_render"] else 'PNG' + scene.render.use_file_extension = False + scene.render.use_placeholder = False + + if global_config["anim_render"]: + bpy.ops.render.render('INVOKE_DEFAULT', animation=True) + else: + bpy.ops.render.render('INVOKE_DEFAULT', write_still=True) else: raise Exception("Unsupported mode %r" % global_config["mode"]) @@ -169,10 +196,18 @@ def demo_mode_update(): # -------------------------------------------------------------------------- # ANIMATE MODE if global_config["mode"] == 'PLAY': + frame = bpy.context.scene.frame_current # check for exit if time_total > global_config["anim_time_max"]: demo_mode_next_file() return + # above cycles and minimum display time + if (time_total > global_config["anim_time_min"]) and \ + (global_state["anim_cycles"] > global_config["anim_cycles"]): + + # looped enough now. + demo_mode_next_file() + return # run update funcs if global_state["reset_anim"]: @@ -180,6 +215,8 @@ def demo_mode_update(): bpy.ops.screen.animation_cancel(restore_frame=False) bpy.ops.screen.animation_play() + # warning, switching the screen can switch the scene + # and mess with our last-frame/cycles counting. if global_config["anim_screen_switch"]: # print(time_delta, 1) if time_delta > global_config["anim_screen_switch"]: @@ -190,16 +227,29 @@ def demo_mode_update(): bpy.context.window.screen = screen_new global_state["last_switch"] = time_current + + # if we also switch scenes then reset last frame + # otherwise it could mess up cycle calc. + if screen.scene != screen_new.scene: + global_state["last_frame"] = -1 #if global_config["mode"] == 'PLAY': if 1: global_state["reset_anim"] = True + # did we loop? + if global_state["last_frame"] > frame: + print("Cycle!") + global_state["anim_cycles"] += 1 + + global_state["last_frame"] = frame + # -------------------------------------------------------------------------- # RENDER MODE elif global_config["mode"] == 'RENDER': if os.path.exists(global_state["render_out"]): # wait until the time has passed + # XXX, todo, if rendering an anim we need some way to check its done. if global_state["render_time"] == -1.0: global_state["render_time"] = time.time() else: @@ -213,6 +263,7 @@ def demo_mode_update(): # ----------------------------------------------------------------------------- # modal operator + class DemoKeepAlive: secret_attr = "_keepalive" @@ -250,7 +301,7 @@ class DemoMode(bpy.types.Operator): DemoKeepAlive.remove() def modal(self, context, event): - # print("DemoMode.modal") + print("DemoMode.modal", global_state["anim_cycles"]) if not self.__class__.enabled: self.cleanup(disable=True) return {'CANCELLED'} @@ -272,7 +323,15 @@ class DemoMode(bpy.types.Operator): return {'PASS_THROUGH'} def execute(self, context): - print("func:DemoMode.execute") + print("func:DemoMode.execute:", len(global_config_files), "files") + + # load config if not loaded + if not global_config_files: + load_config() + if not global_config_files: + self.report({'INFO'}, "No configuration found with text or file: %s. Run File -> Demo Mode Setup" % DEMO_CFG) + return {'CANCELLED'} + # toggle if self.__class__.enabled and self.__class__.first_run == False: # this actually cancells the previous running instance @@ -281,6 +340,7 @@ class DemoMode(bpy.types.Operator): else: self.__class__.enabled = True context.window_manager.modal_handler_add(self) + return {'RUNNING_MODAL'} def cancel(self, context): @@ -304,31 +364,40 @@ def register(): def unregister(): bpy.utils.unregister_class(DemoMode) + bpy.types.INFO_HT_header.remove(menu_func) # ----------------------------------------------------------------------------- # parse args -def load_config(cfg_name="demo.py"): +def load_config(cfg_name=DEMO_CFG): namespace = {} + global_config_files[:] = [] + basedir = os.path.dirname(bpy.data.filepath) + text = bpy.data.texts.get(cfg_name) if text is None: - basedir = os.path.dirname(bpy.data.filepath) - demo_path = os.path.join(basedir, "demo.py") - demo_file = open(demo_path, "r") - demo_data = demo_file.read() + demo_path = os.path.join(basedir, cfg_name) + if os.path.exists(demo_path): + print("Using config file: %r" % demo_path) + demo_file = open(demo_path, "r") + demo_data = demo_file.read() + demo_file.close() + else: + demo_data = "" else: + print("Using config textblock: %r" % cfg_name) demo_data = text.as_string() demo_path = os.path.join(bpy.data.filepath, cfg_name) # fake + if not demo_data: + print("Could not find %r textblock or %r file." % (DEMO_CFG, demo_path)) + return False + namespace["__file__"] = demo_path exec(demo_data, namespace, namespace) - demo_file.close() - - global_config_files[:] = [] - for filecfg in namespace["config"]: # defaults @@ -353,11 +422,11 @@ def load_config(cfg_name="demo.py"): global_state["basedir"] = basedir + return bool(global_config_files) + # support direct execution if __name__ == "__main__": - load_config() register() - # starts the operator - demo_mode_load_file() + demo_mode_load_file() # kick starts the modal operator