game logic config from a bpy script

I wrote a little code to populate bricks and props for a bunch of game objects, and there’s some fields I don’t know how to fill :
how do you add a script name in a python controller ?
how do you add a module name in a python/module controller ?

here’s a code so far. I mean a code… it’s more a ctrl-space/copy/paste exercice :slight_smile:

import bpy

## brick config for psy object

# 'STRING' # FLOAT INT STRING TIMER
# create a psy game object
ob = bpy.context.active_object

# props
bpy.ops.object.game_property_new()
prop = ob.game.properties[-1]
prop.name = 'obtype'
prop.type = 'STRING'
<b>prop = ob.game.properties[-1]</b> # strange error without this
prop.value = 'psy'

bpy.ops.object.game_property_new()
prop = ob.game.properties[-1]
prop.name = 'define'
prop.type = 'BOOL'
prop.value = False


# bricks

# sensor
'''
'ACTUATOR', 'ALWAYS', 'COLLISION', 'DELAY', 'JOYSTICK', 'KEYBOARD',
'MESSAGE', 'MOUSE', 'NEAR', 'PROPERTY', 'RADAR', 'RANDOM', 'RAY', 'TOUCH'
'''
# controllers
'''
'LOGIC_AND', 'LOGIC_OR', 'LOGIC_NAND', 'LOGIC_NOR', 'LOGIC_XOR', 'LOGIC_XNOR',
'EXPRESSION', 'PYTHON'
'''
# actuators
'''
CAMERA', 'CONSTRAINT', 'EDIT_OBJECT', 'FCURVE', 'FILTER_2D', 
'GAME', 'MESSAGE', 'MOTION', 'PARENT', 'PROPERTY', 'RANDOM', 
'SCENE', 'SOUND', 'STATE', 'VISIBILITY'
'''
# psy config
bpy.ops.logic.sensor_add(type='PROPERTY')
sen = ob.game.sensors[-1]
sen.name = 'define'
sen.property = 'define'
sen.evaluation_type = 'PROPEQUAL' #'PROPCHANGED'
sen.value = 'True'
sen.show_expanded = False

bpy.ops.logic.controller_add()
cont = ob.game.controllers[-1]
cont.name = 'config'
cont.type = 'PYTHON'
cont.link(sen)
cont.states = 1

#
bpy.ops.logic.sensor_add(type='MESSAGE')
sen = ob.game.sensors[-1]
sen.name = 'init'
sen.subject = 'started'
sen.show_expanded = False

bpy.ops.logic.controller_add()
cont = ob.game.controllers[-1]
cont.name = 'run'
cont.type = 'LOGIC_AND'
cont.link(sen)
cont.show_expanded = False

bpy.ops.logic.actuator_add(type='STATE')
act = ob.game.actuators[-1]
act.name = 'run'
# 'SET', 'ADD', 'REMOVE', 'CHANGE'
act.operation = 'SET'
states = [False for i in range(30)]
states[1] = True
act.states = states
act.link(cont)
cont.states = 1



# show the list of text blocks in the system. Then, add yours.
bpy.data.texts.new("MyNewScript.py")
myScript = bpy.data.texts[0]
# ... of course, it can be a different position than [0], which you find with list(bpy.data.texts) in console

# With active object selected:
myController = bpy.context.active_object.game.controllers.get('myControllerName')
myController.text = myScript

ah, thanks. there’s a little bug if you don’t add the line in bold below :

bpy.ops.logic.controller_add()
cont = ob.game.controllers[-1]
cont.name = 'config'
cont.type = 'PYTHON'
<b>cont = ob.game.controllers[-1]</b>
cont.link(sen)
cont.states = 1
cont.text = bpy.data.texts['script.py']

else cont.text won’t be available as a method. cont is still refering to the controller as an AND controller type but it seems to be another object.