ability to create polygons and loops from python (low level mesh creation access, not nice api but functional).

updated:
  mesh.from_pydata(vert, edge, polys)
... so it can take polygons rather then faces, this is much better suited for scripter access.
This commit is contained in:
Campbell Barton 2011-09-01 09:11:00 +00:00
parent 1db6d629a0
commit b62b9f16b8
4 changed files with 134 additions and 29 deletions

@ -330,7 +330,8 @@ class Mesh(bpy_types.ID):
"""
self.vertices.add(len(vertices))
self.edges.add(len(edges))
self.faces.add(len(faces))
self.loops.add(sum((len(f) for f in faces)))
self.polygons.add(len(faces))
vertices_flat = [f for v in vertices for f in v]
self.vertices.foreach_set("co", vertices_flat)
@ -340,19 +341,15 @@ class Mesh(bpy_types.ID):
self.edges.foreach_set("vertices", edges_flat)
del edges_flat
def treat_face(f):
if len(f) == 3:
if f[2] == 0:
return f[2], f[0], f[1], 0
else:
return f[0], f[1], f[2], 0
elif f[2] == 0 or f[3] == 0:
return f[2], f[3], f[0], f[1]
return f
faces_flat = [v for f in faces for v in treat_face(f)]
self.faces.foreach_set("vertices_raw", faces_flat)
del faces_flat
# this is different in bmesh
loop_index = 0
for i, p in enumerate(self.polygons):
f = faces[i]
loop_len = len(f)
p.loop_start = loop_index
p.loop_total = loop_len
p.vertices = f
loop_index += loop_len
@property
def edge_keys(self):

@ -280,8 +280,10 @@ struct BMFace *EDBM_findnearestface(struct ViewContext *vc, int *dist);
/* mesh_data.c */
// void ED_mesh_geometry_add(struct Mesh *mesh, struct ReportList *reports, int verts, int edges, int faces);
void ED_mesh_polys_add(struct Mesh *mesh, struct ReportList *reports, int count);
void ED_mesh_faces_add(struct Mesh *mesh, struct ReportList *reports, int count);
void ED_mesh_edges_add(struct Mesh *mesh, struct ReportList *reports, int count);
void ED_mesh_loops_add(struct Mesh *mesh, struct ReportList *reports, int count);
void ED_mesh_vertices_add(struct Mesh *mesh, struct ReportList *reports, int count);
void ED_mesh_transform(struct Mesh *me, float *mat);

@ -75,7 +75,6 @@
#include "mesh_intern.h"
#define GET_CD_DATA(me, data) (me->edit_btmesh ? &me->edit_btmesh->bm->data : &me->data)
static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *layer)
{
Mesh *me = ob->data;
@ -100,6 +99,7 @@ static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *la
(to detect if we're deleting the active layer or not), then use the active
layer data pointer to find where the active layer has ended up.
this is necassary because the deletion functions only support deleting the active
layer. */
actlayerdata = data->layers[CustomData_get_active_layer_index(data, type)].data;
@ -130,10 +130,12 @@ static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *la
}
}
/* set index */
CustomData_set_layer_active(data, type, actindex);
}
if (rndlayerdata != layerdata) {
/* find index */
rndindex = CustomData_get_layer_index(data, type);
@ -144,10 +146,12 @@ static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *la
}
}
/* set index */
CustomData_set_layer_render(data, type, rndindex);
}
if (clonelayerdata != layerdata) {
/* find index */
cloneindex = CustomData_get_layer_index(data, type);
@ -158,10 +162,12 @@ static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *la
}
}
/* set index */
CustomData_set_layer_clone(data, type, cloneindex);
}
if (stencillayerdata != layerdata) {
/* find index */
stencilindex = CustomData_get_layer_index(data, type);
@ -172,6 +178,7 @@ static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *la
}
}
/* set index */
CustomData_set_layer_stencil(data, type, stencilindex);
}
@ -241,6 +248,7 @@ int ED_mesh_uv_texture_add(bContext *C, Mesh *me, const char *name, int active_s
CustomData_set_layer_active(&me->ldata, CD_MLOOPUV, layernum);
}
mesh_update_customdata_pointers(me);
}
@ -290,13 +298,11 @@ int ED_mesh_color_add(bContext *C, Scene *UNUSED(scene), Object *UNUSED(ob), Mes
BM_add_data_layer(em->bm, &em->bm->pdata, CD_MLOOPCOL);
CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPCOL, layernum);
if(layernum) /* copy data from active vertex color layer */
copy_editface_active_customdata(em, CD_MCOL, layernum);
if(active_set || layernum==0)
CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPCOL, layernum);
}
else {
layernum= CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
@ -367,6 +373,7 @@ void MESH_OT_uv_texture_add(wmOperatorType *ot)
ot->description= "Add UV texture layer";
ot->idname= "MESH_OT_uv_texture_add";
/* api callbacks */
ot->poll= layers_poll;
ot->exec= uv_texture_add_exec;
@ -386,16 +393,19 @@ static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
int exitmode= 0;
char name[32];
/* Check context */
if(base==NULL || base->object->type!=OB_MESH) {
BKE_report(op->reports, RPT_ERROR, "Not an Object or Mesh");
return OPERATOR_CANCELLED;
}
/* check input variables */
if(RNA_property_is_set(op->ptr, "filepath")) {
char path[FILE_MAX];
RNA_string_get(op->ptr, "filepath", path);
ima= BKE_add_image_file(path);
}
@ -404,11 +414,13 @@ static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
ima= (Image *)find_id("IM", name);
}
if(!ima) {
BKE_report(op->reports, RPT_ERROR, "Not an Image.");
return OPERATOR_CANCELLED;
}
/* put mesh in editmode */
obedit= base->object;
@ -417,10 +429,10 @@ static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
EDBM_MakeEditBMesh(scene->toolsettings, scene, obedit);
exitmode= 1;
}
if(me->edit_btmesh==NULL)
return OPERATOR_CANCELLED;
ED_uvedit_assign_image(scene, obedit, ima, NULL);
if(exitmode) {
@ -434,8 +446,10 @@ static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
if(v3d)
v3d->flag2 |= V3D_SOLID_TEX;
WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
return OPERATOR_FINISHED;
}
@ -446,13 +460,16 @@ void MESH_OT_drop_named_image(wmOperatorType *ot)
ot->description= "Assigns Image to active UV layer, or creates a UV layer";
ot->idname= "MESH_OT_drop_named_image";
/* api callbacks */
ot->poll= layers_poll;
ot->invoke= drop_named_image_invoke;
/* flags */
ot->flag= OPTYPE_UNDO;
/* properties */
RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Image name to assign.");
RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
@ -476,6 +493,7 @@ void MESH_OT_uv_texture_remove(wmOperatorType *ot)
ot->description= "Remove UV texture layer";
ot->idname= "MESH_OT_uv_texture_remove";
/* api callbacks */
ot->poll= layers_poll;
ot->exec= uv_texture_remove_exec;
@ -505,6 +523,7 @@ void MESH_OT_vertex_color_add(wmOperatorType *ot)
ot->description= "Add vertex color layer";
ot->idname= "MESH_OT_vertex_color_add";
/* api callbacks */
ot->poll= layers_poll;
ot->exec= vertex_color_add_exec;
@ -531,6 +550,7 @@ void MESH_OT_vertex_color_remove(wmOperatorType *ot)
ot->description= "Remove vertex color layer";
ot->idname= "MESH_OT_vertex_color_remove";
/* api callbacks */
ot->exec= vertex_color_remove_exec;
ot->poll= layers_poll;
@ -566,6 +586,7 @@ void MESH_OT_sticky_add(wmOperatorType *ot)
ot->description= "Add sticky UV texture layer";
ot->idname= "MESH_OT_sticky_add";
/* api callbacks */
ot->poll= layers_poll;
ot->exec= sticky_add_exec;
@ -598,6 +619,7 @@ void MESH_OT_sticky_remove(wmOperatorType *ot)
ot->description= "Remove sticky UV texture layer";
ot->idname= "MESH_OT_sticky_remove";
/* api callbacks */
ot->poll= layers_poll;
ot->exec= sticky_remove_exec;
@ -693,6 +715,8 @@ static void mesh_add_edges(Mesh *mesh, int len)
mesh->totedge= totedge;
}
static void mesh_add_faces(Mesh *mesh, int len)
{
CustomData fdata;
@ -723,6 +747,61 @@ static void mesh_add_faces(Mesh *mesh, int len)
mesh->totface= totface;
}
static void mesh_add_loops(Mesh *mesh, int len)
{
CustomData ldata;
MLoop *mloop;
int i, totloop;
if(len == 0)
return;
totloop= mesh->totloop + len; /* new face count */
/* update customdata */
CustomData_copy(&mesh->ldata, &ldata, CD_MASK_MESH, CD_DEFAULT, totloop);
CustomData_copy_data(&mesh->ldata, &ldata, 0, 0, mesh->totloop);
if(!CustomData_has_layer(&ldata, CD_MLOOP))
CustomData_add_layer(&ldata, CD_MLOOP, CD_CALLOC, NULL, totloop);
CustomData_free(&mesh->ldata, mesh->totloop);
mesh->ldata= ldata;
mesh_update_customdata_pointers(mesh);
mesh->totloop= totloop;
}
static void mesh_add_polys(Mesh *mesh, int len)
{
CustomData pdata;
MPoly *mpoly;
int i, totpoly;
if(len == 0)
return;
totpoly= mesh->totpoly + len; /* new face count */
/* update customdata */
CustomData_copy(&mesh->pdata, &pdata, CD_MASK_MESH, CD_DEFAULT, totpoly);
CustomData_copy_data(&mesh->pdata, &pdata, 0, 0, mesh->totpoly);
if(!CustomData_has_layer(&pdata, CD_MPOLY))
CustomData_add_layer(&pdata, CD_MPOLY, CD_CALLOC, NULL, totpoly);
CustomData_free(&mesh->pdata, mesh->totpoly);
mesh->pdata= pdata;
mesh_update_customdata_pointers(mesh);
/* set default flags */
mpoly= &mesh->mpoly[mesh->totpoly];
for(i=0; i<len; i++, mpoly++)
mpoly->flag= ME_FACE_SEL;
mesh->totpoly= totpoly;
}
/*
void ED_mesh_geometry_add(Mesh *mesh, ReportList *reports, int verts, int edges, int faces)
{
@ -746,6 +825,7 @@ void ED_mesh_faces_add(Mesh *mesh, ReportList *reports, int count)
BKE_report(reports, RPT_ERROR, "Can't add faces in edit mode.");
return;
}
mesh_add_faces(mesh, count);
}
@ -759,6 +839,7 @@ void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
mesh_add_edges(mesh, count);
}
void ED_mesh_vertices_add(Mesh *mesh, ReportList *reports, int count)
{
if(mesh->edit_btmesh) {
@ -769,6 +850,26 @@ void ED_mesh_vertices_add(Mesh *mesh, ReportList *reports, int count)
mesh_add_verts(mesh, count);
}
void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
{
if(mesh->edit_btmesh) {
BKE_report(reports, RPT_ERROR, "Can't add loops in edit mode.");
return;
}
mesh_add_loops(mesh, count);
}
void ED_mesh_polys_add(Mesh *mesh, ReportList *reports, int count)
{
if(mesh->edit_btmesh) {
BKE_report(reports, RPT_ERROR, "Can't add polys in edit mode.");
return;
}
mesh_add_polys(mesh, count);
}
void ED_mesh_calc_normals(Mesh *mesh)
{
mesh_calc_normals(mesh->mvert, mesh->totvert, mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly, NULL, NULL, 0, NULL, NULL);

@ -1345,6 +1345,15 @@ static void rna_def_mpolygon(BlenderRNA *brna)
RNA_def_property_int_funcs(prop, "rna_MeshPoly_vertices_get", "rna_MeshPoly_vertices_set", NULL);
RNA_def_property_ui_text(prop, "Vertices", "Vertex indices");
/* these are both very low level access */
prop= RNA_def_property(srna, "loop_start", PROP_INT, PROP_UNSIGNED);
RNA_def_property_int_sdna(prop, NULL, "loopstart");
RNA_def_property_ui_text(prop, "Loop Start", "");
/* also low level */
prop= RNA_def_property(srna, "loop_total", PROP_INT, PROP_UNSIGNED);
RNA_def_property_int_sdna(prop, NULL, "totloop");
RNA_def_property_ui_text(prop, "Loop Total", "");
prop= RNA_def_property(srna, "material_index", PROP_INT, PROP_UNSIGNED);
RNA_def_property_int_sdna(prop, NULL, "mat_nr");
RNA_def_property_ui_text(prop, "Material Index", "");
@ -1785,12 +1794,10 @@ static void rna_def_mesh_loops(BlenderRNA *brna, PropertyRNA *cprop)
{
StructRNA *srna;
#if 0 // BMESH_TODO
PropertyRNA *prop;
FunctionRNA *func;
PropertyRNA *parm;
#endif
// PropertyRNA *parm;
RNA_def_property_srna(cprop, "MeshLoops");
srna= RNA_def_struct(brna, "MeshLoops", NULL);
@ -1801,11 +1808,11 @@ static void rna_def_mesh_loops(BlenderRNA *brna, PropertyRNA *cprop)
prop= RNA_def_property(srna, "active", PROP_INT, PROP_NONE);
RNA_def_property_int_sdna(prop, NULL, "act_face");
RNA_def_property_ui_text(prop, "Active Polygon", "The active polygon for this mesh");
func= RNA_def_function(srna, "add", "ED_mesh_polys_add");
RNA_def_function_flag(func, FUNC_USE_REPORTS);
parm= RNA_def_int(func, "count", 0, 0, INT_MAX, "Count", "Number of polygons to add.", 0, INT_MAX);
#endif
func= RNA_def_function(srna, "add", "ED_mesh_loops_add");
RNA_def_function_flag(func, FUNC_USE_REPORTS);
RNA_def_int(func, "count", 0, 0, INT_MAX, "Count", "Number of loops to add.", 0, INT_MAX);
}
/* mesh.polygons */
@ -1813,12 +1820,10 @@ static void rna_def_mesh_polygons(BlenderRNA *brna, PropertyRNA *cprop)
{
StructRNA *srna;
#if 0 // BMESH_TODO
PropertyRNA *prop;
// PropertyRNA *prop;
FunctionRNA *func;
PropertyRNA *parm;
#endif
RNA_def_property_srna(cprop, "MeshPolygons");
srna= RNA_def_struct(brna, "MeshPolygons", NULL);
@ -1829,11 +1834,11 @@ static void rna_def_mesh_polygons(BlenderRNA *brna, PropertyRNA *cprop)
prop= RNA_def_property(srna, "active", PROP_INT, PROP_NONE);
RNA_def_property_int_sdna(prop, NULL, "act_face");
RNA_def_property_ui_text(prop, "Active Polygon", "The active polygon for this mesh");
#endif
func= RNA_def_function(srna, "add", "ED_mesh_polys_add");
RNA_def_function_flag(func, FUNC_USE_REPORTS);
parm= RNA_def_int(func, "count", 0, 0, INT_MAX, "Count", "Number of polygons to add.", 0, INT_MAX);
#endif
}