ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

用python创建editor蓝图widget

2021-07-30 09:32:47  阅读:407  来源: 互联网

标签:Blueprint widget code python Blueprints Python editor UE4 class


UE4 has supported Python scripting within editor builds for a number of versions now. In versions 4.23 and previous, you had the ability to derive your Python classes from native engine classes and override functions that were previously defined in that C++ class. This allowed you to write custom scripts in Python and have Blueprint nodes interact with them. However, one thing that was missing was the ability to expose Python only defined functions as callable Blueprint nodes. As of UE4 4.24, you will be able to expose Python class methods to the Blueprint VM without having to touch C++ first. This is a great tool for those that don’t know C++ but want to be able to make automation Python scripts that act as Blueprint Function Libraries.

What is a Blueprint Function Library? It is a collection of functions that Blueprints can use. Often, these are sets of functions that bridge between Blueprints and a native layer that does not have a UObject based API (AR/VR). The libraries are never instanced, so all methods are expected to be static and not contain any properties. Because they are more of a concept than a class hierarchy, there are no base methods to overload. This is why being able to define new class methods in Python that are exposed to our UObject API is needed. Prior version support was very close to working, but some small issues needed to be addressed.

The Blueprint VM operates on UE4’s reflection system in order to be able to call functions or access properties. The Python integration automatically generates the corresponding reflection data for Blueprints to use when the Python code is properly decorated. The small bit of Python code below is a Blueprint function library that illustrates how to decorate a class and methods.

Sample Python code as a Blueprint Function Library

First thing is a decoration telling UE4 that the Python class should be exposed to Blueprints. The Python class must derive from a UObject derived class, such as AActor or UBlueprintFunctionLibrary. Note that we expose the classes to the Python environment via the “unreal” namespace and remove the native classes’ prefix, just like in Blueprints.

The next step is to decorate each function that you want exposed as a Blueprint node. Since there is not an underlying native class to pull from, this step provides all of the information the integration needs to make calls to those methods. If you get something wrong in this step, the error messages will show up in the Output window in the editor. These errors can happen at either loading of the Python code or when the Blueprint code tries to execute the Python method. The latter should only happen if the decoration data exposes the parameters or return type incorrectly.

Let’s take a look at the simplest example, the LaunchMaya method. It doesn’t take any parameters and doesn’t have a return value so the only decorations present are the labeling it as static and the meta data needed to place the Blueprint node in a specific Category. The image below shows this in context of adding a new Blueprint node.

The Blueprint node menu

UE4 needs to know the underlying data types for parameters and return types. It creates the corresponding wrapper UProperty types underneath the covers. If the type returned from the Python code does not match the decorated code, there may be a runtime error if it can’t convert it. It’s always best to make those match correctly. The same is true for UE4 sending parameters to the function. It might be able to convert, but it’s best not to rely upon that. In our sample code, you can see two examples of return values being specified and two examples of passing parameters to a function. When specifying the types, make sure to use the Python names for the type and not the UE4 names. The two images below are from Editor Utility Blueprints showing use of each category of functions.

An Editor Utility Blueprint to launch Maya if it is not running

Another utility Blueprint to show passing parameters and consuming return values

If you are creating automation tasks, using Editor Utility Blueprints is an easy way to orchestrate Python processes using a Blueprint graph. For instance, the top graph could look at the currently selected asset, export it to FBX, and launch Maya so the artist can edit it. If you are not familiar with Editor Utility Blueprints, they are Blueprints that have a Run event and can be run from within the Content Browser or from a UI interaction. The images below show the creation and invocation of the Maya flow shown in the image above.

How to create a Editor Utility Blueprint

How to run an existing utility Blueprint from the Content Browser

One thing to note is that you must have the Python code loaded as part of the editor startup process or you will see errors in your Blueprints as shown below.

Missing the Python information

To recreate the samples you’ve seen above, you’ll need to wait for version 4.24 to ship or you’ll need to pull source from GitHub. I mostly work in the XR branch, so the the extra fixes you need for Blueprints to see your functions has been merge to there (Dev-VR branch).

Some extra resources are listed below:

 

https://medium.com/@joe.j.graf/building-ue4-blueprint-function-libraries-in-python-746ea9dd08b2

标签:Blueprint,widget,code,python,Blueprints,Python,editor,UE4,class
来源: https://www.cnblogs.com/Shaojunping/p/15078085.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有