Fix #27816: Outliner does not update when parents are cleared
[blender-staging.git] / source / blender / editors / object / object_hook.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2008 full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/object/object_hook.c
29  *  \ingroup edobj
30  */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_math.h"
39 #include "BLI_editVert.h"
40 #include "BLI_listbase.h"
41 #include "BLI_string.h"
42 #include "BLI_utildefines.h"
43
44 #include "DNA_curve_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49
50 #include "BKE_action.h"
51 #include "BKE_context.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_main.h"
54 #include "BKE_mesh.h"
55 #include "BKE_modifier.h"
56 #include "BKE_object.h"
57 #include "BKE_report.h"
58 #include "BKE_scene.h"
59 #include "BKE_deform.h"
60
61 #include "RNA_define.h"
62 #include "RNA_access.h"
63 #include "RNA_enum_types.h"
64
65 #include "ED_curve.h"
66 #include "ED_mesh.h"
67 #include "ED_screen.h"
68
69 #include "WM_types.h"
70 #include "WM_api.h"
71
72 #include "UI_resources.h"
73
74 #include "object_intern.h"
75
76 static int return_editmesh_indexar(EditMesh *em, int *tot, int **indexar, float *cent)
77 {
78         EditVert *eve;
79         int *index, nr, totvert=0;
80         
81         for(eve= em->verts.first; eve; eve= eve->next) {
82                 if(eve->f & SELECT) totvert++;
83         }
84         if(totvert==0) return 0;
85         
86         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
87         *tot= totvert;
88         nr= 0;
89         zero_v3(cent);
90         
91         for(eve= em->verts.first; eve; eve= eve->next) {
92                 if(eve->f & SELECT) {
93                         *index= nr; index++;
94                         add_v3_v3(cent, eve->co);
95                 }
96                 nr++;
97         }
98         
99         mul_v3_fl(cent, 1.0f/(float)totvert);
100         
101         return totvert;
102 }
103
104 static int return_editmesh_vgroup(Object *obedit, EditMesh *em, char *name, float *cent)
105 {
106         zero_v3(cent);
107
108         if(obedit->actdef) {
109                 const int defgrp_index= obedit->actdef-1;
110                 int i, totvert=0;
111
112                 MDeformVert *dvert;
113                 EditVert *eve;
114
115                 /* find the vertices */
116                 for(eve= em->verts.first; eve; eve= eve->next) {
117                         dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
118
119                         if(dvert) {
120                                 if(defvert_find_weight(dvert, defgrp_index) > 0.0f) {
121                                         add_v3_v3(cent, eve->co);
122                                         totvert++;
123                                 }
124                         }
125                 }
126                 if(totvert) {
127                         bDeformGroup *dg = BLI_findlink(&obedit->defbase, defgrp_index);
128                         BLI_strncpy(name, dg->name, sizeof(dg->name));
129                         mul_v3_fl(cent, 1.0f/(float)totvert);
130                         return 1;
131                 }
132         }
133         
134         return 0;
135 }       
136
137 static void select_editmesh_hook(Object *ob, HookModifierData *hmd)
138 {
139         Mesh *me= ob->data;
140         EditMesh *em= BKE_mesh_get_editmesh(me);
141         EditVert *eve;
142         int index=0, nr=0;
143         
144         if (hmd->indexar == NULL)
145                 return;
146         
147         for(eve= em->verts.first; eve; eve= eve->next, nr++) {
148                 if(nr==hmd->indexar[index]) {
149                         eve->f |= SELECT;
150                         if(index < hmd->totindex-1) index++;
151                 }
152         }
153         EM_select_flush(em);
154
155         BKE_mesh_end_editmesh(me, em);
156 }
157
158 static int return_editlattice_indexar(Lattice *editlatt, int *tot, int **indexar, float *cent)
159 {
160         BPoint *bp;
161         int *index, nr, totvert=0, a;
162         
163         /* count */
164         a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw;
165         bp= editlatt->def;
166         while(a--) {
167                 if(bp->f1 & SELECT) {
168                         if(bp->hide==0) totvert++;
169                 }
170                 bp++;
171         }
172
173         if(totvert==0) return 0;
174         
175         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
176         *tot= totvert;
177         nr= 0;
178         cent[0]= cent[1]= cent[2]= 0.0;
179         
180         a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw;
181         bp= editlatt->def;
182         while(a--) {
183                 if(bp->f1 & SELECT) {
184                         if(bp->hide==0) {
185                                 *index= nr; index++;
186                                 add_v3_v3(cent, bp->vec);
187                         }
188                 }
189                 bp++;
190                 nr++;
191         }
192         
193         mul_v3_fl(cent, 1.0f/(float)totvert);
194         
195         return totvert;
196 }
197
198 static void select_editlattice_hook(Object *obedit, HookModifierData *hmd)
199 {
200         Lattice *lt= obedit->data, *editlt;
201         BPoint *bp;
202         int index=0, nr=0, a;
203
204         editlt= lt->editlatt->latt;
205         /* count */
206         a= editlt->pntsu*editlt->pntsv*editlt->pntsw;
207         bp= editlt->def;
208         while(a--) {
209                 if(hmd->indexar[index]==nr) {
210                         bp->f1 |= SELECT;
211                         if(index < hmd->totindex-1) index++;
212                 }
213                 nr++;
214                 bp++;
215         }
216 }
217
218 static int return_editcurve_indexar(Object *obedit, int *tot, int **indexar, float *cent)
219 {
220         ListBase *editnurb= curve_get_editcurve(obedit);
221         Nurb *nu;
222         BPoint *bp;
223         BezTriple *bezt;
224         int *index, a, nr, totvert=0;
225         
226         for(nu= editnurb->first; nu; nu= nu->next) {
227                 if(nu->type == CU_BEZIER) {
228                         bezt= nu->bezt;
229                         a= nu->pntsu;
230                         while(a--) {
231                                 if(bezt->f1 & SELECT) totvert++;
232                                 if(bezt->f2 & SELECT) totvert++;
233                                 if(bezt->f3 & SELECT) totvert++;
234                                 bezt++;
235                         }
236                 }
237                 else {
238                         bp= nu->bp;
239                         a= nu->pntsu*nu->pntsv;
240                         while(a--) {
241                                 if(bp->f1 & SELECT) totvert++;
242                                 bp++;
243                         }
244                 }
245         }
246         if(totvert==0) return 0;
247         
248         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
249         *tot= totvert;
250         nr= 0;
251         cent[0]= cent[1]= cent[2]= 0.0;
252         
253         for(nu= editnurb->first; nu; nu= nu->next) {
254                 if(nu->type == CU_BEZIER) {
255                         bezt= nu->bezt;
256                         a= nu->pntsu;
257                         while(a--) {
258                                 if(bezt->f1 & SELECT) {
259                                         *index= nr; index++;
260                                         add_v3_v3(cent, bezt->vec[0]);
261                                 }
262                                 nr++;
263                                 if(bezt->f2 & SELECT) {
264                                         *index= nr; index++;
265                                         add_v3_v3(cent, bezt->vec[1]);
266                                 }
267                                 nr++;
268                                 if(bezt->f3 & SELECT) {
269                                         *index= nr; index++;
270                                         add_v3_v3(cent, bezt->vec[2]);
271                                 }
272                                 nr++;
273                                 bezt++;
274                         }
275                 }
276                 else {
277                         bp= nu->bp;
278                         a= nu->pntsu*nu->pntsv;
279                         while(a--) {
280                                 if(bp->f1 & SELECT) {
281                                         *index= nr; index++;
282                                         add_v3_v3(cent, bp->vec);
283                                 }
284                                 nr++;
285                                 bp++;
286                         }
287                 }
288         }
289         
290         mul_v3_fl(cent, 1.0f/(float)totvert);
291         
292         return totvert;
293 }
294
295 static int object_hook_index_array(Object *obedit, int *tot, int **indexar, char *name, float *cent_r)
296 {
297         *indexar= NULL;
298         *tot= 0;
299         name[0]= 0;
300         
301         switch(obedit->type) {
302                 case OB_MESH:
303                 {
304                         Mesh *me= obedit->data;
305                         EditMesh *em = BKE_mesh_get_editmesh(me);
306
307                         /* check selected vertices first */
308                         if( return_editmesh_indexar(em, tot, indexar, cent_r)) {
309                                 BKE_mesh_end_editmesh(me, em);
310                                 return 1;
311                         } else {
312                                 int ret = return_editmesh_vgroup(obedit, em, name, cent_r);
313                                 BKE_mesh_end_editmesh(me, em);
314                                 return ret;
315                         }
316                 }
317                 case OB_CURVE:
318                 case OB_SURF:
319                         return return_editcurve_indexar(obedit, tot, indexar, cent_r);
320                 case OB_LATTICE:
321                 {
322                         Lattice *lt= obedit->data;
323                         return return_editlattice_indexar(lt->editlatt->latt, tot, indexar, cent_r);
324                 }
325                 default:
326                         return 0;
327         }
328 }
329
330 static void select_editcurve_hook(Object *obedit, HookModifierData *hmd)
331 {
332         ListBase *editnurb= curve_get_editcurve(obedit);
333         Nurb *nu;
334         BPoint *bp;
335         BezTriple *bezt;
336         int index=0, a, nr=0;
337         
338         for(nu= editnurb->first; nu; nu= nu->next) {
339                 if(nu->type == CU_BEZIER) {
340                         bezt= nu->bezt;
341                         a= nu->pntsu;
342                         while(a--) {
343                                 if(nr == hmd->indexar[index]) {
344                                         bezt->f1 |= SELECT;
345                                         if(index<hmd->totindex-1) index++;
346                                 }
347                                 nr++;
348                                 if(nr == hmd->indexar[index]) {
349                                         bezt->f2 |= SELECT;
350                                         if(index<hmd->totindex-1) index++;
351                                 }
352                                 nr++;
353                                 if(nr == hmd->indexar[index]) {
354                                         bezt->f3 |= SELECT;
355                                         if(index<hmd->totindex-1) index++;
356                                 }
357                                 nr++;
358                                 
359                                 bezt++;
360                         }
361                 }
362                 else {
363                         bp= nu->bp;
364                         a= nu->pntsu*nu->pntsv;
365                         while(a--) {
366                                 if(nr == hmd->indexar[index]) {
367                                         bp->f1 |= SELECT;
368                                         if(index<hmd->totindex-1) index++;
369                                 }
370                                 nr++;
371                                 bp++;
372                         }
373                 }
374         }
375 }
376
377 static void object_hook_select(Object *ob, HookModifierData *hmd) 
378 {
379         if (hmd->indexar == NULL)
380                 return;
381         
382         if(ob->type==OB_MESH) select_editmesh_hook(ob, hmd);
383         else if(ob->type==OB_LATTICE) select_editlattice_hook(ob, hmd);
384         else if(ob->type==OB_CURVE) select_editcurve_hook(ob, hmd);
385         else if(ob->type==OB_SURF) select_editcurve_hook(ob, hmd);
386 }
387
388 /* special poll operators for hook operators */
389 // TODO: check for properties window modifier context too as alternative?
390 static int hook_op_edit_poll(bContext *C)
391 {
392         Object *obedit= CTX_data_edit_object(C);
393         
394         if (obedit) {
395                 if (ED_operator_editmesh(C)) return 1;
396                 if (ED_operator_editsurfcurve(C)) return 1;
397                 if (ED_operator_editlattice(C)) return 1;
398                 //if (ED_operator_editmball(C)) return 1;
399         }
400         
401         return 0;
402 }
403
404 static Object *add_hook_object_new(Scene *scene, Object *obedit)
405 {
406         Base *base, *basedit;
407         Object *ob;
408
409         ob= add_object(scene, OB_EMPTY);
410         
411         basedit = object_in_scene(obedit, scene);
412         base = object_in_scene(ob, scene);
413         base->lay = ob->lay = obedit->lay;
414         
415         /* icky, add_object sets new base as active.
416          * so set it back to the original edit object */
417         scene->basact = basedit;
418
419         return ob;
420 }
421
422 static void add_hook_object(Main *bmain, Scene *scene, Object *obedit, Object *ob, int mode)
423 {
424         ModifierData *md=NULL;
425         HookModifierData *hmd = NULL;
426         float cent[3];
427         int tot, ok, *indexar;
428         char name[32];
429         
430         ok = object_hook_index_array(obedit, &tot, &indexar, name, cent);
431         
432         if (!ok) return;        // XXX error("Requires selected vertices or active Vertex Group");
433         
434         if (mode==OBJECT_ADDHOOK_NEWOB && !ob) {
435                 
436                 ob = add_hook_object_new(scene, obedit);
437                 
438                 /* transform cent to global coords for loc */
439                 mul_v3_m4v3(ob->loc, obedit->obmat, cent);
440         }
441         
442         md = obedit->modifiers.first;
443         while (md && modifierType_getInfo(md->type)->type==eModifierTypeType_OnlyDeform) {
444                 md = md->next;
445         }
446         
447         hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
448         BLI_insertlinkbefore(&obedit->modifiers, md, hmd);
449         BLI_snprintf(hmd->modifier.name, sizeof(hmd->modifier.name), "Hook-%s", ob->id.name+2);
450         modifier_unique_name(&obedit->modifiers, (ModifierData*)hmd);
451         
452         hmd->object= ob;
453         hmd->indexar= indexar;
454         copy_v3_v3(hmd->cent, cent);
455         hmd->totindex= tot;
456         BLI_strncpy(hmd->name, name, sizeof(hmd->name));
457         
458         /* matrix calculus */
459         /* vert x (obmat x hook->imat) x hook->obmat x ob->imat */
460         /*        (parentinv         )                          */
461         where_is_object(scene, ob);
462         
463         invert_m4_m4(ob->imat, ob->obmat);
464         /* apparently this call goes from right to left... */
465         mul_serie_m4(hmd->parentinv, ob->imat, obedit->obmat, NULL, 
466                                  NULL, NULL, NULL, NULL, NULL);
467         
468         DAG_scene_sort(bmain, scene);
469 }
470
471 static int object_add_hook_selob_exec(bContext *C, wmOperator *op)
472 {
473         Main *bmain= CTX_data_main(C);
474         Scene *scene= CTX_data_scene(C);
475         Object *obedit = CTX_data_edit_object(C);
476         Object *obsel=NULL;
477         
478         CTX_DATA_BEGIN(C, Object*, ob, selected_objects)
479         {
480                 if (ob != obedit) {
481                         obsel = ob;
482                         break;
483                 }
484         }
485         CTX_DATA_END;
486         
487         if (!obsel) {
488                 BKE_report(op->reports, RPT_ERROR, "Can't add hook with no other selected objects.");
489                 return OPERATOR_CANCELLED;
490         }
491         
492         add_hook_object(bmain, scene, obedit, obsel, OBJECT_ADDHOOK_SELOB);
493         
494         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, obedit);
495         return OPERATOR_FINISHED;
496 }
497
498 void OBJECT_OT_hook_add_selobj(wmOperatorType *ot)
499 {
500         /* identifiers */
501         ot->name= "Hook to Selected Object";
502         ot->description= "Hook selected vertices to the first selected Object";
503         ot->idname= "OBJECT_OT_hook_add_selob";
504         
505         /* api callbacks */
506         ot->exec= object_add_hook_selob_exec;
507         ot->poll= hook_op_edit_poll;
508         
509         /* flags */
510         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
511 }
512
513 static int object_add_hook_newob_exec(bContext *C, wmOperator *UNUSED(op))
514 {
515         Main *bmain= CTX_data_main(C);
516         Scene *scene= CTX_data_scene(C);
517         Object *obedit = CTX_data_edit_object(C);
518
519         add_hook_object(bmain, scene, obedit, NULL, OBJECT_ADDHOOK_NEWOB);
520         
521         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
522         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, obedit);
523         return OPERATOR_FINISHED;
524 }
525
526 void OBJECT_OT_hook_add_newobj(wmOperatorType *ot)
527 {
528         /* identifiers */
529         ot->name= "Hook to New Object";
530         ot->description= "Hook selected vertices to the first selected Object";
531         ot->idname= "OBJECT_OT_hook_add_newob";
532         
533         /* api callbacks */
534         ot->exec= object_add_hook_newob_exec;
535         ot->poll= hook_op_edit_poll;
536         
537         /* flags */
538         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
539 }
540
541 static int object_hook_remove_exec(bContext *C, wmOperator *op)
542 {
543         int num= RNA_enum_get(op->ptr, "modifier");
544         Object *ob=NULL;
545         HookModifierData *hmd=NULL;
546
547         ob = CTX_data_edit_object(C);
548         hmd = (HookModifierData *)BLI_findlink(&ob->modifiers, num);
549
550         if (!ob || !hmd) {
551                 BKE_report(op->reports, RPT_ERROR, "Couldn't find hook modifier");
552                 return OPERATOR_CANCELLED;
553         }
554         
555         /* remove functionality */
556         
557         BLI_remlink(&ob->modifiers, (ModifierData *)hmd);
558         modifier_free((ModifierData *)hmd);
559         
560         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
561         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
562         
563         return OPERATOR_FINISHED;
564 }
565
566 static EnumPropertyItem *hook_mod_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
567 {       
568         Object *ob = CTX_data_edit_object(C);
569         EnumPropertyItem tmp = {0, "", 0, "", ""};
570         EnumPropertyItem *item= NULL;
571         ModifierData *md = NULL;
572         int a, totitem= 0;
573         
574         if(!ob)
575                 return DummyRNA_NULL_items;
576         
577         for(a=0, md=ob->modifiers.first; md; md= md->next, a++) {
578                 if (md->type==eModifierType_Hook) {
579                         tmp.value= a;
580                         tmp.icon = ICON_HOOK;
581                         tmp.identifier= md->name;
582                         tmp.name= md->name;
583                         RNA_enum_item_add(&item, &totitem, &tmp);
584                 }
585         }
586         
587         RNA_enum_item_end(&item, &totitem);
588         *free= 1;
589         
590         return item;
591 }
592
593 void OBJECT_OT_hook_remove(wmOperatorType *ot)
594 {
595         PropertyRNA *prop;
596         
597         /* identifiers */
598         ot->name= "Remove Hook";
599         ot->idname= "OBJECT_OT_hook_remove";
600         ot->description= "Remove a hook from the active object";
601         
602         /* api callbacks */
603         ot->exec= object_hook_remove_exec;
604         ot->invoke= WM_menu_invoke;
605         ot->poll= hook_op_edit_poll;
606         
607         /* flags */
608         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
609         
610         /* properties */
611         prop= RNA_def_enum(ot->srna, "modifier", DummyRNA_NULL_items, 0, "Modifier", "Modifier number to remove.");
612         RNA_def_enum_funcs(prop, hook_mod_itemf);
613         ot->prop= prop;
614 }
615
616 static int object_hook_reset_exec(bContext *C, wmOperator *op)
617 {
618         PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_HookModifier);
619         int num= RNA_enum_get(op->ptr, "modifier");
620         Object *ob=NULL;
621         HookModifierData *hmd=NULL;
622         
623         if (ptr.data) {         /* if modifier context is available, use that */
624                 ob = ptr.id.data;
625                 hmd= ptr.data;
626         } 
627         else {                  /* use the provided property */
628                 ob = CTX_data_edit_object(C);
629                 hmd = (HookModifierData *)BLI_findlink(&ob->modifiers, num);
630         }
631         if (!ob || !hmd) {
632                 BKE_report(op->reports, RPT_ERROR, "Couldn't find hook modifier");
633                 return OPERATOR_CANCELLED;
634         }
635         
636         /* reset functionality */
637         if(hmd->object) {
638                 bPoseChannel *pchan= get_pose_channel(hmd->object->pose, hmd->subtarget);
639                 
640                 if(hmd->subtarget[0] && pchan) {
641                         float imat[4][4], mat[4][4];
642                         
643                         /* calculate the world-space matrix for the pose-channel target first, then carry on as usual */
644                         mul_m4_m4m4(mat, pchan->pose_mat, hmd->object->obmat);
645                         
646                         invert_m4_m4(imat, mat);
647                         mul_serie_m4(hmd->parentinv, imat, ob->obmat, NULL, NULL, NULL, NULL, NULL, NULL);
648                 }
649                 else {
650                         invert_m4_m4(hmd->object->imat, hmd->object->obmat);
651                         mul_serie_m4(hmd->parentinv, hmd->object->imat, ob->obmat, NULL, NULL, NULL, NULL, NULL, NULL);
652                 }
653         }
654         
655         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
656         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
657         
658         return OPERATOR_FINISHED;
659 }
660
661 void OBJECT_OT_hook_reset(wmOperatorType *ot)
662 {
663         PropertyRNA *prop;
664         
665         /* identifiers */
666         ot->name= "Reset Hook";
667         ot->description= "Recalculate and clear offset transformation";
668         ot->idname= "OBJECT_OT_hook_reset";
669         
670         /* callbacks */
671         ot->exec= object_hook_reset_exec;
672         ot->poll= hook_op_edit_poll;
673         
674         /* flags */
675         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
676         
677         /* properties */
678         prop= RNA_def_enum(ot->srna, "modifier", DummyRNA_NULL_items, 0, "Modifier", "Modifier number to assign to.");
679         RNA_def_enum_funcs(prop, hook_mod_itemf);
680 }
681
682 static int object_hook_recenter_exec(bContext *C, wmOperator *op)
683 {
684         PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_HookModifier);
685         int num= RNA_enum_get(op->ptr, "modifier");
686         Object *ob=NULL;
687         HookModifierData *hmd=NULL;
688         Scene *scene = CTX_data_scene(C);
689         float bmat[3][3], imat[3][3];
690         
691         if (ptr.data) {         /* if modifier context is available, use that */
692                 ob = ptr.id.data;
693                 hmd= ptr.data;
694         } 
695         else {                  /* use the provided property */
696                 ob = CTX_data_edit_object(C);
697                 hmd = (HookModifierData *)BLI_findlink(&ob->modifiers, num);
698         }
699         if (!ob || !hmd) {
700                 BKE_report(op->reports, RPT_ERROR, "Couldn't find hook modifier");
701                 return OPERATOR_CANCELLED;
702         }
703         
704         /* recenter functionality */
705         copy_m3_m4(bmat, ob->obmat);
706         invert_m3_m3(imat, bmat);
707         
708         sub_v3_v3v3(hmd->cent, scene->cursor, ob->obmat[3]);
709         mul_m3_v3(imat, hmd->cent);
710         
711         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
712         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
713         
714         return OPERATOR_FINISHED;
715 }
716
717 void OBJECT_OT_hook_recenter(wmOperatorType *ot)
718 {
719         PropertyRNA *prop;
720         
721         /* identifiers */
722         ot->name= "Recenter Hook";
723         ot->description= "Set hook center to cursor position";
724         ot->idname= "OBJECT_OT_hook_recenter";
725         
726         /* callbacks */
727         ot->exec= object_hook_recenter_exec;
728         ot->poll= hook_op_edit_poll;
729         
730         /* flags */
731         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
732         
733         /* properties */
734         prop= RNA_def_enum(ot->srna, "modifier", DummyRNA_NULL_items, 0, "Modifier", "Modifier number to assign to.");
735         RNA_def_enum_funcs(prop, hook_mod_itemf);
736 }
737
738 static int object_hook_assign_exec(bContext *C, wmOperator *op)
739 {
740         PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_HookModifier);
741         int num= RNA_enum_get(op->ptr, "modifier");
742         Object *ob=NULL;
743         HookModifierData *hmd=NULL;
744         float cent[3];
745         char name[32];
746         int *indexar, tot;
747         
748         if (ptr.data) {         /* if modifier context is available, use that */
749                 ob = ptr.id.data;
750                 hmd= ptr.data;
751         } 
752         else {                  /* use the provided property */
753                 ob = CTX_data_edit_object(C);
754                 hmd = (HookModifierData *)BLI_findlink(&ob->modifiers, num);
755         }
756         if (!ob || !hmd) {
757                 BKE_report(op->reports, RPT_ERROR, "Couldn't find hook modifier");
758                 return OPERATOR_CANCELLED;
759         }
760         
761         /* assign functionality */
762         
763         if(!object_hook_index_array(ob, &tot, &indexar, name, cent)) {
764                 BKE_report(op->reports, RPT_WARNING, "Requires selected vertices or active vertex group");
765                 return OPERATOR_CANCELLED;
766         }
767         if(hmd->indexar)
768                 MEM_freeN(hmd->indexar);
769         
770         copy_v3_v3(hmd->cent, cent);
771         hmd->indexar= indexar;
772         hmd->totindex= tot;
773         
774         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
775         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
776         
777         return OPERATOR_FINISHED;
778 }
779
780 void OBJECT_OT_hook_assign(wmOperatorType *ot)
781 {
782         PropertyRNA *prop;
783         
784         /* identifiers */
785         ot->name= "Assign to Hook";
786         ot->description= "Assign the selected vertices to a hook";
787         ot->idname= "OBJECT_OT_hook_assign";
788         
789         /* callbacks */
790         ot->exec= object_hook_assign_exec;
791         ot->poll= hook_op_edit_poll;
792         
793         /* flags */
794         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
795         
796         /* properties */
797         prop= RNA_def_enum(ot->srna, "modifier", DummyRNA_NULL_items, 0, "Modifier", "Modifier number to assign to.");
798         RNA_def_enum_funcs(prop, hook_mod_itemf);
799 }
800
801 static int object_hook_select_exec(bContext *C, wmOperator *op)
802 {
803         PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_HookModifier);
804         int num= RNA_enum_get(op->ptr, "modifier");
805         Object *ob=NULL;
806         HookModifierData *hmd=NULL;
807         
808         if (ptr.data) {         /* if modifier context is available, use that */
809                 ob = ptr.id.data;
810                 hmd= ptr.data;
811         } 
812         else {                  /* use the provided property */
813                 ob = CTX_data_edit_object(C);
814                 hmd = (HookModifierData *)BLI_findlink(&ob->modifiers, num);
815         }
816         if (!ob || !hmd) {
817                 BKE_report(op->reports, RPT_ERROR, "Couldn't find hook modifier");
818                 return OPERATOR_CANCELLED;
819         }
820         
821         /* select functionality */
822         object_hook_select(ob, hmd);
823         
824         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
825         
826         return OPERATOR_FINISHED;
827 }
828
829 void OBJECT_OT_hook_select(wmOperatorType *ot)
830 {
831         PropertyRNA *prop;
832         
833         /* identifiers */
834         ot->name= "Select Hook";
835         ot->description= "Selects effected vertices on mesh";
836         ot->idname= "OBJECT_OT_hook_select";
837         
838         /* callbacks */
839         ot->exec= object_hook_select_exec;
840         ot->poll= hook_op_edit_poll;
841         
842         /* flags */
843         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
844         
845         /* properties */
846         prop= RNA_def_enum(ot->srna, "modifier", DummyRNA_NULL_items, 0, "Modifier", "Modifier number to remove.");
847         RNA_def_enum_funcs(prop, hook_mod_itemf);
848 }
849