Skip to content

Instantly share code, notes, and snippets.

@surrealroad
Created October 30, 2019 17:57
Show Gist options
  • Save surrealroad/5c8484c14bbc2a654b13e936f0624ba6 to your computer and use it in GitHub Desktop.
Save surrealroad/5c8484c14bbc2a654b13e936f0624ba6 to your computer and use it in GitHub Desktop.
Update Shotgun field descriptions from a CSV
#!/usr/bin/env python
# encoding: utf-8
"""
sg_import_assetlist.py
Import field descriptions from CSV file
Created by Jack James on 2019-10-30.
Copyright (c) 2019 Autodesk. All rights reserved.
Tasks
• Update fields where a description is set in the CSV
"""
import argparse, csv, unicodedata
from shotgun_api3.shotgun import Shotgun
preview = False
parser = argparse.ArgumentParser(description='Import new asset list from CSV file')
parser.add_argument('-i', '--input', help='Input asset list CSV file', required=True)
parser.add_argument('-s', '--script', help='API Script Name', required=True)
parser.add_argument('-k', '--key', help='API Script Key', required=True)
parser.add_argument('-u', '--url', help='Shotgun Site URL', required=True)
args = vars(parser.parse_args())
print("Connecting to Shotgun")
# Fill this in with your server and script/key info
SERVER= args['url']
LOGIN = args['script']
KEY = args['key']
sg = Shotgun(SERVER, LOGIN, KEY)
# return values from a row
def readFieldMeta(row, headers):
colIndex = 0
for header in headers:
if header == "Entity Type":
entity_type = row[colIndex].decode('iso-8859-1').encode('ascii','ignore').strip()
elif header == "Field Code":
field_code = row[colIndex].decode('iso-8859-1').encode('ascii','ignore').strip()
elif header == "Description":
description = row[colIndex].decode('iso-8859-1').encode('ascii','ignore').strip()
colIndex +=1
return {"entity_type":entity_type, "field_code":field_code, "description":description}
# update the description for a field
def updateFieldDescription(sg, entity_type, field_code, description):
valid_entity_types = ["ActionMenuItem",
"ApiUser",
"ApiUserProjectConnection",
"AppWelcomeUserConnection",
"Asset",
"AssetAssetConnection",
"AssetBlendshapeConnection",
"AssetElementConnection",
"AssetEpisodeConnection",
"AssetLevelConnection",
"AssetMocapTakeConnection",
"AssetSceneConnection",
"AssetSequenceConnection",
"AssetShootDayConnection",
"AssetShotConnection",
"Asset_sg_vendor_groups_Connection",
"Attachment",
"BannerUserConnection",
"Camera",
"CameraMocapTakeConnection",
"ClientUser",
"Composition",
"CustomEntity01",
"Cut",
"CutItem",
"CutVersionConnection",
"Delivery",
"Department",
"ElementShotConnection",
"Episode",
"EventLogEntry",
"FilesystemLocation",
"Group",
"GroupUserConnection",
"HumanUser",
"Icon",
"Launch",
"LaunchSceneConnection",
"LaunchShotConnection",
"Launch_sg_vendor_groups_Connection",
"Level",
"LocalStorage",
"MocapTakeRange",
"MocapTakeRangeShotConnection",
"Note",
"Page",
"PageHit",
"PageSetting",
"Performer",
"PerformerMocapTakeConnection",
"PerformerRoutineConnection",
"PerformerShootDayConnection",
"PermissionRuleSet",
"PhysicalAsset",
"PhysicalAssetMocapTakeConnection",
"PipelineConfiguration",
"PipelineConfigurationUserConnection",
"Playlist",
"PlaylistShare",
"PlaylistVersionConnection",
"Project",
"ProjectTaskTemplateConnection",
"ProjectUserConnection",
"PublishedFile",
"PublishedFileDependency",
"PublishedFileType",
"ReleaseTicketConnection",
"Reply",
"RevisionRevisionConnection",
"RevisionTicketConnection",
"RvLicense",
"Sequence",
"Sequence_sg_vendor_groups_Connection",
"ShootDay",
"ShootDaySceneConnection",
"Shot",
"ShotShotConnection",
"Shot_sg_vendor_groups_Connection",
"Software",
"Status",
"Step",
"Tag",
"Task",
"TaskDependency",
"TaskTemplate",
"Ticket",
"TicketTicketConnection",
"TimeLog",
"Version"
]
if not entity_type in valid_entity_types:
return
if not preview:
schema=None
try:
schema = sg.schema_field_read(entity_type, field_code)
except:
print "Failed finding %s.%s" % (entity_type, field_code)
return
valid_properties = [
"name",
"description",
"summary_default",
"valid_types",
"custom_metadata"
]
properties = {}
for index, (key, value) in enumerate(schema[field_code].items()):
if "value" in value and key in valid_properties:
properties[key] = value["value"]
properties["description"] = description
try:
sg.schema_field_update(entity_type, field_code, properties)
except:
print "Failed %s.%s" % (entity_type, field_code)
return
print("Reading CSV file at "+args['input'])
with open(args['input'], "rU") as csvFile:
csvreader = csv.reader(csvFile)
# get list of events
line = 1
headers = None
for row in csvreader:
if not headers:
# header row
print("Reading headers")
headers = row
else:
field_meta = readFieldMeta(row, headers)
if field_meta["description"]:
print("Setting description for %s.%s: %s" % (field_meta["entity_type"], field_meta["field_code"], field_meta["description"]))
updateFieldDescription(sg, field_meta["entity_type"], field_meta["field_code"], field_meta["description"])
line +=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment