171 lines
5.4 KiB
Python
171 lines
5.4 KiB
Python
|
|
||
|
import bpy
|
||
|
import os
|
||
|
import re
|
||
|
import urllib3
|
||
|
import traceback
|
||
|
import time
|
||
|
|
||
|
from bpy.props import PointerProperty, StringProperty
|
||
|
from pathlib import Path
|
||
|
from vse_toolbox.bl_utils import get_addon_prefs, get_settings
|
||
|
from vse_toolbox.file_utils import install_module, norm_str
|
||
|
from vse_toolbox.resources.trackers.tracker import Tracker
|
||
|
|
||
|
gazu = install_module('gazu')
|
||
|
|
||
|
class Kitsu(Tracker):
|
||
|
name = "Kitsu"
|
||
|
|
||
|
url: StringProperty()
|
||
|
login: StringProperty()
|
||
|
password: StringProperty(subtype='PASSWORD')
|
||
|
|
||
|
project_name = None
|
||
|
# episode_name = None
|
||
|
|
||
|
def draw_prefs(self, layout):
|
||
|
layout.prop(self, 'url', text='Url')
|
||
|
layout.prop(self, 'login', text='Login')
|
||
|
layout.prop(self, 'password', text='Password')
|
||
|
|
||
|
# @property
|
||
|
# def project(self):
|
||
|
# settings = get_settings()
|
||
|
|
||
|
# return next(
|
||
|
# (p for p in settings._projects
|
||
|
# if norm_str(p['name']) == norm_str(settings.project_name)), {})
|
||
|
|
||
|
# @property
|
||
|
# def project_name(self):
|
||
|
# return self.project.get('name', '')
|
||
|
|
||
|
# @property
|
||
|
# def all_episodes(self):
|
||
|
# return self.project.get('episodes', {})
|
||
|
|
||
|
# @property
|
||
|
# def episode_name(self):
|
||
|
# return self.episode.get('name', '')
|
||
|
|
||
|
# @property
|
||
|
# def episode(self):
|
||
|
# settings = get_settings()
|
||
|
# return next(
|
||
|
# (e for e in self.project.get('episodes', {})
|
||
|
# if norm_str(e['name']) == norm_str(settings.episode_template)), {})
|
||
|
|
||
|
def get_projects(self):
|
||
|
return gazu.project.all_open_projects()
|
||
|
|
||
|
def get_episodes(self, project):
|
||
|
return gazu.shot.all_episodes_for_project(project)
|
||
|
|
||
|
def get_episode(self, name):
|
||
|
return gazu.shot.get_episode_by_name(self.project, name)
|
||
|
|
||
|
def new_episode(self, name):
|
||
|
return gazu.shot.new_episode(self.project, name)
|
||
|
|
||
|
# def update_project(self):
|
||
|
# self.project['episode'] = tracker.get_episodes(p)
|
||
|
# return self.project
|
||
|
|
||
|
def connect(self, url=None, login=None, password=None):
|
||
|
'''Connect to kitsu api using provided url, login and password'''
|
||
|
|
||
|
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))
|
||
|
|
||
|
def get_asset_info(self, data, asset_path):
|
||
|
modified = time.time_ns()
|
||
|
catalog = data['entity_type_name'].title()
|
||
|
asset_path = self.prop_rel_path(asset_path, 'source_directory')
|
||
|
|
||
|
asset_info = dict(
|
||
|
filepath=asset_path,
|
||
|
modified=modified,
|
||
|
library_id=self.library.id,
|
||
|
assets=[dict(
|
||
|
catalog=catalog,
|
||
|
metadata=data.get('data', {}),
|
||
|
description=data['description'],
|
||
|
tags=[],
|
||
|
type=self.data_type,
|
||
|
name=data['name'])
|
||
|
]
|
||
|
)
|
||
|
|
||
|
return asset_info
|
||
|
|
||
|
def fetch(self):
|
||
|
"""Gather in a list all assets found in the folder"""
|
||
|
print(f'Fetch Assets for {self.library.name}')
|
||
|
|
||
|
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}
|
||
|
|
||
|
cache = self.read_cache()
|
||
|
|
||
|
for asset_data in gazu.asset.all_assets_for_project(project):
|
||
|
asset_data['entity_type_name'] = entity_types_ids[asset_data.pop('entity_type_id')]
|
||
|
asset_name = asset_data['name']
|
||
|
|
||
|
asset_field_data = dict(asset_name=asset_name, type=asset_data['entity_type_name'], source_directory=self.source_directory)
|
||
|
|
||
|
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
|
||
|
|
||
|
|
||
|
asset_path = self.prop_rel_path(asset_path, 'source_directory')
|
||
|
asset_cache_data = dict(
|
||
|
catalog=asset_data['entity_type_name'].title(),
|
||
|
metadata=asset_data.get('data', {}),
|
||
|
description=asset_data['description'],
|
||
|
tags=[],
|
||
|
type=self.data_type,
|
||
|
name=asset_data['name']
|
||
|
)
|
||
|
|
||
|
cache.add_asset_cache(asset_cache_data, filepath=asset_path)
|
||
|
|
||
|
return cache
|