Realtime Blender python question

Well afther learning some python I started slowly on learning Blender Python for creating games.

I know the basics and I did already some nice codes to move my character around and so on.
Now I’m stuck with something else, switching camera’s using the setCamera function.

My script looks like:


cont = GameLogic.getCurrentController()
own = cont.getOwner()

camera = cont.getActuator("camera")

pressone = cont.getSensor("one")
presstwo = cont.getSensor("two")

if pressone.isPositive():
	own.cam = -1
elif presstwo.isPositive():
	own.cam = 1
else:
	own.cam = 0
	
if own.cam == -1:
	camera.setCamera("camera1")
else:
	camera.setCamera("camera2")

I already did a full setup of the: property: cam, keyboard sensors, and the Scene actuator that’s on set camera option.

When I press 1 or 2 I see the property changing in realtime using the “show debug properties” But it doesn’t do anything.
I also checked the camera names, and they are ok. I also tought about changing setCamera to getCamera, but that causes a blender crash.
I hope someone can help me. :smiley:

You have to apply actuators to get anything to happen:

GameLogic.addActiveActuator(camera,1)

the 1 turns it on, a 0 would turn it off but that’s not relevant for this actuator.

hi,

I`m just looking into the exact same thing JD.

Im finding that .setCam("blah") doesnt actually do a whole lot. :frowning: seems that the OB:CamName text you type in the actuator logic brick, for me anyhow, cannot be changed at the mo. (please don`t quote me :wink: )

:frowning: I`m gonna have to use a WHOLE lot of different actuators loaded with the different names of cams and fire them independantly. Its making a much longer script and is a crying shame. :frowning:

Hope you have more success with this than me…and please, if you find anything out??? :slight_smile:

Lol how stupid, I didn’t activate the actuator. I gota try again. Btqw the game I’m working on is a try on python and see what’s better, Python or bricks. :smiley: Btw the problem about mist slows down the game engine in 2.34, isn’t really true. Well actually if you use the python script for mist it doesn’t even reduce 1 frame at all. But I found a intresting bug too. I’ll post that later. :slight_smile:

Mmmh add active actuator thingy doesn’t really work. Still no change cam, while verything is setup correctly.

cont = GameLogic.getCurrentController()
own = cont.getOwner()

camera = cont.getActuator("camera")

pressone = cont.getSensor("one")
presstwo = cont.getSensor("two")

if pressone.isPositive():
	own.cam = -1
elif presstwo.isPositive():
	own.cam = 1
else:
	own.cam = 0
	
if own.cam == -1:
	camera.getCamera("camera1")
else:
	camera.getCamera("camera2")

GameLogic.addActiveActuator(camera,1)

About the bug I found in 2.34 i this:


1. Log script bug (blender 2.34)
When I open blender 2.34 and add this script (without -- ofcourse)

--
print " ---------------------------"
print "| error console by JD-Multi |"
print " ---------------------------"

import sys

ofile = open("logs/error.log","w")
sys.stdout = ofile
sys.stderr = ofile
--

It runs: [always sensor]--[controller: python: name script]
the console says: "logs/error.log" no such file or directory
then save the total blend file, run again, same error.
When I open it with blender 2.25, it works, so I save as blend (v2.25)
open it in blender 2.34, and the script works.
Why??? error, or way that blender 2.25 saves blend different as 2.34?

I hope someone else has found the problem too, no idea if it’s fixed in the new builds, but otherwise I test and report it. :-?

Why did you change from setCamera() in your first script to getCamera()
in your latest?

I haven’t messed around with the camera actuator yet, but I looked in the reference and there are no python methods for the camera actuator. That means the most you can do is to connect two camera actuators to the controller and switch between them.

camera1 = cont.getActuator(“camera1”)
camera2 = cont.getActuator(“camera2”)

#to use camera1
GameLogic.addActiveActuator(camera2,0)#make sure the other is off
GameLogic.addActiveActuator(camera1,1)

#to use camera2
GameLogic.addActiveActuator(camera1,0)
GameLogic.addActiveActuator(camera2,1)

I’m obviously leaving out decision code here.

Like I say, I haven’t used it yet, but if it says no python methods, that only leaves the general methods for actuators and logic bricks.

edit: Oh Oh. You were using the scene Actuator for set camera.
Ignore all that and use the setCamera() method. He He He.

Think fireside is right, sortof.

There are python methods(setCamera & getCamera) for the camera, but it’s in the scene actuator, which I think JD already knows and that’s what he’s trying to use.

However, there seems to be no way of getting it to work, atleast I couldn’t. But, firesides suggestion works. Create two scene actuators with the setCamera fields hardcoded to “camera1” and “camera2”.

Then activate whatever scene actuator you need.

I couldn’t get setCamera to work, either. That takes a little egg off my face, although using the scene actuator is better than the camera actuator unless you’d want following cameras. It’s still nice to have a central controller for making camera switching decisions and I think it would be worth while.

edit: I got the setCamera method to work by passing the object rather than the name like this:

import GameLogic
cont = GameLogic.getCurrentController()
scene = GameLogic.getCurrentScene()
camera2 = scene.getObjectList()[“OBCamera2”]
act = cont.getActuator(“act”)
act.setCamera(camera2)
GameLogic.addActiveActuator(act,1)

It worked when I passed it the name, also, but it crashed when I pressed escape.

Well I tested something else, but all I got was errors and crashes. So I did something else. I made a ray sensor, python controller, and 2 scene camera actuators. These actuators got controlled by the Python script. When the empty of the 3th person camera hit’s an object in the game, it switches to 1st person. :slight_smile:

cont = GameLogic.getCurrentController()
own = cont.getOwner()

camera1 = cont.getActuator("camera1")
camera2 = cont.getActuator("camera2")
objects = cont.getSensor("object")

if objects.isPositive():
	camera2
	GameLogic.addActiveActuator(camera2, 1)
	GameLogic.addActiveActuator(camera1, 0)
else:
	camera1
	GameLogic.addActiveActuator(camera1, 1)
	GameLogic.addActiveActuator(camera2, 0)

This works fine, it also switches back when the camera doesn’t collides the objects in the game.
The logic brick setup looks like:

[sensor: ray: prop: Object1, range: 5.0, axis: -z] (this is on the 3th person camera) <–>[controller: python: camera.txt] <–> [actuator: scene: camera: camera1] [actuator: scene: camera: camera2]
the ray sensor is called: object, the camera1 and camera2 actuator has the name of the camera.
This script also fixes the camera going trough walls anoying happening. :slight_smile:

Good idea. Sounds really cool.

I’m still working on this, and still learning python. But found another problem.
I’ve got a code:


#Bullet movement
#created by: JD-Multi

cont = GameLogic.getCurrentController()
prop = cont.getOwner()

move = cont.getActuator("fly")
delete = cont.getActuator("delobj")
forwards = move.getDLoc()[1]
always = cont.getSensor("always")
detect = cont.getSensor("detect")

forwards = 1.0

if always.isPositive():
	prop.fly1 = 1
else:
	prop.fly1 = 0

if detect.isPositive():
	prop.hit = 1
else:
	prop.hit = 0
	
if prop.fly1 == 1:
	move.setDLoc(0.0, forwards, 0.0, 1)
else:
	move.setDLoc(0.0, 0.0, 0.0, 1)
	
if prop.hit == 1:
	GameLogic.addActiveActuator(delete, 1)
else:
	GameLogic.addActiveActuator(delete, 0)
	

GameLogic.addActiveActuator(move, 1)

When I shoot on an object in the level with property ‘level’ the bullet needs to be deleted, but the script gives me and error, and drops the fps down to 10 when you shoot some bullets into the direction of an object holding property level.


Traceback (most recent call last):
  File "bullet_movement.txt", line 8, in ?
AttributeError: Unable to find requested actuator

But the actuator has the good name it’s there, and the object gets deleted, but still an error and fps lag.

Also the script:


import sys

ofile = open("logs/error.log","w")
sys.stdout = ofile
sys.stderr = ofile

Doesn’t do anything in blender 2.35, it doesn’t even write bugs from console to the error.log file. While blender 2.25 does. It gives an error, can’t find directory or filename, but they exists, and work in 2.25, but 2.35 gives error can’t find. :-?

I hope someone can help me :slight_smile: