To further enhance the script by incorporating complex logic configurations and a more interactive UI panel for user input, we can add additional logic brick types and parameters, allowing for greater configuration of game logic. We'll introduce options to select different sensor, controller, and actuator types, and allow configuration of commonly adjusted properties for these bricks.
Here's how you can do it:
Updated Script with Enhanced Logic Configurations and User Interface
bl_info = {
"name": "Advanced UPBGE Logic Bricks Generator",
"blender": (2, 80, 0),
"category": "Object",
"description": "Advanced generation and configuration of UPBGE logic bricks",
}
import bpy
class UPBGE_LogicSettings(bpy.types.PropertyGroup):
sensor_type: bpy.props.EnumProperty(
name="Sensor Type",
description="Choose the sensor type to add",
items=[
('ALWAYS', "Always", ""),
('KEYBOARD', "Keyboard", ""),
('MOUSE', "Mouse", ""),
('DELAY', "Delay", "")
],
default='ALWAYS'
)
controller_type: bpy.props.EnumProperty(
name="Controller Type",
description="Choose the controller type to add",
items=[
('LOGIC_AND', "AND", ""),
('LOGIC_OR', "OR", ""),
('EXPRESSION', "Expression", "")
],
default='LOGIC_AND'
)
actuator_type: bpy.props.EnumProperty(
name="Actuator Type",
description="Choose the actuator type to add",
items=[
('MOTION', "Motion", ""),
('SOUND', "Sound", ""),
('VISIBILITY', "Visibility", "")
],
default='MOTION'
)
move_x: bpy.props.FloatProperty(
name="Move X",
description="Movement on X axis",
default=0.0,
min=-10.0,
max=10.0,
)
move_y: bpy.props.FloatProperty(
name="Move Y",
description="Movement on Y axis",
default=0.0,
min=-10.0,
max=10.0,
)
move_z: bpy.props.FloatProperty(
name="Move Z",
description="Movement on Z axis",
default=0.0,
min=-10.0,
max=10.0,
)
keyboard_key: bpy.props.StringProperty(
name="Key",
description="Key to trigger the Keyboard sensor",
default="SPACE",
)
def create_logic_bricks(context):
settings = context.scene.upbge_logic_settings
obj = context.object
if obj is None:
return {'CANCELLED'}
# Create and configure Sensor
sensor = obj.game.sensors.new(name=f"{settings.sensor_type}Sensor", type=settings.sensor_type)
if settings.sensor_type == 'KEYBOARD':
sensor.key = settings.keyboard_key
# Create Controller
controller = obj.game.controllers.new(name=f"{settings.controller_type}Controller", type=settings.controller_type)
# Create and configure Actuator
actuator = obj.game.actuators.new(name=f"{settings.actuator_type}Actuator", type=settings.actuator_type)
if settings.actuator_type == 'MOTION':
actuator.dLoc = (settings.move_x, settings.move_y, settings.move_z)
# Link the logic bricks
sensor.link(controller)
controller.link(actuator)
return {'FINISHED'}
class OBJECT_OT_generate_upbge_logic_bricks(bpy.types.Operator):
"""Generate and configure UPBGE logic bricks for the active object"""
bl_idname = "object.generate_advanced_upbge_logic_bricks"
bl_label = "Generate Advanced UPBGE Logic Bricks"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
return create_logic_bricks(context)
class UPBGE_PT_adv_logic_panel(bpy.types.Panel):
"""Creates a panel in the 3D view context"""
bl_label = "Advanced UPBGE Logic Bricks"
bl_idname = "OBJECT_PT_advanced_upbge_logic_bricks"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'UPBGE'
def draw(self, context):
layout = self.layout
settings = context.scene.upbge_logic_settings
layout.prop(settings, "sensor_type", text="Sensor Type")
if settings.sensor_type == 'KEYBOARD':
layout.prop(settings, "keyboard_key")
layout.prop(settings, "controller_type", text="Controller Type")
layout.prop(settings, "actuator_type", text="Actuator Type")
if settings.actuator_type == 'MOTION':
layout.prop(settings, "move_x")
layout.prop(settings, "move_y")
layout.prop(settings, "move_z")
layout.operator(OBJECT_OT_generate_upbge_logic_bricks.bl_idname)
def register():
bpy.utils.register_class(UPBGE_LogicSettings)
bpy.utils.register_class(OBJECT_OT_generate_upbge_logic_bricks)
bpy.utils.register_class(UPBGE_PT_adv_logic_panel)
bpy.types.Scene.upbge_logic_settings = bpy.props.PointerProperty(type=UPBGE_LogicSettings)
def unregister():
bpy.utils.unregister_class(UPBGE_LogicSettings)
bpy.utils.unregister_class(OBJECT_OT_generate_upbge_logic_bricks)
bpy.utils.unregister_class(UPBGE_PT_adv_logic_panel)
del bpy.types.Scene.upbge_logic_settings
if __name__ == "__main__":
register()
Key Enhancements:
-
UI Flexibility:
- Panels allow users to choose from multiple sensor types (e.g., Keyboard, Mouse) and configure specific properties (e.g., key for Keyboard sensor).
- Allows selection of different controller types (AND, OR, Expression).
- Selection of actuator types with properties relevant to the type chosen (e.g., motion parameters for Motion actuators).
-
Improved Logic:
- Dynamically configures logic brick properties based on user input.
- Links sensors, controllers, and actuators automatically based on selections.
Installation and Use:
- Install in UPBGE: Ensure UPBGE is running, install the addon script, and activate it.
- Use the Tool: Access the panel in the 3D Viewport under the 'UPBGE' tab, configure the options as needed, and click the operator button to generate logic bricks.
This setup enhances the customization and utility of the logic bricks generated, allowing more intricate game logic setups, all configurable through a user-friendly panel in Blender’s interface.