예제 #1
0
    def draw(self, context):
        layout = self.layout

        col = layout.column()
        box = col.box().column(align=True)
        box.label(text=_('Reload local asset JSON files'))
        box.operator(ReloadAssetJsons.bl_idname, icon='FILE_REFRESH')

        preferences = get_preferences()

        box = col.box().column(align=True)
        box.label(text=_('Download and Update to the latest assets'))
        operator = box.operator(UpdateAssetJson.bl_idname,
                                icon='TRIA_DOWN_BAR')
        operator.repo = preferences.asset_json_update_repo
        operator.query = preferences.asset_json_update_query

        props = context.scene.mmd_uuunyaa_tools_asset_operator

        row = col.row(align=True)
        row.prop(
            props,
            'debug_expanded',
            icon='TRIA_DOWN' if props.debug_expanded else 'TRIA_RIGHT',
            icon_only=True,
            emboss=False,
        )
        row.label(text=_('Debug'))

        if not props.debug_expanded:
            return

        box = col.box().column()
        box.label(text=_('Fetch an asset for debug'), icon='MODIFIER')
        box.column(align=True).prop(props,
                                    'debug_issue_number',
                                    text=_('issue #'))

        row = box.row(align=True)
        row.operator(DeleteDebugAssetJson.bl_idname, icon='CANCEL')
        row.operator(
            UpdateDebugAssetJson.bl_idname,
            icon='TRIA_DOWN_BAR').issue_number = props.debug_issue_number

        box = col.box().column()
        box.label(text=_(
            'Download and Update to the latest filtered assets for debug'),
                  icon='FILTER')

        box.prop(props, 'repo', text=_('Repository'))
        box.prop(props, 'query', text=_('Query'))
        box.prop(props, 'output_json', text=_('Write to'))

        operator = box.operator(UpdateAssetJson.bl_idname,
                                text=_('Update Assets JSON by query'),
                                icon='TRIA_DOWN_BAR')
        operator.repo = props.repo
        operator.query = props.query
        operator.output_json = props.output_json
예제 #2
0
    def delete_assets_json(delete_json: str) -> bool:
        preferences = get_preferences()
        json_path = os.path.join(preferences.asset_jsons_folder, delete_json)

        if not os.path.exists(json_path):
            return False

        os.remove(json_path)
        return True
예제 #3
0
    def execute(self, context):
        # pylint: disable=too-many-locals

        preferences = get_preferences()

        max_search_result_count = preferences.asset_search_results_max_display_count

        query = context.scene.mmd_uuunyaa_tools_asset_search.query
        query_type = query.type
        query_text = query.text.lower()
        query_tags = query.tags
        query_is_cached = query.is_cached

        enabled_tag_names = {tag.name for tag in query_tags if tag.enabled}
        enabled_tag_count = len(enabled_tag_names)

        search_results: List[AssetDescription] = []
        search_results = [
            asset for asset in ASSETS.values()
            if (query_type in {AssetType.ALL.name, asset.type.name} and
                enabled_tag_count == len(asset.tag_names & enabled_tag_names)
                and query_text in asset.keywords and
                (Utilities.is_importable(asset) if query_is_cached else True))
        ]

        hit_count = len(search_results)
        update_time = to_int32(time.time_ns() >> 10)
        result = context.scene.mmd_uuunyaa_tools_asset_search.result
        result.count = min(max_search_result_count, hit_count)
        result.hit_count = hit_count
        result.asset_items.clear()
        result.update_time = update_time

        for asset in search_results[:max_search_result_count]:
            CONTENT_CACHE.async_get_content(
                asset.thumbnail_url,
                functools.partial(self._on_thumbnail_fetched, result,
                                  context.region, update_time, asset))

        tag_names = set()
        for asset in search_results:
            tag_names.update(asset.tag_names)

        query.is_updating = True
        try:
            query_tags.clear()
            query.tags_index = 0
            for tag_name in sorted(tag_names):
                tag = query_tags.add()
                tag.name = tag_name
                tag.enabled = tag_name in enabled_tag_names
        finally:
            query.is_updating = False

        return {'FINISHED'}
예제 #4
0
    def reload(self):
        self.delete_cache_object()

        preferences = get_preferences()
        asset_cache_folder = preferences.asset_cache_folder
        if not os.path.exists(asset_cache_folder):
            os.makedirs(asset_cache_folder, exist_ok=True)

        self._cache = ContentCache(
            cache_folder=asset_cache_folder,
            max_cache_size_bytes=preferences.asset_max_cache_size * 1024 *
            1024,
            temporary_dir=tempfile.mkdtemp())
예제 #5
0
def initialize_asset_registory():
    preferences = get_preferences()

    if preferences.asset_json_update_on_startup_enabled:
        try:
            print(f"Asset Auto Update: repo='{preferences.asset_json_update_repo}', query='{preferences.asset_json_update_query}'")
            AssetUpdater.write_assets_json(
                AssetUpdater.fetch_assets_json_by_query(preferences.asset_json_update_repo, preferences.asset_json_update_query),
                AssetUpdater.default_assets_json
            )
        except:  # pylint: disable=bare-except
            traceback.print_exc()

    ASSETS.reload()
예제 #6
0
    def reload(self):
        preferences = get_preferences()

        self.assets.clear()

        json_paths = glob.glob(os.path.join(preferences.asset_jsons_folder, '*.json'))
        json_paths.sort()
        for json_path in json_paths:
            try:
                with open(json_path, encoding='utf-8') as file:
                    for asset in json.load(file)['assets']:
                        try:
                            self.add(_Utilities.from_dict(asset))
                        except:  # pylint: disable=bare-except
                            traceback.print_exc()
            except:  # pylint: disable=bare-except
                traceback.print_exc()
예제 #7
0
    def resolve_path(asset: AssetDescription) -> Tuple[str, str]:
        preferences = get_preferences()
        asset_extract_root_folder = preferences.asset_extract_root_folder
        asset_extract_folder = preferences.asset_extract_folder
        asset_extract_json = preferences.asset_extract_json

        context = _Utilities.to_context(asset)
        asset_path = os.path.join(
            asset_extract_root_folder,
            asset_extract_folder.format(**context)
        )

        asset_json = os.path.join(
            asset_path,
            asset_extract_json.format(**context)
        )

        return (asset_path, asset_json)
예제 #8
0
 def write_assets_json(assets_json_object, output_json: str):
     preferences = get_preferences()
     with open(os.path.join(preferences.asset_jsons_folder, output_json), mode='wt', encoding='utf-8') as file:
         json.dump(assets_json_object, file, ensure_ascii=False, indent=2)