Download

Moving an actor smoothly using python

Hello, there good people.
I tried my best to make it reader-friendly.
What I have achieved so far.
did

I have been practicing python in the unreal engine for a couple of weeks now.
What I am trying to do is, suppose I have a cube blueprint and I want to move it from point A(0,0,0) to point B(0,300,0), smoothly in the viewport itself.
What I want to achieve is
nedd

I was able to move the blueprint from point A(0,0,0) to point B(0,300,0) directly. Using the below code

*actors = el.get_all_level_actors()*
*for actor in actors:*
*    if actor.get_actor_label() == 'cube_bp':*
*            actor.set_actor_location(ue.Vector(0,300,0), False, False)*

I tried using a loop that increments the y-axis value by 1 and ends at 300. this doesn’t work, it directly moves the actor to the final point which is (0,300,3).

*actors = el.get_all_level_actors()*
*for actor in actors:*
*    if actor.get_actor_label() == 'cube_bp':*
*        for i in range(1, 300, 1):*
*            actor.set_actor_location(ue.Vector(0,i,0), False, False)*

I also tried refreshing the viewport after every iteration, that also didn’t work.

*actors = el.get_all_level_actors()*
*for actor in actors:*
*    if actor.get_actor_label() == 'cube_bp':*
*        for i in range(1, 300, 1):*
*            actor.set_actor_location(ue.Vector(0,i,0), False, False)*
              *vpbl.refresh3d_editor_viewport()*

Is there any way to do what I want to do?
Note: I am new to this forum, please consider my mistakes.
Thank you

It’s my understanding that Python is not for run time use.
Not sure what you are trying to achieve? If you have a working blueprint, just use that. Otherwise, drop the cube into the Sequencer and keyframe it moving left to right.

1 Like

Hah yeah. Why to do this other than “because i can”? :grinning:

I know that I could easily do it using the sequencer but i need it to move that cube smoothly in the viewport to complete my project. i just want to move that cube using python, without using sequencer. if you know how to do it using blueprint please advise, if its possible in blueprint then i can do it in python i believe.
Thank you

I tested out some code for this to see how the RunTime of Unreal is with Python. The issue here is all updates in Unreal happen after execution of the script, and not on each call to unreal.xxxx like you would hope. This is why I think even refreshing the screen wouldn’t do anything as the hope would really be that you get some print outs while the code executes. If you try this you will see that even though I am using Time to break up the movements, everything is done after the script is executed:

> import unreal
> import time
> 
> timeout = 5
> timeout_start = time.time()
> MySelection = unreal.EditorLevelLibrary.get_selected_level_actors()
> for actors in MySelection:
>     unreal.log(actors.get_actor_location())
>     while time.time() < timeout_start + timeout:
>         currentTime = time.time()
>         unreal.log(currentTime)
>         actors.add_actor_world_transform(unreal.Transform(location=[0,100,0]),True,False)
>         time.sleep(1)
1 Like

Maybe browse the posted Blueprints. You might find an example that fits your need.

I appreciate your text. It makes, sense.
Thank you.