2022-12-24 15:30:32 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
Plugin for making an asset library of all blender file found in a folder
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from asset_library.adapters.adapter import AssetLibraryAdapter
|
|
|
|
from asset_library.common.template import Template
|
|
|
|
from asset_library.common.file_utils import install_module
|
|
|
|
|
|
|
|
import bpy
|
|
|
|
from bpy.props import (StringProperty, IntProperty, BoolProperty)
|
|
|
|
import re
|
|
|
|
from pathlib import Path
|
|
|
|
from itertools import groupby
|
|
|
|
import uuid
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import json
|
|
|
|
import urllib3
|
|
|
|
import traceback
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
class KitsuLibrary(AssetLibraryAdapter):
|
|
|
|
|
|
|
|
name = "Kitsu"
|
|
|
|
template_name : StringProperty()
|
|
|
|
template_file : StringProperty()
|
2022-12-28 17:44:15 +01:00
|
|
|
source_directory : StringProperty(subtype='DIR_PATH')
|
2022-12-24 15:30:32 +01:00
|
|
|
|
|
|
|
url: StringProperty()
|
|
|
|
login: StringProperty()
|
|
|
|
password: StringProperty(subtype='PASSWORD')
|
|
|
|
project_name: StringProperty()
|
|
|
|
|
|
|
|
def connect(self, url=None, login=None, password=None):
|
|
|
|
'''Connect to kitsu api using provided url, login and password'''
|
|
|
|
|
2022-12-25 02:54:50 +01:00
|
|
|
gazu = install_module('gazu')
|
2022-12-24 15:30:32 +01:00
|
|
|
urllib3.disable_warnings()
|
|
|
|
|
|
|
|
if not self.url:
|
|
|
|
print(f'Kitsu Url: {self.url} is empty')
|
|
|
|
return
|
|
|
|
|
|
|
|
url = self.url
|
|
|
|
if not url.endswith('/api'):
|
|
|
|
url += '/api'
|
|
|
|
|
|
|
|
print(f'Info: Setting Host for kitsu {url}')
|
|
|
|
gazu.client.set_host(url)
|
|
|
|
|
|
|
|
if not gazu.client.host_is_up():
|
|
|
|
print('Error: Kitsu Host is down')
|
|
|
|
|
|
|
|
try:
|
|
|
|
print(f'Info: Log in to kitsu as {self.login}')
|
|
|
|
res = gazu.log_in(self.login, self.password)
|
|
|
|
print(f'Info: Sucessfully login to Kitsu as {res["user"]["full_name"]}')
|
|
|
|
return res['user']
|
|
|
|
except Exception as e:
|
|
|
|
print(f'Error: {traceback.format_exc()}')
|
|
|
|
|
|
|
|
def get_asset_path(self, name, catalog, directory=None):
|
|
|
|
directory = directory or self.source_directory
|
|
|
|
return Path(directory, self.get_asset_relative_path(name, catalog))
|
|
|
|
|
2022-12-28 17:44:15 +01:00
|
|
|
def get_asset_description(self, data, asset_path):
|
2022-12-24 15:30:32 +01:00
|
|
|
|
|
|
|
modified = time.time_ns()
|
|
|
|
catalog = data['entity_type_name']
|
2022-12-28 17:44:15 +01:00
|
|
|
asset_path = self.prop_rel_path(asset_path, 'source_directory')
|
|
|
|
#asset_name = self.norm_file_name(data['name'])
|
2022-12-24 15:30:32 +01:00
|
|
|
|
|
|
|
asset_description = dict(
|
2022-12-28 17:44:15 +01:00
|
|
|
filepath=asset_path,
|
2022-12-24 15:30:32 +01:00
|
|
|
modified=modified,
|
|
|
|
library_id=self.library.id,
|
|
|
|
assets=[dict(
|
|
|
|
catalog=catalog,
|
|
|
|
metadata=data.get('data', {}),
|
|
|
|
description=data['description'],
|
|
|
|
tags=[],
|
|
|
|
type=self.data_type,
|
2022-12-28 17:44:15 +01:00
|
|
|
image=self.template_image.raw,
|
|
|
|
video=self.template_video.raw,
|
2022-12-24 15:30:32 +01:00
|
|
|
name=data['name'])
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2022-12-28 12:02:45 +01:00
|
|
|
return asset_description
|
2022-12-24 15:30:32 +01:00
|
|
|
|
2022-12-25 02:54:50 +01:00
|
|
|
# def bundle(self, cache_diff=None):
|
|
|
|
# """Group all asset in one or multiple blends for the asset browser"""
|
2022-12-24 15:30:32 +01:00
|
|
|
|
2022-12-25 02:54:50 +01:00
|
|
|
# return super().bundle(cache_diff=cache_diff)
|
2022-12-24 15:30:32 +01:00
|
|
|
|
|
|
|
def get_preview(self, asset_data):
|
|
|
|
|
|
|
|
name = asset_data['name']
|
2022-12-27 23:49:57 +01:00
|
|
|
preview = (f / template_image.format(name=name)).resolve()
|
2022-12-24 15:30:32 +01:00
|
|
|
if not preview.exists():
|
|
|
|
preview_blend_file(f, preview)
|
|
|
|
|
|
|
|
return preview
|
|
|
|
|
|
|
|
|
|
|
|
def fetch(self):
|
|
|
|
"""Gather in a list all assets found in the folder"""
|
|
|
|
|
|
|
|
print(f'Fetch Assets for {self.library.name}')
|
|
|
|
|
2022-12-25 02:54:50 +01:00
|
|
|
gazu = install_module('gazu')
|
2022-12-24 15:30:32 +01:00
|
|
|
self.connect()
|
|
|
|
|
|
|
|
template_file = Template(self.template_file)
|
|
|
|
template_name = Template(self.template_name)
|
|
|
|
|
|
|
|
project = gazu.client.fetch_first('projects', {'name': self.project_name})
|
|
|
|
entity_types = gazu.client.fetch_all('entity-types')
|
|
|
|
entity_types_ids = {e['id']: e['name'] for e in entity_types}
|
|
|
|
|
2022-12-28 12:02:45 +01:00
|
|
|
asset_descriptions = []
|
2022-12-25 02:54:50 +01:00
|
|
|
for asset_data in gazu.asset.all_assets_for_project(project)[:10]:
|
2022-12-24 15:30:32 +01:00
|
|
|
asset_data['entity_type_name'] = entity_types_ids[asset_data.pop('entity_type_id')]
|
|
|
|
asset_name = asset_data['name']
|
|
|
|
|
2022-12-28 17:44:15 +01:00
|
|
|
asset_field_data = dict(name=asset_name, type=asset_data['entity_type_name'], source_directory=self.source_directory)
|
2022-12-24 15:30:32 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
asset_field_data.update(template_name.parse(asset_name))
|
|
|
|
except Exception:
|
|
|
|
print(f'Warning: Could not parse {asset_name} with template {template_name}')
|
|
|
|
|
|
|
|
asset_path = template_file.find(asset_field_data)
|
|
|
|
if not asset_path:
|
|
|
|
print(f'Warning: Could not find file for {template_file.format(asset_field_data)}')
|
|
|
|
continue
|
|
|
|
|
|
|
|
#print(asset_path)
|
|
|
|
|
2022-12-28 12:02:45 +01:00
|
|
|
asset_descriptions.append(self.get_asset_description(asset_data, asset_path))
|
2022-12-24 15:30:32 +01:00
|
|
|
|
|
|
|
#asset = load_datablocks(asset_path, data_type='collections', names=asset_data['name'], link=True)
|
|
|
|
#if not asset:
|
|
|
|
# print(f"Asset {asset_name} not found in {asset_path}")
|
|
|
|
|
|
|
|
|
|
|
|
#asset_description = self.get_asset_description(asset)
|
|
|
|
|
2022-12-28 12:02:45 +01:00
|
|
|
#asset_descriptions.append(asset_description)
|
2022-12-24 15:30:32 +01:00
|
|
|
|
|
|
|
#print(assets)
|
|
|
|
# for k, v in assets[0].items():
|
|
|
|
# print(f'- {k} {v}')
|
|
|
|
|
2022-12-28 12:02:45 +01:00
|
|
|
#print('+++++++++++++')
|
|
|
|
#print(asset_descriptions)
|
|
|
|
|
|
|
|
return asset_descriptions
|