20tab / UnrealEnginePython

Embed Python in Unreal Engine 4
MIT License
2.75k stars 746 forks source link

How to use python class or method from Blueprint or C++? #772

Open Mukudori opened 5 years ago

Mukudori commented 5 years ago

I plugged this plugin and everything works fine, however, only script execution without input and output parameters is available. изображение

How do I use the python method with input and output variables from blueprint or c++ classes?

dfb commented 5 years ago

You need to execute/load a python module that declares the Python class. Once you've done that, it will show up in the list of available classes (e.g. via the SpawnActorFromClass node).

You may need to make sure your Python classes get loaded pretty early in startup, otherwise when you restart UE4 the BP's that reference them may load prior to the Python classes being loaded again. See the ue_site.py module as one way to load your Python classes early (i.e. import them in your ue_site.py).

It can also be useful to use C++ to create a little helper utility that takes a class path and returns a class object. You can then use that in your BPs and have the class output pin connected to the SpawnActorFromClass input pin, and that will let you get by with importing the classes later if needed.

tempaccountforissue commented 1 year ago

@dfb Hey, I know it's been like 3 years since you made this comment, but I am very confused and could use some help. I tried entering the name of my python script into the box next to 'script' and I get an error saying "Unable to find file PythonScript5." What am I doing wrong here?

dfb commented 1 year ago

@tempaccountforissue I'm happy to try to help, but yeah, it's been long time since I've looked at this stuff, so I don't know if I'll be of much help. :)

If I'm reading the code properly, that BP node results in an eventual call to FUnrealEnginePythonModule::RunFile. I think you could pass in the full path (like c:\Users\dave\stuff\whatever.py) there and it'll work. Otherwise, if you just pass a filename, then it will look for that file in each of the directories stored in ScriptsPaths.

If you create a Scripts directory in your project's Content folder, then I think it'll get included in ScriptsPaths automatically, and then IIRC there are other ways to add other directories to that list as well - like via one of the settings INI files.

Hopefully this at least gives you an idea of where to poke around - sorry I don't remember more, and good luck!

tempaccountforissue commented 1 year ago

@dfb Hey, thanks for the quick reply! I actually found another way to get it to work, but I'm still running into one other issue. All I need UnrealEnginePython to do is to get a text input from a UEditableTextBox inside of a PyUserWidget and send it to my python script so I can use it as a string. I was able to access the UEditableTextBox by referencing it as a property of my PyUserWidget class: import unreal_engine as ue name_of_string = ue.all_classes()[5494].properties()[1] However, all I'm getting is the name of the variable. I don't know how to get the value from it.