Skip to content

Commit

Permalink
Merge branch 'pr/17'
Browse files Browse the repository at this point in the history
  • Loading branch information
getnamo committed May 21, 2020
2 parents ef7be6f + 3db0859 commit 340a498
Show file tree
Hide file tree
Showing 33 changed files with 503 additions and 322 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ Funny snippets for working with StaticMesh and SkeletalMesh assets: https://gith

More tutorials: https://github.com/20tab/UnrealEnginePython/tree/master/tutorials

# Project Status (IMPORTANT)

Currently (as april 2020) the project is on hold: between 2016 and 2018 20tab invested lot of resources in it but unfortunately epic (during 2018) decided to suddenly release its own implementation and the request made for a megagrant in 2019 by the original plugin author was rejected too.

As this plugin (still) has way more features than the Epic one and many contributors, **we are currently looking for new maintainers** helping us to keep it alive, checking PR and issues. If you are interested in working on it a few hours a week, drop us a line at [email protected] to discuss about it.

If you are interested in game logic scripting/modding in Unreal Engine 4 consider giving a look at the LuaMachine project (https://github.com/rdeioris/LuaMachine/).

The plugin should work up to unreal engine version 4.23 and there are forks/pull requests for 4.24. Since 4.25 Epic refactored the UProperty subsystem, so if you want to port the plugin to a version >= 4.25 you should make a lot of search & replace (basically renaming UProperty to FProperty and Cast to CastField should be enough)

# How and Why ?

This is a plugin embedding a whole Python VM (versions 3.x [the default and suggested one] and 2.7) In Unreal Engine 4 (both the editor and runtime).
Expand All @@ -36,7 +46,7 @@ Once the plugin is installed and enabled, you get access to the 'PythonConsole'

All of the exposed engine features are under the 'unreal_engine' virtual module (it is completely coded in c into the plugin, so do not expect to run 'import unreal_engine' from a standard python shell)

The minimal supported Unreal Engine version is 4.12, while the latest is 4.23
The minimal supported Unreal Engine version is 4.12, while the latest is 4.24

We support official python.org releases as well as IntelPython and Anaconda distributions.

Expand Down
3 changes: 1 addition & 2 deletions Source/PythonAutomation/PythonAutomation.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public PythonAutomation(TargetInfo Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
string enableUnityBuild = System.Environment.GetEnvironmentVariable("UEP_ENABLE_UNITY_BUILD");
bFasterWithoutUnity = string.IsNullOrEmpty(enableUnityBuild);

bUseUnity = string.IsNullOrEmpty(enableUnityBuild);
PrivateIncludePaths.AddRange(
new string[] {
"PythonConsole/Private",
Expand Down
2 changes: 1 addition & 1 deletion Source/PythonConsole/PythonConsole.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public PythonConsole(TargetInfo Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
string enableUnityBuild = System.Environment.GetEnvironmentVariable("UEP_ENABLE_UNITY_BUILD");
bFasterWithoutUnity = string.IsNullOrEmpty(enableUnityBuild);
bUseUnity = string.IsNullOrEmpty(enableUnityBuild);

PrivateIncludePaths.AddRange(
new string[] {
Expand Down
1 change: 1 addition & 0 deletions Source/PythonEditor/Private/PythonEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "PythonEditorStyle.h"
#include "PythonProjectEditor.h"
#include "PythonProject.h"
#include "Subsystems/AssetEditorSubsystem.h"
#include "Runtime/Slate/Public/Framework/MultiBox/MultiBoxBuilder.h"

static const FName PythonEditorTabName( TEXT( "PythonEditor" ) );
Expand Down
11 changes: 10 additions & 1 deletion Source/PythonEditor/Private/PythonProjectEditor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.

#include "PythonProjectEditor.h"

#include "Subsystems/AssetEditorSubsystem.h"

// #include "UnrealEd.h"

#include "SPythonEditor.h"
#include "SPythonProjectEditor.h"
#include "Runtime/Slate/Public/Widgets/Docking/SDockTab.h"
Expand All @@ -13,6 +18,8 @@
#include "PythonProjectEditorCommands.h"
#include "Runtime/Core/Public/HAL/PlatformFilemanager.h"
#include "Runtime/Core/Public/Misc/MessageDialog.h"


#define LOCTEXT_NAMESPACE "PythonEditor"

TWeakPtr<FPythonProjectEditor> FPythonProjectEditor::PythonEditor;
Expand Down Expand Up @@ -209,7 +216,9 @@ void FPythonProjectEditor::RegisterToolbarTab(const TSharedRef<class FTabManager

void FPythonProjectEditor::InitPythonEditor(const EToolkitMode::Type Mode, const TSharedPtr< class IToolkitHost >& InitToolkitHost, class UPythonProject* PythonProject)
{
FAssetEditorManager::Get().CloseOtherEditors(PythonProject, this);
// UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();
// GEditor->GetEditorSubsystem<UAssetEditorSubsystem>()->CloseOtherEditors(PythonProject, this);
GEditor->GetEditorSubsystem<UAssetEditorSubsystem>()->CloseOtherEditors(PythonProject, this);
PythonProjectBeingEdited = PythonProject;

TSharedPtr<FPythonProjectEditor> ThisPtr(SharedThis(this));
Expand Down
4 changes: 3 additions & 1 deletion Source/PythonEditor/PythonEditor.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public PythonEditor(TargetInfo Target)

PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
string enableUnityBuild = System.Environment.GetEnvironmentVariable("UEP_ENABLE_UNITY_BUILD");
bFasterWithoutUnity = string.IsNullOrEmpty(enableUnityBuild);
bUseUnity = string.IsNullOrEmpty(enableUnityBuild);

PrivateIncludePaths.AddRange(
new string[] {
Expand All @@ -32,11 +32,13 @@ public PythonEditor(TargetInfo Target)
"UnrealEd",
"EditorStyle",
"PropertyEditor",
"ContentBrowser",
"Kismet", // for FWorkflowCentricApplication
"InputCore",
"DirectoryWatcher",
"LevelEditor",
"Projects",
"Engine",
"UnrealEnginePython"
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

#include "UEPyFMaterialEditorUtilities.h"

#if WITH_EDITOR

#include "Subsystems/AssetEditorSubsystem.h"

#include "Materials/Material.h"
#include "Runtime/Engine/Classes/EdGraph/EdGraph.h"

Expand Down Expand Up @@ -54,7 +57,9 @@ static PyObject *py_ue_command_apply(PyObject *cls, PyObject * args)
return PyErr_Format(PyExc_Exception, "argument is not a UMaterial");
}

IAssetEditorInstance *Instance = FAssetEditorManager::Get().FindEditorForAsset(Material, false);
//UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();

IAssetEditorInstance *Instance = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>()->FindEditorForAsset(Material, false);
if (!Instance)
{
return PyErr_Format(PyExc_Exception, "unable to retrieve editor for UMaterial");
Expand Down
7 changes: 4 additions & 3 deletions Source/UnrealEnginePython/Private/PythonDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "PythonDelegate.h"
#include "UEPyModule.h"
#include "UEPyCallable.h"
#include "UObject/UEPyUPropertyBackwardsCompatibility.h"

UPythonDelegate::UPythonDelegate()
{
Expand Down Expand Up @@ -37,10 +38,10 @@ void UPythonDelegate::ProcessEvent(UFunction *function, void *Parms)
py_args = PyTuple_New(signature->NumParms);
Py_ssize_t argn = 0;

TFieldIterator<UProperty> PArgs(signature);
TFieldIterator<FProperty> PArgs(signature);
for (; PArgs && argn < signature->NumParms && ((PArgs->PropertyFlags & (CPF_Parm | CPF_ReturnParm)) == CPF_Parm); ++PArgs)
{
UProperty *prop = *PArgs;
FProperty *prop = *PArgs;
PyObject *arg = ue_py_convert_property(prop, (uint8 *)Parms, 0);
if (!arg)
{
Expand All @@ -63,7 +64,7 @@ void UPythonDelegate::ProcessEvent(UFunction *function, void *Parms)
// currently useless as events do not return a value
/*
if (signature_set) {
UProperty *return_property = signature->GetReturnProperty();
FProperty *return_property = signature->GetReturnProperty();
if (return_property && signature->ReturnValueOffset != MAX_uint16) {
if (!ue_py_convert_pyobject(ret, return_property, (uint8 *)Parms)) {
UE_LOG(LogPython, Error, TEXT("Invalid return value type for delegate"));
Expand Down
9 changes: 5 additions & 4 deletions Source/UnrealEnginePython/Private/PythonFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "PythonFunction.h"
#include "UEPyModule.h"
#include "UObject/UEPyUPropertyBackwardsCompatibility.h"


void UPythonFunction::SetPyCallable(PyObject *callable)
Expand Down Expand Up @@ -30,7 +31,7 @@ void UPythonFunction::CallPythonCallable(FFrame& Stack, RESULT_DECL)

// count the number of arguments
Py_ssize_t argn = (Context && !is_static) ? 1 : 0;
TFieldIterator<UProperty> IArgs(function);
TFieldIterator<FProperty> IArgs(function);
for (; IArgs && ((IArgs->PropertyFlags & (CPF_Parm | CPF_ReturnParm)) == CPF_Parm); ++IArgs) {
argn++;
}
Expand All @@ -56,7 +57,7 @@ void UPythonFunction::CallPythonCallable(FFrame& Stack, RESULT_DECL)

// is it a blueprint call ?
if (*Stack.Code == EX_EndFunctionParms) {
for (UProperty *prop = (UProperty *)function->Children; prop; prop = (UProperty *)prop->Next) {
for (FProperty *prop = (FProperty *)function->Children; prop; prop = (FProperty *)prop->Next) {
if (prop->PropertyFlags & CPF_ReturnParm)
continue;
if (!on_error) {
Expand All @@ -75,7 +76,7 @@ void UPythonFunction::CallPythonCallable(FFrame& Stack, RESULT_DECL)
//UE_LOG(LogPython, Warning, TEXT("BLUEPRINT CALL"));
frame = (uint8 *)FMemory_Alloca(function->PropertiesSize);
FMemory::Memzero(frame, function->PropertiesSize);
for (UProperty *prop = (UProperty *)function->Children; *Stack.Code != EX_EndFunctionParms; prop = (UProperty *)prop->Next) {
for (FProperty *prop = (FProperty *)function->Children; *Stack.Code != EX_EndFunctionParms; prop = (FProperty *)prop->Next) {
Stack.Step(Stack.Object, prop->ContainerPtrToValuePtr<uint8>(frame));
if (prop->PropertyFlags & CPF_ReturnParm)
continue;
Expand Down Expand Up @@ -107,7 +108,7 @@ void UPythonFunction::CallPythonCallable(FFrame& Stack, RESULT_DECL)
}

// get return value (if required)
UProperty *return_property = function->GetReturnProperty();
FProperty *return_property = function->GetReturnProperty();
if (return_property && function->ReturnValueOffset != MAX_uint16) {
#if defined(UEPY_MEMORY_DEBUG)
UE_LOG(LogPython, Warning, TEXT("FOUND RETURN VALUE"));
Expand Down
11 changes: 8 additions & 3 deletions Source/UnrealEnginePython/Private/Slate/UEPyFMenuBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "UEPyFMenuBuilder.h"

#include "IAssetTools.h"
#include "Wrappers/UEPyESlateEnums.h"

static PyObject* py_ue_fmenu_builder_begin_section(ue_PyFMenuBuilder* self, PyObject* args)
Expand Down Expand Up @@ -115,6 +115,9 @@ static PyObject* py_ue_fmenu_builder_add_menu_separator(ue_PyFMenuBuilder* self,
}

#if WITH_EDITOR

#include "ContentBrowserModule.h"

static PyObject* py_ue_fmenu_builder_add_asset_actions(ue_PyFMenuBuilder* self, PyObject* args)
{
PyObject* py_assets;
Expand All @@ -140,8 +143,10 @@ static PyObject* py_ue_fmenu_builder_add_asset_actions(ue_PyFMenuBuilder* self,
Py_DECREF(py_assets);

FAssetToolsModule& AssetToolsModule = FModuleManager::LoadModuleChecked<FAssetToolsModule>(TEXT("AssetTools"));
bool addedSomething = AssetToolsModule.Get().GetAssetActions(u_objects, self->menu_builder, true);
if (addedSomething)

FContentBrowserModule& ContentBrowserModule = FModuleManager::LoadModuleChecked<FContentBrowserModule>("ContentBrowser");
TArray<FContentBrowserMenuExtender_SelectedAssets>& AssetMenuExtenderDelegates = ContentBrowserModule.GetAllAssetViewContextMenuExtenders();
if (AssetMenuExtenderDelegates.Num() > 0)
{
Py_RETURN_TRUE;
}
Expand Down
6 changes: 6 additions & 0 deletions Source/UnrealEnginePython/Private/Slate/UEPySTextBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ static PyObject *py_ue_stext_block_set_text(ue_PySTextBlock *self, PyObject * ar
return nullptr;
}

#if ENGINE_MINOR_VERSION < 25
py_STextBlock->SetText(FString(UTF8_TO_TCHAR(text)));
#else
FText tempText;
tempText.FromString(FString(UTF8_TO_TCHAR(text)));
py_STextBlock->SetText(tempText);
#endif

Py_RETURN_SLATE_SELF;
}
Expand Down
4 changes: 2 additions & 2 deletions Source/UnrealEnginePython/Private/UEPyAssetUserData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PyObject *py_ue_asset_import_data(ue_PyUObject * self, PyObject * args)
ue_py_check(self);

UStruct *u_struct = (UStruct *)self->ue_object->GetClass();
UClassProperty *u_property = (UClassProperty *)u_struct->FindPropertyByName(TEXT("AssetImportData"));
FClassProperty *u_property = (FClassProperty *)u_struct->FindPropertyByName(TEXT("AssetImportData"));
if (!u_property)
{
return PyErr_Format(PyExc_Exception, "UObject does not have asset import data.");
Expand Down Expand Up @@ -47,7 +47,7 @@ PyObject *py_ue_asset_import_data_set_sources(ue_PyUObject * self, PyObject * ar
TArray<FString> filenames;

UStruct *u_struct = (UStruct *)self->ue_object->GetClass();
UClassProperty *u_property = (UClassProperty *)u_struct->FindPropertyByName(TEXT("AssetImportData"));
FClassProperty *u_property = (FClassProperty *)u_struct->FindPropertyByName(TEXT("AssetImportData"));
if (!u_property)
{
return PyErr_Format(PyExc_Exception, "UObject does not have asset import data.");
Expand Down
21 changes: 15 additions & 6 deletions Source/UnrealEnginePython/Private/UEPyEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "UnrealEd.h"
#include "FbxMeshUtils.h"
#include "Kismet2/BlueprintEditorUtils.h"

#include "Editor/LevelEditor/Public/LevelEditorActions.h"
#include "Editor/UnrealEd/Public/EditorLevelUtils.h"
#include "Runtime/Projects/Public/Interfaces/IPluginManager.h"
Expand Down Expand Up @@ -41,6 +42,7 @@

#include "UEPyIPlugin.h"


PyObject *py_unreal_engine_redraw_all_viewports(PyObject * self, PyObject * args)
{
FEditorSupportDelegates::RedrawAllViewports.Broadcast();
Expand Down Expand Up @@ -1140,7 +1142,9 @@ PyObject *py_unreal_engine_get_selected_assets(PyObject * self, PyObject * args)

PyObject *py_unreal_engine_get_all_edited_assets(PyObject * self, PyObject * args)
{
TArray<UObject *> assets = FAssetEditorManager::Get().GetAllEditedAssets();

//UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();
TArray<UObject *> assets = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>()->GetAllEditedAssets();
PyObject *assets_list = PyList_New(0);

for (UObject *asset : assets)
Expand Down Expand Up @@ -1168,7 +1172,9 @@ PyObject *py_unreal_engine_open_editor_for_asset(PyObject * self, PyObject * arg
if (!u_obj)
return PyErr_Format(PyExc_Exception, "argument is not a UObject");

if (FAssetEditorManager::Get().OpenEditorForAsset(u_obj))
//UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();

if (GEditor->GetEditorSubsystem<UAssetEditorSubsystem>()->OpenEditorForAsset(u_obj))
{
Py_RETURN_TRUE;
}
Expand All @@ -1193,7 +1199,8 @@ PyObject *py_unreal_engine_find_editor_for_asset(PyObject * self, PyObject * arg
if (py_bool && PyObject_IsTrue(py_bool))
bFocus = true;

IAssetEditorInstance *instance = FAssetEditorManager::Get().FindEditorForAsset(u_obj, bFocus);
//UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();
IAssetEditorInstance *instance = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>()->FindEditorForAsset(u_obj, bFocus);
if (!instance)
return PyErr_Format(PyExc_Exception, "no editor found for asset");

Expand All @@ -1212,14 +1219,16 @@ PyObject *py_unreal_engine_close_editor_for_asset(PyObject * self, PyObject * ar
UObject *u_obj = ue_py_check_type<UObject>(py_obj);
if (!u_obj)
return PyErr_Format(PyExc_Exception, "argument is not a UObject");
FAssetEditorManager::Get().CloseAllEditorsForAsset(u_obj);

//UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();
GEditor->GetEditorSubsystem<UAssetEditorSubsystem>()->CloseAllEditorsForAsset(u_obj);

Py_RETURN_NONE;
}

PyObject *py_unreal_engine_close_all_asset_editors(PyObject * self, PyObject * args)
{
FAssetEditorManager::Get().CloseAllAssetEditors();
GEditor->GetEditorSubsystem<UAssetEditorSubsystem>()->CloseAllAssetEditors();

Py_RETURN_NONE;
}
Expand Down Expand Up @@ -1355,7 +1364,7 @@ PyObject *py_unreal_engine_reload_blueprint(PyObject * self, PyObject * args)
UBlueprint *reloaded_bp = nullptr;

Py_BEGIN_ALLOW_THREADS
reloaded_bp = FKismetEditorUtilities::ReloadBlueprint(bp);
reloaded_bp = FKismetEditorUtilities::ReplaceBlueprint(bp, bp);
Py_END_ALLOW_THREADS

Py_RETURN_UOBJECT(reloaded_bp);
Expand Down
Loading

0 comments on commit 340a498

Please sign in to comment.