mouseover to change color of objects

Noob, Blender 2.49b, very little knowledge/experience with Python & scripts;

I’ve imported some .stl files of some objects, and I want to allow mouseover in BGE to change their color as mouse goes over them and also present some info text about each object, and then color go back to original after mouse leaves the object and text disappears.

Any help would be appreciated!

[edit] I did try this script I found, but it only changed the object color after I exited BGE, not while in the game:

from random import random
import Blender
from Blender import *
scn = Blender.Scene.GetCurrent()
ob =
m = ob.getData(mesh=True)
if(len(m.materials) < 1):

following two lines are indented in script, but don’t show up indented here?

 mat = Material.New('newMat')
 m.materials += [mat] 

m.materials[0].rgbCol = [random(), random(), random()]

[edit] is it better to post this kind of (scripting?) question here, or in Coding-Python, or in both?

I have 2 files. One for 2.49 and one for 2.57. I can’t get the one for 2.49 to work quite right. But the one for 2.57 works great. You should definitely switch to 2.5.

EDIT: This uses no python. If you open up the 2.49 file and there is a horrible blue and green background, my apologies. :stuck_out_tongue:


test_thing_2.49.blend (151 KB)test_game_thing.blend (427 KB)

HG1, Thanks, but your reference Colour2_25x.blend doesn’t work for me in B2.49b.

Sonic14: your 2.49 example worked, but with a delay on both mouseover and mouse leave, not sure yet if you made that happen on purpose or not, or if it’s my system, some things take a while, others don’t (eg: with some imported objects, select object with RMB takes a couple of seconds, but if I go to Outline and select an object, selection happens as soon as I get back to it).

And error says “scene.001 does not exist, not removed”, maybe that’s why it has delay, even though color does change?

(it didn’t have any horrible background, and I did add show the cursor)

The timing was all messed up for me too. Not sure how you would fix that. It worked really good in 2.5. You should at least download blender 2.5 and try it out. You won’t regret it!

Yeah, people keep suggesting move up to 2.5, and I think this is the second instance I’ve had of things being slow for me in 2.49 but apparently not in 2.5; I’ll continue to consider it!

One more problem with using scenes to change the color is that I have about 10 different objects in my main test, with quite a few more in real effort, I suspect it would be a lot of work to make 2 individual scenes for each object, assuming that’s what would be necessary using scenes to change color rather than script.

I’d like to use a script to change the color property, but don’t know hardly anything about Python, so can’t roll it myself.

The code that you postet above is not working in the game engine.

I thought you know how to change the code back. Here I made a vertex color example for Blender 2.49b.
The second one is not from me. I only change the code to work with Blender 2.49b.

Changing the object color with a script, did not work with Blender 2.49b.


VertexColor249b.blend (129 KB)ColorSliderExample_finished.blend (460 KB)

HG1, nope, I’m a rank Blender noob ; I understand the concept of programming & have small familiarity with another language, but know almost nothing about scripting in Python.

I see that you defined a red color in the script in your first example (I don’t really understand the difference between changing or specifying the color of an object vs changing the color of the vertex (mesh?), but that’s not too important right now, I think).

What I need to know is how to allow that a mouseover any object can change its color, whether it’s by acting on the object, its properties, or its vertices. So, I think I need to have a mouseover script perceive which object it’s over, and then have that objects mesh be acted upon to change its color, if I’m making any sense or understanding how Blender might work in this regard. Then I’ll need to know how to allow the new color to change back to the original color when mouse leaves the object, and I think that might involve another mouseover sensor with an inverted output, maybe?

Hi I made two new examples thee examples.
File 2 changing between two vertex colors. Each object needs a script.
Fiel 3 changing between new color and the original color. Each object needs a script.
File 4 changing between new color and the original color. One script for all. It change the color of all objects (also walls etc.) that have mouse over.
File 5 Changing between new color and inverted color. One script for all. It change the color of all objects (also walls etc.) that have mouse over.

Don’t mean to thread hi-jack but I have been watching this thread and have a question to ask concerning the same exact thing. I used the script you used in File 4, and it works great! However for what I’m trying to do I have objects that run through one another. I’m guessing this is problematic due to how the vertex position is interpreted? It calculates for the position of the object that the mouse is over, but whenever there’s an object over that object (say something running through a hollow cylinder) it doesn’t know to kick over to the next object because it’s still in-bounds of the original vertex…it doesn’t know the other object is ontop of it or doesn’t care. I’ve got an example of what I describe here:

mouseOverMultiple.blend (162 KB)

What would be the easiest way to take care of this problem?

HG1, THANKS!! Number 4 worked PERFECTLY for what I want!! Now I’ll study it to see how you did it!

[edit] Discovered one problem, though, don’t know if it’s resolvable: I will have a LOT of objects in a scene that I’ll want this to work on, so I wonder if there’s a way to make an “init” script that will run through a derived list of all the objects, and exclude lamps and camera, and then set up each required logic brick situation for each object from within the init script, rather than hand setup logic bricks for each object?

[edit] hmm, ok, I see now, it’s only the text that’s using individual logic bricks, the “hitObject” in the script allows that any object will be color switched, neat! I wonder if there’s a way to move the logic for the text into the script like that also?

[edit2] This is one NEAT, and COMPLETE example! The more I look at it, the more impressed I am!
You covered a LOT of bases with it, and EXPLAINED a lot, too!

@ phreak.
That has noting to to with the vertex. The problem is, when you move the mouse from blue cylinder over the green, the mouse over any sensor don’t send new pulses. You can enable on the mouse over any sensor the Pulse button then it send a new positive, but no negative puls, if you leave the previous object. So the previous object did not set back to his original color. It is possible to change the script but it is very tricky.
At the moment I would suggest to use the file number 3 and use a script on every object to change the color. You can easily copy the logic to the other objects, if you select all objects with no logic then select the object with logic and press Ctrl+C and select Logic Bricks.
Let me know if that solution ok for you, otherwise I try to find a solution for the single script.

@ olderLearner.

  1. Yes it is possible to use a list or property on the object or prefix on the object make to include or exclude objects, but on lamps and cameras the vertex color has no effect.
  2. Yes you can use the script to hide and unhide the text. But you need to reference to the object name of the text. You can reference it withe a list [object, textobject] or use a string property on the object or you parent the text to the object and get the parent name.
    The last option is the comfortable one.
    Let me know what that solution you want.

@ phreak.
Meanwhile I have changed the script so that it works now with a single script.
Same download folder. File mouseOverMultiple3.blend

ok, I spoke too soon.

got an error when put your script into my test piece and “called” it from an emptyMesh I had there through “mouse over any”:

Blender Game Engine Started
Python script error from controller “cont11#CONTR#14”:
Traceback (most recent call last):
File “MouseOver”, line 5, in <module>
KeyError: ‘requested item “MouseOver” does not exist’
Python script error from controller “cont11#CONTR#14”:
Traceback (most recent call last):
File “MouseOver”, line 5, in <module>
KeyError: ‘requested item “MouseOver” does not exist’
Python script error from controller “cont11#CONTR#14”:
Traceback (most recent call last):
File “MouseOver”, line 5, in <module>
KeyError: ‘requested item “MouseOver” does not exist’
Blender Game Engine Finished

So, noticing that I had neglected to add a “mouse over” sensor to any selected object, I did so, but I can’t figure out how to also add a section of logic bricks relating to a text/font, as in pic of your setup:

Can you/someone tell me how to do that so I can proceed? Or tell me what I need to tell/show you so you can know/understand what I’ve done or done wrong, and what I may have neglected to do that needs to be done

File “MouseOver”, line 5, in <module>
KeyError: ‘requested item “MouseOver” does not exist’

The name of the mouse over any senor must be MouseOver.

To connect the object to the text you only need to select both objects (shift+right mouse click).

<chagrin!> oh. At first I thought, “well, I know that!”, until I actually thought about it (select BOTH objects, oh, ok!). Thanks again!

I’m not sure I understand: I see that every object (the two cubes and the plane) has to have logic bricks set up for them, secondarily to allow the text to appear and disappear (under control of the logic brick connections), and firstly to have a “mouseover” sensor set up for every relevant object, to allow the last “if” section in your script to newColor or oldColor any object the mouse is over. But I have (or will have) so many objects eventually that I’d much rather not have to set up the logic bricks for every object, but instead do as much of that as possible in the script. In other words, to set up the necessary logic IN the script so that every object can be handled by the last “if” statement without having to set up mouseover sensors via logic bricks. Does that make sense, and is it possible, and if so how?

So, two things to do: change and reset the color, and display and erase the text, for mouse over any object; my thought is that I’d like to be able to do ALL of that (via script discerned lists of objects and associated text) from within a script, with minimal use of logic bricks. So for the text, I think I’d prefer to reference it withe a list [object, textobject] .

I do not know if we keep talking past each other.
The script that changes color, runs only on Empty (mouse over any) and does not require the LogicBriks of the cube.
The LogicBriks on the cube (mouse over) are only for showing and hiding the text.

I’d prefer to reference it withe a list [object, textobject] .

But then you must type in the name of every object and every text object in the script.
When you have so many objects that is a lot of work.

When you parent the text to the object then you don’t need to write a list. I think that is the best method.

To hide an unhide the text I will make a script soon.

The reason I though a list would be better is because when I looked at your script, trying to understand it from the point of view of a noob, I saw where a list variable was declared as an empty, and then was filled, I think, by an “append” within a “for” loop, which was working its way through a bunch of vertices; I presumed that you could do the same kind of thing for the objects, like some statement which would return the count of objects, then a “for” loop up to that count getting the names of the objects and of the texts, and then associating them; in other words, let the execution of statements in the script fill the list, not me fill it manually. But like I said, I don’t know Python, so I may be totally misunderstanding what you meant by using a list.

I’m very appreciative of whatever you can work up for me, you’ve been great, thanks!

And so you’ll know, I’m preferring scripts, even though I don’t understand Python, because I can at least see what’s being made to happen in essentially one place, as opposed to the large number of parameters within the bricks, sometimes hidden. At least that’s what I’m thinking, maybe I’m fooling myself.

Sure it is possible to generate a list of all objects in the scene, but how you will find out which object belongs to which text. So that will not work.
The only way to do this is to use a name suffix. E.g. Cube.J001 belongs to Text.J001 and Cube.J002 belongs to Text.J002 and so on. But for that I don’t need to generate a list.

Meanwhile I have done it with parenting. File with number 6.

@ phreak.
Meanwhile I have changed the script so that it works now with a single script.
Same download folder. File mouseOverMultiple3.blend

You the man! I tried it using the multiple scripts for each object and that seemed to work well. I’ll give this one a whirl