Beginners tutorial: Using video textures in the Blender Game Engine

In a previous post I complained about the fact that the Blender Game Engine didn’t support video textures. Since the release of Blender 2.49 I have nothing left to complain about! One of the many new features in this version is the VideoTexture module. Great news!
So how does it work? The how-to on the Blender site is good enough to get you started if you have some experience with Blender and the Game Engine. If you’re only slightly familiar with it then it becomes a bit more difficult. In this mini-tutorial I will try to make it easy for beginners. Give me a shout if you feel I’ve missed some essential points.

(Click here for a Polish version of this post)

Here’s what we’re going to do to produce the desired result:
- Create a plane
- Load an image texture & UV unwrap
- Write the Python script that loads and plays the video
- Write the Python script that refreshes the video
- Create logic in the Game Engine that will trigger the scripts
- Start the Game Engine and see the result

Create a plane

  • Open Blender
  • Save the .blend file somewhere on your harddrive
  • Download this video and save it in the same directory as the .blend file
  • Download this image and save it in the same directory as the .blend file
  • If the default cube is in the scene then delete it by pressing ‘x’
  • Press the spacebar and select “Add > Mesh > Plane” from the menu
  • Press ‘n’ to show the “Transform Properties” window
  • In this window, find the DimX and DimY properties and change their values to 9.000 and 5.000 respectively

Load an image texture & UV unwrap

Now we need to UV unwrap the plane. Read this article on Wikipedia if you’re unfamiliar with UV mapping and this post if you want to know more about UV coordinates.

  • Split the screen by right-clicking on the line that separates the 3D view and the buttons window:

    Splitting the screen

  • In the newly created window, choose “UV/Image Editor” from the “Window Type” menu:

    The Window Type menu

  • From the UV/Image Editor menu select “Image > Open” and browse for the “bbb.png” file on your harddrive.
  • Go back to the 3D view and press ‘tab’ to switch to “Edit Mode”. Press ‘u’ to bring up the “UV Calculation” menu. From this menu choose “Project from View (Bounds)”. Your UV/Image Editor window should now look like this:

    UV/Image editor window

  • Press ‘F5′ to go to the Shading view in the Buttons window and press the “Add new” button in the “Links and Pipeline” tab:

    Add new material

  • This creates a new material for our plane. Now we need to link the “bbb.png” image to this material. Press ‘F6′ to go the Texture buttons view in the Buttons window and press the “Add new” button:

    Add a new texture

  • Choose “Image” from the “Texture Type” dropdown
  • Go to the “Image” tab an press the small button to the left of the “Load” button. Pick “bbb.png” from the list:

    Choose a texture

  • Go back to the Shading Panel by clicking ‘F5′. On the right hand side you can find a panel with three tabs: “Texture”, “Map Input” & “Map To”. Select the “Map Input” tab and click the “UV” button. This will enable UV mapping on our plane object.

    Enabling UV mapping

  • Press ‘F12′ to render the scene. The output should look like this:

    Choose a texture

  • Before continuing, make sure that you’re in “Object mode” (press ‘tab’) and the draw type is “Textured”:

    Draw type textured

Write the Python script that loads and plays the video

  • Go to the “Window type” menu in the “UV/Image Editor” window and choose “Text Editor”
  • Click on the Python button and the “AB” button to enable Python text plugins and syntax highlighting
  • Go to the menu and choose “Text > New”. This creates a new text file. Name the text file “LoadVideo”:

    Giving the text file a name

  • Paste the code below in the text editor. The code is explained in the comments.
    
    import VideoTexture
    
    # -- Gets the Python controller associated with this Python script.
    contr = GameLogic.getCurrentController()
    
    # -- Gets the game object associated with this logic brick.
    obj = contr.owner
    
    # -- Check if the "video" property has been defined on "GameLogic"
    if not hasattr(GameLogic, 'video'):
    
    	# -- Get the material that is using our texture
    	matID = VideoTexture.materialID(obj, 'IMbbb.png')
    
    	# -- Create the video texture
    	GameLogic.video = VideoTexture.Texture(obj, matID)
    
    	# -- Get the path to the video file
    	movie = GameLogic.expandPath('//trailer_400p.ogg')
    
    	# -- Load the file
    	GameLogic.video.source = VideoTexture.VideoFFmpeg(movie)
    
    	# -- scale the video
    	GameLogic.video.source.scale = True
    
    	# -- play the video
    	GameLogic.video.source.play()
    		


Write the Python script that refreshes the video

Create logic in the Game Engine that will trigger the scripts

  • Press ‘F4′ to open the Logic panel in the Buttons window
  • Click on the “Add” button next to the “Plane” button to add a new sensor. Change the sensor type to “Keyboard” and press the button next to “Key”:

    Add a new sensor

  • After clicking on the button next to “Key” the caption changes to “Press a key”. This is exactly what we’re going to do. Press the “Enter” key.
  • When the user presses the Enter key during the game the “LoadVideo” script should be triggered. This can be done by creating a controller and connecting it to the sensor. Under “Controllers”, click the “Add” button next to the “Plane” button. This creates a controller. The default type is “AND”. Change this to “Python”. In the input box next to the drop down we can indicate which script to execute. Type “LoadVideo” here. The controller should look like this now:

    Add new material

  • Now we’re going to create a sensor that is triggered every frame. Press the “Add” button next to the “Plane” button again. Change the sensor type to “Always”.
  • Add another Python controller and put the name “UpdateVideo” in the input box.
  • Connect the sensor and the controller. Make sure the “Activate TRUE level triggering (pulse mode)” button is pressed (see the yellow circle below). It should look like this now:

    Sensors and controllers

Start the Game Engine and see the result

You can start the game engine by pressing ‘p’. When you press ‘enter’ the video will start playing. It should look like this:

The result

Press ‘esc’ to quit the Game Engine.

Resources

  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

31 thoughts on “Beginners tutorial: Using video textures in the Blender Game Engine

  1. Lasphere says:

    Hi! Thanks for the tut, but I cant seem to get i to work. When I press ‘enter’, the plane just turnes white, as if it cannot find the videotexture. :(

  2. @RH2: that’s indeed a good idea for another tutorial :)

    @Lasphere: do you get any error messages in the Python console? Did you try the .blend file from the tutorial?

  3. Thomas says:

    Thanks for the clearly understandable tutorial.

    How can I stop the video before it’s played in full lenght and what I have to ad, to let the video repeat?

  4. Lasphere says:

    I tried you .blend too. I get no error messages in the console, or anywhere else. Its like, it cant find the moviefile.. Strange.

  5. Minifig says:

    Sweet!
    I have been wanting to try this, but didn’t know python.
    It works like a charm!

    Is it possible to render something in realtime?
    Like on the billboard at the back of Pokemon Stadium in smash bros.?

  6. Excellent!!!!!! Finally your guidance made it work. I’ve tried to follow tut after tut, vid after vid.
    I had these exact (well almost). They left out a couple very important details that a noob like me would never figure out.But it was your instructions that seen it through.
    I think the part that made me see where I been going wrong was putting my video & image in the script.
    And since I didn’t download anything from here, I was able to see why my vid/img weren’t working DAh! Great tutorial. Thanks a bunch. Your the man!

  7. MDxm says:

    So, what do I have to do when I would like to rotate the plane? If I do it either in Edit or Object mode, it stops working, it’s just a black screen (I used black texture)
    Maybe delete the UV map and remap it, but is there any other easier way?

  8. Lasphere says:

    @Dennis

    Yearh, I did. :) I can see the bbb picture, but when I press ‘enter’ the plane turns white. Anyway, I guess its because blender cant locate the movie file. (the .blend from the blenders website works)

  9. Vladimir Mejia says:

    Hi, thanks for the tutorial, very usefull, now, could you make another tutorial about video texture but using a real time camera of an scene?, i need to project multiple sources in different planes at the same time, imagine four planes and on each face projecting what its happening in different GE scenes running background…or a character moving from a plane to another…

    Thanks.

  10. abc says:

    That’s my question, too:

    How can I add two or more planes with different video textures at the same time?

    Thanks.

  11. wimmm says:

    I don’t know if this thread is dead, but I can’t get it to work either. I get this in the console

    Python script error from controller “cont#CONTR#1″:
    Traceback (most recent call last):
    File “LoadVideo.py”, line 8, in
    AttributeError: ‘NoneType’ object has no attribute ‘owner’
    Python script error from controller “cont#CONTR#1″:
    Traceback (most recent call last):
    File “LoadVideo.py”, line 8, in
    AttributeError: ‘NoneType’ object has no attribute ‘owner’

    Any thoughts?

    thanks for the tutorial nevertheless

    grtz,

    wimmm

  12. mox says:

    Hi,
    I wish use this technique, for a presentation. I have some video with alpha and some portion of this video must be overlayed, like a still with a video loop of scroll title.
    I need to understand how trigger the frame number from a textured video, to stop end/or start another overlayed video…
    Sorry for my english and the strange exposition…
    Thanks for the tutorial

  13. Tom says:

    Hi i did evrything what there was written but i getting error like this:

    Python script error from controller ”cont#CONTR#1”=
    Traceback :
    File “LoadingVideo”, line 13, in
    RunTimeError: VideoTexture.materialID: Object doesn’t have material with given name

    Did evrything but still same..

  14. Great!
    Is it possible to make this working for the regular 3D-View?
    So i can see animated textures on a plane while playing the the timeline – WITHOUT GameEngine???

  15. Futch says:

    I did everything according to the tutorial, but i keep getting the error message:

    Python script error from controller ”cont#CONTR#1”=
    Traceback :
    File “LoadingVideo”, line 13, in
    RunTimeError: VideoTexture.materialID: Object doesn’t have material with given name

    Any Ideas?

  16. jim says:

    Thank you Dennis for the tutorial. i’s very informative and easy to understand. worked for me first time.
    cheers

  17. Christian Brackett says:

    I see many people here have the same question, including me. Can we get someone to answer why we’re getting this message:

    Python script error from controller ”cont#CONTR#1”=
    Traceback :
    File “LoadingVideo”, line 13, in
    RunTimeError: VideoTexture.materialID: Object doesn’t have material with given name

  18. Christian Brackett says:

    Why is it there’s always something missing from these tutorials? Obviously people want to use their own video, but you don’t mention how to do that–sort of an intrinsic part of this tutorial’s purpose. I assume I replace your video and image names in the text with my own, which I’ve put in the same directory as the .blend file. I get that same message that other people are getting, which no one in two years has bothered to respond to. I also can’t find anywhere what video file types blender is limited to…the original tutorial states “includes but not limited to…” etc. I created a video using blender and the only one that would actually produce a video with animation and sound was a quicktime .mov. I have xvid and other codecs but none of the other options worked…so can I use a .mov file for this? Who knows? Everything having to do with blender takes days and days and days to figure out, because a third of the time it doesn’t work and there’s no one to ask. (Try searching on blenderartists.org–the search engine sucks.) I am so tired of this!

  19. Christian Brackett says:

    Well, I figured out how to solve this error message (in case anyone is still looking for an answer) if you’re only looking to play animation (and not sound):

    Open your movie file in both the UV window and under “Image” in the texture panel.

    Your python script should refer to the name of the MATERIAL you’re using, not the image. For example, I named the material I was using on my plane (the one containing the movie texture) “invite”:

    # — Get the material that is using our texture
    matID = VideoTexture.materialID(obj, ‘MAinvite’) <—changed this

    This enabled the video to play at least the animation. Now my problem is, it's not playing the audio for some reason. My way around this was to trigger the audio and video as separate files, at the same time. This probably wouldn't work too well if you need to sync up audio & video close together. In my case I didn't need it so exact.

    If anyone out there in the internet void knows how to solve my problem, please let me know. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>