Core¶
- class pyshotgrid.core.SGEntity(sg, entity_id, entity_type=None)[source]¶
Bases:
object
An instance of this class represents a single entity in ShotGrid.
Note
Try to avoid creating instances of this class in production code and use the
pyshotgrid.new_entity
method instead. This will make sure that you always get the correct entity class to work with.-
DEFAULT_SG_ENTITY_TYPE:
Optional
[str
] = None¶ The SG entity type that will be used when you do not specify it in the init arguments. For the base SGEntity this should always be None, but for sub classes this should be set to the SG entity type that the class should represent.
- property url: str¶
- Returns:
The ShotGrid URL for this entity.
Note
This will only work on entities that have a detail view enabled in the system settings.
- property name: Field¶
- Returns:
The field that represents the name of the entity. Usually either the “code” or “name” field.
- Raises:
- RuntimeError:
When the current entity does not have a “name” or “code” field.
- all_field_values(project_entity=None, raw_values=False)[source]¶
- Parameters:
- Return type:
- Returns:
All fields and values from this entity in a dict. If a project entity is given only fields that are visible to the project are returned.
- to_dict()[source]¶
Creates a dict with just “type” and “id” (and does not call SG).
Example:
>>> sg_entity.to_dict() {'type': 'CustomEntity01', 'id': 1}
- set(data, multi_entity_update_modes=None)[source]¶
Set many fields at once on this entity.
- Parameters:
data (
dict
[str
,Any
]) – A dict with the fields and values to set.multi_entity_update_modes (
Optional
[dict
[str
,Any
]]) –Optional dict indicating what update mode to use when updating a multi-entity link field. The keys in the dict are the fields to set the mode for, and the values from the dict are one of
set
,add
, orremove
. Defaults toset
.multi_entity_update_modes={"shots": "add", "assets": "remove"}
- Return type:
- get(fields, raw_values=False)[source]¶
Query many fields at once on this entity.
- Parameters:
- Return type:
- Returns:
A dict with the fields and their corresponding values.
- delete()[source]¶
Delete this entity.
Note
The python object that represents this entity does not make sense any more after you ran this method and will create errors if you keep calling functions on it.
- Return type:
- Returns:
Whether the entity was successfully deleted.
-
DEFAULT_SG_ENTITY_TYPE:
- class pyshotgrid.core.SGSite(sg)[source]¶
Bases:
object
An instance of this class represents a ShotGrid site as a whole.
Note
Try to avoid creating instances of this class in production code and use the
pyshotgrid.new_site
method instead. This gives you more ways to initialize the this class and ensures that the plugin system is correctly used.- __init__(sg)[source]¶
- Parameters:
sg (
Shotgun
) – A fully initialized instance of shotgun_api3.Shotgun.
- create(entity_type, data)[source]¶
The same function as
Shotgun.create
, but it accepts and returns a pyshotgrid object.
- find(entity_type, filters, order=None, filter_operator=None, limit=0, retired_only=False, page=0, include_archived_projects=True, additional_filter_presets=None)[source]¶
The same function as
Shotgun.find
, but it accepts and returns pyshotgrid objects.
- find_one(entity_type, filters, order=None, filter_operator=None, limit=0, retired_only=False, page=0, include_archived_projects=True, additional_filter_presets=None)[source]¶
The same function as
Shotgun.find_one
, but it accepts and returns pyshotgrid objects.
- projects(names_or_ids=None, include_archived=False, template_projects=False)[source]¶
- Parameters:
names_or_ids (
Optional
[list
[Union
[str
,int
]]]) – list of names or ids of the projects to return. The names can either match the “tank_name” (recommended) or the “name” field.include_archived (
bool
) – Whether to include archived projects or not.template_projects (
bool
) – Whether to return template projects or not.
- Return type:
- Returns:
A list of SG projects.
- class pyshotgrid.core.FieldSchema(sg, entity_type, name)[source]¶
Bases:
object
This class represents the schema of a field.
- class pyshotgrid.core.Field(name, entity)[source]¶
Bases:
FieldSchema
This class represents a field on a ShotGrid entity. It provides an interface to manage various aspects of a field.
- get(raw_values=False)[source]¶
- Parameters:
raw_values (
bool
) – Whether to return the raw dict values or the values converted to pyshotgrid objects.- Return type:
- Returns:
The value of the field. Any entities will be automatically converted to pyshotgrid objects. Fields with type “url” will return their values as follows:
- Uploaded Files:
Will return the dict that describes the uploaded file attachment, so you can pass it on to Shotgun.download_attachment().
- Link to local files:
Will return the platform dependent absolute path to the linked file.
- Link to a URL:
Will return the URL.
- download(path, create_folders=True)[source]¶
Download a file from a field.
- Parameters:
- Raises:
- RuntimeError:
When the field is not a “url” or “image” field.
- RuntimeError:
When nothing was uploaded to this field.
- Return type:
- Returns:
The full path of the downloaded file.
- property schema: FieldSchema¶
- Returns:
The schema of this field.
- pyshotgrid.core.new_entity(sg, *args, **kwargs)[source]¶
Create a new instance of a pyshotgrid class that represents a ShotGrid entity. This function is meant to be used as the main way to create new pyshotgrid instances and will always return the correct entity instance that you should work with.
Note
This does NOT create an entity in Shotgrid. It just gives you a python object that represents an entity.
The function can be used in 3 ways which all do the same thing:
>>> import pyshotgrid as pysg >>> sg_entity_a = pysg.new_entity(sg, {"id": 1, "type": "Project"}) >>> sg_entity_b = pysg.new_entity(sg, 1, "Project") >>> sg_entity_c = pysg.new_entity(sg, entity_id=1, entity_type="Project") >>> assert sg_entity_a == sg_entity_b == sg_entity_c
- pyshotgrid.core.new_site(*args, **kwargs)[source]¶
This function will create a new
pyshotgrid.SGSite
instance that represents a ShotGrid site. You can pass in either a shotgun_api3.Shotgun instance or the parameters of shotgun_api3.Shotgun itself. So this is equivalent:>>> sg = shotgun_api3.Shotgun(base_url='https://example.shotgunstudio.com', ... script_name='Some User', ... api_key='$ome_password') >>> sg_site = new_site(sg) >>> sg_site = new_site(base_url='https://example.shotgunstudio.com', ... script_name='Some User', ... api_key='$ome_password')
- Return type:
- Returns:
A new instance of the pyshotgrid site.
- pyshotgrid.core.register_pysg_class(pysg_class, shotgrid_type=None)[source]¶
Register a class for a ShotGrid type to pyshotgrid. This is best illustrated as by an example: Suppose you have a custom entity setup where you have an Episode in each project that collects some sequences of shots. It would be nice to have some additional functionality on the ShotGridEntity for episode objects (like a “sequences” function that returns all the sequences belonging to that episode). What you would do is to create a class SGEpisode that inherits from SGEntity and add all the functionality you like to it. After that you call:
register_plugin(SGEpisode)
or if you like it more explicitly:
register_plugin(pysg_class=SGEpisode, shotgrid_type="CustomProjectEntity01")
This will register the class to pyshotgrid and the new_entity function will automatically create an “SGEpisode” instance as soon as it encounters an Episode entity. This is also true for all queries that happen from a SGEntity. So sg_project[‘sg_episodes’] would return “SGEpisode” instances as well.
Note
Registering a class for an existing entity will overwrite the existing entity class. This way you can add/overwrite functionality for the classes that are shipped by default.
- pyshotgrid.core.register_sg_site_class(sg_site_class)[source]¶
Register a class that represents the ShotGrid site.
Note
This defaults to the SGSite class, but you can use it to overwrite this behaviour.
- pyshotgrid.core.convert_fields_to_pysg(sg, fields)[source]¶
Convert all the values from a fields dict to pysg objects where possible.
- pyshotgrid.core.convert_fields_to_dicts(fields)[source]¶
Convert all the values from a fields dict to simple dictionaries. The counterpart function to func:_convert_fields_to_pysg.
- pyshotgrid.core.convert_value_to_dict(value)[source]¶
Convert any pysg objects form the given value to simple dictionaries.
- pyshotgrid.core.convert_filters_to_dict(filters)[source]¶
Convert any pysg objects form the given shotgun_api3 filter to simple dictionaries.
Example:
>>> person = SGEntity(shotgun_api3.Shotgun('...'), entity_type='HumanUser', entity_id=5) >>> convert_filters_to_dict([['user', 'is', person]]) [['user', 'is', {'type': 'HumanUser', 'id': 5}]]