Logic Editor Python API: link/unlink logics through python

After initial talk with Matt (awhile ago) we realzed that rna_api would fit well for this instead of an operator.

The next step would be to move the current UI code to use the rna funcs instead.
Note: it takes the s/c/a as argument, not its name. (e.g. cont.link(actuator=act) )

Sample code to link all the logic bricks between each other:
ob = bpy.context.object
for cont in ob.game.controllers:
    for sens in ob.game.sensors:
        cont.link(sensor=sens)
    for act in ob.game.actuators:
        cont.link(actuator=act)

For a script to create bricks, link bricks, unlink bricks and remove them:
http://www.pasteall.org/14266
This commit is contained in:
Dalai Felinto
2010-07-10 21:15:10 +00:00
parent e531f3736d
commit 96a7e478b6
10 changed files with 296 additions and 32 deletions

View File

@@ -37,6 +37,9 @@ struct Object;
struct bController;
struct bActuator;
void link_logicbricks(void **poin, void ***ppoin, short *tot, short size);
void unlink_logicbricks(void **poin, void ***ppoin, short *tot);
void unlink_controller(struct bController *cont);
void unlink_controllers(struct ListBase *lb);
void free_controller(struct bController *cont);

View File

@@ -45,6 +45,7 @@
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_library.h"
#include "BKE_sca.h"
/* ******************* SENSORS ************************ */
@@ -189,26 +190,13 @@ void unlink_controller(bController *cont)
{
bSensor *sens;
Object *ob;
int a, removed;
/* check for controller pointers in sensors */
ob= G.main->object.first;
while(ob) {
sens= ob->sensors.first;
while(sens) {
removed= 0;
for(a=0; a<sens->totlinks; a++) {
if(removed) (sens->links)[a-1] = (sens->links)[a];
else if((sens->links)[a] == cont) removed= 1;
}
if(removed) {
sens->totlinks--;
if(sens->totlinks==0) {
MEM_freeN(sens->links);
sens->links= NULL;
}
}
unlink_logicbricks((void **)&cont, (void ***)&(sens->links), &sens->totlinks);
sens= sens->next;
}
ob= ob->id.next;
@@ -313,26 +301,13 @@ void unlink_actuator(bActuator *act)
{
bController *cont;
Object *ob;
int a, removed;
/* check for actuator pointers in controllers */
ob= G.main->object.first;
while(ob) {
cont= ob->controllers.first;
while(cont) {
removed= 0;
for(a=0; a<cont->totlinks; a++) {
if(removed) (cont->links)[a-1] = (cont->links)[a];
else if((cont->links)[a] == act) removed= 1;
}
if(removed) {
cont->totlinks--;
if(cont->totlinks==0) {
MEM_freeN(cont->links);
cont->links= NULL;
}
}
unlink_logicbricks((void **)&act, (void ***)&(cont->links), &cont->totlinks);
cont= cont->next;
}
ob= ob->id.next;
@@ -512,7 +487,6 @@ bActuator *new_actuator(int type)
}
/* ******************** GENERAL ******************* */
void clear_sca_new_poins_ob(Object *ob)
{
bSensor *sens;
@@ -807,3 +781,56 @@ void sca_move_actuator(bActuator *act_to_move, Object *ob, int move_up)
}
}
}
void link_logicbricks(void **poin, void ***ppoin, short *tot, short size)
{
void **old_links= NULL;
int ibrick;
/* check if the bricks are already linked */
for (ibrick=0; ibrick < *tot; ibrick++) {
if ((*ppoin)[ibrick] == *poin)
return;
}
if (*ppoin) {
old_links= *ppoin;
(*tot) ++;
*ppoin = MEM_callocN((*tot)*size, "new link");
for (ibrick=0; ibrick < *tot - 1; ibrick++) {
(*ppoin)[ibrick] = old_links[ibrick];
}
(*ppoin)[ibrick] = *poin;
if(old_links) MEM_freeN(old_links);
}
else {
(*tot) = 1;
*ppoin = MEM_callocN((*tot)*size, "new link");
(*ppoin)[0] = *poin;
}
}
void unlink_logicbricks(void **poin, void ***ppoin, short *tot)
{
int ibrick, removed;
removed= 0;
for (ibrick=0; ibrick < *tot; ibrick++) {
if(removed) (*ppoin)[ibrick - removed] = (*ppoin)[ibrick];
else if((*ppoin)[ibrick] == *poin) removed = 1;
}
if (removed) {
(*tot) --;
if(*tot == 0) {
MEM_freeN(*ppoin);
(*ppoin)= NULL;
}
return;
}
}

View File

@@ -2237,7 +2237,7 @@ RNAProcessItem PROCESS_ITEMS[]= {
{"rna_action.c", "rna_action_api.c", RNA_def_action},
{"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
{"rna_animviz.c", NULL, RNA_def_animviz},
{"rna_actuator.c", NULL, RNA_def_actuator},
{"rna_actuator.c", "rna_actuator_api.c", RNA_def_actuator},
{"rna_armature.c", "rna_armature_api.c", RNA_def_armature},
{"rna_boid.c", NULL, RNA_def_boid},
{"rna_brush.c", NULL, RNA_def_brush},
@@ -2246,7 +2246,7 @@ RNAProcessItem PROCESS_ITEMS[]= {
{"rna_color.c", NULL, RNA_def_color},
{"rna_constraint.c", NULL, RNA_def_constraint},
{"rna_context.c", NULL, RNA_def_context},
{"rna_controller.c", NULL, RNA_def_controller},
{"rna_controller.c", "rna_controller_api.c", RNA_def_controller},
{"rna_curve.c", NULL, RNA_def_curve},
{"rna_fcurve.c", "rna_fcurve_api.c", RNA_def_fcurve},
{"rna_fluidsim.c", NULL, RNA_def_fluidsim},
@@ -2273,7 +2273,7 @@ RNAProcessItem PROCESS_ITEMS[]= {
{"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
{"rna_screen.c", NULL, RNA_def_screen},
{"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
{"rna_sensor.c", NULL, RNA_def_sensor},
{"rna_sensor.c", "rna_sensor_api.c", RNA_def_sensor},
{"rna_sequencer.c", "rna_sequencer_api.c", RNA_def_sequencer},
{"rna_smoke.c", NULL, RNA_def_smoke},
{"rna_space.c", NULL, RNA_def_space},

View File

@@ -486,6 +486,8 @@ void rna_def_actuator(BlenderRNA *brna)
RNA_def_property_boolean_sdna(prop, NULL, "flag", ACT_SHOW);
RNA_def_property_ui_text(prop, "Expanded", "Set actuator expanded in the user interface");
RNA_def_property_ui_icon(prop, ICON_TRIA_RIGHT, 1);
RNA_api_actuator(srna);
}
static void rna_def_action_actuator(BlenderRNA *brna)

View File

@@ -0,0 +1,72 @@
/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2010 Blender Foundation.
* All rights reserved.
*
*
* Contributor(s):
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <stdlib.h>
#include <stdio.h>
#include "WM_types.h"
#include "RNA_define.h"
#ifdef RNA_RUNTIME
#include "BKE_sca.h"
#include "DNA_controller_types.h"
#include "DNA_actuator_types.h"
static void rna_Actuator_link(bActuator *act, bController *cont)
{
link_logicbricks((void **)&act, (void ***)&(cont->links), &cont->totlinks, sizeof(bActuator *));
}
static void rna_Actuator_unlink(bActuator *act, bController *cont)
{
unlink_logicbricks((void **)&act, (void ***)&(cont->links), &cont->totlinks);
}
#else
void RNA_api_actuator(StructRNA *srna)
{
FunctionRNA *func;
PropertyRNA *parm;
func= RNA_def_function(srna, "link", "rna_Actuator_link");
RNA_def_function_ui_description(func, "Link the actuator to a controller.");
parm= RNA_def_pointer(func, "controller", "Controller", "", "Controller to link to.");
RNA_def_property_flag(parm, PROP_REQUIRED);
RNA_def_property_update(parm, NC_LOGIC, NULL);
func= RNA_def_function(srna, "unlink", "rna_Actuator_unlink");
RNA_def_function_ui_description(func, "Unlink the actuator from a controller.");
parm= RNA_def_pointer(func, "controller", "Controller", "", "Controller to unlink from.");
RNA_def_property_flag(parm, PROP_REQUIRED);
RNA_def_property_update(parm, NC_LOGIC, NULL);
}
#endif

View File

@@ -156,6 +156,8 @@ void RNA_def_controller(BlenderRNA *brna)
RNA_def_struct_refine_func(srna, "rna_Controller_refine");
RNA_def_struct_ui_text(srna, "Controller", "Game engine logic brick to process events, connecting sensors to actuators");
RNA_api_controller(srna);
prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
RNA_def_property_ui_text(prop, "Name", "");
RNA_def_struct_name_property(srna, prop);

View File

@@ -0,0 +1,81 @@
/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2010 Blender Foundation.
* All rights reserved.
*
*
* Contributor(s):
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <stdlib.h>
#include <stdio.h>
#include "WM_types.h"
#include "RNA_define.h"
#ifdef RNA_RUNTIME
#include "BKE_sca.h"
#include "DNA_sensor_types.h"
#include "DNA_controller_types.h"
#include "DNA_actuator_types.h"
static void rna_Controller_link(bController *cont, bSensor *sens, bActuator *act)
{
if(sens)
link_logicbricks((void **)&cont, (void ***)&(sens->links), &sens->totlinks, sizeof(bController *));
if(act)
link_logicbricks((void **)&act, (void ***)&(cont->links), &cont->totlinks, sizeof(bActuator *));
}
static void rna_Controller_unlink(bController *cont, bSensor *sens, bActuator *act)
{
if(sens)
unlink_logicbricks((void **)&cont, (void ***)&(sens->links), &sens->totlinks);
if(act)
unlink_logicbricks((void **)&act, (void ***)&(cont->links), &cont->totlinks);
}
#else
void RNA_api_controller(StructRNA *srna)
{
FunctionRNA *func;
PropertyRNA *parm;
func= RNA_def_function(srna, "link", "rna_Controller_link");
RNA_def_function_ui_description(func, "Link the controller with a sensor/actuator.");
parm= RNA_def_pointer(func, "sensor", "Sensor", "", "Sensor to link the controller to.");
RNA_def_property_update(parm, NC_LOGIC, NULL);
parm= RNA_def_pointer(func, "actuator", "Actuator", "", "Actuator to link the controller to.");
RNA_def_property_update(parm, NC_LOGIC, NULL);
func= RNA_def_function(srna, "unlink", "rna_Controller_unlink");
RNA_def_function_ui_description(func, "Unlink the controller from a sensor/actuator.");
parm= RNA_def_pointer(func, "sensor", "Sensor", "", "Sensor to unlink the controller from.");
RNA_def_property_update(parm, NC_LOGIC, NULL);
parm= RNA_def_pointer(func, "actuator", "Actuator", "", "Actuator to unlink the controller from.");
RNA_def_property_update(parm, NC_LOGIC, NULL);
}
#endif

View File

@@ -237,6 +237,9 @@ void RNA_api_sequence_strip(StructRNA *srna);
void RNA_api_text(struct StructRNA *srna);
void RNA_api_ui_layout(struct StructRNA *srna);
void RNA_api_wm(struct StructRNA *srna);
void RNA_api_sensor(struct StructRNA *srna);
void RNA_api_controller(struct StructRNA *srna);
void RNA_api_actuator(struct StructRNA *srna);
/* main collection functions */
void RNA_def_main_cameras(BlenderRNA *brna, PropertyRNA *cprop);

View File

@@ -302,6 +302,8 @@ static void rna_def_sensor(BlenderRNA *brna)
RNA_def_property_boolean_funcs(prop, NULL, "rna_Sensor_tap_set");
RNA_def_property_ui_text(prop, "Tap", "Trigger controllers only for an instant, even while the sensor remains true");
RNA_def_property_update(prop, NC_LOGIC, NULL);
RNA_api_sensor(srna);
}
static void rna_def_always_sensor(BlenderRNA *brna)

View File

@@ -0,0 +1,72 @@
/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2010 Blender Foundation.
* All rights reserved.
*
*
* Contributor(s):
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <stdlib.h>
#include <stdio.h>
#include "WM_types.h"
#include "RNA_define.h"
#ifdef RNA_RUNTIME
#include "BKE_sca.h"
#include "DNA_sensor_types.h"
#include "DNA_controller_types.h"
static void rna_Sensor_link(bSensor *sens, bController *cont)
{
link_logicbricks((void **)&cont, (void ***)&(sens->links), &sens->totlinks, sizeof(bController *));
}
static void rna_Sensor_unlink(bSensor *sens, bController *cont)
{
unlink_logicbricks((void **)&cont, (void ***)&(sens->links), &sens->totlinks);
}
#else
void RNA_api_sensor(StructRNA *srna)
{
FunctionRNA *func;
PropertyRNA *parm;
func= RNA_def_function(srna, "link", "rna_Sensor_link");
RNA_def_function_ui_description(func, "Link the sensor to a controller.");
parm= RNA_def_pointer(func, "controller", "Controller", "", "Controller to link to.");
RNA_def_property_flag(parm, PROP_REQUIRED);
RNA_def_property_update(parm, NC_LOGIC, NULL);
func= RNA_def_function(srna, "unlink", "rna_Sensor_unlink");
RNA_def_function_ui_description(func, "Unlink the sensor from a controller.");
parm= RNA_def_pointer(func, "controller", "Controller", "", "Controller to unlink from.");
RNA_def_property_flag(parm, PROP_REQUIRED);
RNA_def_property_update(parm, NC_LOGIC, NULL);
}
#endif