Change Version Number when creating Assets/Asset Versions
1 1

9 posts in this topic

How do i define version numbers within the ftrack API?  This is the code that's creating the "asset" of the thumbnail below.   

self.task_asset = self.ftrack.create('Asset', {
    'name': self.version,
    'type': asset_type,
    'parent': self.entity_data
})

Right now however, i'm just creating a new "Asset" for every version and giving it a version number where "name" is.   

Basically what i'm wanting to do is create a shot 000_0500, and this would be 'taskName v23' in my ideal world.   I just couldn't ever get the version number to change so i'm doing it this way.  I've tried using Assets, AssetVesions, etc... 

I have everything 'working' in terms of uploading quicktimes, image previews, thumbnails etc... but there's definitely something that's not right in what i'm doing with either the way i'm conceptualizing Assets or AssetVersions, or components or something. 

would love to know the proper way of controlling the number in v1 below!

Thanks

Capture.PNG

Share this post


Link to post
Share on other sites

Hi @Tom Mikota, although we allow to take control the versioning system this is something I would highly discourage of doing  if not for extreme and well planned cases,
as it would risk to compromise your versions consistency. Is always better let have ftrack come up with the right version at publish time.
(more than one user could be in the process of publishing the same version , hitting a conflict !) .

If you want go ahead anyhow here is a little snippet for you to use (again please be careful), to publish a new version under a shot:

import ftrack_api

s = ftrack_api.Session()
asset = s.query('Asset').first()
shot = asset['parent']
status = s.query('Status').first()


version = s.create('AssetVersion', {
    'asset': asset,
    'status': status,
    'comment': 'Added more leaves.',
    'task': shot,
    'version': 1000
})

s.commit()

 

Share this post


Link to post
Share on other sites

When i attempt to create a second AssetVersion and connect it to an Existing Asset i get a "duplicate entry" error:

Quote

ftrack_api.exception.ServerError: Server reported error: IntegrityError((_mysql_exceptions.IntegrityError) (1062, "Duplicate entry '8f4144e0-a8e6-11e2-9e96-0800200c9a66-010_0400_plate-16c81285-619' for key 'asset_Asset.typeid_key'") [SQL: u'INSERT INTO asset (id, name, context_id, taskid, type_id) VALUES (%s, %s, %s, %s, %s)'] [parameters: (u'2d1e88f5-c439-4c80-a446-2edc43f158f1', '010_0400_plate', u'16c81285-619c-400b-837a-b6f54c641ab4', None, u'8f4144e0-a8e6-11e2-9e96-0800200c9a66')] (Background on this error at: http://sqlalche.me/e/gkpj))

self.version_data = self.ftrack.create('AssetVersion', {
    'asset': self.task_asset,
    'task': self.task_asset['parent'],
})

This is the way i'm currently doing it.  Adding "version" to this doesn't change me getting the error.   This works fine the first time i run it and create the initial "assetVersion" but after that it gives me an error, so i've been getting around it in other ways (creating an Asset for each AssetVersion) but i'd love to do it the proper "ftrack" way, as i'm running into some hurdles with this methodology.

Share this post


Link to post
Share on other sites

That link is what i've based all my code off of.   I"ve restructured my code for the purposes of this conversation to look as identical to the docs as possible.   I have the same result.  I can run this code once, but get "duplicate entry" errors after that.   

My question is - what is ftrack expecting as a trigger that i'm uploading a new 'version' and therefore starts incrementing on the version numbers.   I would assume based off this conversation that i don't change anything on the create('assetversion') code.

task = session.get('Task', 'dfa65df9-d955-4d4e-9658-e2bf8c11a10c')
asset_parent = task['parent']

asset_type = session.query('AssetType where name is "Geometry"').one()
asset = session.create('Asset', {
    'name': task['name'],
    'type': asset_type,
    'parent': asset_parent
})
asset_version = session.create('AssetVersion', {
    'asset': asset,
    'task': task
})

session.commit()

asset_version.create_component(path=preview_path,
                               data={
                                    'name': 'ftrackreview-image',
                               },
                               location='auto'
                               )
asset_version.create_component(path=thumb_path,
                               data={
                                    'name': 'thumbnail'
                               },
                               location='auto'
                               )

session.commit()

Share this post


Link to post
Share on other sites

I feel kind of dumb on this one, i've spent days on this, figured out hacks to work around the duplicate entry problem, and now i'm back to it trying to figure it out properly.  I'm assuming there's just a fundamental thing i'm not seeing because i have a different paradigm in my head for what's going on under the hood.   But i'm not sure what it is.  

 

 

Share this post


Link to post
Share on other sites

OK i got this figured out, and yes it was dumb lol.   Expressing my thoughts in a written form really helped me debug this one.   This is my final code for these tests just in case anyone else runs into this.  I just had to add a simple query (which i was already doing in my code but for some reason couldn't get it working until now) for checking to see if the Asset already existed.  Once you have the asset Ftrack handles the version numbers.   Which is exactly what you're saying, but something about the mutiple components flying around got me confused.   Thanks for your patience all! 

session = ftrack_api.Session(server_url=server_url, api_key=api_key, api_user=api_user, auto_connect_event_hub=True)
server_location = session.query('Location where name is "ftrack.server"').first()
path = r'D:\VFX\FRIDAY_ROOT\testco\render\cgl_unittest\shots\020\0100\plate\tmiko\000.001\high\Capture.PNG'
preview_path = r'D:\VFX\FRIDAY_ROOT\testco\render\cgl_unittest\shots\020\0200\plate\tmiko\000.001\high\.preview\Capture.jpg'
thumb_path = r'D:\VFX\FRIDAY_ROOT\testco\render\cgl_unittest\shots\020\0200\plate\tmiko\000.001\high\.thumb\Capture.jpg'

task = session.get('Task', 'dfa65df9-d955-4d4e-9658-e2bf8c11a10c')
asset_parent = task['parent']
asset = session.query('Asset where name is "{0}" and parent.id is "{1}"'.format(task['name'], asset_parent['id'])).one()
asset_type = session.query('AssetType where name is "Geometry"').one()
if not asset:
    asset = session.create('Asset', {
        'name': task['name'],
        'type': asset_type,
        'parent': asset_parent
    })
    session.commit()

asset_version = session.query('AssetVersion where asset_id is %s' % asset['id']).first()

asset_version = session.create('AssetVersion', {
    'asset': asset,
    'task': task
})
print asset_version['id'], 'asset_version_id'

session.commit()

asset_version.create_component(path=preview_path,
                               data={
                                    'name': 'ftrackreview-image',
                               },
                               location=server_location
                               )
asset_version.create_component(path=thumb_path,
                               data={
                                    'name': 'thumbnail'
                               },
                               location=server_location
                               )

session.commit()

Share this post


Link to post
Share on other sites

Hi, I would change the Asset query from .one() to .first() if you leave .one() it will complain that the Asset doesn't exist (ftrack_api.exception.NoResultFoundError), setting it to .first() will return None which will properly trigger your if not asset: code block.  There should only be one Asset with the same name under the same parent so .first() should work just fine. If you still need to set the version numbers yourself (at times we do that to make sure that the version has the same number that the component published) and you want to avoid duplicate errors, much like what you did with the Asset, you can query the AssetVersion with the specific version number under the same asset and see if it returns a value. If it does you could either notify the user that a previous version exist, and perhaps as if overwriting is desired (this can be dangerous but useful at times).

As a side note, on the code you provided you are setting the value of asset_version the first time to a query that returns the first entry it finds under the asset, than overriding it with a new AssetVersion entity you create.

Share this post


Link to post
Share on other sites

Good points Yas, and yeah i'm finding that i am needing to control the version numbers at times, at least now i've got the version up happening on the end of Ftrack.   I like your explanation of first() and one() as well, i'm still brute forcing my way thought some things as i get a larger overview.   On the asset_version - that's a leftover from a test i was doing, forgot to delete it lol.   

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
1 1