3
$\begingroup$

In this simple code below, named "example.py" I want to get the current scene and create a sphere.
I am using bge.logic.getCurrentScene() but there is an error named:

name 'bge' is not defined.

I need to run this example on cmd, for example: "Blender Path" -P example.py

I have a blender big program, but there is a problem in this specific part, so I need be sure that this part will work.

import bpy
import bge

mej = bpy.ops.mesh.primitive_plane_add(1)
scj = bge.logic.getCurrentScene() 
obj = scj.objects.new(mej,'sphere')
$\endgroup$
  • $\begingroup$ Are you using the code in the game engine, or just "normal" blender? $\endgroup$ – David Sep 30 '15 at 0:04
  • $\begingroup$ Just "normal" code $\endgroup$ – Jimmy Sep 30 '15 at 3:19
6
$\begingroup$

The issue with creating an object is where you are doing it, the scene's list of objects doesn't allow you to create new objects, only to link objects to the scene. To create a new object you use bpy.data.objects.new()

An example of manually creating a mesh object would be -

import bpy

# get the current scene
scn = bpy.context.scene

# create a mesh data block
mymesh = bpy.data.meshes.new('mymesh')

# define some vertices
verts = [Vector((-1,  1, 0)),
         Vector(( 1,  1, 0)),
         Vector(( 1, -1, 0)),
         Vector((-1, -1, 0)),
        ]

edges = []
faces = [[0, 1, 2, 3]]
# add the vertices to the mesh
mymesh.from_pydata(verts, edges, faces)

# create an object that uses the mesh data
myobj = bpy.data.objects.new('myobject', mymesh)

# link the object to the scene
scn.objects.link(myobj)
$\endgroup$
  • $\begingroup$ In myobj = bpy.data.objects.new('myobject', mymesh) "myobject" is a name that I choose for my object, in this case "mymesh" ? I'm a little confusing, because I need to write a blender 2.49b code to 2.74 like this: def standardObj(Name,shapeType) if shapeType == 102 : mej = Mesh.Primitives.Plane(1) scj = Scene.GetCurrent() obj = scj.objects.new(mej,Name+'ln') So, in this case the code add a "mej" with " Name+'ln' " data? Sorry, but I didn't write this code with blender 2.49b. $\endgroup$ – Jimmy Oct 1 '15 at 3:38
  • 1
    $\begingroup$ Perhaps @Jimmy reading your responses to David and Sambler you should first read some simple blender python api tutorials, get the hang of it a little and then go and try rewrite 2.49b code. Otherwise you will get stuck on every other line and it will feel like a struggle. $\endgroup$ – Jaroslav Jerryno Novotny Oct 1 '15 at 6:12
  • $\begingroup$ myobj is a variable pointing to the object, mymesh is the variable pointing to the mesh data block that holds the mesh data used by myobj, the names within quotes are the user visible names. Coding for blender <=2.49 is a lot different to coding for blender 2.50+ it isn't just some small adjustments, it is mostly a rewrite. You may want to go over the scripting intro pages here $\endgroup$ – sambler Oct 1 '15 at 9:23
1
$\begingroup$

The bge module in only available in the game engine, hence why you are getting the error.

To access the current scene for blender scripting you must use bpy.context.scene. Link to the bpy context.

If you are not using the game engine, there is not reason to import bge because none of it will work anyway.

$\endgroup$
  • $\begingroup$ @Jimmy since this solved your problem please accept the answer. $\endgroup$ – David Sep 30 '15 at 3:45
  • $\begingroup$ Thanks David! I was not finding this way to get the current scene without bge. Is there another way to add a new object in this scene ? Because there is an error in obj = scj.objects.new(mej,'sphere') named: " object has no attribute 'new' " $\endgroup$ – Jimmy Sep 30 '15 at 3:53
  • 1
    $\begingroup$ You should accept this answer @Jimmy as it correctly answers what you have asked. I know other problem risen up but that should be another question and this question should be cleared. $\endgroup$ – Jaroslav Jerryno Novotny Oct 1 '15 at 6:16

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.