Skip to main content
Richard Rosenman
Inspiring
October 13, 2023
Question

File Prompt with AEGP_ExecuteScript

  • October 13, 2023
  • 1 reply
  • 452 views

After some advice from Shachar, I've set out to use AEGP_ExecuteScript to trigger a file prompt, for loading a file from within my plugin. After some research from the forum, this is what I've got that should work:

 

 

AEGP_SuiteHandler suites(in_data->pica_basicP);
A_Boolean outAvailablePB;
AEGP_MemHandle outResultPH;
AEGP_MemHandle outErrorStringPH;
			
const char* javascriptCode =
	"var fileToOpen = File.openDialog('Select a file to open');\r\n"
	"if (fileToOpen) {\r\n"
	"    app.open(fileToOpen);\r\n"
	"}";
				
ERR(suites.UtilitySuite4()->AEGP_IsScriptingAvailable(&outAvailablePB));
ERR(suites.UtilitySuite4()->AEGP_ExecuteScript(NULL, javascriptCode, true, &outResultPH, &outErrorStringPH));

A_char* result = NULL;
ERR(suites.MemorySuite1()->AEGP_LockMemHandle(outResultPH, reinterpret_cast<void**>(&result)));

ERR(suites.MemorySuite1()->AEGP_FreeMemHandle(outResultPH));
ERR(suites.MemorySuite1()->AEGP_FreeMemHandle(outErrorStringPH));

FILE* file = fopen(result, "r");
if (file) {
... Do Stuff Here...

 

From my understanding, outResultPH contains the selected filename and it needs to be passed to a char (result) using AEGP_LockMemHandle which I am doing above.

 

However, it crashes. When I debug, I can see that it crashes on the AEGP_ExecuteScript line.

 

I've got this in my SmartRender function for now - whether that is the right or wrong place for it isn't the main issue right now. I just want to get it working.

 

What am I doing wrong?

 

Thanks,

-Rich

This topic has been closed for replies.

1 reply

Community Expert
October 13, 2023

at a glace, your code seems correct, except for not relying on the result of outAvailablePB before executing the script.

can you try moving the code to somehwere other than the render? the render thread might be foridden from running scripts or opening modals...

Richard Rosenman
Inspiring
October 14, 2023

HI @shachar carmi 

 

Thanks, as always, for your helpful advice.

 

Well, I moved it over to the UserChangedParam section and you are right - it doesn't crash on that line anymore.

 

It displays the file prompt when I run it. However, upon selecting a file to import (OBJ), I get the After Effects warning: "Save changes to "Untitled Project.aep" before closing?".

 

And upon clicking "Don't Save", I get "Unable to execute script at line 3. After Effects error. Can't import file "3dmodel.obj": unsupported file type or extension."

 

Then I get a brand new AE workspace, much like selecting "New Project". And then it crashes.

 

So this leads me to believe this is trying to import the file directly into After Effects?? Like loading an AE .aep project. If so, this is not at all what I'm trying to achieve. I want to open a file from my plugin and read data into my plugin. Not import a file in AE. Is this the wrong approach??

 

Regards,

-Rich 

Community Expert
October 14, 2023

obj files are not yet natively supported in AE (it's in the beta version, if i'm not mistaking), so perhaps all that turmoil is due to mishandling of the file type by ae? can you try importing a jpg instead?

also, in some SDK samples the "user changed param" function is also triggered by the UPDATE_PARAMS_UI call, which is the wrong time to import stuff. can you check if that's the case?