Blender V2.61 - r43446

rna_lattice.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Blender Foundation (2008).
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00028 #include <stdlib.h>
00029 
00030 #include "RNA_define.h"
00031 
00032 #include "rna_internal.h"
00033 
00034 #include "DNA_curve_types.h"
00035 #include "DNA_key_types.h"
00036 #include "DNA_lattice_types.h"
00037 #include "DNA_meshdata_types.h"
00038 
00039 #ifdef RNA_RUNTIME
00040 
00041 #include "DNA_object_types.h"
00042 #include "DNA_scene_types.h"
00043 
00044 #include "BKE_depsgraph.h"
00045 #include "BKE_lattice.h"
00046 #include "BKE_main.h"
00047 #include "BKE_deform.h"
00048 
00049 #include "WM_api.h"
00050 #include "WM_types.h"
00051 
00052 static void rna_LatticePoint_co_get(PointerRNA *ptr, float *values)
00053 {
00054     Lattice *lt= (Lattice*)ptr->id.data;
00055     BPoint *bp= (BPoint*)ptr->data;
00056     int a= bp - lt->def;
00057     int x= a % lt->pntsu;
00058     int y= (a/lt->pntsu) % lt->pntsv;
00059     int z= (a/(lt->pntsu*lt->pntsv));
00060 
00061     values[0]= lt->fu + x*lt->du;
00062     values[1]= lt->fv + y*lt->dv;
00063     values[2]= lt->fw + z*lt->dw;
00064 }
00065 
00066 static void rna_LatticePoint_groups_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
00067 {
00068     Lattice *lt= (Lattice*)ptr->id.data;
00069 
00070     if(lt->dvert) {
00071         BPoint *bp= (BPoint*)ptr->data;
00072         MDeformVert *dvert= lt->dvert + (bp-lt->def);
00073 
00074         rna_iterator_array_begin(iter, (void*)dvert->dw, sizeof(MDeformWeight), dvert->totweight, 0, NULL);
00075     }
00076     else
00077         rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL);
00078 }
00079 
00080 static void rna_Lattice_points_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
00081 {
00082     Lattice *lt= (Lattice*)ptr->data;
00083     int tot= lt->pntsu*lt->pntsv*lt->pntsw;
00084 
00085     if(lt->editlatt && lt->editlatt->latt->def)
00086         rna_iterator_array_begin(iter, (void*)lt->editlatt->latt->def, sizeof(BPoint), tot, 0, NULL);
00087     else if(lt->def)
00088         rna_iterator_array_begin(iter, (void*)lt->def, sizeof(BPoint), tot, 0, NULL);
00089     else
00090         rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL);
00091 }
00092 
00093 static void rna_Lattice_update_data(Main *UNUSED(bmain), Scene *UNUSED(scene), PointerRNA *ptr)
00094 {
00095     ID *id= ptr->id.data;
00096 
00097     DAG_id_tag_update(id, 0);
00098     WM_main_add_notifier(NC_GEOM|ND_DATA, id);
00099 }
00100 
00101 static void rna_Lattice_update_size(Main *bmain, Scene *scene, PointerRNA *ptr)
00102 {
00103     Lattice *lt= ptr->id.data;
00104     Object *ob;
00105     int newu, newv, neww;
00106 
00107     /* we don't modify the actual pnts, but go through opnts instead */
00108     newu= (lt->opntsu > 0)? lt->opntsu: lt->pntsu;
00109     newv= (lt->opntsv > 0)? lt->opntsv: lt->pntsv;
00110     neww= (lt->opntsw > 0)? lt->opntsw: lt->pntsw;
00111 
00112     /* resizelattice needs an object, any object will have the same result */
00113     for(ob=bmain->object.first; ob; ob= ob->id.next) {
00114         if(ob->data == lt) {
00115             resizelattice(lt, newu, newv, neww, ob);
00116             if(lt->editlatt)
00117                 resizelattice(lt->editlatt->latt, newu, newv, neww, ob);
00118             break;
00119         }
00120     }
00121 
00122     /* otherwise without, means old points are not repositioned */
00123     if(!ob) {
00124         resizelattice(lt, newu, newv, neww, NULL);
00125         if(lt->editlatt)
00126             resizelattice(lt->editlatt->latt, newu, newv, neww, NULL);
00127     }
00128 
00129     rna_Lattice_update_data(bmain, scene, ptr);
00130 }
00131 
00132 static void rna_Lattice_use_outside_set(PointerRNA *ptr, int value)
00133 {
00134     Lattice *lt= ptr->data;
00135 
00136     if(value) lt->flag |= LT_OUTSIDE;
00137     else lt->flag &= ~LT_OUTSIDE;
00138 
00139     outside_lattice(lt);
00140 
00141     if(lt->editlatt) {
00142         if(value) lt->editlatt->latt->flag |= LT_OUTSIDE;
00143         else lt->editlatt->latt->flag &= ~LT_OUTSIDE;
00144 
00145         outside_lattice(lt->editlatt->latt);
00146     }
00147 }
00148 
00149 static int rna_Lattice_size_editable(PointerRNA *ptr)
00150 {
00151     Lattice *lt= (Lattice*)ptr->data;
00152 
00153     return lt->key == NULL;
00154 }
00155 
00156 static void rna_Lattice_points_u_set(PointerRNA *ptr, int value)
00157 {
00158     Lattice *lt= (Lattice*)ptr->data;
00159 
00160     lt->opntsu= CLAMPIS(value, 1, 64);
00161 }
00162 
00163 static void rna_Lattice_points_v_set(PointerRNA *ptr, int value)
00164 {
00165     Lattice *lt= (Lattice*)ptr->data;
00166 
00167     lt->opntsv= CLAMPIS(value, 1, 64);
00168 }
00169 
00170 static void rna_Lattice_points_w_set(PointerRNA *ptr, int value)
00171 {
00172     Lattice *lt= (Lattice*)ptr->data;
00173 
00174     lt->opntsw= CLAMPIS(value, 1, 64);
00175 }
00176 
00177 static void rna_Lattice_vg_name_set(PointerRNA *ptr, const char *value)
00178 {
00179     Lattice *lt= ptr->data;
00180     BLI_strncpy(lt->vgroup, value, sizeof(lt->vgroup));
00181 
00182     if(lt->editlatt) {
00183         BLI_strncpy(lt->editlatt->latt->vgroup, value, sizeof(lt->editlatt->latt->vgroup));
00184     }
00185 }
00186 
00187 /* annoying, but is a consequence of RNA structures... */
00188 static char *rna_LatticePoint_path(PointerRNA *ptr)
00189 {
00190     Lattice *lt= (Lattice*)ptr->id.data;
00191     void *point= ptr->data;
00192     BPoint *points = NULL;
00193     
00194     if (lt->editlatt && lt->editlatt->latt->def)
00195         points = lt->editlatt->latt->def;
00196     else
00197         points = lt->def;
00198     
00199     if (points && point) {
00200         int tot= lt->pntsu*lt->pntsv*lt->pntsw;
00201         
00202         /* only return index if in range */
00203         if ((point >= (void *)points) && (point < (void *)(points + tot))) {
00204             int pt_index = (int)((BPoint *)point - points);
00205             
00206             return BLI_sprintfN("points[%d]", pt_index);
00207         }
00208     }
00209 
00210     return BLI_strdup("");
00211 }
00212 
00213 
00214 #else
00215 
00216 static void rna_def_latticepoint(BlenderRNA *brna)
00217 {
00218     StructRNA *srna;
00219     PropertyRNA *prop;
00220 
00221     srna= RNA_def_struct(brna, "LatticePoint", NULL);
00222     RNA_def_struct_sdna(srna, "BPoint");
00223     RNA_def_struct_ui_text(srna, "LatticePoint", "Point in the lattice grid");
00224     RNA_def_struct_path_func(srna, "rna_LatticePoint_path");
00225 
00226     prop= RNA_def_property(srna, "co", PROP_FLOAT, PROP_TRANSLATION);
00227     RNA_def_property_array(prop, 3);
00228     RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00229     RNA_def_property_float_funcs(prop, "rna_LatticePoint_co_get", NULL, NULL);
00230     RNA_def_property_ui_text(prop, "Location", "");
00231 
00232     prop= RNA_def_property(srna, "co_deform", PROP_FLOAT, PROP_TRANSLATION);
00233     RNA_def_property_float_sdna(prop, NULL, "vec");
00234     RNA_def_property_array(prop, 3);
00235     RNA_def_property_ui_text(prop, "Deformed Location", "");
00236     RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
00237 
00238     prop= RNA_def_property(srna, "groups", PROP_COLLECTION, PROP_NONE);
00239     RNA_def_property_collection_funcs(prop, "rna_LatticePoint_groups_begin", "rna_iterator_array_next", "rna_iterator_array_end", "rna_iterator_array_get", NULL, NULL, NULL, NULL);
00240     RNA_def_property_struct_type(prop, "VertexGroupElement");
00241     RNA_def_property_ui_text(prop, "Groups", "Weights for the vertex groups this point is member of");
00242 }
00243 
00244 static void rna_def_lattice(BlenderRNA *brna)
00245 {
00246     StructRNA *srna;
00247     PropertyRNA *prop;
00248 
00249     static EnumPropertyItem prop_keyblock_type_items[] = {
00250         {KEY_LINEAR, "KEY_LINEAR", 0, "Linear", ""},
00251         {KEY_CARDINAL, "KEY_CARDINAL", 0, "Cardinal", ""},
00252         {KEY_BSPLINE, "KEY_BSPLINE", 0, "BSpline", ""},
00253         {0, NULL, 0, NULL, NULL}};
00254 
00255     srna= RNA_def_struct(brna, "Lattice", "ID");
00256     RNA_def_struct_ui_text(srna, "Lattice", "Lattice datablock defining a grid for deforming other objects");
00257     RNA_def_struct_ui_icon(srna, ICON_LATTICE_DATA);
00258 
00259     prop= RNA_def_property(srna, "points_u", PROP_INT, PROP_NONE);
00260     RNA_def_property_int_sdna(prop, NULL, "pntsu");
00261     RNA_def_property_int_funcs(prop, NULL, "rna_Lattice_points_u_set", NULL);
00262     RNA_def_property_range(prop, 1, 64);
00263     RNA_def_property_ui_text(prop, "U", "Point in U direction (can't be changed when there are shape keys)");
00264     RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
00265     RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
00266 
00267     prop= RNA_def_property(srna, "points_v", PROP_INT, PROP_NONE);
00268     RNA_def_property_int_sdna(prop, NULL, "pntsv");
00269     RNA_def_property_int_funcs(prop, NULL, "rna_Lattice_points_v_set", NULL);
00270     RNA_def_property_range(prop, 1, 64);
00271     RNA_def_property_ui_text(prop, "V", "Point in V direction (can't be changed when there are shape keys)");
00272     RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
00273     RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
00274 
00275     prop= RNA_def_property(srna, "points_w", PROP_INT, PROP_NONE);
00276     RNA_def_property_int_sdna(prop, NULL, "pntsw");
00277     RNA_def_property_int_funcs(prop, NULL, "rna_Lattice_points_w_set", NULL);
00278     RNA_def_property_range(prop, 1, 64);
00279     RNA_def_property_ui_text(prop, "W", "Point in W direction (can't be changed when there are shape keys)");
00280     RNA_def_property_update(prop, 0, "rna_Lattice_update_size");
00281     RNA_def_property_editable_func(prop, "rna_Lattice_size_editable");
00282 
00283     prop= RNA_def_property(srna, "interpolation_type_u", PROP_ENUM, PROP_NONE);
00284     RNA_def_property_enum_sdna(prop, NULL, "typeu");
00285     RNA_def_property_enum_items(prop, prop_keyblock_type_items);
00286     RNA_def_property_ui_text(prop, "Interpolation Type U", "");
00287     RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
00288 
00289     prop= RNA_def_property(srna, "interpolation_type_v", PROP_ENUM, PROP_NONE);
00290     RNA_def_property_enum_sdna(prop, NULL, "typev");
00291     RNA_def_property_enum_items(prop, prop_keyblock_type_items);
00292     RNA_def_property_ui_text(prop, "Interpolation Type V", "");
00293     RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
00294 
00295     prop= RNA_def_property(srna, "interpolation_type_w", PROP_ENUM, PROP_NONE);
00296     RNA_def_property_enum_sdna(prop, NULL, "typew");
00297     RNA_def_property_enum_items(prop, prop_keyblock_type_items);
00298     RNA_def_property_ui_text(prop, "Interpolation Type W", "");
00299     RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
00300 
00301     prop= RNA_def_property(srna, "use_outside", PROP_BOOLEAN, PROP_NONE);
00302     RNA_def_property_boolean_sdna(prop, NULL, "flag", LT_OUTSIDE);
00303     RNA_def_property_boolean_funcs(prop, NULL, "rna_Lattice_use_outside_set");
00304     RNA_def_property_ui_text(prop, "Outside", "Only draw, and take into account, the outer vertices");
00305     RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
00306     
00307     prop= RNA_def_property(srna, "vertex_group", PROP_STRING, PROP_NONE);
00308     RNA_def_property_string_sdna(prop, NULL, "vgroup");
00309     RNA_def_property_ui_text(prop, "Vertex Group", "Vertex group to apply the influence of the lattice");
00310     RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Lattice_vg_name_set");
00311     RNA_def_property_update(prop, 0, "rna_Lattice_update_data");
00312 
00313     prop= RNA_def_property(srna, "shape_keys", PROP_POINTER, PROP_NONE);
00314     RNA_def_property_pointer_sdna(prop, NULL, "key");
00315     RNA_def_property_ui_text(prop, "Shape Keys", "");
00316 
00317     prop= RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
00318     RNA_def_property_struct_type(prop, "LatticePoint");
00319     RNA_def_property_collection_funcs(prop, "rna_Lattice_points_begin", "rna_iterator_array_next", "rna_iterator_array_end", "rna_iterator_array_get", NULL, NULL, NULL, NULL);
00320     RNA_def_property_ui_text(prop, "Points", "Points of the lattice");
00321     
00322     /* pointers */
00323     rna_def_animdata_common(srna);
00324 }
00325 
00326 void RNA_def_lattice(BlenderRNA *brna)
00327 {
00328     rna_def_lattice(brna);
00329     rna_def_latticepoint(brna);
00330 }
00331 
00332 #endif
00333