Hello,
I have to change some properties/values of material based on specific frames from an mp4 file when the movie is playing. I am trying this code, but it always gives me the Frame number as -1. Is there a way to trigger some code when a movie (mp4) is playing particular frame numbers or at particular movie durations? Moreover, the movie just stops playing when the 'time.sleep()' code is active or in 'while time.time() < end_time:' while waiting, even when the mp.setActive(true) is not in the loop. I tried with image sequence also, but there also, I can't run the animation in a for loop using playCAnimation("X", i+1, i+10).
Script Editor
material = findMaterial("X")
mp = vrMoviePlayer2(material, "video_path")
Variant Set
mp.setActive(true)
time.sleep(1)
print(mp.getFrameNumber())
time.sleep(1)
print(mp.getFrameNumber())
time.sleep(1)
print(mp.getFrameNumber())
Hi,
can you describe what you want to do exactly?
Some advice first. Whenever it is possible dont use time.sleep, it blocks python execution.
While loops are, at least in my opinion, also not good practice.
Why you want to run animation in a for loop?
Now to the VRED issue. As you already found out, getFrameNumber() is not working. Dont know why.
If you dont need to do exactly something when a certain frame number is reached, you can work with the movie time in seconds.
In my example, we use getPosition(). This will return the current time of the movie.
We use the variable "marker" to be able to set different time markers, where something should happen.
Then we use the vrTimer to be able to not check every frame (for better performance). I put everything in a class, so you can do the same with more then one video at a time.
So instead of using a whilte or for loop, we connect the function "time_output" (to check the movie time) to the vrTimer.
The code below you can put in the ScriptEditor or a variantset for initialisation.
Then you can have a variantset with "thread.start()" and another with "thread.cancel()".
from vrTimer import vrTimer
class InfiniteTimer():
def __init__(self, seconds, target, video):
self._should_continue = False
self.is_running = False
self.seconds = seconds
self.target = target
self.thread = None
self.thread = vrTimer(self.seconds)
self.thread.connect(self._handle_target)
self.video = video
def _handle_target(self):
self.is_running = True
self.target()
self.is_running = False
self._start_timer()
def _start_timer(self):
if self._should_continue: # Code could have been running when cancel was called.
##### with vrTimer #####
#
self.thread.setActive(True)
############################################
def start(self):
if not self._should_continue and not self.is_running:
self._should_continue = True
self._start_timer()
self.video.setPlay(1)
self.video.setActive(true)
else:
print("Timer already started or running, please wait if you're restarting.")
def cancel(self):
if self.thread is not None:
self._should_continue = False # Just in case thread is running and cancel fails.
##### with vrTimer #####
print("Timer was active: " + str(self.thread.isActive()))
self.thread.setActive(False)
self.video.setPlay(0)
else:
print("Timer never started or failed to initialize.")
global video
global marker
marker = 0
def time_output():
global video
global marker
print(str(video.getPosition()) + str(" sec"))
## do something after 4sec
if video.getPosition() > 4 and marker < 1:
print("do something")
marker += 1
## do something after 6sec
if video.getPosition() > 6 and marker < 2:
print("do something else")
marker += 2
myscreen = findNode("Plane")
myvideo = "yourpath/test.wmv"
video = vrMoviePlayer2(myscreen.getMaterial(), myvideo)
video.setLoop(1)
timerinterval = 0.5 ## approx. 1sec , do a sharper value if needed f.e. 0.1, 0.05 etc.
thread = InfiniteTimer(timerinterval, time_output, video)
Thank you, Andreas!
It is for a unique project requirement. Let me give this code a try. Thanks a lot for your quick support. Highly appreciate that!! ![]()
Can't find what you're looking for? Ask the community or share your knowledge.