Shotgun常用代码段

zhangly 2021-11-06 23:16:35
Categories: > Tags:

用api key获取sg对象

sg = shotgun_api3.Shotgun("https://piedpiper.shotgunstudio.com",
                          script_name="compress",
                          api_key="01234567ef0123456789abcdef")

获取所有项目

def getAllProjects():
    fields = ['id','name','type']
    projects= sg.find("Project",[],fields)
    
    if len(projects) < 1:
        print "couldn't find any projects"
    else:
        print "Found " + str(len(projects)) + " projects"
    
    return projects

>>> {'type': 'Project', 'id': 87, 'name': 'TST'}

获取指定Project所有的sequence(需要Project id)

def getSequencesByProjId(proj_id):

    fields = ['id','type','code']
    filters = [['project','is',{'type':'Project','id':proj_id}]]
    sequences= sg.find("Sequence",filters,fields)
    
    if len(sequences) < 1:
        print "couldn't find any sequences"

    return sequences

>>> {'code': 'test_seq001', 'type': 'Sequence', 'id': 154}

获取指定seq下所有shot(需要sequence id)

def getShotsBySeqId(seq_id):
    fields = ['id','type','code']
    filters = [['sg_sequence','is',{'type':'Sequence','id':seq_id}]]
    shots= sg.find("Shot",filters,fields)
    
    if len(shots) < 1:
        print "couldn't find any shots"
    else:
        None   

    return shots

>>> {'code': 'zly_test_001', 'type': 'Shot', 'id': 3485}

列出指定Project Sequence下的Character资产

fields = ['id', 'code', 'sg_asset_type']
sequence_id = 154
project_id = 87
filters = [
    ['project', 'is', {'type': 'Project', 'id': project_id}],
    ['sg_asset_type', 'is', 'Character'],
    ['sequences', 'is', {'type': 'Sequence', 'id': sequence_id}]
]
assets = sg.find("Asset",filters,fields)
print assets

下载指定version id的mov file

version = sg.find_one("Version", [["id", "is", 8649]], ["sg_uploaded_movie"])
local_file_path = os.path.join(r"D:\temp", version["sg_uploaded_movie"]["name"])
sg.download_attachment(version["sg_uploaded_movie"], file_path=local_file_path)

用名称寻找asset

def findAssetByName(proj_id, name):
    fields = ['id', 'code', 'sg_asset_type', 'tasks']
    filters = [['project', 'is', {'type': 'Project', 'id': proj_id}], ['code', 'is', name]]
    result = sg.find('Asset', filters, fields)

    return result

指定project id,场次名和镜头号,获取镜头信息

def findShotByName(project_id, seq, shot):
    fields = ['id', 'code', 'sg_asset_type', 'tasks', 'sg_sequence']
    filters = [['project', 'is', {'type': 'Project', 'id': project_id}], ['code', 'is', shot]]

    result = sg.find('Shot', filters, fields)

    for x in result:
        name = x['sg_sequence']['name']
        if name == seq:
            return x

    return

打印出指定项目下,所有Sequence,和Seq下所有Shots

fields = ['id', 'type', 'code']
filters = [['project', 'is', {'type': 'Project', 'id': 87}]]
sequences = sg.find("Sequence", filters, fields)
for seq in sequences:
    print "Sequence: ", seq.get('code'), seq.get('id')
    fields = ['id', 'type', 'code']
    filters = [['sg_sequence', 'is', {'type': 'Sequence', 'id': seq.get('id')}]]
    shots = sg.find("Shot", filters, fields)

    for shot in shots:
        print 'Shot: ', shot.get('code')

创建一个镜头,任务,和版本(指定Project id和Sequence id)

# 创建Shot
shot_name = 'TESTSHOT_001'
data = {
    'project': {'type': 'Project', 'id': 87},
    'sg_sequence': {'type': 'Sequence', 'id': 246},
    'code': shot_name,
    'sg_status_list': 'ip'
}

s = sg.create('Shot', data)

# 在Shot下创建Task
data = {
    'project': {'type': 'Project', 'id': 87},
    'content': 'ani01', # 任务名
    'step': {'name': 'Animation', 'type': 'Step', 'id': const.PIPELINE_STEP_ID.get('Animation')}, # 任务类型
    'entity': {'type': 'Shot', 'id': s.get('id')} # 这里指定Shot id
}

t = sg.create('Task', data)
print t

# 在Task下创建Version
data = {
        'project': {'type': 'Project', 'id': 87},
        'code': 'testshot_ani_v001',
        'sg_status_list': 'rev',
        'entity': {'type': 'Shot', 'id': s.get('id')}, # Shot id
        'sg_task': {'type': 'Task', 'id': t.get('id')}, # Task id
        'user': {'type': 'HumanUser', 'id': 220} # 创建Version的人员
        }

version = sg.create('Version', data)
print version

对自定义Entity进行创建

data = {
    'project': {'type': 'Project', 'id': 87},
    'code': 'test_plate_v005',
    'sg_shot': {'type': 'Shot', 'id': 6661},
    'updated_by': {'type': 'Group', 'id': 3}, # 更新用户为群组
    'sg_original_resolution': '1920x1080',
    'sg_plate_type': 'BG01',
    'sg_frames_scanned': 150,
    'sg_scan_start': 1,
    'sg_scan_end': 150
}

plate = sg.create('CustomEntity10', data)
print plate

对自定义Entity进行查找

import pprint 

filters = [
    ['project', 'is', {'type': 'Project', 'id': 87}]
]
fields = ['code']
result = sg.find('CustomEntity10', filters, fields)

pprint.pprint(result)

查询指定类型的资产(指定Project,Sequence id)

fields = ['id', 'sg_asset_type', 'code']
filters = [
    ['project', 'is', {'type': 'Project', 'id': 87}],
    ['sequence', 'is', {'type': 'Sequence', 'id': 247}],
    ['sg_asset_type', 'is', 'Character'] # 指定Character类型

]
chr = sg.find('Asset', filters, fields)
pprint.pprint(chr)