예제 #1
0
    def import_vpd(vpd_file_path, scale=0.08, asset=None):
        asset_path, _ = _Utilities.resolve_path(asset)

        print(f'import_vpd({vpd_file_path},{scale},{asset_path})')
        try:
            bpy.ops.mmd_tools.import_vpd('INVOKE_DEFAULT',
                                         filepath=os.path.join(
                                             asset_path, vpd_file_path),
                                         scale=scale)
        except AttributeError as ex:
            if str(
                    ex
            ) != 'Calling operator "bpy.ops.mmd_tools.import_vpd" error, could not be found':
                raise
            raise MessageException(
                _('Failed to invoke mmd_tools\nPlease install mmd_tools.')
            ) from ex
        except RuntimeError as ex:
            if str(
                    ex
            ) != 'Operator bpy.ops.mmd_tools.import_vpd.poll() failed, context is incorrect':
                raise
            raise MessageException(
                _('Select an object.\nThe target object for pose import is not selected.'
                  )) from ex
예제 #2
0
    def un7zip(zip_file_path=None, password=None, asset=None):
        asset_path, asset_json = _Utilities.resolve_path(asset)

        print(f'un7zip({zip_file_path},{asset_path},{asset_json})')

        if _Utilities.is_extracted(asset):
            return

        namespace = 'x7zipfile'
        loader = importlib.machinery.SourceFileLoader(
            namespace,
            os.path.join(PACKAGE_PATH, 'externals', 'x7zipfile',
                         'x7zipfile.py'))
        x7zipfile = loader.load_module(namespace)  # pylint: disable=deprecated-method

        try:
            with x7zipfile.x7ZipFile(zip_file_path, pwd=password) as zip_file:
                zip_file.extractall(path=asset_path)
        except x7zipfile.x7ZipCannotExec as ex:
            raise MessageException(
                _('Failed to execute 7z\nPlease install p7zip-full or 7-zip and setup the PATH properly.'
                  )) from ex

        _Utilities.write_json(asset)
        ImportActionExecutor.chmod_recursively(asset_path, stat.S_IWRITE)
예제 #3
0
    def import_world(blend_file_path, world_name, asset=None):
        asset_path, _ = _Utilities.resolve_path(asset)

        print(f'import_world({blend_file_path},{world_name},{asset_path})')

        bpy.ops.wm.append(
            directory=os.path.join(asset_path, blend_file_path, 'World'),
            files=[{
                'name': world_name
            }],
        )

        bpy.context.scene.world = bpy.data.worlds[world_name]
예제 #4
0
    def import_collections(blend_file_path, *collection_names, asset=None):
        asset_path, _ = _Utilities.resolve_path(asset)

        print(
            f'import_collections({blend_file_path},{collection_names},{asset_path})'
        )
        bpy.ops.wm.append(
            # 'INVOKE_DEFAULT',
            # filepath=os.path.join(asset_path, blend_file_path, 'Collection', collection_name),
            directory=os.path.join(asset_path, blend_file_path, 'Collection'),
            files=[{
                'name': name
            } for name in collection_names],
        )
예제 #5
0
    def import_pmx(pmx_file_path, scale=0.08, asset=None):
        asset_path, _ = _Utilities.resolve_path(asset)

        print(f'import_pmx({pmx_file_path},{scale},{asset_path})')
        try:
            bpy.ops.mmd_tools.import_model('INVOKE_DEFAULT',
                                           filepath=os.path.join(
                                               asset_path, pmx_file_path),
                                           scale=scale)
        except AttributeError as ex:
            if str(
                    ex
            ) != 'Calling operator "bpy.ops.mmd_tools.import_model" error, could not be found':
                raise
            raise MessageException(
                _('Failed to invoke mmd_tools\nPlease install mmd_tools.')
            ) from ex
예제 #6
0
    def link(to_name, from_path=None, asset=None):
        asset_path, asset_json = _Utilities.resolve_path(asset)

        print(f'link({to_name},{from_path},{asset_path},{asset_json})')

        if _Utilities.is_extracted(asset):
            return

        to_path = os.path.join(asset_path, to_name)

        os.makedirs(asset_path, exist_ok=True)
        try:
            os.link(from_path, to_path)
        except OSError as ex:
            # Invalid cross-device link
            if ex.errno != errno.EXDEV:
                raise ex
            shutil.copyfile(from_path, to_path)

        _Utilities.write_json(asset)
        ImportActionExecutor.chmod_recursively(asset_path, stat.S_IWRITE)
예제 #7
0
    def unzip(zip_file_path=None, encoding='cp437', password=None, asset=None):
        asset_path, asset_json = _Utilities.resolve_path(asset)

        print(f'unzip({zip_file_path},{asset_path},{asset_json})')

        if _Utilities.is_extracted(asset):
            return

        pwd = password.encode() if password else None

        with zipfile.ZipFile(zip_file_path) as zip_file:
            for info in zip_file.infolist():
                if info.flag_bits ^ 0x800:
                    info.filename = info.orig_filename.encode('cp437').decode(
                        encoding)

                if os.sep != '/' and os.sep in info.filename:
                    info.filename = info.filename.replace(os.sep, '/')

                zip_file.extract(info, path=asset_path, pwd=pwd)

        _Utilities.write_json(asset)
        ImportActionExecutor.chmod_recursively(asset_path, stat.S_IWRITE)
예제 #8
0
    def unrar(rar_file_path=None, password=None, asset=None):
        asset_path, asset_json = _Utilities.resolve_path(asset)

        print(f'unrar({rar_file_path},{asset_path},{asset_json})')

        if _Utilities.is_extracted(asset):
            return

        namespace = 'xrarfile'
        loader = importlib.machinery.SourceFileLoader(
            namespace,
            os.path.join(PACKAGE_PATH, 'externals', 'xrarfile', 'xrarfile.py'))
        xrarfile = loader.load_module(namespace)  # pylint: disable=deprecated-method

        try:
            with xrarfile.XRarFile(rar_file_path) as rar:
                rar.extractall(path=asset_path, pwd=password)
        except xrarfile.XRarCannotExec as ex:
            raise MessageException(
                _('Failed to execute unrar or WinRAR\nPlease install unrar or WinRAR and setup the PATH properly.'
                  )) from ex

        _Utilities.write_json(asset)
        ImportActionExecutor.chmod_recursively(asset_path, stat.S_IWRITE)