1
$\begingroup$

The question is almost the same one that has been published before. But I think it's good to treat it again because it is not very clear, at least, for me.Also it does not have an accepted answer.

I want to draw a line using: bge.render.drawLine(fromVec, toVec, color) Draw a line in the 3D scene.

Parameters:

fromVec (list [x, y, z]) – the origin of the line

toVec (list [x, y, z]) – the end of the line

color (list [r, g, b]) – the color of the line

but I get a error message:

NameError: name 'bge' is not defined

Reading the question someone did before, first I put enable the game engine by selecting it in the render engine menu:

enter image description here

But I get an error when a try to import bge

ImportError: No module named 'bge'
$\endgroup$
  • 2
    $\begingroup$ Are you trying to execute the script with the "Run Script" button? I believe that the bge module is only valid while a game is running in Blender. You'll have to execute it with a Python controller. $\endgroup$ – Scott Milner Jan 5 '18 at 2:56
  • $\begingroup$ If by chance you want to draw lines on the blender UI use the bgl module $\endgroup$ – batFINGER Jan 5 '18 at 14:28
3
$\begingroup$

Blender Scripts

Pressing the "run script" button makes the code be executed as Blender script. This does not rely to the BGE. Importing the BGE API will always fail with:

ImportError: No module named 'bge'

BGE scripts

BGE Python code gets executed by python controllers. This means in the logic editor you add a python controller.

The code gets executed when any connected sensor triggers the controller. e.g. by an Always sensor right after loading the object.

Script mode

In Script mode you enter the name of the text block into the value field. The text block will be executed from to to button.

E.g. drawALine

import bge

bge.render.drawLine([0,0,0], [10,10,10], [1,1,1,1]])

Script: drawALine

Module Mode

In Module mode you enter the name of the module (the file name without ".py") with "." which concatenates a function name of the module. The module can be a text block or an external python file that is in the python search path.

E.g. painter.py

import bge

def drawALine(controller):
    bge.render.drawLine([0,0,0], [10,10,10], [1,1,1,1]])

Module: painter.drawALine

Blender API inside the BGE

using hte blender API "bpy" insode the BGE will not fail wen you run your game embedded in Blender. But any writing to the Blender API will not take effecto before ending your game.

When running your game without Blender importing bpy will fail the same way as running bge scripts inside of Blender:

ImportError: No module named 'bpy'
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.