0

I am currently using the game engine in Blender. I was wondering how can I change the properties of an object via script, like position, rotation, color, and change the related variables in the objects.

scene = bpy.data.scenes["Scene"]
scene.Cube.diffuse_color = [0]=red [1]=green [2]=blue

In the example above I tried to change the color of a Cube, but it fails with the error "Cannot assign to literal". How can I solve it?

2 Answers 2

0

A couple of suggestions

  1. Before starting to code in Blender you have to be sure of what you are doing because unless it is going to become hard to find a solution to your problem.
  2. Always look at the documentation.
  3. Blender has a hidden console that you can show up by dragging and dropping the higher part of the UI; every time you do something in the UI (move an object, change a color,...), the corresponding Python command is displayed here: enter image description here

About your question

The following is a simple snippet to show you how to change the color of the default Cube in Blender:

import bpy
activeObject = bpy.context.active_object #Set active object to variable
mat = bpy.data.materials.new(name="MaterialName") #set new material to variable
activeObject.data.materials.append(mat) #add the material to the object
bpy.context.object.active_material.diffuse_color = (1, 0, 0) #change color

You can also access an object's transformations through the relevant properties (location, rotation_euler - for Euler XYZ rotations, and scale) this way:

>>> object.location
Vector((-2.609607696533203, -3.618044137954712, 1.8581657409667969))

>>> object.rotation_euler
Euler((-0.6668576598167419, 0.697817325592041, -1.8853096961975098), 'XYZ')

>>> object.scale
Vector((1.2768771648406982, 1.4246054887771606, 0.6418209075927734))

Hope this is a good hint for you to start with.

Sign up to request clarification or add additional context in comments.

Comments

0

Firstly, the error you get is because the code you show is not valid python code, to change the color value, you need to have the index (the [0]) attached to the property name and the value to assign goes on the other side of the = sign.

color[0] = 0.1

It is possible to assign the same value to several properties in one line -

color[0] = color[1] = color[2] = 0.2

You can also assign all four values (color includes an alpha) to the color property by using a tuple -

red = blue = green = alpha = 0.2

color = (red, green, blue, alpha)

Now on to what you are trying to do -

There are two ways to access blenders data, bpy is only used for accessing data while you are modeling, texturing etc.

When the game engine is running bpy is not available, you need to use bge to access any data from scripts you assign to a python controller.

To get what you are trying to work, first enable object color for the material, you can find this under options in the material settings. With this enabled you can adjust the objects color property -

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

own.color[0] = 0.2 # red
own.color[1] = 0.3 # green
own.color[2] = 0.4 # blue

It is possible to access other objects, you use getCurrentScene() to get the current scene which has the list of objects,

scn = bge.logic.getCurrentScene()
enemy = scn.objects['enemy']

You can find sample code in many of the API pages for game engine types and will find blender.stackexchange is a better place to ask blender questions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.