Webcam as Texture in Blender Game Engine

Introduction

This quick tutorial shows how to use a webcam (aka video and audio input device) as a texture in the Blender Game Engine on Windows.

The Blender version I used was 2.69.

Background

I searched for quite a while to figure out this one and can see many people also having problems with it.

So that’s why I created this tutorial since a webcam as a texture is a very cool thing.

Prerequisites

A webcam, windows and blender 2.69

Instructions

STEP 1

Open blender in default settings, and choose ‘Blender Game’, ‘GLSL Shading’ and ‘Texture’ mode.

Webcam As Texture Tutorial Image 1

Webcam As Texture Tutorial Image 1

STEP 2

Select the default cube.

Go to it’s texture editor, choose ‘Image or Movie’ as type.

Copy the png below and open it in blender. The filename should remain ‘videolayer.png’.

videolayer.png

videolayer.png

Webcam As Texture Tutorial Image 2

Webcam As Texture Tutorial Image 2

 

STEP 3

Create a new text file using the text editor in blender and name it ‘videolayer.py’.

Copy paste the python code below.

import bge
cont = bge.logic.getCurrentController()
obj = cont.owner
if not hasattr(bge.logic, 'video'):
    matID = bge.texture.materialID(obj, 'IMvideolayer.png')
    bge.logic.video = bge.texture.Texture(obj, matID)
    bge.logic.video.source = bge.texture.VideoFFmpeg("0", 0) 
    bge.logic.video.source.play()
bge.logic.video.refresh(True)

The code above references the png that was opened in step 2. ’videolayer.png’

The code VideoFFmpeg(“0″, 0) is telling Blender to use the device it finds at 0. This is a windows thing.

Webcam As Texture Tutorial Image 3

Webcam As Texture Tutorial Image 3

 

STEP 4

Select the cube again.

Open the node editor.

Create a new ‘Always’ sensor.

Select ‘TRUE’ triggering on the ‘Always’ sensor

Create a new ‘Python’ controller.

Choose the ‘videolayer.py’ script in the ‘Python’ Controller.

Link them together.

Webcam As Texture Tutorial Image 4

Webcam As Texture Tutorial Image 4

 

STEP 5

Select Game->Start Game Engine or put mouse in viewport and press ‘P’ to see it work.

Webcam As Texture Tutorial Image 5

Webcam As Texture Tutorial Image 5

 

Source Files

Download ‘Webcam As Texture’ Source Files

You can also watch this tutorial on youtube.

http://www.youtube.com/watch?v=lwi-2nXH3vc

 

SeanWasEre