- 'always' named 'sAlways',
- 'keyboard' named 'sKeyboard' with 'all' selected,
- 'mouse' named 'sMouseRight' with 'right button' selected,
- 'mouse' named 'sMouseLeft' with 'left button' selected,
- 'mouse' named 'sMouse' with 'movement selected'
#!BPY
"""
Name: 'HowToBuildAnInteractiveBGEScene'
Blender: 248
Group: ''
"""
##########################################################3
import Blender
import bpy
from Blender import *
from Blender.Scene import Render
from Blender import Text
##############################################################
# load the template blender file
Blender.Load('c:\\tmp\\bgeExample.blend')
##############################################################
# reload the modules because a load resets the Python interpreter
import Blender
import bpy
from Blender import *
from Blender.Scene import Render
from Blender import Text
##############################################################
# name for the embedded script in the loaded file
gameScriptName = 'MainScript.py'
##############################################################
# Get rid of the lamp and cube from the default scene
# - leave the camera, it has the linkages for the scripting
# for the BGE.
# - Note: Do NOT make any object the BGE scripts invisible
# via ob.layers=[], this will disable the scripts
# attached to the object.
scene = Scene.GetCurrent()
for ob in scene.objects:
print ob.getName()
if ((cmp(ob.getName(),'Lamp')==0) |
(cmp(ob.getName(),'Cube')==0)):
scene.objects.unlink(ob)
##############################################################
# reuse the camera in the default scene
##############################################################
# add a lamp and set it up
#
lampdata = Lamp.New()
lampdata.setEnergy(10.0)
lampdata.setType('Hemi')
lamp = scene.objects.new(lampdata)
lamp.setLocation(-5.0,-6.0,5.0)
lamp.setEuler(0.0,0.0,0.0)
##############################################################
# create the mesh and bind a material to it
#
coords=[ [-1,-1,-1], [1,-1,-1], [1,1,-1], [-1,1,-1], [0,0,1] ]
faces= [ [3,2,1,0], [0,1,4], [1,2,4], [2,3,4], [3,0,4] ]
me = bpy.data.meshes.new('myMesh')
me.verts.extend(coords)
me.faces.extend(faces)
ob = scene.objects.new(me,'myObj')
ob.setEuler(0.2,0.2,0.1)
#
mat = Material.New('newMat') # create a new Material called 'newMat'
mat.rgbCol = [0.8, 0.2, 0.2] # change its color
#
# attach the material to the mesh
# note 1) a different technique is used for meshes
# compared to how materials are attached to objects
# note 2) we can attach the material to the mesh even after
# the mesh was assigned to the object. This happens
# because the mesh and the object really are pointers
# to the same information in memory.
me.materials += [mat]
#
Window.RedrawAll()
#
#######################################
# setup the BGE handler to animate the polygon
# based on keyboard inputs
#
# create the text that handles the event
gameScript = """
#######################################
# tasks for each iteration thru this script
# 1) Output diagnostic information to console
# 2) rotate or move myObj based on keyboard input
# - the arrow keys move the object up/dn, rt/left
# - shift + arrows key rotates the object
print 'BGE script executing...'
import Blender
print Blender.sys.time()
# GameLogic has been added to the global namespace no need to import
# for keyboard event comparison
import GameKeys
# support for Vector(), Matrix() types and advanced functions like AngleBetweenVecs(v1,v2) and RotationMatrix(...)
import Mathutils
# for functions like getWindowWidth(), getWindowHeight()
import Rasterizer
# for matrix operations
import Mathutils
def main():
cont = GameLogic.getCurrentController()
sce = GameLogic.getCurrentScene()
ob = sce.getObjectList()['OBmyObj']
sens = cont.getSensor('sKeyboard')
if sens.isPositive():
print '--key pressed--'
fullKeyList = sens.getCurrentlyPressedKeys()
keyList=[]
for key in fullKeyList:
keyList.append(key[0])
pos = ob.getPosition()
orientL = ob.getOrientation()
orientM = Mathutils.Matrix(orientL[0],orientL[1],orientL[2])
orientM.transpose()
orient = orientM.toEuler()
if (GameKeys.RIGHTSHIFTKEY in keyList) or (GameKeys.LEFTSHIFTKEY in keyList):
print '--Shift Pressed--'
if GameKeys.RIGHTARROWKEY in keyList:
orient[1]=orient[1]+0.5
print '--Right Arrow Key--'
elif GameKeys.LEFTARROWKEY in keyList:
orient[1]=orient[1]-0.5
print '--Left Arrow Key--'
if GameKeys.UPARROWKEY in keyList:
orient[0]=orient[0]+0.5
print '--Up Arrow Key--'
elif GameKeys.DOWNARROWKEY in keyList:
orient[0]=orient[0]-0.5
print '--Down Arrow Key--'
else:
print '--other key--'
else:
if GameKeys.RIGHTARROWKEY in keyList:
pos[0]=pos[0]+0.1
print '--Right Arrow Key--'
elif GameKeys.LEFTARROWKEY in keyList:
pos[0]=pos[0]-0.1
print '--Left Arrow Key--'
if GameKeys.UPARROWKEY in keyList:
pos[1]=pos[1]+0.1
print '--Up Arrow Key--'
elif GameKeys.DOWNARROWKEY in keyList:
pos[1]=pos[1]-0.1
print '--Down Arrow Key--'
else:
print '--other key--'
orientM = orient.toMatrix()
orientM.transpose()
ob.setOrientation(orientM)
ob.setPosition(pos)
main()
"""
txt = Text.Get(gameScriptName) #get the gamescript
txt.clear() # clear the existing script
txt.write(gameScript) # appending text
#######################################
# Save the blender file
#
Blender.Save('C:\\tmp\\FullBGEExample.blend',1)
This script deletes the default lamp and cube, then adds a new lamp and polygon. The default camera is kept since it has the links for the game engine attached to it. The script adds the game engine script to the 'MainScript.py' text block. Once the script executes, Blender should open and look like this:
New code formatting: Thanks to techqi and Alex Gorbatchev.
Please post the py and blend files. It is difficult to cut, past and print these web pages for future reference.
ReplyDeleteI found this tutorial to be quite helpful have converted it to Blender 2.5 python using the 2.49 conversion method.
Anon,
ReplyDeleteYou can get a clean version of the code by clicking on the icon in the upper left hand which looks like a piece of paper with a '<>' over top of it.
I haven't worked out where I want to post source files at yet. I'll add a link in the future with source code postings.
edt
Hallo,
ReplyDeletethere is a small error in line 43: wrong indentation ..
and does not yet run in Blender 2.49
(by the way, it complains too about deprecated use e.g. getObjectList() )
Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
ReplyDelete