copy_component


import sys
sys.path.insert(0, r'D:\code\wuzu\motionbuilder_pipe\src')
import ssl
import _ssl
reload(_ssl)
reload(ssl)
import pipe
import nftrack
import ftrack_api
from nftrack import utils
from pipe import config
import ftrack_api

session = nftrack.load_session()
session.api_user

cache_client.flush_all()
from pymemcache.client import base
cache_client = base.Client('pipe.rs.netease.com:11211')
cache_client.get('rsvs_fb98f191-1bd8-419d-86cb-2a4ed29deae4')

# pb = publish_base.BasePubWindow()
# pb.show()
session.rollback()
tasks= session.get('Task', '4910cfde-4c58-11ed-9c97-927c4f031638')['parent']['id']
session.items

show = session.get('Folder', '0ae82a16-6e1b-11ec-9625-5a2eadbea560')
session.get('Component', '83a8afec-b25a-11ec-b892-9ed4c8068700').get('parent')
source_asset_build = session.get('AssetBuild', entity_id)
target_asset_build = session.create('AssetBuild', {
    'parent': show,
    'type': source_asset_build['type'],
    'name': source_asset_build['name'],
    'status': source_asset_build['status'],
    'description': source_asset_build['description'],
})
target_asset_build['id']
print('创建资产')
session.commit()
session.get('AssetBuild', 'acdf1760-f7ab-4dc5-97b6-9b8abfb0d3e5')
print source_asset_build['metadata'].items()
if source_asset_build['metadata']:
    target_asset_build['metadata'] = source_asset_build['metadata']
    session.commit()

for task in session.query('Task where parent_id is {}'.format(entity_id)):
    target_task = session.create('Task', {
        'parent': target_asset_build,
        'type': task['type'],
        'name': task['name']
    })
    print('创建任务')
    session.commit()
    # 筛选最新版本
    version_entity = session.query(
        'AssetVersion where task.id is {} '
        'order by version descending'.format(task['id']))

    if version_entity:
        latest_version = version_entity.first()
    else:
        continue

    target_version = copy_version_entity(session, latest_version,
                                         target_task)
    print('创建version')
    session.commit()
    for component in latest_version['components']:
        copy_component(session, component, target_version)
        print('创建component')


def copy_version_entity(session, version, target_task):
    asset_type_entity = version['asset']
    query_str = ('Asset where name is "{}" '
                 'and type.id is "{}" '
                 'and parent.id is "{}"').format(
        asset_type_entity['name'],
        asset_type_entity['type']['id'],
        target_task['parent']['id'])

    asset_entity = session.query(query_str).first()

    if not asset_entity:
        asset_entity = session.create('Asset', {
            'name': asset_type_entity['name'],
            'type': asset_type_entity['type'],
            'parent': target_task['parent'],
        })

    version_entity = session.query(
        'AssetVersion where task.id is {} '
        'order by version descending'.format(target_task['id']))

    if version_entity:
        version_num = version_entity.first()['version'] + 1
    else:
        version_num = 1
    version_data = {'version': version_num,
                    'asset': asset_entity,
                    'task': target_task,
                    'comment': u'复制版本,原版本ID:{}'.format(version['id'])
                    }
    target_version = session.create('AssetVersion', version_data)
    target_version['thumbnail'] = version['thumbnail']
    session.commit()
    return target_version


def copy_component(session, component, target_version):
    location = component['component_locations'][0]['location']
    location_type = type(location.accessor)
    tmp_filepath = ''
    if location_type == ftrack_api.accessor.disk.DiskAccessor:
        tmp_filepath = location.get_filesystem_path(component)
    elif location_type == ftrack_api.accessor.server._ServerAccessor:
        tmp_filepath = location.get_url(component)
    elif location_type == ftrack_api.symbol.Symbol:
        tmp_filepath = component.get('component_locations')[0][
            'resource_identifier']
    if not tmp_filepath:
        return
    data = {'name': component['name'],
            'file_type': component['file_type'],
            'metadata': component['metadata'] or {}}
    location = session.query(
        'Location where label is "asset_unmanaged"').first()
    ftrack_api.mixin(location,
                     ftrack_api.entity.location.UnmanagedLocationMixin)
    location.accessor = ftrack_api.accessor.disk.DiskAccessor(prefix='')
    location.structure = ftrack_api.structure.origin.OriginStructure()

    component = target_version.create_component(tmp_filepath, data, location)
    pipe.eventlog.publish('copy_asset',
                          level=logging.INFO,
                          remark=u'component{}:{}  复制完成'.format(
                              component['name'], component['id']),
                          metadata='')
    print(component['name'])
    session.commit()

评论
  目录