Finding out the latest asset version number/string via python
-
Hi! I'm currently trying to get the latest version of an asset in the asset browser. At first, I thought it should work with "assetDescription.GetVersion()", but that only gives me some kind of ID. The same goes for "GetIdAndVersion()".
Maybe someone here has an idea on how to get the latest asset version number (or string) in python?
Cheers!
MPB -
Hello @MPB,
Welcome to the Maxon developers forum and its community, it is great to have you with us!
Getting Started
Before creating your next postings, we would recommend making yourself accustomed with our forum and support procedures. You did not do anything wrong, we point all new users to these rules.
- Forum Overview: Provides a broad overview of the fundamental structure and rules of this forum, such as the purpose of the different sub-forums or the fact that we will ban users who engage in hate speech or harassment.
- Support Procedures: Provides a more in detail overview of how we provide technical support for APIs here. This topic will tell you how to ask good questions and limits of our technical support.
- Forum Features: Provides an overview of the technical features of this forum, such as Markdown markup or file uploads.
It is strongly recommended to read the first two topics carefully, especially the section Support Procedures: How to Ask Questions.
About your First Question
Without your code and the asset you are trying to read, helping youn concretely is impossible. Asset versioning is demonstrated pretty throughly in this code example.
I think your major misconception is that an asset version string has to be what you would consider a version descriptor, .e.g., "1.0". But that is absolutely not the case, and you can put there anything you want. The system will by default put there hashes; which is probably what you mean with "some kind of ID".
You must evaluate the date of last modification of each version of an asset to temporally order them. But that is all not really necessary when you just want to get the last version of an asset, as that is already baked into search operations with
maxon.ASSET_FIND_MODE.LATEST. I.e., when you have either an asset ID or an asset description, you just search for that asset ID and set the find mode to latest. See this example for a concrete case.You can also search for all versions of an asset and then sort them yourself via the time stamp (see also first example link). You can also access the other versions of an asset if you have just one asset description, as each asset links to its other versions with a list of asset-id-version tuples.
Cheers,
Ferdinand -
Hi Ferdinant,
I'm very sorry for not thinking more about my post. Yes, the devil is in the details: I'm trying to access the version description and - if possible - counting it up +0.1 for the automatic upload of a new version of the asset.
Here is part of my code as an example, although it's just a proof of concept at the moment. I've added some comments for clarification.
def process_object(obj, doc, repo, target_category_id, force_new_id=False): #creating a new asset and using the commentary-tag to store asset information tag = obj.GetTag(c4d.Tannotation) or obj.MakeTag(c4d.Tannotation) tag.SetName(TAG_NAME) tag[c4d.ANNOTATIONTAG_TEXT] = obj.GetName() existing_id_str = tag[c4d.ANNOTATIONTAG_URL] #using the URL-field for the asset-id asset_id = None if existing_id_str and not force_new_id: try: temp_id = maxon.Id(existing_id_str) #check if the asset-ID is in the database asset_desc = repo.FindLatestAsset(maxon.AssetTypes.File().GetId(), temp_id, maxon.Id(), maxon.ASSET_FIND_MODE.LATEST) if not asset_desc.IsNullValue(): #if the asset is found.... asset_id = temp_id #...use the temp ID as the current ID #get latest version description and try to set the version number +0.1 print(asset_desc.GetVersion()) print(asset_desc.GetRepositoryId()) print(asset_desc.GetMetaData()) current_version_str = str(asset_desc.GetVersion()) # <--- this is the tricky part --- if current_version_str: try: current_version = float(current_version_str) + 0.1 #try to convert it to float and add 0.1 except ValueError: current_version = 1.0 #...if unsuccessful (string that shouldn't be used), then start with 1.0 again else: #if none current_version = 1.0 .....I hope this makes sense.
Thank you very much for any help! -
Hey,
as I already explained,
maxon.ASSET_FIND_MODE.LATESTensures that you find the last version of an asset. So this line of yours:asset_desc = repo.FindLatestAsset(maxon.AssetTypes.File().GetId(), temp_id, maxon.Id(), maxon.ASSET_FIND_MODE.LATEST)will retrieve the last asset version of an asset with the ID
temp_idwhich is of asset typeFile. If you would usemaxon.ASSET_FIND_MODE.ALLfor example and then also notFindLatestAssetbutFindAssets, you would find all assets that have the IDtemp_id.The id of an asset is not necessarily unique within a repository. It shares its ID with all other versions of that asset. Only the ID and version can uniquely identify an asset. And as said before, the version of an asset does not have to be a (quasi) numeric value as you assume it to be. The asset version is always a string, and in some cases, for example in the code example I linked to above, people put there something that could be parsed into a numeric value. But the asset version can also just be the string 'Bob's your uncle' or just some hash. So, you cannot sort assets temporally by their asset version. It is just another identifier that makes that version unique within the namespace of the asset ID.
If you want to temporally sort asset versions, first search for all assets with that ID, and then sort them using their timestamp. This, asset versioning, time stamps and other asset metadata, all has been extensively covered in the code example I already linked to above. Please read the example.
Cheers,
Ferdinand