ExtendScript: Inconsistency when adding new XMP metadata field

Explorer ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

Hi! I'm having trouble sorting out a function in my Premiere CEP panel that used to seem to work, but is no longer behaving the way I expect it to. My goal is to create a custom metadata field if that field doesn't already exist in the Premiere project's metadata schema, and return whether or not that operation was successful. It works sometimes, and doesn't work depending on what Premiere project is active, even though the metadata field seems to be successfully created in either one. Here's my function, which is based on the PPro sample panel:

 

createCustomMetadata: function (name, label, type) {
   var kPProPrivateProjectMetadataURI = "http://ns.adobe.com/premierePrivateProjectMetaData/1.0/";
   var customMetadataSuccess = false;
   if (app.isDocumentOpen()) {
      var projectItem = app.project.rootItem.children[0];
      if (projectItem) {
         if (ExternalObject.AdobeXMPScript === undefined) 
            ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
         if (ExternalObject.AdobeXMPScript !== undefined) {
            var projectMetadata = projectItem.getProjectMetadata();
	    var xmp = new XMPMeta(projectMetadata);
            //check if custom metadata field already exists in this project.
	    var propertyExists = xmp.doesPropertyExist(kPProPrivateProjectMetadataURI, label);
	    if (!propertyExists){ // if the custom field doesn't already exist, add it now.
	       var successfullyAdded = app.project.addPropertyToProjectMetadataSchema(name, label, type);
	       alert('successfully added: ' + successfullyAdded);
	       var newProjectMetadata = projectItem.getProjectMetadata();
	       var newXMP = new XMPMeta(newProjectMetadata);
	       propertyExists = newXMP.doesPropertyExist(kPProPrivateProjectMetadataURI, label);
	    }
            if (propertyExists) //should be true at this point unless successfullyAdded is false
               customMetadataSuccess = true;
         }
      }
   }
   return customMetadataSuccess;
}

 

And I call it elsewhere like this:

 

var success = $.runScript.createCustomMetadata("TranscodeOriginalPath", "TranscodeOriginalPath", 2);
if (!success) // if metadata field still doesn't exist, immediately stop.
   alert("Could not initialize replace function.")

 

I'm running four tests to compare what happens.
In Test 1, I'm using a project I did this with a few months ago (so it already has the TranscodeOriginalPath metadata field created). When createCustomMetadata() runs, it doesn't print anything because it never enters the if (propertyExists){...} block. It's called in my second code snippet with no issues.
In Test 2, I'm using the same project as Test 1 but I manually deleted the TranscodeOriginalPath metadata field from within the Premiere project. When createCustomMetadata() runs, it prints "successfully added: true" and also returns true in my second code snippet.

However, in Test 3 I'm using another project (I've tried multiple projects, including one with a similar bin structure to the project from the first two tests as well as new projects with nothing but one sequence and one video clip).When createCustomMetadata() runs, it prints "successfully added: true" but then returns false in my second code snippet and prints "Could not initialize renaming function." However, the TranscodeOriginalPath field is created (which I confirm by looking at the project's Metadata Display).

In Test 4, I'm using the same project as Test 3 without making any changes. When createCustomMetadata() runs, it prints "successfully added: undefined" (which I assume is because the field already exists), returns false in my second code snippet and prints "Could not initialize renaming function."

What gives? Why is it that I can confirm the custom metadata field is successfully created in every test, but my calls to doesPropertyExist() return different values depending on the project? I tried writing the new XMP to a file and reading through it, and indeed the TranscodeOriginalPath field only shows up in the first two tests. Why is that the case when my call to addPropertyToMetadataSchema() works as expected every time (or even when I manually add TranscodeOriginalPath to my project through the Metadata Display in Premiere)? It seems like something is preventing my panel from properly reading the XMP data, but only under certain circumstances that I don't understand.

I'm also wondering if it's just not worth using doesPropertyExist() to be safe and check for the new custom field, even if that's the way the PPro sample panel does it. Is it enough to check 

 

if (propertyExists || successfullyAdded)
   customMetadataSuccess = true;

 

instead of 

 

if (propertyExists)
   customMetadataSuccess = true;

 

near the end of my first code snippet, and assume that calls to addPropertyToMetadataSchema() will never fail? That seems to be a more reliable indicator that the field was created than doesPropertyExist(), at least, unless there's something I'm missing.

Any help is really appreciated, thanks!

TOPICS
SDK

Views

145

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Explorer , Apr 26, 2021 Apr 26, 2021
Thanks! I think I see what's going on now, because the snippet you shared did work for me... but there's still some behavior I think is worth mentioning.What my code didn't do was set a value for the custom field I was adding. This line in your code makes all the difference: xmp.setProperty(kPProPrivateProjectMetadataURI, newField, "PProPanel set this, using addPropertyToProjectMetadataSchema().");I don't want my newly created to field to have that text value, so I replaced the line above with: ...

Likes

translate

Translate

Translate
Adobe Employee ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied


@adriant83512276 wrote:

Hi! I'm having trouble sorting out a function in my Premiere CEP panel that used to seem to work, but is no longer behaving the way I expect it to.

 

What in your configuration (or code) has changed, since it used to work? Also...you're trusting (like the PProPanel sample) that the first item at the root of the project, is what you want to act on...

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

My code has remained the same. The main change in my configuration, to my knowledge, has been updating to Premiere version 15.x (whereas this function was developed when I was using 14.x). I ran tests in both Premiere 15.0 and 15.1 and wasn't able to have it work, thinking maybe it was an issue with 15.x versions... but then I tried doing the same thing in Premiere 14.9 and it didn't work there either. So I'm unclear on what's different, other than that the TranscodeOriginalPath field was created in an older version of Premiere (it's been a few months since I used the feature that relies on this function). 

And, yes: the first child of the root of the project is really just what I'm testing on to check the XMP metadata schema. In one of the projects where this function is successful, that's a bin named "Audio." I recreated the same bin structure in another project, where it didn't work, with a bin named "Audio" also being that first item at the root... it didn't seem to make a difference what that projectItem was. Should that matter? Is there a better way to check a custom metadata field exists than to use that first projectItem?

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

I stripped down the PProPanel example code, and cannot get it to fail in current PPro builds. 

Does [below] work for you? [Remember to delete the added field, using the "Metadata Display..." dialog available from the Metadata panel's "hamburger" menu, between each test run...]

var kPProPrivateProjectMetadataURI	= "http://ns.adobe.com/premierePrivateProjectMetaData/1.0/";
var newField						= "ExampleFieldName";
var desc							= "Column.PropertyText.Description";

if (app.isDocumentOpen()) {
	var projectItem = app.project.rootItem.children[0]; // just grabs first projectItem.
	if (projectItem) {
		if (ExternalObject.AdobeXMPScript === undefined) {
			ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
		}
		if (ExternalObject.AdobeXMPScript !== undefined) { // safety-conscious!
			var projectMetadata 			= projectItem.getProjectMetadata();
			var successfullyAdded 			= app.project.addPropertyToProjectMetadataSchema(newField, "ExampleFieldLabel", 2);
			var xmp							= new XMPMeta(projectMetadata);
			xmp.setProperty(kPProPrivateProjectMetadataURI, newField, "PProPanel set this, using addPropertyToProjectMetadataSchema().");

			var array = [];
			array[0] = newField;
			
			var str = xmp.serialize();
			projectItem.setProjectMetadata(str, array);

			// test: is it in there?

			var newblob = projectItem.getProjectMetadata();
			var newXMP = new XMPMeta(newblob);
			var foundYet = newXMP.doesPropertyExist(kPProPrivateProjectMetadataURI, newField);

			if (foundYet) {
				alert("PProPanel successfully added a field to the project metadata schema, and set a value for it.");
			}
		}
	}
}

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

Thanks! I think I see what's going on now, because the snippet you shared did work for me... but there's still some behavior I think is worth mentioning.

What my code didn't do was set a value for the custom field I was adding. This line in your code makes all the difference: 

xmp.setProperty(kPProPrivateProjectMetadataURI, newField, "PProPanel set this, using addPropertyToProjectMetadataSchema().");

I don't want my newly created to field to have that text value, so I replaced the line above with: 

xmp.setProperty(kPProPrivateProjectMetadataURI, newField, "");

As soon as I did that, it stopped working. So then I tried:

xmp.setProperty(kPProPrivateProjectMetadataURI, label, "?");

and it started working again.

 

So doesPropertyExist() doesn't actually seem to check whether the field exists in the project schema, but rather whether a value has been set for that field. This also explains why it was working in some of my old projects but not new ones: I noticed that if I set a value for a custom metadata field, then remove it, and then add that custom metadata field with the same name again, it'll still remember its old value. So when I was testing new projects, I was frustrated that my panel was only creating the metadata field in some hidden way. In reality, I'd just never assigned it a value. In my older projects, I'd already given that property a value at some point before I deleted it for testing, so doesPropertyExist() worked as I expected there.


Anyway, if that all sounds right to you, I'll work with it. I wish I didn't have to set a value upon creation just to check whether a field exists, but I can work around that for now by using a filler value like "?" that I'll explicitly check for.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

Glad you got it sorted! 

In a clearer world, the function might be better named doesValueForPropertyExist()...

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2021 Apr 27, 2021

Copy link to clipboard

Copied

Thanks for your help!

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines