Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / blender / editors / space_buttons / buttons_ops.c
index 1766113..057f35a 100644 (file)
@@ -35,8 +35,6 @@
 #include "DNA_group_types.h"
 #include "DNA_object_types.h"
 #include "DNA_material_types.h"
-#include "DNA_meshdata_types.h"
-#include "DNA_modifier_types.h"
 #include "DNA_node_types.h"
 #include "DNA_texture_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_space_types.h"
 #include "DNA_world_types.h"
 
-#include "BKE_bvhutils.h"
-#include "BKE_cdderivedmesh.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
-#include "BKE_DerivedMesh.h"
 #include "BKE_group.h"
 #include "BKE_font.h"
 #include "BKE_library.h"
 #include "BKE_main.h"
 #include "BKE_material.h"
-#include "BKE_modifier.h"
 #include "BKE_node.h"
 #include "BKE_particle.h"
-#include "BKE_pointcache.h"
 #include "BKE_scene.h"
 #include "BKE_texture.h"
 #include "BKE_utildefines.h"
 #include "BKE_world.h"
 
-#include "BLI_arithb.h"
 #include "BLI_editVert.h"
 #include "BLI_listbase.h"
 
@@ -75,7 +67,6 @@
 
 #include "ED_curve.h"
 #include "ED_mesh.h"
-#include "ED_particle.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
 #include "buttons_intern.h"    // own include
 
 
+/********************** group operators *********************/
+
+static int group_add_exec(bContext *C, wmOperator *op)
+{
+       Main *bmain= CTX_data_main(C);
+       Scene *scene= CTX_data_scene(C);
+       Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+       Base *base;
+       Group *group;
+       int value= RNA_enum_get(op->ptr, "group");
+
+       if(!ob)
+               return OPERATOR_CANCELLED;
+       
+       base= object_in_scene(ob, scene);
+       if(!base)
+               return OPERATOR_CANCELLED;
+       
+       if(value == -1)
+               group= add_group( "Group" );
+       else
+               group= BLI_findlink(&bmain->group, value);
+
+       if(group) {
+               add_to_group(group, ob);
+               ob->flag |= OB_FROMGROUP;
+               base->flag |= OB_FROMGROUP;
+       }
+
+       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
+       
+       return OPERATOR_FINISHED;
+}
+
+static EnumPropertyItem group_items[]= {
+       {-1, "ADD_NEW", 0, "Add New Group", ""},
+       {0, NULL, 0, NULL, NULL}};
+
+static EnumPropertyItem *group_itemf(bContext *C, PointerRNA *ptr, int *free)
+{      
+       EnumPropertyItem tmp = {0, "", 0, "", ""};
+       EnumPropertyItem *item= NULL;
+       Main *bmain;
+       Group *group;
+       int a, totitem= 0;
+       
+       if(!C) /* needed for docs */
+               return group_items;
+       
+       RNA_enum_items_add_value(&item, &totitem, group_items, -1);
+
+       bmain= CTX_data_main(C);
+       if(bmain->group.first)
+               RNA_enum_item_add_separator(&item, &totitem);
+
+       for(a=0, group=bmain->group.first; group; group=group->id.next, a++) {
+               tmp.value= a;
+               tmp.identifier= group->id.name+2;
+               tmp.name= group->id.name+2;
+               RNA_enum_item_add(&item, &totitem, &tmp);
+       }
+
+       RNA_enum_item_end(&item, &totitem);
+
+       *free= 1;
+
+       return item;
+}
+
+void OBJECT_OT_group_add(wmOperatorType *ot)
+{
+       PropertyRNA *prop;
+
+       /* identifiers */
+       ot->name= "Add Group";
+       ot->idname= "OBJECT_OT_group_add";
+       
+       /* api callbacks */
+       ot->exec= group_add_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+
+       /* properties */
+       prop= RNA_def_enum(ot->srna, "group", group_items, -1, "Group", "Group to add object to.");
+       RNA_def_enum_funcs(prop, group_itemf);
+}
+
+static int group_remove_exec(bContext *C, wmOperator *op)
+{
+       Scene *scene= CTX_data_scene(C);
+       Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+       Group *group= CTX_data_pointer_get_type(C, "group", &RNA_Group).data;
+       Base *base;
+
+       if(!ob || !group)
+               return OPERATOR_CANCELLED;
+
+       base= object_in_scene(ob, scene);
+       if(!base)
+               return OPERATOR_CANCELLED;
+
+       rem_from_group(group, ob);
+
+       if(find_group(ob, NULL) == NULL) {
+               ob->flag &= ~OB_FROMGROUP;
+               base->flag &= ~OB_FROMGROUP;
+       }
+
+       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
+       
+       return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_group_remove(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Remove Group";
+       ot->idname= "OBJECT_OT_group_remove";
+       
+       /* api callbacks */
+       ot->exec= group_remove_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
 /********************** material slot operators *********************/
 
 static int material_slot_add_exec(bContext *C, wmOperator *op)
@@ -142,6 +260,7 @@ void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
 
 static int material_slot_assign_exec(bContext *C, wmOperator *op)
 {
+       Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
 
        if(!ob)
@@ -179,8 +298,8 @@ static int material_slot_assign_exec(bContext *C, wmOperator *op)
                }
        }
 
-    DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
-    WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
+    DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
+    WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, ob);
        
        return OPERATOR_FINISHED;
 }
@@ -258,7 +377,7 @@ static int material_slot_de_select(bContext *C, int select)
                }
        }
 
-    WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
+    WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, ob);
 
        return OPERATOR_FINISHED;
 }
@@ -494,12 +613,6 @@ void OBJECT_OT_particle_system_remove(wmOperatorType *ot)
 
 /********************** new particle settings operator *********************/
 
-static int psys_poll(bContext *C)
-{
-       PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem);
-       return (ptr.data != NULL);
-}
-
 static int new_particle_settings_exec(bContext *C, wmOperator *op)
 {
        Scene *scene = CTX_data_scene(C);
@@ -529,7 +642,7 @@ static int new_particle_settings_exec(bContext *C, wmOperator *op)
        psys_check_boid_data(psys);
 
        DAG_scene_sort(scene);
-       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
+       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
 
        WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
        
@@ -544,7 +657,6 @@ void PARTICLE_OT_new(wmOperatorType *ot)
        
        /* api callbacks */
        ot->exec= new_particle_settings_exec;
-       ot->poll= psys_poll;
 
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
@@ -576,7 +688,7 @@ static int new_particle_target_exec(bContext *C, wmOperator *op)
        BLI_addtail(&psys->targets, pt);
 
        DAG_scene_sort(scene);
-       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
+       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
 
        WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
        
@@ -623,7 +735,7 @@ static int remove_particle_target_exec(bContext *C, wmOperator *op)
                pt->flag |= PTARGET_CURRENT;
 
        DAG_scene_sort(scene);
-       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
+       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
 
        WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
        
@@ -647,6 +759,7 @@ void PARTICLE_OT_remove_target(wmOperatorType *ot)
 
 static int target_move_up_exec(bContext *C, wmOperator *op)
 {
+       Scene *scene= CTX_data_scene(C);
        PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem);
        ParticleSystem *psys= ptr.data;
        Object *ob = ptr.id.data;
@@ -661,7 +774,7 @@ static int target_move_up_exec(bContext *C, wmOperator *op)
                        BLI_remlink(&psys->targets, pt);
                        BLI_insertlink(&psys->targets, pt->prev->prev, pt);
 
-                       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
+                       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
                        WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
                        break;
                }
@@ -686,6 +799,7 @@ void PARTICLE_OT_target_move_up(wmOperatorType *ot)
 
 static int target_move_down_exec(bContext *C, wmOperator *op)
 {
+       Scene *scene= CTX_data_scene(C);
        PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem);
        ParticleSystem *psys= ptr.data;
        Object *ob = ptr.id.data;
@@ -699,7 +813,7 @@ static int target_move_down_exec(bContext *C, wmOperator *op)
                        BLI_remlink(&psys->targets, pt);
                        BLI_insertlink(&psys->targets, pt->next, pt);
 
-                       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
+                       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
                        WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
                        break;
                }
@@ -720,225 +834,6 @@ void PARTICLE_OT_target_move_down(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-/************************ connect/disconnect hair operators *********************/
-
-static void disconnect_hair(Scene *scene, Object *ob, ParticleSystem *psys)
-{
-       ParticleSystemModifierData *psmd = psys_get_modifier(ob,psys);
-       ParticleData *pa = psys->particles;
-       PTCacheEdit *edit = psys->edit;
-       PTCacheEditPoint *point = edit ? edit->points : NULL;
-       PTCacheEditKey *ekey = NULL;
-       HairKey *key;
-       int i, k;
-       float hairmat[4][4];
-
-       if(!ob || !psys || psys->flag & PSYS_GLOBAL_HAIR)
-               return;
-
-       if(!psys->part || psys->part->type != PART_HAIR)
-               return;
-
-       for(i=0; i<psys->totpart; i++,pa++) {
-               if(point) {
-                       ekey = point->keys;
-                       point++;
-               }
-
-               psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, pa, hairmat);
-
-               for(k=0,key=pa->hair; k<pa->totkey; k++,key++) {
-                       Mat4MulVecfl(hairmat,key->co);
-                       
-                       if(ekey) {
-                               ekey->flag &= ~PEK_USE_WCO;
-                               ekey++;
-                       }
-               }
-       }
-
-       psys_free_path_cache(psys, psys->edit);
-
-       psys->flag |= PSYS_GLOBAL_HAIR;
-
-       PE_update_object(scene, ob, 0);
-}
-
-static int disconnect_hair_exec(bContext *C, wmOperator *op)
-{
-       Scene *scene= CTX_data_scene(C);
-       Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
-       PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem);
-       ParticleSystem *psys= NULL;
-       int all = RNA_boolean_get(op->ptr, "all");
-
-       if(!ob)
-               return OPERATOR_CANCELLED;
-
-       if(all) {
-               for(psys=ob->particlesystem.first; psys; psys=psys->next) {
-                       disconnect_hair(scene, ob, psys);
-               }
-       }
-       else {
-               psys = ptr.data;
-               disconnect_hair(scene, ob, psys);
-       }
-
-       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
-
-       return OPERATOR_FINISHED;
-}
-
-void PARTICLE_OT_disconnect_hair(wmOperatorType *ot)
-{
-       ot->name= "Disconnect Hair";
-       ot->description= "Disconnect hair from the emitter mesh.";
-       ot->idname= "PARTICLE_OT_disconnect_hair";
-
-       ot->exec= disconnect_hair_exec;
-       
-       /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-
-       RNA_def_boolean(ot->srna, "all", 0, "All hair", "Disconnect all hair systems from the emitter mesh");
-}
-
-static void connect_hair(Scene *scene, Object *ob, ParticleSystem *psys)
-{
-       ParticleSystemModifierData *psmd = psys_get_modifier(ob,psys);
-       ParticleData *pa = psys->particles;
-       PTCacheEdit *edit = psys->edit;
-       PTCacheEditPoint *point = edit ? edit->points : NULL;
-       PTCacheEditKey *ekey;
-       HairKey *key;
-       BVHTreeFromMesh bvhtree;
-       BVHTreeNearest nearest;
-       MFace *mface;
-       DerivedMesh *dm = NULL;
-       int numverts;
-       int i, k;
-       float hairmat[4][4], imat[4][4];
-       float v[4][3], vec[3];
-
-       if(!psys || !psys->part || psys->part->type != PART_HAIR)
-               return;
-
-       if(psmd->dm->deformedOnly)
-               dm= psmd->dm;
-       else
-               dm= mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
-
-       numverts = dm->getNumVerts (dm);
-
-       memset( &bvhtree, 0, sizeof(bvhtree) );
-
-       /* convert to global coordinates */
-       for (i=0; i<numverts; i++)
-               Mat4MulVecfl (ob->obmat, CDDM_get_vert(dm, i)->co);
-
-       bvhtree_from_mesh_faces(&bvhtree, dm, 0.0, 2, 6);
-
-       for(i=0; i<psys->totpart; i++,pa++) {
-               key = pa->hair;
-
-               nearest.index = -1;
-               nearest.dist = FLT_MAX;
-
-               BLI_bvhtree_find_nearest(bvhtree.tree, key->co, &nearest, bvhtree.nearest_callback, &bvhtree);
-
-               if(nearest.index == -1) {
-                       printf("No nearest point found for hair root!");
-                       continue;
-               }
-
-               mface = CDDM_get_face(dm,nearest.index);
-
-               VecCopyf(v[0], CDDM_get_vert(dm,mface->v1)->co);
-               VecCopyf(v[1], CDDM_get_vert(dm,mface->v2)->co);
-               VecCopyf(v[2], CDDM_get_vert(dm,mface->v3)->co);
-               if(mface->v4) {
-                       VecCopyf(v[3], CDDM_get_vert(dm,mface->v4)->co);
-                       MeanValueWeights(v, 4, nearest.co, pa->fuv);
-               }
-               else
-                       MeanValueWeights(v, 3, nearest.co, pa->fuv);
-
-               pa->num = nearest.index;
-               pa->num_dmcache = psys_particle_dm_face_lookup(ob,psmd->dm,pa->num,pa->fuv,NULL);
-               
-               psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, pa, hairmat);
-               Mat4Invert(imat,hairmat);
-
-               VECSUB(vec, nearest.co, key->co);
-
-               if(point) {
-                       ekey = point->keys;
-                       point++;
-               }
-
-               for(k=0,key=pa->hair; k<pa->totkey; k++,key++) {
-                       VECADD(key->co, key->co, vec);
-                       Mat4MulVecfl(imat,key->co);
-
-                       if(ekey) {
-                               ekey->flag |= PEK_USE_WCO;
-                               ekey++;
-                       }
-               }
-       }
-
-       free_bvhtree_from_mesh(&bvhtree);
-       if(!psmd->dm->deformedOnly)
-               dm->release(dm);
-
-       psys_free_path_cache(psys, psys->edit);
-
-       psys->flag &= ~PSYS_GLOBAL_HAIR;
-
-       PE_update_object(scene, ob, 0);
-}
-
-static int connect_hair_exec(bContext *C, wmOperator *op)
-{
-       Scene *scene= CTX_data_scene(C);
-       Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
-       PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem);
-       ParticleSystem *psys= NULL;
-       int all = RNA_boolean_get(op->ptr, "all");
-
-       if(!ob)
-               return OPERATOR_CANCELLED;
-
-       if(all) {
-               for(psys=ob->particlesystem.first; psys; psys=psys->next) {
-                       connect_hair(scene, ob, psys);
-               }
-       }
-       else {
-               psys = ptr.data;
-               connect_hair(scene, ob, psys);
-       }
-
-       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
-
-       return OPERATOR_FINISHED;
-}
-
-void PARTICLE_OT_connect_hair(wmOperatorType *ot)
-{
-       ot->name= "Connect Hair";
-       ot->description= "Connect hair to the emitter mesh.";
-       ot->idname= "PARTICLE_OT_connect_hair";
-
-       ot->exec= connect_hair_exec;
-       
-       /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-
-       RNA_def_boolean(ot->srna, "all", 0, "All hair", "Connect all hair systems to the emitter mesh");
-}
-
 /********************** render layer operators *********************/
 
 static int render_layer_add_exec(bContext *C, wmOperator *op)
@@ -1053,9 +948,6 @@ static int file_browse_exec(bContext *C, wmOperator *op)
        FileBrowseOp *fbo= op->customdata;
        char *str;
        
-       if (RNA_property_is_set(op->ptr, "filename")==0 || fbo==NULL)
-               return OPERATOR_CANCELLED;
-       
        str= RNA_string_get_alloc(op->ptr, "filename", 0, 0);
        RNA_property_string_set(&fbo->ptr, fbo->prop, str);
        RNA_property_update(C, &fbo->ptr, fbo->prop);