Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Upgrade to support latest UE 4.13 (from 4.8) #32

Merged
merged 1 commit into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions Scripts/uetorch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ struct UObject;
struct AActor;
struct UMaterial;

AActor *FindActor(const char *fullName);

void GetViewportSize(IntSize* r);
bool CaptureScreenshot(IntSize* size, void* data);
bool CaptureSegmentation(UObject* _this, const IntSize* size, void* seg_data, int stride, const AActor** objects, int nObjects, bool verbose);
bool CaptureMasks(UObject* _this, const IntSize* size, void* seg_data, int stride, const AActor** objects, int nObjects, bool verbose);
bool CaptureOpticalFlow(UObject* _this, const IntSize* size, void* flow_data, void* rgb_data, float maxFlow, int stride, bool verbose);
bool CaptureDepthField(UObject* _this, const IntSize* size, void* data, int stride, bool verbose);

void PressKey(const char *key, int ControllerId, int eventType);
void PressKey(UObject* _this, const char *key, int ControllerId, int eventType);
void SetMouse(int x, int y);
bool SetTickDeltaBounds(UObject* _this, float MinDeltaSeconds, float MaxDeltaSeconds);
bool SetResolution(int x, int y);
Expand Down Expand Up @@ -193,12 +195,12 @@ local IE_RELEASED = 1

-- press and hold the key with this name
function uetorch.PressKey(key)
utlib.PressKey(key, 0, IE_PRESSED)
utlib.PressKey(this, key, 0, IE_PRESSED)
end

-- release the key with this name
function uetorch.ReleaseKey(key)
utlib.PressKey(key, 0, IE_RELEASED)
utlib.PressKey(this, key, 0, IE_RELEASED)
end

local _tapped = {}
Expand Down Expand Up @@ -233,8 +235,8 @@ end
function uetorch.GetActor(name)
local level = UE.GetFullName(UE.GetCurrentLevel(this))
level = string.sub(level, 7, -1) -- remove "Level"
local actor = UE.FindObject(Actor.Class(), nil, level..'.'..name)
if tostring(actor) ~= 'userdata: (nil)' then
local actor = utlib.FindActor(level .. '.' .. name)
if tonumber(actor)) ~= 0 then
return actor
else
return nil
Expand All @@ -257,9 +259,9 @@ function uetorch.Screen(tensor)
end

tensor = tensor or torch.FloatTensor()
assert(torch.type(tensor) == 'torch.FloatTensor')
tensor = tensor:resize(3, size[0].Y, size[0].X):contiguous()

if not utlib.CaptureScreenshot(size, tensor:storage():cdata().data) then
if not utlib.CaptureScreenshot(size, tensor:data()) then
print("ERROR: Unable to capture screenshot")
return nil
end
Expand Down Expand Up @@ -298,7 +300,7 @@ function uetorch.ObjectSegmentation(objects, stride, verbose)

local objectArr = ffi.new(string.format("AActor*[%d]",#objects), objects)

if not utlib.CaptureSegmentation(this, size, seg:storage():cdata().data, stride, objectArr, #objects, verbose) then
if not utlib.CaptureSegmentation(this, size, seg:data(), stride, objectArr, #objects, verbose) then
print("ERROR: Unable to capture segmentation")
return nil
end
Expand Down Expand Up @@ -338,7 +340,7 @@ function uetorch.ObjectMasks(objects, stride, verbose)

local objectArr = ffi.new(string.format("AActor*[%d]",#objects), objects)

if not utlib.CaptureMasks(this, size, masks:storage():cdata().data, stride, objectArr, #objects, verbose) then
if not utlib.CaptureMasks(this, size, masks:data(), stride, objectArr, #objects, verbose) then
print("ERROR: Unable to capture segmentation")
return nil
end
Expand Down Expand Up @@ -381,7 +383,7 @@ function uetorch.OpticalFlow(maxFlow, stride, verbose)
math.ceil(size[0].X/stride),
3)

if not utlib.CaptureOpticalFlow(this, size, flow:storage():cdata().data, rgb:storage():cdata().data, maxFlow, stride, verbose) then
if not utlib.CaptureOpticalFlow(this, size, flow:data(), rgb:data(), maxFlow, stride, verbose) then
print("ERROR: Unable to capture optical flow")
return nil
end
Expand Down Expand Up @@ -414,7 +416,7 @@ function uetorch.DepthField(stride, verbose)
local depth = torch.FloatTensor(math.ceil(size[0].Y/stride),
math.ceil(size[0].X/stride))

if not utlib.CaptureDepthField(this, size, depth:storage():cdata().data, stride, verbose) then
if not utlib.CaptureDepthField(this, size, depth:data(), stride, verbose) then
print("ERROR: Unable to capture depth field")
return nil
end
Expand Down
20 changes: 18 additions & 2 deletions Scripts/uetorch_example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-------------------------------------------------------------------------------

local uetorch = require 'uetorch'
print("loading uetorch_example")

local M = {}

Expand Down Expand Up @@ -43,13 +44,16 @@ function my_repl()
end

local cubes = {}
for i = 1, 11 do
cubes[i] = uetorch.GetActor(string.format('Cube%02d', i))
for i = 1, 14 do
-- this is hardcoded for the objects in FirstPersonExampleMap,the first-person shooter demo map
-- At least in 4.13, these have names like 'EditorCube8_2' for some reason
cubes[i] = uetorch.GetActor(string.format('EditorCube%d_%d', i + 7, i + 1))
end

-------------------------------------------------------------------------------
-- Tick Hook: automatically move towards blocks
-------------------------------------------------------------------------------
local frame = 0
local function ExampleTickHandler(dt)
local seg = uetorch.ObjectSegmentation(cubes, 8)
local centerStrip = seg:select(2, math.floor(seg:size(2)/2))
Expand All @@ -64,6 +68,18 @@ local function ExampleTickHandler(dt)
-- move block 7 into the sky for fun
local loc = uetorch.GetActorLocation(cubes[7])
uetorch.SetActorLocation(cubes[7], loc.x, loc.y, loc.z + 1)

-- Demonstrate taking a screenshot.
-- There are other images you can capture like optical flow;
-- check out uetorch.lua for all of them.
if frame == 100 then
local screen = uetorch.Screen()
local filename = "./uetorch_screenshot.png"
print("Saving screenshot to " .. filename)
require 'image'
image.save(filename, screen)
end
frame = frame + 1
end

uetorch.AddTickHook(ExampleTickHandler)
Expand Down
26 changes: 12 additions & 14 deletions Setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@ set -e

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if git rev-parse 4.8-UETorch > /dev/null 2>&1; then
if [ $(git rev-parse 4.8-UETorch^1) != $(git rev-parse 4.8) ]; then
echo "git branch 4.8-UETorch already exists and has your commits on it. If you really want to re-run setup, you'll need to clean up and delete this branch yourself."
exit 1
fi
git checkout 4.8
git branch -D 4.8-UETorch
fi

echo "=== Checking out the baseline UE4 commit... ==="
git checkout 4.8

echo "=== Patching UE4 ==="
cd $DIR/../../..
git branch 4.8-UETorch
git checkout 4.8-UETorch
git branch UETorch_base
git checkout UETorch_base
git apply $DIR/UnrealEngine.patch
git add -u
git commit -m "UETorch patches"
Expand All @@ -30,12 +18,22 @@ echo "=== Setting up Lua... === "
# tar zxf lua-5.2.4.tar.gz
# cd lua-5.2.4
# make CFLAGS='-fPIC -DLUA_USE_LINUX' linux test

# find Lua
LUA=`which lua`
echo "LUA= $LUA"
if [ $? != 0 ]; then
echo "Couldn't find Lua. Did you forget to install torch and run torch-activate?"
exit 1
fi

# check Lua version
LUA_VERSION=$($LUA -v | cut -c -7)
if [ "$LUA_VERSION" != "Lua 5.2" ]; then
echo "Expected Lua 5.2, but your Lua version is $LUA_VERSION, which is not supported."
exit 1
fi

TORCH_BIN=`dirname $LUA`
if [ ! -f "$TORCH_BIN/../include/lua.h" ]; then
echo "Couldn't find lua.h relative to lua. Did you forget to install torch and run torch-activate?"
Expand Down
18 changes: 9 additions & 9 deletions Source/UETorch/Classes/TorchPluginComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FTorchContext;
* loaded on game start, and can register functions to be executed on each
* iteration ('tick') of the game loop.
*/
UCLASS(Blueprintable, ClassGroup = Script, hidecategories = (Activation, Collision), meta = (BlueprintSpawnableComponent))
UCLASS(ClassGroup = Script, hidecategories = (Object), meta = (BlueprintSpawnableComponent))
class UETORCH_API UTorchPluginComponent : public UActorComponent
{
GENERATED_UCLASS_BODY()
Expand Down Expand Up @@ -49,14 +49,14 @@ class UETORCH_API UTorchPluginComponent : public UActorComponent
UFUNCTION(BlueprintCallable, Category = "Script|Functions")
virtual bool CallTorchFunctionString(FString FunctionName, FString In, FString &Out);

/**
* Calls a script defined function (Array<string> -> string)
* @param FunctionName Name of the function to call
* @param In Array of String arguments to the function
* @param Out String output from the function
*/
UFUNCTION(BlueprintCallable, Category = "Script|Functions")
virtual bool CallTorchFunctionArray(FString FunctionName, TArray<FString> In, FString &Out);
/**
* Calls a script defined function (Array<string> -> string)
* @param FunctionName Name of the function to call
* @param In Array of String arguments to the function
* @param Out String output from the function
*/
UFUNCTION(BlueprintCallable, Category = "Script|Functions")
virtual bool CallTorchFunctionArray(FString FunctionName, TArray<FString> In, FString &Out);


// Begin UActorComponent interface.
Expand Down
15 changes: 10 additions & 5 deletions Source/UETorch/Private/TorchPluginComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ UTorchPluginComponent::UTorchPluginComponent(const FObjectInitializer& ObjectIni
bTickInEditor = false;
bAutoActivate = true;
bWantsInitializeComponent = true;
MainModule = TEXT("<MainModule>");
MainModule = TEXT("");
Context = NULL;
}

FString UTorchPluginComponent::MakeLuaInitString() {
FString InitStr =
"require 'uetorch';" // FIXME: use local uetorch package
"local _main = require '" + MainModule + "';"
"if type(_main)=='table' and _main.initialize then _main.initialize() end";
FString InitStr;
if (MainModule != "") {
InitStr =
"require 'uetorch';"
"local _main = require '" + MainModule + "';"
"if type(_main)=='table' and _main.initialize then _main.initialize() end";
} else {
InitStr = "require 'uetorch';";
}

return InitStr;
}
Expand Down
Loading