这篇文章主要讲对目录的操作,对资源的操作,创建自定义C++类,将资源加入场景中。

对目录的操作

判断目录是否存在

unreal.EditorAssetLibrary.dose_directory_exist(dir_path)

创建目录

unreal.EditorAssetLibrary.make_directory(new_dir)

复制目录

unreal.EditorAssetLibrary.duplicate_directory(source_path, target_path)

删除目录

unreal.EditorAssetLibrary.delete_directory(dir_path)

重命名目录

unreal.EditorAssetLibrary.rename_directory(old_path, new_path)

对资源的操作

判断资源是否存在

unreal.EditorAssetLibrary.dose_asset_exist(asset_path)

复制资源

unreal.EditorAssetLibrary.duplicate_asset(source_asset, target_asset)

删除资源

unreal.EditorAssetLibrary.delete_asset(asset_path)

复制资源(带对话框)

import os

def duplicateAssetDialog(source_path, show_dialog=True):
  asset_path = os.path.dirname(source_path)
  asset_name = os.path.basename(source_path)

  if show_dialog:
    return unreal.AssetToolsHelpers.get_asset_tools().
      duplicate_asset_with_dialog(asset_name=asset_name,
                                  package_path=asset_path,
                                  original_object=unreal.load_asset(source_path))
  else:
    return unreal.duplicate_asset.get_asset_tools().duplicate_asset(
      asset_name=asset_name,
      package_path=asset_path,
      original_object=unreal.load_asset(source_path)
      )

修改资源名

unreal.EditorAssetLibrary.rename_asset(old_asset_name, new_asset_name)

修改资源名(带对话框)

import os

def renameAssetDialog(old_asset_path, new_asset_path, show_dialog=True):
  rename_data0 = unreal.AssetRenameData(
    asset=unreal.load_asset(old_asset_name),
    new_package_path=os.path.dirname(old_asset_path),
    new_name=os.path.basename(old_asset_path))

  rename_data1 = unreal.AssetRenameData(
    asset=unreal.load_asset(old_asset_path),
    new_package_path=os.path.dirname(new_asset_path),
    new_name=os.path.basename(new_asset_path))

  if show_dialog:
    return unreal.AssetToolsHelpers.get_asset_tools().rename_asssets_with_dialog(
    assets_and_names=[rename_data0, rename_data1])
  else:
    return unreal.AssetToolsHelpers.get_asset_tools().rename_assets(
    assets_and_names=[rename_data0, rename_data1])

创建自定义C++类

1.新建一个C++类

菜单栏选择 File - New C++ Class

u001.jpg

2.选择类的类型为Blueprint Function Library

u002.jpg

3.定义类名,创建Class

首次使用的话可能会提示没有安装VScode,按提示安装就好
这里取的类名为Zfunctions

u003.jpg

点击Create Class后会自动打开VScode软件
修改Zfunctions.cpp和Zfunctions.h两个文件

u004.jpg

Zfunctions.cpp修改为:

// Fill out your copyright notice in the Description page of Project Settings.


#include "Zfunctions.h"

void UZfunctions::CalledFromPython(FString InputString)
{
    UE_LOG(LogTemp, Error, TEXT("%s"), *InputString);
}

Zfunctions.h修改为:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Zfunctions.generated.h"

/**
 *
 */
UCLASS()
class PROJECTTEST001_API UZfunctions : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintCallable)
        static void CalledFromPython(FString InputString);

};

修改完成后进行编译操作

u005.jpg

然后回到UE4,Output Log窗口:

import unreal
for x in sorted(dir(unreal)):
  print x

在输出的结果里就可以看到刚刚我们创建的类 Zfunctions

u006.jpg

再看下这个类下面有哪些函数

for x in sorted(dir(unreal.Zfunctions)):
  print x

可以看到输出的结果里,有我们声明的函数 CalledFromPython
不过它是将驼峰命名更改成了下划线命名:

u007.jpg

最后试试调用这个函数

unreal.Zfunctions.called_from_python("Hello world")

将资源加入场景中

这里有一个实例,随机将多个石块摆放到场景中,并携带进度条。

import random

def deferredSpawnActor():
  # 这里声明的是调用一个蓝图的类
  actor_class = unreal.EditorAssetLibrary.load_blueprint_class('/Game/BP_Actor')
  actor_location = unreal.Vector(random.uniform(0, 2000), random.uniform(0, 2000), 0)
  actor_rotation = unreal.Rotator(random.uniform(0, 360), random.uniform(0, 360), 0)

  # 导入到场景的操作
  actor = unreal.EditorLevelLibray.spawn_actor_from_class(actor_class, actor_location, actor_rotation)
  return actor

def executeSlowTask():
  quantity_steps_in_slow_task = 100
  with unreal.ScopedSlowTask(quantity_steps_in_slow_task, ‘My Slow Task Text...’) as slow_task:

    # 创建的进度窗口
    slow_task.make_dialog(True)
    for x in range(quantity_steps_in_slow_task):
      if slow_task.should_cancel():
        break

      # 改变进度窗口的进度值
      slow_task.enter_progress_frame(1,
        'My Slow Task Text... ' + str(x)+'/'+str(quantity_steps_in_slow_task))
      deferredSpawnActor()

(完)


From zero to hero