Last active
August 13, 2024 14:32
-
-
Save michalfratczak/9579c3a009230228e3c0be04f2c2607a to your computer and use it in GitHub Desktop.
SG sandbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import shotgun_api3 | |
from pprint import pprint | |
import re | |
SERVER_PATH = "https://******.shotgunstudio.com" | |
SCRIPT_NAME = '*******' | |
SCRIPT_KEY = '*******' | |
sg = shotgun_api3.Shotgun(SERVER_PATH, SCRIPT_NAME, SCRIPT_KEY) | |
pid_0000_sgdev01 = 14065 | |
rex_filename = re.compile('(\w+\d\d\d)(sh\d\d\d\d)_(\w+?)(__\w+)?_v(\d+)(\..+)?') # seq010sh0020_taskname__optional_sub_name_v001.hip | |
def filename_to_task(fname): | |
''' | |
parse filename and break into seq/shot/step/sub_step/version | |
''' | |
mo = re.match(rex_filename, fname) | |
if not mo: | |
return | |
res = { | |
'seq': mo.group(1), | |
'shot': mo.group(2), | |
'step': mo.group(3), | |
'sub_step': mo.group(4), | |
'version_number': mo.group(5), | |
} | |
return res | |
def get_project_id(pr_name, *args, **kwargs): | |
filters = [ ['name', 'is', pr_name] ] | |
fields = ['id', 'name'] | |
res = sg.find_one('Project', filters, fields) | |
if res: | |
return res['id'] | |
def get_sequence(pid, seq_name): | |
print(f'get_sequence({pid}, {seq_name})') | |
filters = [ | |
['project', 'is', {'type': 'Project', 'id': pid}], | |
['code', 'is', seq_name] ] | |
fields = ['id', 'content', 'code', 'entity'] | |
seq = sg.find_one('Sequence', filters, fields) | |
return seq | |
def get_shot(pid, shot_name): | |
filters = [ | |
['project', 'is', {'type': 'Project', 'id': pid}], | |
['code', 'is', shot_name], | |
] | |
fields = ['id', 'content', 'entity'] | |
shot = sg.find_one('Shot', filters, fields) | |
return shot | |
def get_task(pid, task_name, shot_id): | |
print( f'get_task({pid}, {task_name}, {shot_id})' ) | |
filters = [ | |
['project', 'is', {'type': 'Project', 'id': pid}], | |
['content', 'is', task_name], | |
# ['entity', 'is', {'type': 'entity', 'id': shot_id}], | |
] | |
fields = ['id', 'content', 'entity'] # step, task_assignees | |
tasks = sg.find('Task', filters, fields) | |
for t in tasks: | |
if t['entity']['id'] == shot_id and t['entity']['type'] == 'Shot': | |
return t | |
def get_entity_fields(pid, entity_id, entity_type, fields): | |
filters = [ | |
['project', 'is', {'type': 'Project', 'id': pid}], | |
['id', 'is', entity_id] ] | |
_fields = sg.find_one(entity_type, filters, fields) | |
if 'id' in _fields: del _fields['id'] | |
if 'type' in _fields: del _fields['type'] | |
return _fields | |
def main(): | |
proj_name = '0000_sgdev01' | |
pid = get_project_id(pr_name = proj_name) | |
print(f'pid {pid}') | |
# parse imaginary scene filename to get info on seq/shot/task/step | |
task_name_parts = filename_to_task( fname='sgd020sh0010_anim_v001.hip' ) | |
if not task_name_parts: | |
raise RuntimeError('this scene file is not a SHOT task') | |
print('task_name_parts') | |
pprint(task_name_parts) | |
seq_name = task_name_parts['seq'] | |
seq = get_sequence(pid, seq_name) | |
print('seq') | |
pprint(seq) | |
shot_name = task_name_parts['seq'] + task_name_parts['shot'] | |
shot = get_shot(pid, shot_name) | |
print('shot') | |
pprint(shot) | |
task_name = '{seq}{shot}_{step}'.format(**task_name_parts) | |
print(f'task_name {task_name}') | |
task = get_task( pid, task_name, shot['id'] ) | |
print('task') | |
pprint(task) | |
# get some info | |
fields = ['sg_cut_in', 'sg_cut_out', 'sg_lens_info', 'sg_resolution'] | |
fields_vals = get_entity_fields(pid, shot['id'], 'Shot', fields) | |
print('fields_vals') | |
pprint(fields_vals) | |
# dalej nie zadziała, bo w sekwencjach nie ma lens_info i resolution | |
# for any fields missing in shot, go up and seqrch in sequence | |
missing_fields = [ k for k in fields if not fields_vals.get(k, None) ] | |
seq_fields_vals = get_entity_fields(pid, seq['id'], "Sequence", missing_fields) | |
fields_vals.update(seq_fields_vals) | |
print('fields_vals - after sequence') | |
pprint(fields_vals) | |
# dalej nie zadziała, bo w projekcie nie ma lens_info i resolution | |
# for any fields missing in sequence, go up and search in project | |
missing_fields = [ k for k in fields if not fields_vals.get(k, None) ] | |
seq_fields_vals = get_entity_fields(pid, pid, "Project", missing_fields) | |
fields_vals.update(seq_fields_vals) | |
print('fields_vals - after project') | |
pprint(fields_vals) | |
if __name__ == '__main__': | |
print('') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment