[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [21136] branches/soc-2009-kazanbas: - added API functions:

Arystanbek Dyussenov arystan.d at gmail.com
Wed Jun 24 21:23:34 CEST 2009


Revision: 21136
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21136
Author:   kazanbas
Date:     2009-06-24 21:23:34 +0200 (Wed, 24 Jun 2009)

Log Message:
-----------
- added API functions:
  * Main.remove_object
  * Scene.add_object
  * Scene.remove_object
  * Object.convert_to_triface
  * Object.create_preview_mesh
  
- a small tweak in set_mesh (blenkernel/inter/mesh.c) to make it work on objects having data == NULL 

Modified Paths:
--------------
    branches/soc-2009-kazanbas/release/scripts/export_obj-2.5.py
    branches/soc-2009-kazanbas/source/blender/blenkernel/intern/mesh.c
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/makesrna.c
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_main_api.c
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_mesh_api.c
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_object_api.c
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_scene.c

Modified: branches/soc-2009-kazanbas/release/scripts/export_obj-2.5.py
===================================================================
--- branches/soc-2009-kazanbas/release/scripts/export_obj-2.5.py	2009-06-24 18:39:00 UTC (rev 21135)
+++ branches/soc-2009-kazanbas/release/scripts/export_obj-2.5.py	2009-06-24 19:23:34 UTC (rev 21136)
@@ -307,7 +307,8 @@
 	temp_mesh_name = '~tmp-mesh'
 
 	time1 = sys.time()
-	scn = Scene.GetCurrent()
+# 	scn = Scene.GetCurrent()
+	scene = context.scene
 
 	file = open(filename, "w")
 	
@@ -383,16 +384,16 @@
 			if ob.type != 'MESH':
 				continue
 
-			me = ob.data
-
 			# XXX
 # 			if EXPORT_UV:
 # 				faceuv= me.faceUV
 # 			else:
 # 				faceuv = False
 
-			convert_to_tri = False
+			me = ob.create_render_mesh()
 
+			newob = ob
+
 			# We have a valid mesh
 			if EXPORT_TRI and me.faces:
 				# Add a dummy object to it.
@@ -403,7 +404,10 @@
 						has_quads = True
 						break
 				
-				convert_to_tri = has_quads
+				if has_quads:
+					newob = bpy.data.add_object('MESH', 'temp_object')
+					scene.add_object(newob)
+					newob.convert_to_triface(scene)
 # 					oldmode = Mesh.Mode()
 # 					Mesh.Mode(Mesh.SelectModes['FACE'])
 					
@@ -418,8 +422,6 @@
 			if EXPORT_ROTX90:
 				ob_mat *= mat_xrot90
 
-			me = ob.create_render_mesh(True, ob_mat, convert_to_tri)
-
 			# Make our own list so it can be sorted to reduce context switching
 			faces = [ f for f in me.faces ]
 			
@@ -429,6 +431,10 @@
 				edges = []
 			
 			if not (len(faces)+len(edges)+len(me.verts)): # Make sure there is somthing to write
+
+				if newob != ob:
+					scene.remove_object(newob)
+
 				continue # dont bother with this mesh.
 
 			# done above ^

Modified: branches/soc-2009-kazanbas/source/blender/blenkernel/intern/mesh.c
===================================================================
--- branches/soc-2009-kazanbas/source/blender/blenkernel/intern/mesh.c	2009-06-24 18:39:00 UTC (rev 21135)
+++ branches/soc-2009-kazanbas/source/blender/blenkernel/intern/mesh.c	2009-06-24 19:23:34 UTC (rev 21136)
@@ -542,7 +542,8 @@
 	
 	if(ob->type==OB_MESH) {
 		old= ob->data;
-		old->id.us--;
+		if (old) /* to make set_mesh work on objects created with add_only_object, i.e. having ob->data == NULL */
+			old->id.us--;
 		ob->data= me;
 		id_us_plus((ID *)me);
 	}

Modified: branches/soc-2009-kazanbas/source/blender/makesrna/intern/makesrna.c
===================================================================
--- branches/soc-2009-kazanbas/source/blender/makesrna/intern/makesrna.c	2009-06-24 18:39:00 UTC (rev 21135)
+++ branches/soc-2009-kazanbas/source/blender/makesrna/intern/makesrna.c	2009-06-24 19:23:34 UTC (rev 21136)
@@ -1853,7 +1853,7 @@
 	{"rna_particle.c", NULL, RNA_def_particle},
 	{"rna_pose.c", NULL, RNA_def_pose},
 	{"rna_property.c", NULL, RNA_def_gameproperty},
-	{"rna_scene.c", NULL, RNA_def_scene},
+	{"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
 	{"rna_screen.c", NULL, RNA_def_screen},
 	{"rna_scriptlink.c", NULL, RNA_def_scriptlink},
 	{"rna_sensor.c", NULL, RNA_def_sensor},

Modified: branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_main_api.c
===================================================================
--- branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_main_api.c	2009-06-24 18:39:00 UTC (rev 21135)
+++ branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_main_api.c	2009-06-24 19:23:34 UTC (rev 21136)
@@ -62,9 +62,31 @@
 
 static Object* rna_Main_add_object(Main *main, int type, char *name)
 {
-	return add_only_object(type, name);
+	Object *ob= add_only_object(type, name);
+	ob->id.us--;
+	return ob;
 }
 
+/*
+  WARNING: the following example shows when this function should not be called
+
+  ob = bpy.data.add_object()
+  scene.add_object(ob)
+
+  # ob is freed here
+  scene.remove_object(ob)
+
+  # don't do this since ob is already freed!
+  bpy.data.remove_object(ob)
+*/
+static void rna_Main_remove_object(Main *main, ReportList *reports, Object *ob)
+{
+	if(ob->id.us == 0)
+		free_libblock(&main->object, ob);
+	else
+		BKE_report(reports, RPT_ERROR, "Object must have zero users to be removed.");
+}
+
 #else
 
 void RNA_api_main(StructRNA *srna)
@@ -89,13 +111,19 @@
 
 	func= RNA_def_function(srna, "add_object", "rna_Main_add_object");
 	RNA_def_function_ui_description(func, "Add a new object.");
-	parm= RNA_def_enum(func, "type", object_type_items, 0, "Type", "Type of Object.");
+	parm= RNA_def_enum(func, "type", object_type_items, 0, "", "Type of Object.");
 	RNA_def_property_flag(parm, PROP_REQUIRED);
 	parm= RNA_def_string(func, "name", "Object", 0, "", "New name for the datablock.");
 	RNA_def_property_flag(parm, PROP_REQUIRED);
 	parm= RNA_def_pointer(func, "object", "Object", "", "New object.");
 	RNA_def_function_return(func, parm);
 
+	func= RNA_def_function(srna, "remove_object", "rna_Main_remove_object");
+	RNA_def_function_flag(func, FUNC_USE_REPORTS);
+	RNA_def_function_ui_description(func, "Remove an object if it has zero users.");
+	parm= RNA_def_pointer(func, "object", "Object", "", "Object to remove.");
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+
 	func= RNA_def_function(srna, "add_mesh", "rna_Main_add_mesh");
 	RNA_def_function_ui_description(func, "Add a new mesh.");
 	parm= RNA_def_string(func, "name", "Mesh", 0, "", "New name for the datablock.");
@@ -108,7 +136,6 @@
 	RNA_def_function_ui_description(func, "Remove a mesh if it has zero users.");
 	parm= RNA_def_pointer(func, "mesh", "Mesh", "", "Mesh to remove.");
 	RNA_def_property_flag(parm, PROP_REQUIRED);
-
 }
 
 #endif

Modified: branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_mesh_api.c
===================================================================
--- branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_mesh_api.c	2009-06-24 18:39:00 UTC (rev 21135)
+++ branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_mesh_api.c	2009-06-24 19:23:34 UTC (rev 21136)
@@ -36,6 +36,7 @@
 
 #include "BKE_customdata.h"
 #include "BKE_DerivedMesh.h"
+#include "BLI_arithb.h"
 
 #include "DNA_mesh_types.h"
 #include "DNA_scene_types.h"
@@ -60,7 +61,7 @@
 	int i;
 	MVert *mvert= me->mvert;
 
-	for(i= 0; i < mesh->totvert; i++, mvert++) {
+	for(i= 0; i < me->totvert; i++, mvert++) {
 		Mat4MulVecfl(mat, mvert->co);
 	}
 }

Modified: branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_object_api.c
===================================================================
--- branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_object_api.c	2009-06-24 18:39:00 UTC (rev 21135)
+++ branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_object_api.c	2009-06-24 19:23:34 UTC (rev 21136)
@@ -28,6 +28,8 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
+#include <time.h>
 
 #include "RNA_define.h"
 #include "RNA_types.h"
@@ -40,6 +42,7 @@
 #include "BKE_DerivedMesh.h"
 #include "BKE_anim.h"
 #include "BKE_report.h"
+#include "BKE_depsgraph.h"
 
 #include "DNA_mesh_types.h"
 #include "DNA_scene_types.h"
@@ -47,15 +50,17 @@
 
 #include "BLI_arithb.h"
 
+#include "BLO_sys_types.h" /* needed for intptr_t used in ED_mesh.h */
+
+#include "ED_mesh.h"
+
 /* copied from init_render_mesh (render code) */
-static Mesh *rna_Object_create_render_mesh(Object *ob, bContext *C, ReportList *reports, int apply_matrix, float *matrix)
+static Mesh *create_mesh(Object *ob, bContext *C, ReportList *reports, int render_mesh)
 {
 	CustomDataMask mask = CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL;
 	DerivedMesh *dm;
 	Mesh *me;
 	Scene *sce;
-	int a;
-	MVert *mvert;
 
 	sce= CTX_data_scene(C);
 	
@@ -65,7 +70,7 @@
 		return NULL;
 	}
 	
-	dm= mesh_create_derived_render(sce, ob, mask);
+	dm= render_mesh ? mesh_create_derived_render(sce, ob, mask) : mesh_create_derived_view(sce, ob, mask);
 
 	if(!dm) {
 		/* TODO: report */
@@ -77,21 +82,17 @@
 	DM_to_mesh(dm, me);
 	dm->release(dm);
 
-	if (apply_matrix) {
-		float *mat = (float*)ob->obmat;
+	return me;
+}
 
-		if (matrix) {
-			/* apply custom matrix */
-			mat = matrix;
-		}
+static Mesh *rna_Object_create_render_mesh(Object *ob, bContext *C, ReportList *reports)
+{
+	return create_mesh(ob, C, reports, 1);
+}
 
-		/* is this really that simple? :) */
-		for(a= 0, mvert= me->mvert; a < me->totvert; a++, mvert++) {
-			Mat4MulVecfl(ob->obmat, mvert->co);
-		}
-	}
-
-	return me;
+static Mesh *rna_Object_create_preview_mesh(Object *ob, bContext *C, ReportList *reports)
+{
+	return create_mesh(ob, C, reports, 0);
 }
 
 /* When no longer needed, duplilist should be freed with Object.free_duplilist */
@@ -102,9 +103,9 @@
 		return;
 	}
 
-	/* free duplilist if a user forget to */
+	/* free duplilist if a user forgets to */
 	if (ob->duplilist) {
-		BKE_report(reports, RPT_WARNING, "%s.dupli_list has not been freed.", RNA_struct_identifier(&RNA_Object));
+		BKE_reportf(reports, RPT_WARNING, "%s.dupli_list has not been freed.", RNA_struct_identifier(&RNA_Object));
 
 		free_object_duplilist(ob->duplilist);
 		ob->duplilist= NULL;
@@ -117,15 +118,41 @@
 
 static void rna_Object_free_duplilist(Object *ob, ReportList *reports)
 {
-	PointerRNA obptr;
-	PropertyRNA *prop;
-
 	if (ob->duplilist) {
 		free_object_duplilist(ob->duplilist);
 		ob->duplilist= NULL;
 	}
 }
 
+static void rna_Object_convert_to_triface(Object *ob, bContext *C, ReportList *reports, Scene *sce)
+{
+	Mesh *me;
+	int ob_editing = CTX_data_edit_object(C) == ob;
+
+	if (ob->type != OB_MESH) {
+		BKE_report(reports, RPT_ERROR, "Object should be of type MESH.");
+		return;
+	}
+
+	me= (Mesh*)ob->data;
+
+	if (!ob_editing)
+		make_editMesh(sce, ob);
+
+	/* select all */
+	EM_set_flag_all(me->edit_mesh, SELECT);
+
+	convert_to_triface(me->edit_mesh, 0);
+
+	load_editMesh(sce, ob);
+
+	if (!ob_editing)
+		free_editMesh(me->edit_mesh);
+
+	DAG_object_flush_update(sce, ob, OB_RECALC_DATA);
+}
+
+
 #else
 
 void RNA_api_object(StructRNA *srna)
@@ -149,22 +176,30 @@
 		{0, NULL, 0, NULL, NULL}};
 
 	func= RNA_def_function(srna, "create_render_mesh", "rna_Object_create_render_mesh");
-	RNA_def_function_ui_description(func, "Create a Mesh datablock with all modifiers applied.");
+	RNA_def_function_ui_description(func, "Create a Mesh datablock with all modifiers applied for rendering.");
 	RNA_def_function_flag(func, FUNC_USE_CONTEXT|FUNC_USE_REPORTS);
-	parm= RNA_def_boolean(func, "apply_matrix", 0, "", "True if object matrix or custom matrix should be applied to geometry.");
-	RNA_def_property_clear_flag(parm, PROP_REQUIRED);

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list