- hide members of bpy from dir() and autocomp.

- path functions bpy.utils.script_paths(), bpy.utils_preset_paths(subdir)
- further simplified presets, use a generic draw function for preset menus and define the preset subdir and operator in the class
This commit is contained in:
Campbell Barton 2009-11-22 11:23:19 +00:00
parent 058454368a
commit 771406df94
7 changed files with 93 additions and 71 deletions

@ -25,21 +25,15 @@ context = _bpy.context
# python modules
from bpy import utils
from bpy import ops as ops_module
from bpy import ops as _ops_module
# fake operator module
ops = ops_module.ops_fake_module
# load all scripts
import os
import sys
# a bit nasty but this prevents help() and input() from locking blender
# Ideally we could have some way for the console to replace sys.stdin but
# python would lock blender while waiting for a return value, not easy :|
sys.stdin = None
ops = _ops_module.ops_fake_module
def load_scripts(reload_scripts=False):
import os
import sys
import traceback
def test_import(module_name):
@ -49,34 +43,44 @@ def load_scripts(reload_scripts=False):
traceback.print_exc()
return None
base_path = os.path.join(os.path.dirname(__file__), "..", "..")
base_path = os.path.normpath(base_path) # clean
for base_path in utils.script_paths():
print(base_path)
for path_subdir in ("ui", "op", "io"):
path = os.path.join(base_path, path_subdir)
sys.path.insert(0, path)
for f in sorted(os.listdir(path)):
if f.endswith(".py"):
# python module
mod = test_import(f[0:-3])
elif "." not in f:
# python package
mod = test_import(f)
else:
mod = None
for path_subdir in ("ui", "op", "io"):
path = os.path.join(base_path, path_subdir)
sys.path.insert(0, path)
for f in sorted(os.listdir(path)):
if f.endswith(".py"):
# python module
mod = test_import(f[0:-3])
elif "." not in f:
# python package
mod = test_import(f)
else:
mod = None
if reload_scripts and mod:
print("Reloading:", mod)
reload(mod)
if reload_scripts and mod:
print("Reloading:", mod)
reload(mod)
def _main():
# a bit nasty but this prevents help() and input() from locking blender
# Ideally we could have some way for the console to replace sys.stdin but
# python would lock blender while waiting for a return value, not easy :|
import sys
sys.stdin = None
if "-d" in sys.argv and False: # Enable this to measure startup speed
import cProfile
cProfile.run('import bpy; bpy.load_scripts()', 'blender.prof')
import pstats
p = pstats.Stats('blender.prof')
p.sort_stats('cumulative').print_stats(100)
else:
load_scripts()
_main()
if "-d" in sys.argv and False: # Enable this to measure startup speed
import cProfile
cProfile.run('import bpy; bpy.load_scripts()', 'blender.prof')
import pstats
p = pstats.Stats('blender.prof')
p.sort_stats('cumulative').print_stats(100)
else:
load_scripts()

@ -25,3 +25,27 @@ def expandpath(path):
return path
# base scripts
_scripts = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir)
_scripts = (os.path.normpath(_scripts), )
def script_paths(*args):
if not args:
return _scripts
subdir = os.path.join(*args)
script_paths = []
for path in _scripts:
script_paths.append(os.path.join(path, subdir))
return script_paths
_presets = os.path.join(_scripts[0], "presets") # FIXME - multiple paths
def preset_paths(subdir):
'''
Returns a list of paths for a spesific preset.
'''
return (os.path.join(_presets, subdir), )

@ -110,7 +110,7 @@ class Operator(StructRNA, metaclass=OrderedMeta):
class Menu(StructRNA):
def path_menu(self, searchpath, operator):
def path_menu(self, searchpaths, operator):
layout = self.layout
# hard coded to set the operators 'path' to the filename.
@ -133,12 +133,24 @@ class Menu(StructRNA):
layout = self.layout
for f in sorted(os.listdir(searchpath)):
# collect paths
files = []
for path in searchpaths:
files.extend([(f, os.path.join(path, f)) for f in os.listdir(path)])
files.sort()
for f, path in files:
if f.startswith("."):
continue
path = os.path.join(searchpath, f)
path = os.path.normpath(path)
layout.item_stringO(operator, "path", path, text=path_to_name(f))
def draw_preset(self, context):
'''Define these on the subclass
- preset_operator
- preset_subdir
'''
import bpy
self.path_menu(bpy.utils.preset_paths(self.preset_subdir), self.preset_operator)

@ -37,11 +37,9 @@ def active_node_mat(mat):
class MATERIAL_MT_sss_presets(bpy.types.Menu):
bl_label = "SSS Presets"
def draw(self, context):
import os
template_dir = os.path.join(os.path.dirname(__file__), os.path.pardir, "presets", "sss")
self.path_menu(template_dir, "script.python_file_run")
preset_subdir = "sss"
preset_operator = "script.python_file_run"
draw = bpy.types.Menu.draw_preset
class MaterialButtonsPanel(bpy.types.Panel):

@ -35,11 +35,9 @@ class CLOTH_MT_presets(bpy.types.Menu):
Creates the menu items by scanning scripts/templates
'''
bl_label = "Cloth Presets"
def draw(self, context):
import os
template_dir = os.path.join(os.path.dirname(__file__), os.path.pardir, "presets", "cloth")
self.path_menu(template_dir, "script.python_file_run")
preset_subdir = "cloth"
preset_operator = "script.python_file_run"
draw = bpy.types.Menu.draw_preset
class PhysicButtonsPanel(bpy.types.Panel):

@ -23,15 +23,10 @@ narrowui = 180
class RENDER_MT_presets(bpy.types.Menu):
'''
Creates the menu items by scanning scripts/templates
'''
bl_label = "Render Presets"
def draw(self, context):
import os
template_dir = os.path.join(os.path.dirname(__file__), os.path.pardir, "presets", "render")
self.path_menu(template_dir, "script.python_file_run")
preset_subdir = "render"
preset_operator = "script.python_file_run"
draw = bpy.types.Menu.draw_preset
class RenderButtonsPanel(bpy.types.Panel):

@ -158,14 +158,6 @@ class TEXT_MT_text(bpy.types.Menu):
layout.itemO("text.properties", icon='ICON_MENU_PANEL')
#ifndef DISABLE_PYTHON
# XXX layout.column()
# XXX uiDefIconTextBlockBut(block, text_template_scriptsmenu, NULL, ICON_RIGHTARROW_THIN, "Script Templates", 0, yco-=20, 120, 19, "");
# XXX uiDefIconTextBlockBut(block, text_plugin_scriptsmenu, NULL, ICON_RIGHTARROW_THIN, "Text Plugins", 0, yco-=20, 120, 19, "");
#endif
layout.itemM("TEXT_MT_templates")
@ -177,8 +169,7 @@ class TEXT_MT_templates(bpy.types.Menu):
def draw(self, context):
import os
template_dir = os.path.join(os.path.dirname(__file__), os.path.pardir, "templates")
self.path_menu(template_dir, "text.open")
self.path_menu(bpy.utils.script_paths("templates"), "text.open")
class TEXT_MT_edit_view(bpy.types.Menu):