5
$\begingroup$

Is it (if at all) possible in Blender 2.6+?

If not, will future versions of Blender support it?

I see some old posts (this and this) saying that it is not possible. However, I found a recent post referring to operators under bpy.ops.logic package that allowed me to add sensors and controllers from python.

bpy.ops.logic.sensor_add(type='ALWAYS', object=my_obj.name)
bpy.ops.logic.controller_add(type='LOGIC_AND', object=my_obj.name)

However I am not able to find any way to link them from python. I tried following (but did not work):

new_controller = my_obj.controllers[-1]
new_sensor = my_obj.sensors[-1]

It throws AttributeError saying that object has no attribute 'controllers' :-/

$\endgroup$
1
  • 1
    $\begingroup$ It is possible, as explained in the answer below. But I don't believe you can use this as a dynamic way to modify logic bricks during game-time. (Just want to make it clear for other people who might be wanting to do that) $\endgroup$
    – Mike Pan
    Commented Aug 9, 2013 at 1:25

2 Answers 2

9
$\begingroup$

This is done using the bpy.types.Sensor.link method, e.g.

import bpy

obj = bpy.context.object
sensors = obj.game.sensors
controllers = obj.game.controllers
actuators = obj.game.actuators

bpy.ops.logic.sensor_add(type="ALWAYS", object=obj.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", object=obj.name)
bpy.ops.logic.actuator_add(type="ACTION", object=obj.name)

# Newly added logic blocks will be the last ones:
sensor = sensors[-1]
controller = controllers[-1]
actuator = actuators[-1]

sensor.link(controller)
actuator.link(controller)
$\endgroup$
3
  • $\begingroup$ Wow! I wasn't aware about the Object.game property. Seems interesting! Thanks :-) $\endgroup$
    – mg007
    Commented Aug 8, 2013 at 13:27
  • $\begingroup$ Could you please add link to GameObjectSettings in your answer? It looks like it contains information on settings available to python API. $\endgroup$
    – mg007
    Commented Aug 8, 2013 at 13:33
  • $\begingroup$ Feel free to edit or create your own more complete answer. $\endgroup$
    – Aldrik
    Commented Aug 8, 2013 at 15:28
0
$\begingroup$

Recently I needed to assign multiple Logic Bricks to the selected objects. Maybe these can be of use or some easy copy-paste stuff for somebody:

Script 1: Assign Always and Action Logic Bricks to selected objects:

import bpy

#########################################################
# INSERT ACTION LOGIC BRICKS TO SELECTED OBJECTS SCRIPT #
#########################################################

#########################################################
# Note:
#
# Each of the selected objects must have a transform 
# (Location/Rotation/Scale) action (animation/keyframes) 
# before running this script. The action can be empty.
# Otherwise nothing will be inserted to the objects.
#########################################################

############
# Settings #
############

action_brick_start_frame = 0

action_brick_end_frame = 9999 

# action_brick_end_frame = bpy.context.scene.frame_end
# Uncomment to set the Action Logic Brick end frame to be 
# the end frame of the Timeline.

#############################################################
# Make sure that each of the selected objects has an action #
#############################################################

all_objs_have_action = True

for obj in bpy.context.selected_objects:
    ad = obj.animation_data
    if ad == None:
        obj.animation_data_create()
        all_objs_have_action = False
        #print(obj.name,"has no object.animation_data, creating...")
    if ad:
        if not ad.action:
            all_objs_have_action = False
            #print(obj.name,"does NOT have an action")
        #else:
            #print(obj.name,"has an action")

if all_objs_have_action == True:
    print("Inserting some Logic Bricks to the selected objects")

if all_objs_have_action == False:
    def oops(self, context):
        self.layout.label("All of the selected objects must have an action. Cancelling... ")

    bpy.context.window_manager.popup_menu(oops, title="Error", icon='ERROR')
    print("All of the selected objects must have an action. Cancelling... ")

####################################################################
# The following adds the Logic Bricks to objects that allow them to 
# be playable in Blender Game Engine, but only to the selected 
# objects that have an action. Actionless objects are ignored.
# Credit goes mostly to Jeremy Behreandt
####################################################################

if all_objs_have_action == True:
    ops=bpy.ops
    context=bpy.context

    # Cache shortcuts.
    ops_mesh = ops.mesh
    ops_object = ops.object
    ops_logic = ops.logic
    scene = context.scene

    selected = bpy.context.selected_objects

    for obj in selected:
        ad = obj.animation_data
        if ad:
            if ad.action:
                bpy.context.scene.objects.active = obj

                current_object = context.active_object

                # Cache shortcut to cube's game engine.
                game_engine = current_object.game

                # Add sensor.
                ops_logic.sensor_add(type='ALWAYS')
                                     #name='Always')
                                     #object=cube_name)
                sensor = game_engine.sensors[-1]

                # Add controller.
                ops_logic.controller_add(type='LOGIC_AND')
                                         #name='And')
                                         #object=cube_name)
                controller = game_engine.controllers[-1]

                # Add actuator.
                ops_logic.actuator_add(type='ACTION')
                                       #name='Action',
                                       #object=cube_name)
                actuator = game_engine.actuators[-1]

                # Set actuator action to cube's animation data action.
                actuator.action = current_object.animation_data.action

                # Play mode options are:
                # ['PLAY', 'PINGPONG', 'FLIPPER', 'LOOPSTOP', 'LOOPEND', 'PROPERTY']
                # 'PLAY' is the default.
                #actuator.play_mode = play_mode

                # Set frame start to scene end.
                actuator.frame_start = action_brick_start_frame

                # Set frame end to scene end.
                actuator.frame_end = action_brick_end_frame

                # Link logic together.
                sensor.link(controller)
                actuator.link(controller)

Script 2: Assign Toggle Visibility Logic Bricks to selected objects:

# Usage: 
# - Select objects you want to hide/show in BGE, set delay below, run script. 
# - Visibility Toggle happens when the delay frame below is hit.
# - You only need to apply this for the parent object, all the children will be hidden/shown at the same time.
# - In the Logic Editor (Shift+F2) open the side panel (N) to toggle on or off the HIDE property.
# - If it's ON, the objects are visible and will hide when the delay frame is hit.
# - If it's OFF, the objects are hidden and will show when the delay frame is hit.
# - You can create another point of show/hide toggle by adding a new Delay sensor and dragging the connection to the HIDE property toggle like so: https://i.imgur.com/0mYflPy.png

import bpy
sensor_delay=145 #frames

ops=bpy.ops
context=bpy.context

# Cache shortcuts.
ops_mesh = ops.mesh
ops_object = ops.object
ops_logic = ops.logic
scene = context.scene

selected = bpy.context.selected_objects

#First row of logic bricks

for obj in selected:
    bpy.context.scene.objects.active = obj

    current_object = context.active_object

    # Cache shortcut to cube's game engine.
    game_engine = current_object.game

    # Add property 'HIDE'.
    bpy.ops.object.game_property_new(type='BOOL', name="HIDE")
    game_engine.properties[-1].value=True

    # Add sensor.
    ops_logic.sensor_add(type='DELAY')
                         #name='Always')
                         #object=cube_name)
    sensor = game_engine.sensors[-1]
    sensor.delay=sensor_delay


    # Add controller.
    ops_logic.controller_add(type='LOGIC_AND')
                             #name='And')
                             #object=cube_name)
    controller = game_engine.controllers[-1]

    # Add actuator.
    ops_logic.actuator_add(type='PROPERTY')
                           #name='Action',
                           #object=cube_name)
    actuator = game_engine.actuators[-1]
    actuator.mode='TOGGLE'
    actuator.property=game_engine.properties.items()[-1][0]

    # Link logic together.
    sensor.link(controller)
    actuator.link(controller)

#Second row of logic bricks

for obj in selected:
    bpy.context.scene.objects.active = obj

    current_object = context.active_object

    # Cache shortcut to cube's game engine.
    game_engine = current_object.game

    # Add sensor.
    ops_logic.sensor_add(type='PROPERTY')
                         #name='Always')
                         #object=cube_name)
    sensor = game_engine.sensors[-1]
    sensor.use_pulse_true_level=True
    sensor.property=game_engine.properties.items()[-1][0]
    sensor.value='True'

    # Add controller.
    ops_logic.controller_add(type='LOGIC_AND')
                             #name='And')
                             #object=cube_name)
    controller = game_engine.controllers[-1]

    # Add actuator.
    ops_logic.actuator_add(type='VISIBILITY')
                           #name='Action',
                           #object=cube_name)
    actuator = game_engine.actuators[-1]
    #actuator.use_visible=True
    actuator.apply_to_children=True

    # Link logic together.
    sensor.link(controller)
    actuator.link(controller)

#Third row of logic bricks

for obj in selected:
    bpy.context.scene.objects.active = obj

    current_object = context.active_object

    # Cache shortcut to cube's game engine.
    game_engine = current_object.game

    # Add sensor.
    ops_logic.sensor_add(type='PROPERTY')
                         #name='Always')
                         #object=cube_name)
    sensor = game_engine.sensors[-1]
    sensor.use_pulse_true_level=True
    sensor.property=game_engine.properties.items()[-1][0]
    sensor.value='False'


    # Add controller.
    ops_logic.controller_add(type='LOGIC_AND')
                             #name='And')
                             #object=cube_name)
    controller = game_engine.controllers[-1]

    # Add actuator.
    ops_logic.actuator_add(type='VISIBILITY')
                           #name='Action',
                           #object=cube_name)
    actuator = game_engine.actuators[-1]
    actuator.use_visible=False
    actuator.use_occlusion=True
    actuator.apply_to_children=True

    # Link logic together.
    sensor.link(controller)
    actuator.link(controller)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .