[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [30190] trunk/blender/source/blender: Logic Editor Python API: link/unlink logics through python

Dalai Felinto dfelinto at gmail.com
Sat Jul 10 23:15:10 CEST 2010


Revision: 30190
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=30190
Author:   dfelinto
Date:     2010-07-10 23:15:10 +0200 (Sat, 10 Jul 2010)

Log Message:
-----------
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

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_sca.h
    trunk/blender/source/blender/blenkernel/intern/sca.c
    trunk/blender/source/blender/makesrna/intern/makesrna.c
    trunk/blender/source/blender/makesrna/intern/rna_actuator.c
    trunk/blender/source/blender/makesrna/intern/rna_controller.c
    trunk/blender/source/blender/makesrna/intern/rna_internal.h
    trunk/blender/source/blender/makesrna/intern/rna_sensor.c

Added Paths:
-----------
    trunk/blender/source/blender/makesrna/intern/rna_actuator_api.c
    trunk/blender/source/blender/makesrna/intern/rna_controller_api.c
    trunk/blender/source/blender/makesrna/intern/rna_sensor_api.c

Modified: trunk/blender/source/blender/blenkernel/BKE_sca.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_sca.h	2010-07-10 21:09:38 UTC (rev 30189)
+++ trunk/blender/source/blender/blenkernel/BKE_sca.h	2010-07-10 21:15:10 UTC (rev 30190)
@@ -37,6 +37,9 @@
 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);

Modified: trunk/blender/source/blender/blenkernel/intern/sca.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/sca.c	2010-07-10 21:09:38 UTC (rev 30189)
+++ trunk/blender/source/blender/blenkernel/intern/sca.c	2010-07-10 21:15:10 UTC (rev 30190)
@@ -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 @@
 {
 	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 @@
 {
 	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 @@
 }
 
 /* ******************** GENERAL ******************* */
-
 void clear_sca_new_poins_ob(Object *ob)
 {
 	bSensor *sens;
@@ -807,3 +781,56 @@
 		}
 	}
 }
+
+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;
+	}
+}

Modified: trunk/blender/source/blender/makesrna/intern/makesrna.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/makesrna.c	2010-07-10 21:09:38 UTC (rev 30189)
+++ trunk/blender/source/blender/makesrna/intern/makesrna.c	2010-07-10 21:15:10 UTC (rev 30190)
@@ -2237,7 +2237,7 @@
 	{"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 @@
 	{"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 @@
 	{"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},

Modified: trunk/blender/source/blender/makesrna/intern/rna_actuator.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_actuator.c	2010-07-10 21:09:38 UTC (rev 30189)
+++ trunk/blender/source/blender/makesrna/intern/rna_actuator.c	2010-07-10 21:15:10 UTC (rev 30190)
@@ -486,6 +486,8 @@
 	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)

Added: trunk/blender/source/blender/makesrna/intern/rna_actuator_api.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_actuator_api.c	                        (rev 0)
+++ trunk/blender/source/blender/makesrna/intern/rna_actuator_api.c	2010-07-10 21:15:10 UTC (rev 30190)
@@ -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
+


Property changes on: trunk/blender/source/blender/makesrna/intern/rna_actuator_api.c
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Modified: trunk/blender/source/blender/makesrna/intern/rna_controller.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_controller.c	2010-07-10 21:09:38 UTC (rev 30189)
+++ trunk/blender/source/blender/makesrna/intern/rna_controller.c	2010-07-10 21:15:10 UTC (rev 30190)
@@ -156,6 +156,8 @@
 	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);

Added: trunk/blender/source/blender/makesrna/intern/rna_controller_api.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_controller_api.c	                        (rev 0)
+++ trunk/blender/source/blender/makesrna/intern/rna_controller_api.c	2010-07-10 21:15:10 UTC (rev 30190)
@@ -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.
+ *

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list