Python Controller: odify the script text

Hi
Is there a way to modify the name of the script of a python controller via python code?

I tried in this way:
bpy.context.main.objects[‘obj’].game.controllers[’’].text.name

but this type of instruction can’t be used if the filed that contains the name of the python script is empty!

In order to summarize: I would dynamically assing a python script name to a python controller!:frowning:

Try this


import bpy

ctr = bpy.ops.logic.controller_add(type="PYTHON",name="ctrPy",object="Cube")
obj = bpy.data.objects["Cube"].game
obj_ctr = obj.controllers[0]
obj_ctr.mode = "MODULE"
obj_ctr.module = "myScript.functionToCall"

bye

No!
My mode isn’t MODULE but Script!
So, if the object has already a python controller with the name of a script in the textbox I can modify it:

bpy.context.main.objects[‘obj’].game.controllers[’’].text.name = ‘new_name.py’

but if the object hasn’t a python controller yet, I can’t insert the name of a pyton script!

Are you talking about in the game engine?

Simply controller.script = “textname”.

I use this quite often when I have an initialisation function:

# Player.py

def init(cont):
    cont.script = "Player.main"

def main(cont):
    pass

Also, bpy shouldn’t be used in the game engine. Use the relevant BGE modules.
2.5 docs: http://www.blender.org/documentation/250PythonDoc/
2.49 docs: http://www.blender.org/documentation/249PythonDoc/GE/index.html

I have a python text to insert into the field Script, not a function or a method.
In the documentation:
script:
The value of this variable depends on the execution methid.

  • When ‘Script’ execution mode is set this value contains the entire python script as a single string (not the script name as you might expect) which can be modified to run different scripts.
  • When ‘Module’ execution mode is set this value will contain a single line string - module name and function “module.func” or “package.modile.func” where the module names are python textblocks or external scripts.
    I would write in the field Script something like “python_file_name.py”, exactly like in the interface mode.

Yeah,
now I’m able to write the func name of the python script via python code thanks to the andrew’s suggestion and his portion of code.
Now the problem is:
I have an object in the scene that have 3 sensors (always, mouseover, leftmouse) and a python controller (that can be dynamically modified).
When I duplicate this object (because I need more copies of the same object), every copy will have the same python controller and if I write, in the python code, print (cont.owner) this returns the names of all my duplicated object.
It’s quite obvious!
But I would that on clicking on a particular object the scripts returns the single name of the clicked object and not every copy.
I can’t write a script for every copy because I don’t previously know how many copies there are.
:frowning: