如何从 python 中的虚幻向量中提取单独的 x、y、z 坐标?

问题描述 投票:0回答:2

我在虚幻引擎 5.1 中使用 python 脚本。我正在尝试从 python 中的虚幻向量中分别提取 x、y、z 对。

我尝试了所有语法,如:

vector = unreal.vector() #defined as a vector
vector[0]
vector(0)
vector{0}

但这些都不起作用。有人可以告诉我怎么做吗?

这是我正在使用的 python 脚本。

import unreal
import torch

Assets = unreal.Actor(name='cylinder_bp').get_actor_location()

print(Assets)
#print(torch.tensor(Assets))

错误:

LogPython: Error: Traceback (most recent call last):
LogPython: Error:   File "C:/Devanshu/RL/actors.py", line 6, in <module>
LogPython: Error:     for a in Assets:
LogPython: Error: TypeError: 'Vector' object is not iterable
LogPython: C:\Devanshu\RL\actors.py
LogPython: <Struct 'Vector' (0x000001C98DB7F9C0) {x: 0.000000, y: 0.000000, z: 0.000000}>
LogPython: C:\Devanshu\RL\actors.py
LogPython: Error: Traceback (most recent call last):
LogPython: Error:   File "C:/Devanshu/RL/actors.py", line 6, in <module>
LogPython: Error:     print(Assets(0))
LogPython: Error: TypeError: 'Vector' object is not callable
LogPython: C:\Devanshu\RL\actors.py
LogPython: Error: Traceback (most recent call last):
LogPython: Error:   File "C:/Devanshu/RL/actors.py", line 6, in <module>
LogPython: Error:     print(Assets[0])
LogPython: Error: TypeError: 'Vector' object is not subscriptable

此外,有人可以告诉我如何获取特定演员的位置吗?在我的例子中,有两个演员,

cart
cylinder_bp
.

我试过这个语法

"Assets = unreal.Actor(name='cylinder_bp').get_actor_location()"
但这不起作用。

python unreal-engine4 unreal-engine5
2个回答
0
投票

我从来没有使用过这个库所以不得不自己查找解决方案。我用谷歌搜索“python unreal vector”找到文档。阅读文档通常是找到解决方案的更好方法,而不仅仅是猜测。

您可以看到有一个名为“编辑器属性”的部分,其中列出了

x
y
z
。这意味着您可以执行
vector.x
vector.y
vector.z
来获得这些值。


0
投票

您拥有的代码不会像我在this answer中提到的那样正确地打印演员的数据。

你想要完成的事情可以这样完成:

# Enable these plugins in UE4Editor:
#   - Python Editor Script Plugin
#   - Editor Scripting Utilities
#
import unreal

actorsList = unreal.EditorLevelLibrary.get_all_level_actors()
for actor in actorsList:
    actorLabel = actor.get_actor_label()
    actorPos = actor.get_actor_location()
    if (actorLabel == 'cylinder_bp'):
        print('actorPos.x= %f actorPos.y= %f actorPos.z= %f' % (actorPos.x, actorPos.y, actorPos.z))
© www.soinside.com 2019 - 2024. All rights reserved.