Example #1
0
def app_template_paths(subdir=None):
    """
    Returns valid application template paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :return: app template paths.
    :rtype: generator
    """

    # note: LOCAL, USER, SYSTEM order matches script resolution order.
    subdir_tuple = (subdir, ) if subdir is not None else ()

    path = _os.path.join(*(resource_path('LOCAL'), "scripts", "startup",
                           "bl_app_templates_user", *subdir_tuple))
    if _os.path.isdir(path):
        yield path
    else:
        path = _os.path.join(*(resource_path('USER'), "scripts", "startup",
                               "bl_app_templates_user", *subdir_tuple))
        if _os.path.isdir(path):
            yield path

    path = _os.path.join(*(resource_path('SYSTEM'), "scripts", "startup",
                           "bl_app_templates_system", *subdir_tuple))
    if _os.path.isdir(path):
        yield path
Example #2
0
def is_path_builtin(path):
    """
    Returns True if the path is one of the built-in paths used by Blender.

    :arg path: Path you want to check if it is in the built-in settings directory
    :type path: str
    :rtype: bool
    """
    # Note that this function is is not optimized for speed,
    # it's intended to be used to check if it's OK to remove presets.
    #
    # If this is used in a draw-loop for example, we could cache some of the values.
    user_path = resource_path('USER')

    for res in ('SYSTEM', 'LOCAL'):
        parent_path = resource_path(res)
        if not parent_path or parent_path == user_path:
            # Make sure that the current path is not empty string and that it is
            # not the same as the user config path. IE "~/.config/blender" on Linux
            # This can happen on portable installs.
            continue

        try:
            if _os.path.samefile(_os.path.commonpath([parent_path]),
                                 _os.path.commonpath([parent_path, path])):
                return True
        except FileNotFoundError:
            # The path we tried to look up doesn't exist.
            pass
        except ValueError:
            # Happens on Windows when paths don't have the same drive.
            pass

    return False
Example #3
0
def app_template_paths(subdir=None):
    """
    Returns valid application template paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :return: app template paths.
    :rtype: generator
    """

    # note: LOCAL, USER, SYSTEM order matches script resolution order.
    subdir_tuple = (subdir,) if subdir is not None else ()

    path = _os.path.join(*(
        resource_path('LOCAL'), "scripts", "startup",
        "bl_app_templates_user", *subdir_tuple))
    if _os.path.isdir(path):
        yield path
    else:
        path = _os.path.join(*(
            resource_path('USER'), "scripts", "startup",
            "bl_app_templates_user", *subdir_tuple))
        if _os.path.isdir(path):
            yield path

    path = _os.path.join(*(
        resource_path('SYSTEM'), "scripts", "startup",
        "bl_app_templates_system", *subdir_tuple))
    if _os.path.isdir(path):
        yield path
Example #4
0
def app_template_paths(subdir=None):
    """
    Returns valid application template paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :return: app template paths.
    :rtype: generator
    """
    # Note: keep in sync with: Blender's BKE_appdir_app_template_any

    subdir_tuple = (subdir, ) if subdir is not None else ()

    # Avoid adding 'bl_app_templates_system' twice.
    # Either we have a portable build or an installed system build.
    for resource_type, module_name in (
        ('USER', "bl_app_templates_user"),
        ('LOCAL', "bl_app_templates_system"),
        ('SYSTEM', "bl_app_templates_system"),
    ):
        path = resource_path(resource_type)
        if path:
            path = _os.path.join(*(path, "scripts", "startup", module_name,
                                   *subdir_tuple))
            if _os.path.isdir(path):
                yield path
                # Only load LOCAL or SYSTEM (never both).
                if resource_type == 'LOCAL':
                    break
Example #5
0
def app_template_paths(subdir=None):
    """
    Returns valid application template paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :return: app template paths.
    :rtype: generator
    """
    # Note: keep in sync with: Blender's BKE_appdir_app_template_any

    subdir_tuple = (subdir,) if subdir is not None else ()

    # Avoid adding 'bl_app_templates_system' twice.
    # Either we have a portable build or an installed system build.
    for resource_type, module_name in (
            ('USER', "bl_app_templates_user"),
            ('LOCAL', "bl_app_templates_system"),
            ('SYSTEM', "bl_app_templates_system"),
    ):
        path = resource_path(resource_type)
        if path:
            path = _os.path.join(
                *(path, "scripts", "startup", module_name, *subdir_tuple))
            if _os.path.isdir(path):
                yield path
                # Only load LOCAL or SYSTEM (never both).
                if resource_type == 'LOCAL':
                    break
Example #6
0
def is_path_builtin(path):
    """
    Returns True if the path in question in one of the built in paths used by blender.

    :arg path: Path you want to check if it is in the built in settings directory
    """
    search_path = _os.path.abspath(path)
    user_path = resource_path('USER')

    for res in ('SYSTEM', 'LOCAL'):
        parent_path = resource_path(res)
        if not parent_path or parent_path == user_path:
            # Make sure that the current path is not empty string and that it is
            # not the same as the user config path. IE "~/.config/blender" on linux
            # This can happen on portable installs.
            continue

        if _os.path.samefile(_os.path.commonpath([parent_path]),
                             _os.path.commonpath([parent_path, path])):
            return True

    return False
Example #7
0
def script_paths(subdir=None, user_pref=True, check_all=False):
    """
    Returns a list of valid script paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :arg user_pref: Include the user preference script path.
    :type user_pref: bool
    :arg check_all: Include local, user and system paths rather just the paths
       blender uses.
    :type check_all: bool
    :return: script paths.
    :rtype: list
    """
    scripts = list(_scripts)
    prefs = _bpy.context.user_preferences

    # add user scripts dir
    if user_pref:
        user_script_path = prefs.filepaths.script_directory
    else:
        user_script_path = None

    if check_all:
        # all possible paths
        base_paths = tuple(
            _os.path.join(resource_path(res), "scripts")
            for res in ('LOCAL', 'USER', 'SYSTEM'))
    else:
        # only paths blender uses
        base_paths = _bpy_script_paths()

    for path in base_paths + (user_script_path, ):
        if path:
            path = _os.path.normpath(path)
            if path not in scripts and _os.path.isdir(path):
                scripts.append(path)

    if subdir is None:
        return scripts

    script_paths = []
    for path in scripts:
        path_subdir = _os.path.join(path, subdir)
        if _os.path.isdir(path_subdir):
            script_paths.append(path_subdir)

    return script_paths
Example #8
0
def script_paths(subdir=None, user_pref=True, all=False):
    """
    Returns a list of valid script paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :arg user_pref: Include the user preference script path.
    :type user_pref: bool
    :arg all: Include local, user and system paths rather just the paths
       blender uses.
    :type all: bool
    :return: script paths.
    :rtype: list
    """
    scripts = list(_scripts)
    prefs = _bpy.context.user_preferences

    # add user scripts dir
    if user_pref:
        user_script_path = prefs.filepaths.script_directory
    else:
        user_script_path = None

    if all:
        # all possible paths
        base_paths = tuple(_os.path.join(resource_path(res), "scripts")
                           for res in ('LOCAL', 'USER', 'SYSTEM'))
    else:
        # only paths blender uses
        base_paths = _bpy_script_paths()

    for path in base_paths + (user_script_path, ):
        if path:
            path = _os.path.normpath(path)
            if path not in scripts and _os.path.isdir(path):
                scripts.append(path)

    if subdir is None:
        return scripts

    script_paths = []
    for path in scripts:
        path_subdir = _os.path.join(path, subdir)
        if _os.path.isdir(path_subdir):
            script_paths.append(path_subdir)

    return script_paths
Example #9
0
def script_paths(subdir=None, user_pref=True, check_all=False):
    """
    Returns a list of valid script paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :arg user_pref: Include the user preference script path.
    :type user_pref: bool
    :arg check_all: Include local, user and system paths rather just the paths
       blender uses.
    :type check_all: bool
    :return: script paths.
    :rtype: list
    """
    scripts = list(_scripts)

    # Only paths Blender uses.
    #
    # Needed this is needed even when 'check_all' is enabled,
    # so the 'BLENDER_SYSTEM_SCRIPTS' environment variable will be used.
    base_paths = _bpy_script_paths()

    if check_all:
        # All possible paths, no duplicates, keep order.
        base_paths = (
            *(path for path in (_os.path.join(resource_path(res), "scripts")
                                for res in ('LOCAL', 'USER', 'SYSTEM'))
              if path not in base_paths),
            *base_paths,
        )

    for path in (*base_paths, script_path_user(), script_path_pref()):
        if path:
            path = _os.path.normpath(path)
            if path not in scripts and _os.path.isdir(path):
                scripts.append(path)

    if subdir is None:
        return scripts

    scripts_subdir = []
    for path in scripts:
        path_subdir = _os.path.join(path, subdir)
        if _os.path.isdir(path_subdir):
            scripts_subdir.append(path_subdir)

    return scripts_subdir
Example #10
0
def script_paths(subdir=None, user_pref=True, check_all=False):
    """
    Returns a list of valid script paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :arg user_pref: Include the user preference script path.
    :type user_pref: bool
    :arg check_all: Include local, user and system paths rather just the paths
       blender uses.
    :type check_all: bool
    :return: script paths.
    :rtype: list
    """
    scripts = list(_scripts)

    # Only paths Blender uses.
    #
    # Needed this is needed even when 'check_all' is enabled,
    # so the 'BLENDER_SYSTEM_SCRIPTS' environment variable will be used.
    base_paths = _bpy_script_paths()

    if check_all:
        # All possible paths, no duplicates, keep order.
        base_paths = (
            *(path for path in (_os.path.join(resource_path(res), "scripts")
              for res in ('LOCAL', 'USER', 'SYSTEM')) if path not in base_paths),
            *base_paths,
            )

    for path in (*base_paths, script_path_user(), script_path_pref()):
        if path:
            path = _os.path.normpath(path)
            if path not in scripts and _os.path.isdir(path):
                scripts.append(path)

    if subdir is None:
        return scripts

    scripts_subdir = []
    for path in scripts:
        path_subdir = _os.path.join(path, subdir)
        if _os.path.isdir(path_subdir):
            scripts_subdir.append(path_subdir)

    return scripts_subdir
Example #11
0
 def user_paths(name):
     paths = []
     dirpath = _os.path.join(resource_path('USER'), 'config')
     path = _os.path.join(dirpath, name + '.pth')
     if _os.path.isfile(path):
         cwd = _os.getcwd()
         _os.chdir(dirpath)
         try:
             with open(path) as f:
                 for line in f.readlines():
                     line = line.rstrip('\n')
                     if line:
                         if line.startswith('#'):
                             continue
                         p = _os.path.abspath(_os.path.expanduser(line))
                         if _os.path.isdir(p):
                             if p not in paths:
                                 paths.append(p)
         except:
             pass
         _os.chdir(cwd)
     return paths
Example #12
0
def script_paths(subdir=None, user_pref=True, check_all=False):
    """
    Returns a list of valid script paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :arg user_pref: Include the user preference script path.
    :type user_pref: bool
    :arg check_all: Include local, user and system paths rather just the paths
       blender uses.
    :type check_all: bool
    :return: script paths.
    :rtype: list
    """
    scripts = list(_scripts)

    if check_all:
        # all possible paths
        base_paths = tuple(_os.path.join(resource_path(res), "scripts") for res in ("LOCAL", "USER", "SYSTEM"))
    else:
        # only paths blender uses
        base_paths = _bpy_script_paths()

    for path in base_paths + (script_path_user(), script_path_pref()):
        if path:
            path = _os.path.normpath(path)
            if path not in scripts and _os.path.isdir(path):
                scripts.append(path)

    if subdir is None:
        return scripts

    scripts_subdir = []
    for path in scripts:
        path_subdir = _os.path.join(path, subdir)
        if _os.path.isdir(path_subdir):
            scripts_subdir.append(path_subdir)

    return scripts_subdir
Example #13
0
def script_paths(subdir=None, user_pref=True, check_all=False):
    """
    Returns a list of valid script paths.

    :arg subdir: Optional subdir.
    :type subdir: string
    :arg user_pref: Include the user preference script path.
    :type user_pref: bool
    :arg check_all: Include local, user and system paths rather just the paths
       blender uses.
    :type check_all: bool
    :return: script paths.
    :rtype: list
    """
    scripts = list(_scripts)

    # Only paths Blender uses.
    #
    # Needed this is needed even when 'check_all' is enabled,
    # so the 'BLENDER_SYSTEM_SCRIPTS' environment variable will be used.
    base_paths = _bpy_script_paths()

    if check_all:
        # All possible paths, no duplicates, keep order.
        base_paths = (
            *(path for path in (_os.path.join(resource_path(res), "scripts")
              for res in ('LOCAL', 'USER', 'SYSTEM')) if path not in base_paths),
            *base_paths,
            )

    for path in (*base_paths, script_path_user(), script_path_pref()):
        if path:
            path = _os.path.normpath(path)
            if path not in scripts and _os.path.isdir(path):
                scripts.append(path)

    def user_paths(name):
        paths = []
        dirpath = _os.path.join(resource_path('USER'), 'config')
        path = _os.path.join(dirpath, name + '.pth')
        if _os.path.isfile(path):
            cwd = _os.getcwd()
            _os.chdir(dirpath)
            try:
                with open(path) as f:
                    for line in f.readlines():
                        line = line.rstrip('\n')
                        if line:
                            if line.startswith('#'):
                                continue
                            p = _os.path.abspath(_os.path.expanduser(line))
                            if _os.path.isdir(p):
                                if p not in paths:
                                    paths.append(p)
            except:
                pass
            _os.chdir(cwd)
        return paths

    for p in user_paths('scripts'):
        if p not in scripts:
            scripts.append(p)

    if subdir is None:
        return scripts

    scripts_subdir = []
    for path in scripts:
        path_subdir = _os.path.join(path, subdir)
        if _os.path.isdir(path_subdir):
            scripts_subdir.append(path_subdir)

    for p in user_paths(subdir):
        if p not in scripts_subdir:
            scripts_subdir.append(p)

    return scripts_subdir