20tab / UnrealEnginePython

Embed Python in Unreal Engine 4
MIT License
2.7k stars 737 forks source link

How to run a simple python function, with inputs & outputs, from blueprints? #889

Open TylerDuncan opened 1 year ago

TylerDuncan commented 1 year ago

I searched the forum for this issue specifically but didn't see any answers that seemed to fit.

If I wanted to write a python function that took an integer as input, squared it, then spit out the new integer as an output, how do I call this from a blueprint node? The current execute python method is just a string input for path with no other inputs or outputs.

Cheers, Tyler

TylerDuncan commented 1 year ago

Ah! So you’re passing in the arguments by calling the method with the arguments in the same string? That makes sense now that I think about it.

I did think about using text files or CSV for the output data, but it feels like there has to be a better way to pass data back.

I suppose I could place the code on an actor, and run the execute python string for the input. Then create a blueprint function that takes inputs, and call that function from the python script to pass the data back? I may be creating issues with this process but it’s worth a shot.

TylerDuncan commented 1 year ago

I took another stab at it tonight and got farther! I've successfully been able to call a function in the python script, pass an argument to the function (number as a string), run the function that multiplies the number by itself, then print to console, and print screen, the output of multiplication.

My next tests will be:

To answer your question: I believe I have seen a way to set the value of a variable from blueprint code. It's either in the examples or the docs on github. Depending on if you're creating the actor from code then referencing it, or getting the actor in python via some find by class method. I haven't successfully done this yet, because I'm still not able to get a reference to the actor properly in python. I just tried for half an hour but didn't get anywhere. I see there are "set python attr int" and other nodes for python character or component. I'm trying from a PyActor but those nodes don't exist there.

TylerDuncan commented 1 year ago

@tempaccountforissue Agreed. If I can figure out how to pass a proper reference, I think that would solve a lot of overall issues.

I believe this is the document I'm referring to. It requires you to use it as a uobject which I'm hoping isn't editor only. Do you have any idea?

https://github.com/20tab/UnrealEnginePython/blob/master/docs/uobject_API.md

TylerDuncan commented 1 year ago

@tempaccountforissue I think you can break the struct up and pass it back and forth as a string if absolutely necessary, it would just be more tedious to have to convert it back and forth. I also don't know if that doc is old and maybe they are supported now?

TylerDuncan commented 1 year ago

@tempaccountforissue Unfortunately, I never figured out how to get this to properly work. I think I lack the understanding of c++ to do so. Instead I’m attempting to pay someone on fiverr to just create me a c++ library that will hopefully do what I wanted to do in Python, natively in c++. If you ever figure it out, let me know!