From 3daa283604930167be0bc2dad5d1684a871ca254 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Wed, 9 Sep 2009 11:52:56 +0000 Subject: 2.5: Object module * Split object_edit.c into multiple files: object_add.c, object_edit.c, object_hook.c, object_relations.c, object_select.c, object_transform.c. * Rename files to have consistent object_ and mball_ prefix: object_shapekey.c, object_lattice.c, object_vgroup.c, mball_edit.c. * Added operators: * vertex group menu and set active * apply location, rotation, scale, visual transform (location is new) * make local * make vertex parent * move to layer * convert to curve/mesh (not finished yet) * Many small fixes for marked issues, but still much code to be cleaned up here... --- projectfiles_vc9/blender/editors/ED_editors.vcproj | 14 +- release/ui/space_info.py | 16 +- source/blender/blenkernel/BKE_animsys.h | 3 + source/blender/blenkernel/intern/anim_sys.c | 26 + source/blender/blenkernel/intern/library.c | 5 +- source/blender/blenlib/intern/util.c | 5 - source/blender/editors/armature/editarmature.c | 46 +- .../editors/armature/editarmature_retarget.c | 4 +- source/blender/editors/armature/meshlaplacian.c | 24 +- source/blender/editors/armature/poseobject.c | 2 +- source/blender/editors/include/ED_armature.h | 4 +- source/blender/editors/include/ED_mball.h | 6 + source/blender/editors/include/ED_mesh.h | 22 +- source/blender/editors/include/ED_object.h | 7 +- source/blender/editors/metaball/editmball.c | 679 -- source/blender/editors/metaball/mball_edit.c | 679 ++ source/blender/editors/object/editconstraint.c | 1418 ---- source/blender/editors/object/editgroup.c | 257 - source/blender/editors/object/editkey.c | 539 -- source/blender/editors/object/editlattice.c | 388 - source/blender/editors/object/object_add.c | 1451 ++++ source/blender/editors/object/object_constraint.c | 1416 ++++ source/blender/editors/object/object_edit.c | 7584 +++----------------- source/blender/editors/object/object_group.c | 364 + source/blender/editors/object/object_hook.c | 608 ++ source/blender/editors/object/object_intern.h | 73 +- source/blender/editors/object/object_lattice.c | 382 + source/blender/editors/object/object_modifier.c | 52 - source/blender/editors/object/object_ops.c | 46 +- source/blender/editors/object/object_relations.c | 1766 +++++ source/blender/editors/object/object_select.c | 974 +++ source/blender/editors/object/object_shapekey.c | 539 ++ source/blender/editors/object/object_transform.c | 926 +++ source/blender/editors/object/object_vgroup.c | 1607 ++--- source/blender/editors/screen/screen_context.c | 6 + source/blender/editors/sculpt_paint/paint_vertex.c | 52 +- source/blender/editors/space_buttons/buttons_ops.c | 127 - source/blender/editors/space_view3d/drawobject.c | 2 +- .../blender/editors/space_view3d/view3d_buttons.c | 2 +- source/blender/editors/util/undo.c | 1 + source/blender/makesrna/RNA_enum_types.h | 2 + source/blender/makesrna/intern/rna_object.c | 16 + .../blender/windowmanager/intern/wm_event_system.c | 2 +- 43 files changed, 11268 insertions(+), 10874 deletions(-) delete mode 100644 source/blender/editors/metaball/editmball.c create mode 100644 source/blender/editors/metaball/mball_edit.c delete mode 100644 source/blender/editors/object/editconstraint.c delete mode 100644 source/blender/editors/object/editgroup.c delete mode 100644 source/blender/editors/object/editkey.c delete mode 100644 source/blender/editors/object/editlattice.c create mode 100644 source/blender/editors/object/object_add.c create mode 100644 source/blender/editors/object/object_constraint.c create mode 100644 source/blender/editors/object/object_group.c create mode 100644 source/blender/editors/object/object_hook.c create mode 100644 source/blender/editors/object/object_lattice.c create mode 100644 source/blender/editors/object/object_relations.c create mode 100644 source/blender/editors/object/object_select.c create mode 100644 source/blender/editors/object/object_shapekey.c create mode 100644 source/blender/editors/object/object_transform.c diff --git a/projectfiles_vc9/blender/editors/ED_editors.vcproj b/projectfiles_vc9/blender/editors/ED_editors.vcproj index 9f2fc769e49..db0b851fd74 100644 --- a/projectfiles_vc9/blender/editors/ED_editors.vcproj +++ b/projectfiles_vc9/blender/editors/ED_editors.vcproj @@ -1075,19 +1075,19 @@ Name="object" > + + first; strip; strip=strip->next) { + if(strip->act) make_local_action(strip->act); + if(strip->remap && strip->remap->target) make_local_action(strip->remap->target); + + make_local_strips(&strip->strips); + } +} + +void BKE_animdata_make_local(AnimData *adt) +{ + NlaTrack *nlt; + + if(adt->action) make_local_action(adt->action); + if(adt->tmpact) make_local_action(adt->tmpact); + if(adt->remap && adt->remap->target) make_local_action(adt->remap->target); + + for(nlt=adt->nla_tracks.first; nlt; nlt=nlt->next) + make_local_strips(&nlt->strips); +} + /* *********************************** */ /* KeyingSet API */ diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c index f15552ab40c..02d92a62b59 100644 --- a/source/blender/blenkernel/intern/library.c +++ b/source/blender/blenkernel/intern/library.c @@ -194,7 +194,10 @@ int id_make_local(ID *id, int test) case ID_WV: return 0; /* deprecated */ case ID_LT: - if(!test) make_local_lattice((Lattice*)id); + if(!test) { + make_local_lattice((Lattice*)id); + make_local_key(((Lattice*)id)->key); + } return 1; case ID_LA: if(!test) make_local_lamp((Lamp*)id); diff --git a/source/blender/blenlib/intern/util.c b/source/blender/blenlib/intern/util.c index acf236d382b..c7bb7a54457 100644 --- a/source/blender/blenlib/intern/util.c +++ b/source/blender/blenlib/intern/util.c @@ -77,11 +77,6 @@ #include "BLI_winstuff.h" -/* for duplicate_defgroup */ -#if !(defined vsnprintf) -#define vsnprintf _vsnprintf -#endif - #endif diff --git a/source/blender/editors/armature/editarmature.c b/source/blender/editors/armature/editarmature.c index 65051c384b3..4f5d8872384 100644 --- a/source/blender/editors/armature/editarmature.c +++ b/source/blender/editors/armature/editarmature.c @@ -239,7 +239,7 @@ static void fix_bonelist_roll (ListBase *bonelist, ListBase *editbonelist) } /* put EditMode back in Object */ -void ED_armature_from_edit(Scene *scene, Object *obedit) +void ED_armature_from_edit(Object *obedit) { bArmature *arm= obedit->data; EditBone *eBone, *neBone; @@ -343,21 +343,19 @@ void ED_armature_from_edit(Scene *scene, Object *obedit) DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); } - - -void apply_rot_armature (Scene *scene, Object *ob, float mat[3][3]) +void ED_armature_apply_transform(Object *ob, float mat[4][4]) { EditBone *ebone; bArmature *arm= ob->data; - float scale = Mat3ToScalef(mat); /* store the scale of the matrix here to use on envelopes */ + float scale = Mat4ToScalef(mat); /* store the scale of the matrix here to use on envelopes */ /* Put the armature into editmode */ ED_armature_to_edit(ob); /* Do the rotations */ for (ebone = arm->edbo->first; ebone; ebone=ebone->next){ - Mat3MulVecfl(mat, ebone->head); - Mat3MulVecfl(mat, ebone->tail); + Mat4MulVecfl(mat, ebone->head); + Mat4MulVecfl(mat, ebone->tail); ebone->rad_head *= scale; ebone->rad_tail *= scale; @@ -365,7 +363,7 @@ void apply_rot_armature (Scene *scene, Object *ob, float mat[3][3]) } /* Turn the list into an armature */ - ED_armature_from_edit(scene, ob); + ED_armature_from_edit(ob); ED_armature_edit_free(ob); } @@ -411,7 +409,7 @@ void docenter_armature (Scene *scene, View3D *v3d, Object *ob, int centermode) } /* Turn the list into an armature */ - ED_armature_from_edit(scene, ob); + ED_armature_from_edit(ob); /* Adjust object location for new centerpoint */ if(centermode && obedit==NULL) { @@ -557,7 +555,7 @@ static int apply_armature_pose2bones_exec (bContext *C, wmOperator *op) } /* convert editbones back to bones */ - ED_armature_from_edit(scene, ob); + ED_armature_from_edit(ob); /* flush positions of posebones */ where_is_pose(scene, ob); @@ -791,7 +789,7 @@ int join_armature_exec(bContext *C, wmOperator *op) DAG_scene_sort(scene); // because we removed object(s) - ED_armature_from_edit(scene, ob); + ED_armature_from_edit(ob); ED_armature_edit_free(ob); WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene); @@ -994,7 +992,7 @@ static void separate_armature_bones (Scene *scene, Object *ob, short sel) } /* exit editmode (recalculates pchans too) */ - ED_armature_from_edit(scene, ob); + ED_armature_from_edit(ob); ED_armature_edit_free(ob); } @@ -1037,7 +1035,7 @@ static int separate_armature_exec (bContext *C, wmOperator *op) oldob->mode &= ~OB_MODE_POSE; //oldbase->flag &= ~OB_POSEMODE; - ED_armature_from_edit(scene, obedit); + ED_armature_from_edit(obedit); ED_armature_edit_free(obedit); /* 2) duplicate base */ @@ -4334,7 +4332,7 @@ int ED_do_pose_selectbuffer(Scene *scene, Base *base, unsigned int *buffer, shor /* in weightpaint we select the associated vertex group too */ if (ob->mode & OB_MODE_WEIGHT_PAINT) { if (nearBone->flag & BONE_ACTIVE) { - vertexgroup_select_by_name(OBACT, nearBone->name); + ED_vgroup_select_by_name(OBACT, nearBone->name); DAG_id_flush_update(&OBACT->id, OB_RECALC_DATA); } } @@ -4445,7 +4443,7 @@ static int bone_skinnable(Object *ob, Bone *bone, void *datap) return 0; } -static int add_defgroup_unique_bone(Object *ob, Bone *bone, void *data) +static int ED_vgroup_add_unique_bone(Object *ob, Bone *bone, void *data) { /* This group creates a vertex group to ob that has the * same name as bone (provided the bone is skinnable). @@ -4453,7 +4451,7 @@ static int add_defgroup_unique_bone(Object *ob, Bone *bone, void *data) */ if (!(bone->flag & BONE_NO_DEFORM)) { if (!get_named_vertexgroup(ob,bone->name)) { - add_defgroup_name(ob, bone->name); + ED_vgroup_add_name(ob, bone->name); return 1; } } @@ -4497,7 +4495,7 @@ static int dgroup_skinnable(Object *ob, Bone *bone, void *datap) segments = 1; if (!(defgroup = get_named_vertexgroup(ob, bone->name))) - defgroup = add_defgroup_name(ob, bone->name); + defgroup = ED_vgroup_add_name(ob, bone->name); if (data->list != NULL) { hgroup = (bDeformGroup ***) &data->list; @@ -4548,17 +4546,17 @@ static void envelope_bone_weighting(Object *ob, Mesh *mesh, float (*verts)[3], i /* add the vert to the deform group if weight!=0.0 */ if (distance!=0.0) - add_vert_to_defgroup (ob, dgroup, i, distance, WEIGHT_REPLACE); + ED_vgroup_vert_add (ob, dgroup, i, distance, WEIGHT_REPLACE); else - remove_vert_defgroup (ob, dgroup, i); + ED_vgroup_vert_remove (ob, dgroup, i); /* do same for mirror */ if (dgroupflip && dgroupflip[j] && iflip >= 0) { if (distance!=0.0) - add_vert_to_defgroup (ob, dgroupflip[j], iflip, distance, + ED_vgroup_vert_add (ob, dgroupflip[j], iflip, distance, WEIGHT_REPLACE); else - remove_vert_defgroup (ob, dgroupflip[j], iflip); + ED_vgroup_vert_remove (ob, dgroupflip[j], iflip); } } } @@ -4748,10 +4746,10 @@ void create_vgroups_from_armature(Scene *scene, Object *ob, Object *par, int mod /* Traverse the bone list, trying to create empty vertex * groups cooresponding to the bone. */ - bone_looper(ob, arm->bonebase.first, NULL, add_defgroup_unique_bone); + bone_looper(ob, arm->bonebase.first, NULL, ED_vgroup_add_unique_bone); if (ob->type == OB_MESH) - create_dverts(ob->data); + ED_vgroup_data_create(ob->data); } else if(mode == ARM_GROUPS_ENVELOPE || mode == ARM_GROUPS_AUTO) { /* Traverse the bone list, trying to create vertex groups @@ -5659,7 +5657,7 @@ void generateSkeletonFromReebGraph(Scene *scene, ReebGraph *rg) if (obedit != NULL) { - ED_armature_from_edit(scene, obedit); + ED_armature_from_edit(obedit); ED_armature_edit_free(obedit); } diff --git a/source/blender/editors/armature/editarmature_retarget.c b/source/blender/editors/armature/editarmature_retarget.c index 1d87ca8a6df..16e78f7c8d1 100644 --- a/source/blender/editors/armature/editarmature_retarget.c +++ b/source/blender/editors/armature/editarmature_retarget.c @@ -2732,7 +2732,7 @@ static void adjustGraphs(bContext *C, RigGraph *rigg) /* Turn the list into an armature */ arm->edbo = rigg->editbones; - ED_armature_from_edit(scene, rigg->ob); + ED_armature_from_edit(rigg->ob); ED_undo_push(C, "Retarget Skeleton"); } @@ -2762,7 +2762,7 @@ static void retargetGraphs(bContext *C, RigGraph *rigg) /* Turn the list into an armature */ arm->edbo = rigg->editbones; - ED_armature_from_edit(scene, rigg->ob); + ED_armature_from_edit(rigg->ob); } char *RIG_nameBone(RigGraph *rg, int arc_index, int bone_index) diff --git a/source/blender/editors/armature/meshlaplacian.c b/source/blender/editors/armature/meshlaplacian.c index 1b167518a5a..a6c94bee5b1 100644 --- a/source/blender/editors/armature/meshlaplacian.c +++ b/source/blender/editors/armature/meshlaplacian.c @@ -672,9 +672,9 @@ void heat_bone_weighting(Object *ob, Mesh *me, float (*verts)[3], int numbones, /* clear weights */ if(bbone && firstsegment) { for(a=0; atotvert; a++) { - remove_vert_defgroup(ob, dgrouplist[j], a); + ED_vgroup_vert_remove(ob, dgrouplist[j], a); if(vertsflipped && dgroupflip[j] && vertsflipped[a] >= 0) - remove_vert_defgroup(ob, dgroupflip[j], vertsflipped[a]); + ED_vgroup_vert_remove(ob, dgroupflip[j], vertsflipped[a]); } } @@ -694,32 +694,32 @@ void heat_bone_weighting(Object *ob, Mesh *me, float (*verts)[3], int numbones, if(bbone) { if(solution > 0.0f) - add_vert_to_defgroup(ob, dgrouplist[j], a, solution, + ED_vgroup_vert_add(ob, dgrouplist[j], a, solution, WEIGHT_ADD); } else { weight= heat_limit_weight(solution); if(weight > 0.0f) - add_vert_to_defgroup(ob, dgrouplist[j], a, weight, + ED_vgroup_vert_add(ob, dgrouplist[j], a, weight, WEIGHT_REPLACE); else - remove_vert_defgroup(ob, dgrouplist[j], a); + ED_vgroup_vert_remove(ob, dgrouplist[j], a); } /* do same for mirror */ if(vertsflipped && dgroupflip[j] && vertsflipped[a] >= 0) { if(bbone) { if(solution > 0.0f) - add_vert_to_defgroup(ob, dgroupflip[j], vertsflipped[a], + ED_vgroup_vert_add(ob, dgroupflip[j], vertsflipped[a], solution, WEIGHT_ADD); } else { weight= heat_limit_weight(solution); if(weight > 0.0f) - add_vert_to_defgroup(ob, dgroupflip[j], vertsflipped[a], + ED_vgroup_vert_add(ob, dgroupflip[j], vertsflipped[a], weight, WEIGHT_REPLACE); else - remove_vert_defgroup(ob, dgroupflip[j], vertsflipped[a]); + ED_vgroup_vert_remove(ob, dgroupflip[j], vertsflipped[a]); } } } @@ -734,16 +734,16 @@ void heat_bone_weighting(Object *ob, Mesh *me, float (*verts)[3], int numbones, /* remove too small vertex weights */ if(bbone && lastsegment) { for(a=0; atotvert; a++) { - weight= get_vert_defgroup(ob, dgrouplist[j], a); + weight= ED_vgroup_vert_weight(ob, dgrouplist[j], a); weight= heat_limit_weight(weight); if(weight <= 0.0f) - remove_vert_defgroup(ob, dgrouplist[j], a); + ED_vgroup_vert_remove(ob, dgrouplist[j], a); if(vertsflipped && dgroupflip[j] && vertsflipped[a] >= 0) { - weight= get_vert_defgroup(ob, dgroupflip[j], vertsflipped[a]); + weight= ED_vgroup_vert_weight(ob, dgroupflip[j], vertsflipped[a]); weight= heat_limit_weight(weight); if(weight <= 0.0f) - remove_vert_defgroup(ob, dgroupflip[j], vertsflipped[a]); + ED_vgroup_vert_remove(ob, dgroupflip[j], vertsflipped[a]); } } } diff --git a/source/blender/editors/armature/poseobject.c b/source/blender/editors/armature/poseobject.c index a4a518bc003..0ae92de4407 100644 --- a/source/blender/editors/armature/poseobject.c +++ b/source/blender/editors/armature/poseobject.c @@ -1673,7 +1673,7 @@ void pose_activate_flipped_bone(Scene *scene) /* in weightpaint we select the associated vertex group too */ if(ob->mode & OB_MODE_WEIGHT_PAINT) { - vertexgroup_select_by_name(OBACT, name); + ED_vgroup_select_by_name(OBACT, name); DAG_id_flush_update(&OBACT->id, OB_RECALC_DATA); } diff --git a/source/blender/editors/include/ED_armature.h b/source/blender/editors/include/ED_armature.h index 738cbf094cb..8bdfe41ef80 100644 --- a/source/blender/editors/include/ED_armature.h +++ b/source/blender/editors/include/ED_armature.h @@ -95,7 +95,7 @@ void ED_operatortypes_armature(void); void ED_keymap_armature(struct wmWindowManager *wm); /* editarmature.c */ -void ED_armature_from_edit(struct Scene *scene, struct Object *obedit); +void ED_armature_from_edit(struct Object *obedit); void ED_armature_to_edit(struct Object *ob); void ED_armature_edit_free(struct Object *ob); void ED_armature_edit_remake(struct Object *obedit); @@ -116,6 +116,8 @@ void transform_armature_mirror_update(struct Object *obedit); void clear_armature(struct Scene *scene, struct Object *ob, char mode); void docenter_armature (struct Scene *scene, struct View3D *v3d, struct Object *ob, int centermode); +void ED_armature_apply_transform(struct Object *ob, float mat[4][4]); + #define ARM_GROUPS_NAME 1 #define ARM_GROUPS_ENVELOPE 2 #define ARM_GROUPS_AUTO 3 diff --git a/source/blender/editors/include/ED_mball.h b/source/blender/editors/include/ED_mball.h index adb50867bf9..49c1d856a27 100644 --- a/source/blender/editors/include/ED_mball.h +++ b/source/blender/editors/include/ED_mball.h @@ -26,6 +26,10 @@ * ***** END GPL LICENSE BLOCK ***** */ +struct bContext; +struct Object; +struct wmWindowManager; + void ED_operatortypes_metaball(void); void ED_keymap_metaball(struct wmWindowManager *wm); @@ -37,3 +41,5 @@ void free_editMball(struct Object *obedit); void make_editMball(struct Object *obedit); void load_editMball(struct Object *obedit); +void undo_push_mball(struct bContext *C, char *name); + diff --git a/source/blender/editors/include/ED_mesh.h b/source/blender/editors/include/ED_mesh.h index 0face00f82b..a2dba89ec20 100644 --- a/source/blender/editors/include/ED_mesh.h +++ b/source/blender/editors/include/ED_mesh.h @@ -157,25 +157,23 @@ void EM_deselect_by_material(struct EditMesh *em, int index); /* editface.c */ struct MTFace *EM_get_active_mtface(struct EditMesh *em, struct EditFace **act_efa, struct MCol **mcol, int sloppy); -/* editdeform.c XXX rename functions? */ +/* object_vgroup.c */ #define WEIGHT_REPLACE 1 #define WEIGHT_ADD 2 #define WEIGHT_SUBTRACT 3 -void add_defgroup (Object *ob); -void create_dverts(struct ID *id); -float get_vert_defgroup (Object *ob, struct bDeformGroup *dg, int vertnum); -void remove_vert_defgroup (Object *ob, struct bDeformGroup *dg, int vertnum); -void remove_verts_defgroup (Object *obedit, int allverts); -void vertexgroup_select_by_name(Object *ob, char *name); -void add_vert_to_defgroup (Object *ob, struct bDeformGroup *dg, int vertnum, - float weight, int assignmode); +struct bDeformGroup *ED_vgroup_add(struct Object *ob); +struct bDeformGroup *ED_vgroup_add_name(struct Object *ob, char *name); +void ED_vgroup_select_by_name(struct Object *ob, char *name); +void ED_vgroup_data_create(struct ID *id); -struct bDeformGroup *add_defgroup_name (Object *ob, char *name); -struct MDeformWeight *verify_defweight (struct MDeformVert *dv, int defgroup); -struct MDeformWeight *get_defweight (struct MDeformVert *dv, int defgroup); +void ED_vgroup_vert_add(struct Object *ob, struct bDeformGroup *dg, int vertnum, float weight, int assignmode); +void ED_vgroup_vert_remove(struct Object *ob, struct bDeformGroup *dg, int vertnum); +float ED_vgroup_vert_weight(struct Object *ob, struct bDeformGroup *dg, int vertnum); +struct MDeformWeight *ED_vgroup_weight_verify(struct MDeformVert *dv, int defgroup); +struct MDeformWeight *ED_vgroup_weight_get(struct MDeformVert *dv, int defgroup); #endif /* ED_MESH_H */ diff --git a/source/blender/editors/include/ED_object.h b/source/blender/editors/include/ED_object.h index d31f85d08ea..67dc6dada5f 100644 --- a/source/blender/editors/include/ED_object.h +++ b/source/blender/editors/include/ED_object.h @@ -89,14 +89,11 @@ void object_test_constraints(struct Object *ob); void ED_object_constraint_rename(struct Object *ob, struct bConstraint *con, char *oldname); void ED_object_constraint_set_active(struct Object *ob, struct bConstraint *con); -/* editlattice.c */ +/* object_lattice.c */ void mouse_lattice(struct bContext *C, short mval[2], int extend); void undo_push_lattice(struct bContext *C, char *name); -/* editmball.c */ -void undo_push_mball(struct bContext *C, char *name); - -/* editkey.c */ +/* object_shapekey.c */ void insert_shapekey(struct Scene *scene, struct Object *ob); void delete_key(struct Scene *scene, struct Object *ob); void key_to_mesh(struct KeyBlock *kb, struct Mesh *me); diff --git a/source/blender/editors/metaball/editmball.c b/source/blender/editors/metaball/editmball.c deleted file mode 100644 index 9ab985fb3fb..00000000000 --- a/source/blender/editors/metaball/editmball.c +++ /dev/null @@ -1,679 +0,0 @@ -/** - * $Id$ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * The Original Code is: all of this file. - - * Contributor(s): none yet. - * - * ***** END GPL LICENSE BLOCK ***** - */ - -#include -#include - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include "MEM_guardedalloc.h" - -#include "BLI_blenlib.h" -#include "BLI_arithb.h" -#include "BLI_rand.h" - -#include "DNA_meta_types.h" -#include "DNA_object_types.h" -#include "DNA_scene_types.h" -#include "DNA_view3d_types.h" -#include "DNA_windowmanager_types.h" -#include "DNA_userdef_types.h" - -#include "RNA_define.h" -#include "RNA_access.h" - -#include "BKE_utildefines.h" -#include "BKE_depsgraph.h" -#include "BKE_object.h" -#include "BKE_context.h" - -#include "ED_screen.h" -#include "ED_view3d.h" -#include "ED_transform.h" -#include "ED_util.h" - -#include "WM_api.h" -#include "WM_types.h" - -/* This function is used to free all MetaElems from MetaBall */ -void free_editMball(Object *obedit) -{ -} - -/* This function is called, when MetaBall Object is - * switched from object mode to edit mode */ -void make_editMball(Object *obedit) -{ - MetaBall *mb = (MetaBall*)obedit->data; - MetaElem *ml;/*, *newml;*/ - - ml= mb->elems.first; - - while(ml) { - if(ml->flag & SELECT) mb->lastelem = ml; - ml= ml->next; - } - - mb->editelems = &mb->elems; -} - -/* This function is called, when MetaBall Object switched from - * edit mode to object mode. List od MetaElements is copied - * from object->data->edit_elems to to object->data->elems. */ -void load_editMball(Object *obedit) -{ - MetaBall *mb = (MetaBall*)obedit->data; - - mb->editelems= NULL; - mb->lastelem= NULL; -} - -/* Add metaelem primitive to metaball object (which is in edit mode) */ -MetaElem *add_metaball_primitive(bContext *C, int type, int newname) -{ - Scene *scene= CTX_data_scene(C); - View3D *v3d= CTX_wm_view3d(C); - RegionView3D *rv3d = CTX_wm_region_view3d(C); - Object *obedit= CTX_data_edit_object(C); - MetaBall *mball = (MetaBall*)obedit->data; - MetaElem *ml; - float *curs, mat[3][3], cent[3], imat[3][3], cmat[3][3]; - - if(!obedit) return NULL; - - /* Deselect all existing metaelems */ - ml= mball->editelems->first; - while(ml) { - ml->flag &= ~SELECT; - ml= ml->next; - } - - Mat3CpyMat4(mat, obedit->obmat); - if(v3d) { - curs= give_cursor(scene, v3d); - VECCOPY(cent, curs); - } - else - cent[0]= cent[1]= cent[2]= 0.0f; - - cent[0]-= obedit->obmat[3][0]; - cent[1]-= obedit->obmat[3][1]; - cent[2]-= obedit->obmat[3][2]; - - if (rv3d) { - if (!(newname) || U.flag & USER_ADD_VIEWALIGNED || !rv3d) - Mat3CpyMat4(imat, rv3d->viewmat); - else - Mat3One(imat); - Mat3MulVecfl(imat, cent); - Mat3MulMat3(cmat, imat, mat); - Mat3Inv(imat,cmat); - Mat3MulVecfl(imat, cent); - } - else - Mat3One(imat); - - ml= MEM_callocN(sizeof(MetaElem), "metaelem"); - - ml->x= cent[0]; - ml->y= cent[1]; - ml->z= cent[2]; - ml->quat[0]= 1.0; - ml->quat[1]= 0.0; - ml->quat[2]= 0.0; - ml->quat[3]= 0.0; - ml->rad= 2.0; - ml->s= 2.0; - ml->flag= SELECT | MB_SCALE_RAD; - - switch(type) { - case MB_BALL: - ml->type = MB_BALL; - ml->expx= ml->expy= ml->expz= 1.0; - break; - case MB_TUBE: - ml->type = MB_TUBE; - ml->expx= ml->expy= ml->expz= 1.0; - break; - case MB_PLANE: - ml->type = MB_PLANE; - ml->expx= ml->expy= ml->expz= 1.0; - break; - case MB_ELIPSOID: - ml->type = MB_ELIPSOID; - ml->expx= 1.2f; - ml->expy= 0.8f; - ml->expz= 1.0; - break; - case MB_CUBE: - ml->type = MB_CUBE; - ml->expx= ml->expy= ml->expz= 1.0; - break; - default: - break; - } - - mball->lastelem= ml; - - return ml; -} - -/***************************** Select/Deselect operator *****************************/ - -/* Select or deselect all MetaElements */ -static int select_deselect_all_metaelems_exec(bContext *C, wmOperator *op) -{ - //Scene *scene= CTX_data_scene(C); - Object *obedit= CTX_data_edit_object(C); - MetaBall *mb = (MetaBall*)obedit->data; - MetaElem *ml; - int any_sel= 0; - - /* Is any metaelem selected? */ - ml= mb->editelems->first; - if(ml) { - while(ml) { - if(ml->flag & SELECT) break; - ml= ml->next; - } - if(ml) any_sel= 1; - - ml= mb->editelems->first; - while(ml) { - if(any_sel) ml->flag &= ~SELECT; - else ml->flag |= SELECT; - ml= ml->next; - } - WM_event_add_notifier(C, NC_GEOM|ND_SELECT, mb); - //DAG_id_flush_update(obedit->data, OB_RECALC_DATA); - } - - return OPERATOR_FINISHED; -} - -void MBALL_OT_select_deselect_all_metaelems(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Select/Deselect All"; - ot->idname= "MBALL_OT_select_deselect_all_metaelems"; - - /* callback functions */ - ot->exec= select_deselect_all_metaelems_exec; - ot->poll= ED_operator_editmball; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/***************************** Select inverse operator *****************************/ - -/* Invert metaball selection */ -static int select_inverse_metaelems_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - MetaBall *mb = (MetaBall*)obedit->data; - MetaElem *ml; - - ml= mb->editelems->first; - if(ml) { - while(ml) { - if(ml->flag & SELECT) - ml->flag &= ~SELECT; - else - ml->flag |= SELECT; - ml= ml->next; - } - WM_event_add_notifier(C, NC_GEOM|ND_SELECT, mb); - } - - return OPERATOR_FINISHED; -} - -void MBALL_OT_select_inverse_metaelems(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Inverse"; - ot->idname= "MBALL_OT_select_inverse_metaelems"; - - /* callback functions */ - ot->exec= select_inverse_metaelems_exec; - ot->poll= ED_operator_editmball; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/***************************** Select random operator *****************************/ - -/* Random metaball selection */ -static int select_random_metaelems_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - MetaBall *mb = (MetaBall*)obedit->data; - MetaElem *ml; - float percent= RNA_float_get(op->ptr, "percent"); - - if(percent == 0.0) - return OPERATOR_CANCELLED; - - ml= mb->editelems->first; - BLI_srand( BLI_rand() ); /* Random seed */ - - /* Stupid version of random selection. Should be improved. */ - while(ml) { - if(BLI_frand() < percent) - ml->flag |= SELECT; - else - ml->flag &= ~SELECT; - ml= ml->next; - } - - WM_event_add_notifier(C, NC_GEOM|ND_SELECT, mb); - - return OPERATOR_FINISHED; -} - - -void MBALL_OT_select_random_metaelems(struct wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Random..."; - ot->idname= "MBALL_OT_select_random_metaelems"; - - /* callback functions */ - ot->exec= select_random_metaelems_exec; - ot->invoke= WM_operator_props_popup; - ot->poll= ED_operator_editmball; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - /* properties */ - RNA_def_float_percentage(ot->srna, "percent", 0.5f, 0.0f, 1.0f, "Percent", "Percentage of metaelems to select randomly.", 0.0001f, 1.0f); -} - -/***************************** Duplicate operator *****************************/ - -/* Duplicate selected MetaElements */ -static int duplicate_metaelems_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - MetaBall *mb = (MetaBall*)obedit->data; - MetaElem *ml, *newml; - - ml= mb->editelems->last; - if(ml) { - while(ml) { - if(ml->flag & SELECT) { - newml= MEM_dupallocN(ml); - BLI_addtail(mb->editelems, newml); - mb->lastelem= newml; - ml->flag &= ~SELECT; - } - ml= ml->prev; - } - WM_event_add_notifier(C, NC_GEOM|ND_DATA, mb); - DAG_id_flush_update(obedit->data, OB_RECALC_DATA); - } - - return OPERATOR_FINISHED; -} - -static int duplicate_metaelems_invoke(bContext *C, wmOperator *op, wmEvent *event) -{ - int retv= duplicate_metaelems_exec(C, op); - - if (retv == OPERATOR_FINISHED) { - RNA_int_set(op->ptr, "mode", TFM_TRANSLATION); - WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr); - } - - return retv; -} - - -void MBALL_OT_duplicate_metaelems(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Duplicate"; - ot->idname= "MBALL_OT_duplicate_metaelems"; - - /* callback functions */ - ot->exec= duplicate_metaelems_exec; - ot->invoke= duplicate_metaelems_invoke; - ot->poll= ED_operator_editmball; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - /* to give to transform */ - RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX); -} - -/***************************** Delete operator *****************************/ - -/* Delete all selected MetaElems (not MetaBall) */ -static int delete_metaelems_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - MetaBall *mb= (MetaBall*)obedit->data; - MetaElem *ml, *next; - - ml= mb->editelems->first; - if(ml) { - while(ml) { - next= ml->next; - if(ml->flag & SELECT) { - if(mb->lastelem==ml) mb->lastelem= NULL; - BLI_remlink(mb->editelems, ml); - MEM_freeN(ml); - } - ml= next; - } - WM_event_add_notifier(C, NC_GEOM|ND_DATA, mb); - DAG_id_flush_update(obedit->data, OB_RECALC_DATA); - } - - return OPERATOR_FINISHED; -} - -void MBALL_OT_delete_metaelems(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Delete"; - ot->idname= "MBALL_OT_delete_metaelems"; - - /* callback functions */ - ot->exec= delete_metaelems_exec; - ot->poll= ED_operator_editmball; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/***************************** Hide operator *****************************/ - -/* Hide selected MetaElems */ -static int hide_metaelems_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - MetaBall *mb= (MetaBall*)obedit->data; - MetaElem *ml; - int hide_unselected= RNA_boolean_get(op->ptr, "unselected"); - - ml= mb->editelems->first; - - if(ml) { - /* Hide unselected metaelems */ - if(hide_unselected) { - while(ml){ - if(!(ml->flag & SELECT)) - ml->flag |= MB_HIDE; - ml= ml->next; - } - /* Hide selected metaelems */ - } else { - while(ml){ - if(ml->flag & SELECT) - ml->flag |= MB_HIDE; - ml= ml->next; - } - } - WM_event_add_notifier(C, NC_GEOM|ND_DATA, mb); - DAG_id_flush_update(obedit->data, OB_RECALC_DATA); - } - - return OPERATOR_FINISHED; -} - -void MBALL_OT_hide_metaelems(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Hide"; - ot->idname= "MBALL_OT_hide_metaelems"; - - /* callback functions */ - ot->exec= hide_metaelems_exec; - ot->poll= ED_operator_editmball; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - /* props */ - RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected."); -} - -/***************************** Unhide operator *****************************/ - -/* Unhide all edited MetaElems */ -static int reveal_metaelems_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - MetaBall *mb= (MetaBall*)obedit->data; - MetaElem *ml; - - ml= mb->editelems->first; - - if(ml) { - while(ml) { - ml->flag &= ~MB_HIDE; - ml= ml->next; - } - WM_event_add_notifier(C, NC_GEOM|ND_DATA, mb); - DAG_id_flush_update(obedit->data, OB_RECALC_DATA); - } - - return OPERATOR_FINISHED; -} - -void MBALL_OT_reveal_metaelems(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Reveal"; - ot->idname= "MBALL_OT_reveal_metaelems"; - - /* callback functions */ - ot->exec= reveal_metaelems_exec; - ot->poll= ED_operator_editmball; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/* Select MetaElement with mouse click (user can select radius circle or - * stiffness circle) */ -void mouse_mball(bContext *C, short mval[2], int extend) -{ - static MetaElem *startelem=NULL; - Object *obedit= CTX_data_edit_object(C); - ViewContext vc; - MetaBall *mb = (MetaBall*)obedit->data; - MetaElem *ml, *act=NULL; - int a, hits; - unsigned int buffer[4*MAXPICKBUF]; - rcti rect; - - view3d_set_viewcontext(C, &vc); - - rect.xmin= mval[0]-12; - rect.xmax= mval[0]+12; - rect.ymin= mval[1]-12; - rect.ymax= mval[1]+12; - - hits= view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect); - - /* does startelem exist? */ - ml= mb->editelems->first; - while(ml) { - if(ml==startelem) break; - ml= ml->next; - } - - if(ml==NULL) startelem= mb->editelems->first; - - if(hits>0) { - ml= startelem; - while(ml) { - for(a=0; aselcol1==buffer[ 4 * a + 3 ]){ - ml->flag |= MB_SCALE_RAD; - act= ml; - } - if(ml->selcol2==buffer[ 4 * a + 3 ]){ - ml->flag &= ~MB_SCALE_RAD; - act= ml; - } - } - if(act) break; - ml= ml->next; - if(ml==NULL) ml= mb->editelems->first; - if(ml==startelem) break; - } - - /* When some metaelem was found, then it is neccessary to select or - * deselet it. */ - if(act) { - if(extend==0) { - /* Deselect all existing metaelems */ - ml= mb->editelems->first; - while(ml) { - ml->flag &= ~SELECT; - ml= ml->next; - } - /* Select only metaelem clicked on */ - act->flag |= SELECT; - } - else { - if(act->flag & SELECT) - act->flag &= ~SELECT; - else - act->flag |= SELECT; - } - mb->lastelem= act; - - WM_event_add_notifier(C, NC_GEOM|ND_SELECT, mb); - } - } -} - - -/* ************* undo for MetaBalls ************* */ - -/* free all MetaElems from ListBase */ -static void freeMetaElemlist(ListBase *lb) -{ - MetaElem *ml, *next; - - if(lb==NULL) return; - - ml= lb->first; - while(ml){ - next= ml->next; - BLI_remlink(lb, ml); - MEM_freeN(ml); - ml= next; - } - - lb->first= lb->last= NULL; -} - - -static void undoMball_to_editMball(void *lbu, void *lbe) -{ - ListBase *lb= lbu; - ListBase *editelems= lbe; - MetaElem *ml, *newml; - - freeMetaElemlist(editelems); - - /* copy 'undo' MetaElems to 'edit' MetaElems */ - ml= lb->first; - while(ml){ - newml= MEM_dupallocN(ml); - BLI_addtail(editelems, newml); - ml= ml->next; - } - -} - -static void *editMball_to_undoMball(void *lbe) -{ - ListBase *editelems= lbe; - ListBase *lb; - MetaElem *ml, *newml; - - /* allocate memory for undo ListBase */ - lb= MEM_callocN(sizeof(ListBase), "listbase undo"); - lb->first= lb->last= NULL; - - /* copy contents of current ListBase to the undo ListBase */ - ml= editelems->first; - while(ml){ - newml= MEM_dupallocN(ml); - BLI_addtail(lb, newml); - ml= ml->next; - } - - return lb; -} - -/* free undo ListBase of MetaElems */ -static void free_undoMball(void *lbv) -{ - ListBase *lb= lbv; - - freeMetaElemlist(lb); - MEM_freeN(lb); -} - -ListBase *metaball_get_editelems(Object *ob) -{ - if(ob && ob->type==OB_MBALL) { - struct MetaBall *mb= (struct MetaBall*)ob->data; - return mb->editelems; - } - return NULL; -} - - -static void *get_data(bContext *C) -{ - Object *obedit= CTX_data_edit_object(C); - return metaball_get_editelems(obedit); -} - -/* this is undo system for MetaBalls */ -void undo_push_mball(bContext *C, char *name) -{ - undo_editmode_push(C, name, get_data, free_undoMball, undoMball_to_editMball, editMball_to_undoMball, NULL); -} - diff --git a/source/blender/editors/metaball/mball_edit.c b/source/blender/editors/metaball/mball_edit.c new file mode 100644 index 00000000000..9ab985fb3fb --- /dev/null +++ b/source/blender/editors/metaball/mball_edit.c @@ -0,0 +1,679 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "MEM_guardedalloc.h" + +#include "BLI_blenlib.h" +#include "BLI_arithb.h" +#include "BLI_rand.h" + +#include "DNA_meta_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" +#include "DNA_view3d_types.h" +#include "DNA_windowmanager_types.h" +#include "DNA_userdef_types.h" + +#include "RNA_define.h" +#include "RNA_access.h" + +#include "BKE_utildefines.h" +#include "BKE_depsgraph.h" +#include "BKE_object.h" +#include "BKE_context.h" + +#include "ED_screen.h" +#include "ED_view3d.h" +#include "ED_transform.h" +#include "ED_util.h" + +#include "WM_api.h" +#include "WM_types.h" + +/* This function is used to free all MetaElems from MetaBall */ +void free_editMball(Object *obedit) +{ +} + +/* This function is called, when MetaBall Object is + * switched from object mode to edit mode */ +void make_editMball(Object *obedit) +{ + MetaBall *mb = (MetaBall*)obedit->data; + MetaElem *ml;/*, *newml;*/ + + ml= mb->elems.first; + + while(ml) { + if(ml->flag & SELECT) mb->lastelem = ml; + ml= ml->next; + } + + mb->editelems = &mb->elems; +} + +/* This function is called, when MetaBall Object switched from + * edit mode to object mode. List od MetaElements is copied + * from object->data->edit_elems to to object->data->elems. */ +void load_editMball(Object *obedit) +{ + MetaBall *mb = (MetaBall*)obedit->data; + + mb->editelems= NULL; + mb->lastelem= NULL; +} + +/* Add metaelem primitive to metaball object (which is in edit mode) */ +MetaElem *add_metaball_primitive(bContext *C, int type, int newname) +{ + Scene *scene= CTX_data_scene(C); + View3D *v3d= CTX_wm_view3d(C); + RegionView3D *rv3d = CTX_wm_region_view3d(C); + Object *obedit= CTX_data_edit_object(C); + MetaBall *mball = (MetaBall*)obedit->data; + MetaElem *ml; + float *curs, mat[3][3], cent[3], imat[3][3], cmat[3][3]; + + if(!obedit) return NULL; + + /* Deselect all existing metaelems */ + ml= mball->editelems->first; + while(ml) { + ml->flag &= ~SELECT; + ml= ml->next; + } + + Mat3CpyMat4(mat, obedit->obmat); + if(v3d) { + curs= give_cursor(scene, v3d); + VECCOPY(cent, curs); + } + else + cent[0]= cent[1]= cent[2]= 0.0f; + + cent[0]-= obedit->obmat[3][0]; + cent[1]-= obedit->obmat[3][1]; + cent[2]-= obedit->obmat[3][2]; + + if (rv3d) { + if (!(newname) || U.flag & USER_ADD_VIEWALIGNED || !rv3d) + Mat3CpyMat4(imat, rv3d->viewmat); + else + Mat3One(imat); + Mat3MulVecfl(imat, cent); + Mat3MulMat3(cmat, imat, mat); + Mat3Inv(imat,cmat); + Mat3MulVecfl(imat, cent); + } + else + Mat3One(imat); + + ml= MEM_callocN(sizeof(MetaElem), "metaelem"); + + ml->x= cent[0]; + ml->y= cent[1]; + ml->z= cent[2]; + ml->quat[0]= 1.0; + ml->quat[1]= 0.0; + ml->quat[2]= 0.0; + ml->quat[3]= 0.0; + ml->rad= 2.0; + ml->s= 2.0; + ml->flag= SELECT | MB_SCALE_RAD; + + switch(type) { + case MB_BALL: + ml->type = MB_BALL; + ml->expx= ml->expy= ml->expz= 1.0; + break; + case MB_TUBE: + ml->type = MB_TUBE; + ml->expx= ml->expy= ml->expz= 1.0; + break; + case MB_PLANE: + ml->type = MB_PLANE; + ml->expx= ml->expy= ml->expz= 1.0; + break; + case MB_ELIPSOID: + ml->type = MB_ELIPSOID; + ml->expx= 1.2f; + ml->expy= 0.8f; + ml->expz= 1.0; + break; + case MB_CUBE: + ml->type = MB_CUBE; + ml->expx= ml->expy= ml->expz= 1.0; + break; + default: + break; + } + + mball->lastelem= ml; + + return ml; +} + +/***************************** Select/Deselect operator *****************************/ + +/* Select or deselect all MetaElements */ +static int select_deselect_all_metaelems_exec(bContext *C, wmOperator *op) +{ + //Scene *scene= CTX_data_scene(C); + Object *obedit= CTX_data_edit_object(C); + MetaBall *mb = (MetaBall*)obedit->data; + MetaElem *ml; + int any_sel= 0; + + /* Is any metaelem selected? */ + ml= mb->editelems->first; + if(ml) { + while(ml) { + if(ml->flag & SELECT) break; + ml= ml->next; + } + if(ml) any_sel= 1; + + ml= mb->editelems->first; + while(ml) { + if(any_sel) ml->flag &= ~SELECT; + else ml->flag |= SELECT; + ml= ml->next; + } + WM_event_add_notifier(C, NC_GEOM|ND_SELECT, mb); + //DAG_id_flush_update(obedit->data, OB_RECALC_DATA); + } + + return OPERATOR_FINISHED; +} + +void MBALL_OT_select_deselect_all_metaelems(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Select/Deselect All"; + ot->idname= "MBALL_OT_select_deselect_all_metaelems"; + + /* callback functions */ + ot->exec= select_deselect_all_metaelems_exec; + ot->poll= ED_operator_editmball; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/***************************** Select inverse operator *****************************/ + +/* Invert metaball selection */ +static int select_inverse_metaelems_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + MetaBall *mb = (MetaBall*)obedit->data; + MetaElem *ml; + + ml= mb->editelems->first; + if(ml) { + while(ml) { + if(ml->flag & SELECT) + ml->flag &= ~SELECT; + else + ml->flag |= SELECT; + ml= ml->next; + } + WM_event_add_notifier(C, NC_GEOM|ND_SELECT, mb); + } + + return OPERATOR_FINISHED; +} + +void MBALL_OT_select_inverse_metaelems(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Inverse"; + ot->idname= "MBALL_OT_select_inverse_metaelems"; + + /* callback functions */ + ot->exec= select_inverse_metaelems_exec; + ot->poll= ED_operator_editmball; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/***************************** Select random operator *****************************/ + +/* Random metaball selection */ +static int select_random_metaelems_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + MetaBall *mb = (MetaBall*)obedit->data; + MetaElem *ml; + float percent= RNA_float_get(op->ptr, "percent"); + + if(percent == 0.0) + return OPERATOR_CANCELLED; + + ml= mb->editelems->first; + BLI_srand( BLI_rand() ); /* Random seed */ + + /* Stupid version of random selection. Should be improved. */ + while(ml) { + if(BLI_frand() < percent) + ml->flag |= SELECT; + else + ml->flag &= ~SELECT; + ml= ml->next; + } + + WM_event_add_notifier(C, NC_GEOM|ND_SELECT, mb); + + return OPERATOR_FINISHED; +} + + +void MBALL_OT_select_random_metaelems(struct wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Random..."; + ot->idname= "MBALL_OT_select_random_metaelems"; + + /* callback functions */ + ot->exec= select_random_metaelems_exec; + ot->invoke= WM_operator_props_popup; + ot->poll= ED_operator_editmball; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_float_percentage(ot->srna, "percent", 0.5f, 0.0f, 1.0f, "Percent", "Percentage of metaelems to select randomly.", 0.0001f, 1.0f); +} + +/***************************** Duplicate operator *****************************/ + +/* Duplicate selected MetaElements */ +static int duplicate_metaelems_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + MetaBall *mb = (MetaBall*)obedit->data; + MetaElem *ml, *newml; + + ml= mb->editelems->last; + if(ml) { + while(ml) { + if(ml->flag & SELECT) { + newml= MEM_dupallocN(ml); + BLI_addtail(mb->editelems, newml); + mb->lastelem= newml; + ml->flag &= ~SELECT; + } + ml= ml->prev; + } + WM_event_add_notifier(C, NC_GEOM|ND_DATA, mb); + DAG_id_flush_update(obedit->data, OB_RECALC_DATA); + } + + return OPERATOR_FINISHED; +} + +static int duplicate_metaelems_invoke(bContext *C, wmOperator *op, wmEvent *event) +{ + int retv= duplicate_metaelems_exec(C, op); + + if (retv == OPERATOR_FINISHED) { + RNA_int_set(op->ptr, "mode", TFM_TRANSLATION); + WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr); + } + + return retv; +} + + +void MBALL_OT_duplicate_metaelems(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Duplicate"; + ot->idname= "MBALL_OT_duplicate_metaelems"; + + /* callback functions */ + ot->exec= duplicate_metaelems_exec; + ot->invoke= duplicate_metaelems_invoke; + ot->poll= ED_operator_editmball; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* to give to transform */ + RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX); +} + +/***************************** Delete operator *****************************/ + +/* Delete all selected MetaElems (not MetaBall) */ +static int delete_metaelems_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + MetaBall *mb= (MetaBall*)obedit->data; + MetaElem *ml, *next; + + ml= mb->editelems->first; + if(ml) { + while(ml) { + next= ml->next; + if(ml->flag & SELECT) { + if(mb->lastelem==ml) mb->lastelem= NULL; + BLI_remlink(mb->editelems, ml); + MEM_freeN(ml); + } + ml= next; + } + WM_event_add_notifier(C, NC_GEOM|ND_DATA, mb); + DAG_id_flush_update(obedit->data, OB_RECALC_DATA); + } + + return OPERATOR_FINISHED; +} + +void MBALL_OT_delete_metaelems(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Delete"; + ot->idname= "MBALL_OT_delete_metaelems"; + + /* callback functions */ + ot->exec= delete_metaelems_exec; + ot->poll= ED_operator_editmball; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/***************************** Hide operator *****************************/ + +/* Hide selected MetaElems */ +static int hide_metaelems_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + MetaBall *mb= (MetaBall*)obedit->data; + MetaElem *ml; + int hide_unselected= RNA_boolean_get(op->ptr, "unselected"); + + ml= mb->editelems->first; + + if(ml) { + /* Hide unselected metaelems */ + if(hide_unselected) { + while(ml){ + if(!(ml->flag & SELECT)) + ml->flag |= MB_HIDE; + ml= ml->next; + } + /* Hide selected metaelems */ + } else { + while(ml){ + if(ml->flag & SELECT) + ml->flag |= MB_HIDE; + ml= ml->next; + } + } + WM_event_add_notifier(C, NC_GEOM|ND_DATA, mb); + DAG_id_flush_update(obedit->data, OB_RECALC_DATA); + } + + return OPERATOR_FINISHED; +} + +void MBALL_OT_hide_metaelems(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Hide"; + ot->idname= "MBALL_OT_hide_metaelems"; + + /* callback functions */ + ot->exec= hide_metaelems_exec; + ot->poll= ED_operator_editmball; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* props */ + RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected."); +} + +/***************************** Unhide operator *****************************/ + +/* Unhide all edited MetaElems */ +static int reveal_metaelems_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + MetaBall *mb= (MetaBall*)obedit->data; + MetaElem *ml; + + ml= mb->editelems->first; + + if(ml) { + while(ml) { + ml->flag &= ~MB_HIDE; + ml= ml->next; + } + WM_event_add_notifier(C, NC_GEOM|ND_DATA, mb); + DAG_id_flush_update(obedit->data, OB_RECALC_DATA); + } + + return OPERATOR_FINISHED; +} + +void MBALL_OT_reveal_metaelems(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Reveal"; + ot->idname= "MBALL_OT_reveal_metaelems"; + + /* callback functions */ + ot->exec= reveal_metaelems_exec; + ot->poll= ED_operator_editmball; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/* Select MetaElement with mouse click (user can select radius circle or + * stiffness circle) */ +void mouse_mball(bContext *C, short mval[2], int extend) +{ + static MetaElem *startelem=NULL; + Object *obedit= CTX_data_edit_object(C); + ViewContext vc; + MetaBall *mb = (MetaBall*)obedit->data; + MetaElem *ml, *act=NULL; + int a, hits; + unsigned int buffer[4*MAXPICKBUF]; + rcti rect; + + view3d_set_viewcontext(C, &vc); + + rect.xmin= mval[0]-12; + rect.xmax= mval[0]+12; + rect.ymin= mval[1]-12; + rect.ymax= mval[1]+12; + + hits= view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect); + + /* does startelem exist? */ + ml= mb->editelems->first; + while(ml) { + if(ml==startelem) break; + ml= ml->next; + } + + if(ml==NULL) startelem= mb->editelems->first; + + if(hits>0) { + ml= startelem; + while(ml) { + for(a=0; aselcol1==buffer[ 4 * a + 3 ]){ + ml->flag |= MB_SCALE_RAD; + act= ml; + } + if(ml->selcol2==buffer[ 4 * a + 3 ]){ + ml->flag &= ~MB_SCALE_RAD; + act= ml; + } + } + if(act) break; + ml= ml->next; + if(ml==NULL) ml= mb->editelems->first; + if(ml==startelem) break; + } + + /* When some metaelem was found, then it is neccessary to select or + * deselet it. */ + if(act) { + if(extend==0) { + /* Deselect all existing metaelems */ + ml= mb->editelems->first; + while(ml) { + ml->flag &= ~SELECT; + ml= ml->next; + } + /* Select only metaelem clicked on */ + act->flag |= SELECT; + } + else { + if(act->flag & SELECT) + act->flag &= ~SELECT; + else + act->flag |= SELECT; + } + mb->lastelem= act; + + WM_event_add_notifier(C, NC_GEOM|ND_SELECT, mb); + } + } +} + + +/* ************* undo for MetaBalls ************* */ + +/* free all MetaElems from ListBase */ +static void freeMetaElemlist(ListBase *lb) +{ + MetaElem *ml, *next; + + if(lb==NULL) return; + + ml= lb->first; + while(ml){ + next= ml->next; + BLI_remlink(lb, ml); + MEM_freeN(ml); + ml= next; + } + + lb->first= lb->last= NULL; +} + + +static void undoMball_to_editMball(void *lbu, void *lbe) +{ + ListBase *lb= lbu; + ListBase *editelems= lbe; + MetaElem *ml, *newml; + + freeMetaElemlist(editelems); + + /* copy 'undo' MetaElems to 'edit' MetaElems */ + ml= lb->first; + while(ml){ + newml= MEM_dupallocN(ml); + BLI_addtail(editelems, newml); + ml= ml->next; + } + +} + +static void *editMball_to_undoMball(void *lbe) +{ + ListBase *editelems= lbe; + ListBase *lb; + MetaElem *ml, *newml; + + /* allocate memory for undo ListBase */ + lb= MEM_callocN(sizeof(ListBase), "listbase undo"); + lb->first= lb->last= NULL; + + /* copy contents of current ListBase to the undo ListBase */ + ml= editelems->first; + while(ml){ + newml= MEM_dupallocN(ml); + BLI_addtail(lb, newml); + ml= ml->next; + } + + return lb; +} + +/* free undo ListBase of MetaElems */ +static void free_undoMball(void *lbv) +{ + ListBase *lb= lbv; + + freeMetaElemlist(lb); + MEM_freeN(lb); +} + +ListBase *metaball_get_editelems(Object *ob) +{ + if(ob && ob->type==OB_MBALL) { + struct MetaBall *mb= (struct MetaBall*)ob->data; + return mb->editelems; + } + return NULL; +} + + +static void *get_data(bContext *C) +{ + Object *obedit= CTX_data_edit_object(C); + return metaball_get_editelems(obedit); +} + +/* this is undo system for MetaBalls */ +void undo_push_mball(bContext *C, char *name) +{ + undo_editmode_push(C, name, get_data, free_undoMball, undoMball_to_editMball, editMball_to_undoMball, NULL); +} + diff --git a/source/blender/editors/object/editconstraint.c b/source/blender/editors/object/editconstraint.c deleted file mode 100644 index c1b1062bfc4..00000000000 --- a/source/blender/editors/object/editconstraint.c +++ /dev/null @@ -1,1418 +0,0 @@ -/** - * $Id$ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * The Original Code is: all of this file. - * - * Contributor(s): Joshua Leung, Blender Foundation - * - * ***** END GPL LICENSE BLOCK ***** - */ - -#include -#include - -#include "MEM_guardedalloc.h" - -#include "BLI_blenlib.h" -#include "BLI_arithb.h" -#include "BLI_dynstr.h" - -#include "DNA_action_types.h" -#include "DNA_armature_types.h" -#include "DNA_constraint_types.h" -#include "DNA_curve_types.h" -#include "DNA_object_types.h" -#include "DNA_scene_types.h" -#include "DNA_screen_types.h" -#include "DNA_text_types.h" -#include "DNA_view3d_types.h" - -#include "BKE_action.h" -#include "BKE_armature.h" -#include "BKE_constraint.h" -#include "BKE_context.h" -#include "BKE_depsgraph.h" -#include "BKE_global.h" -#include "BKE_main.h" -#include "BKE_object.h" -#include "BKE_report.h" -#include "BKE_utildefines.h" - -#ifndef DISABLE_PYTHON -#include "BPY_extern.h" -#endif - -#include "WM_api.h" -#include "WM_types.h" - -#include "RNA_access.h" -#include "RNA_define.h" -#include "RNA_enum_types.h" -#include "RNA_types.h" - -#include "ED_object.h" -#include "ED_screen.h" - -#include "UI_interface.h" - -#include "object_intern.h" - -/* XXX */ -static int pupmenu() {return 0;} - -/* -------------- Get Active Constraint Data ---------------------- */ - -/* if object in posemode, active bone constraints, else object constraints */ -ListBase *get_active_constraints (Object *ob) -{ - if (ob == NULL) - return NULL; - - if (ob->mode & OB_MODE_POSE) { - bPoseChannel *pchan; - - pchan = get_active_posechannel(ob); - if (pchan) - return &pchan->constraints; - } - else - return &ob->constraints; - - return NULL; -} - -/* single constraint */ -bConstraint *get_active_constraint (Object *ob) -{ - ListBase *lb= get_active_constraints(ob); - - if (lb) { - bConstraint *con; - - for (con= lb->first; con; con=con->next) { - if (con->flag & CONSTRAINT_ACTIVE) - return con; - } - } - - return NULL; -} -/* -------------- Constraint Management (Add New, Remove, Rename) -------------------- */ -/* ------------- PyConstraints ------------------ */ - -/* this callback sets the text-file to be used for selected menu item */ -void validate_pyconstraint_cb (void *arg1, void *arg2) -{ - bPythonConstraint *data = arg1; - Text *text= NULL; - int index = *((int *)arg2); - int i; - - /* exception for no script */ - if (index) { - /* innovative use of a for...loop to search */ - for (text=G.main->text.first, i=1; text && index!=i; i++, text=text->id.next); - } - data->text = text; -} - -#ifndef DISABLE_PYTHON -/* this returns a string for the list of usable pyconstraint script names */ -char *buildmenu_pyconstraints (Text *con_text, int *pyconindex) -{ - DynStr *pupds= BLI_dynstr_new(); - Text *text; - char *str; - char buf[64]; - int i; - - /* add title first */ - sprintf(buf, "Scripts: %%t|[None]%%x0|"); - BLI_dynstr_append(pupds, buf); - - /* init active-index first */ - if (con_text == NULL) - *pyconindex= 0; - - /* loop through markers, adding them */ - for (text=G.main->text.first, i=1; text; i++, text=text->id.next) { - /* this is important to ensure that right script is shown as active */ - if (text == con_text) *pyconindex = i; - - /* only include valid pyconstraint scripts */ - if (BPY_is_pyconstraint(text)) { - BLI_dynstr_append(pupds, text->id.name+2); - - sprintf(buf, "%%x%d", i); - BLI_dynstr_append(pupds, buf); - - if (text->id.next) - BLI_dynstr_append(pupds, "|"); - } - } - - /* convert to normal MEM_malloc'd string */ - str= BLI_dynstr_get_cstring(pupds); - BLI_dynstr_free(pupds); - - return str; -} -#endif /* DISABLE_PYTHON */ - -/* this callback gets called when the 'refresh' button of a pyconstraint gets pressed */ -void update_pyconstraint_cb (void *arg1, void *arg2) -{ - Object *owner= (Object *)arg1; - bConstraint *con= (bConstraint *)arg2; -#ifndef DISABLE_PYTHON - if (owner && con) - BPY_pyconstraint_update(owner, con); -#endif -} - -/* Creates a new constraint, initialises its data, and returns it */ -bConstraint *add_new_constraint (short type) -{ - bConstraint *con; - bConstraintTypeInfo *cti; - - con = MEM_callocN(sizeof(bConstraint), "Constraint"); - - /* Set up a generic constraint datablock */ - con->type = type; - con->flag |= CONSTRAINT_EXPAND; - con->enforce = 1.0f; - - /* Load the data for it */ - cti = constraint_get_typeinfo(con); - if (cti) { - con->data = MEM_callocN(cti->size, cti->structName); - - /* only constraints that change any settings need this */ - if (cti->new_data) - cti->new_data(con->data); - - /* set the name based on the type of constraint */ - strcpy(con->name, cti->name); - } - else - strcpy(con->name, "Const"); - - return con; -} - -/* Adds the given constraint to the Object-level set of constraints for the given Object */ -void add_constraint_to_object (bConstraint *con, Object *ob) -{ - ListBase *list; - list = &ob->constraints; - - if (list) { - unique_constraint_name(con, list); - BLI_addtail(list, con); - - if (proxylocked_constraints_owner(ob, NULL)) - con->flag |= CONSTRAINT_PROXY_LOCAL; - - con->flag |= CONSTRAINT_ACTIVE; - for (con= con->prev; con; con= con->prev) - con->flag &= ~CONSTRAINT_ACTIVE; - } -} - -/* helper function for add_constriant - sets the last target for the active constraint */ -static void set_constraint_nth_target (bConstraint *con, Object *target, char subtarget[], int index) -{ - bConstraintTypeInfo *cti= constraint_get_typeinfo(con); - ListBase targets = {NULL, NULL}; - bConstraintTarget *ct; - int num_targets, i; - - if (cti && cti->get_constraint_targets) { - cti->get_constraint_targets(con, &targets); - num_targets= BLI_countlist(&targets); - - if (index < 0) { - if (abs(index) < num_targets) - index= num_targets - abs(index); - else - index= num_targets - 1; - } - else if (index >= num_targets) { - index= num_targets - 1; - } - - for (ct=targets.first, i=0; ct; ct= ct->next, i++) { - if (i == index) { - ct->tar= target; - strcpy(ct->subtarget, subtarget); - break; - } - } - - if (cti->flush_constraint_targets) - cti->flush_constraint_targets(con, &targets, 0); - } -} - -/* ------------- Constraint Sanity Testing ------------------- */ - -/* checks validity of object pointers, and NULLs, - * if Bone doesnt exist it sets the CONSTRAINT_DISABLE flag - */ -static void test_constraints (Object *owner, const char substring[]) -{ - bConstraint *curcon; - ListBase *conlist= NULL; - int type; - - if (owner==NULL) return; - - /* Check parents */ - if (strlen(substring)) { - switch (owner->type) { - case OB_ARMATURE: - type = CONSTRAINT_OBTYPE_BONE; - break; - default: - type = CONSTRAINT_OBTYPE_OBJECT; - break; - } - } - else - type = CONSTRAINT_OBTYPE_OBJECT; - - /* Get the constraint list for this object */ - switch (type) { - case CONSTRAINT_OBTYPE_OBJECT: - conlist = &owner->constraints; - break; - case CONSTRAINT_OBTYPE_BONE: - { - Bone *bone; - bPoseChannel *chan; - - bone = get_named_bone( ((bArmature *)owner->data), substring ); - chan = get_pose_channel(owner->pose, substring); - if (bone && chan) { - conlist = &chan->constraints; - } - } - break; - } - - /* Check all constraints - is constraint valid? */ - if (conlist) { - for (curcon = conlist->first; curcon; curcon=curcon->next) { - bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon); - ListBase targets = {NULL, NULL}; - bConstraintTarget *ct; - - /* clear disabled-flag first */ - curcon->flag &= ~CONSTRAINT_DISABLE; - - if (curcon->type == CONSTRAINT_TYPE_KINEMATIC) { - bKinematicConstraint *data = curcon->data; - - /* bad: we need a separate set of checks here as poletarget is - * optional... otherwise poletarget must exist too or else - * the constraint is deemed invalid - */ - if (exist_object(data->tar) == 0) { - data->tar = NULL; - curcon->flag |= CONSTRAINT_DISABLE; - } - else if (data->tar == owner) { - if (!get_named_bone(get_armature(owner), data->subtarget)) { - curcon->flag |= CONSTRAINT_DISABLE; - } - } - - if (data->poletar) { - if (exist_object(data->poletar) == 0) { - data->poletar = NULL; - curcon->flag |= CONSTRAINT_DISABLE; - } - else if (data->poletar == owner) { - if (!get_named_bone(get_armature(owner), data->polesubtarget)) { - curcon->flag |= CONSTRAINT_DISABLE; - } - } - } - - /* targets have already been checked for this */ - continue; - } - else if (curcon->type == CONSTRAINT_TYPE_ACTION) { - bActionConstraint *data = curcon->data; - - /* validate action */ - if (data->act == NULL) - curcon->flag |= CONSTRAINT_DISABLE; - } - else if (curcon->type == CONSTRAINT_TYPE_FOLLOWPATH) { - bFollowPathConstraint *data = curcon->data; - - /* don't allow track/up axes to be the same */ - if (data->upflag==data->trackflag) - curcon->flag |= CONSTRAINT_DISABLE; - if (data->upflag+3==data->trackflag) - curcon->flag |= CONSTRAINT_DISABLE; - } - else if (curcon->type == CONSTRAINT_TYPE_TRACKTO) { - bTrackToConstraint *data = curcon->data; - - /* don't allow track/up axes to be the same */ - if (data->reserved2==data->reserved1) - curcon->flag |= CONSTRAINT_DISABLE; - if (data->reserved2+3==data->reserved1) - curcon->flag |= CONSTRAINT_DISABLE; - } - else if (curcon->type == CONSTRAINT_TYPE_LOCKTRACK) { - bLockTrackConstraint *data = curcon->data; - - if (data->lockflag==data->trackflag) - curcon->flag |= CONSTRAINT_DISABLE; - if (data->lockflag+3==data->trackflag) - curcon->flag |= CONSTRAINT_DISABLE; - } - - /* Check targets for constraints */ - if (cti && cti->get_constraint_targets) { - cti->get_constraint_targets(curcon, &targets); - - /* disable and clear constraints targets that are incorrect */ - for (ct= targets.first; ct; ct= ct->next) { - /* general validity checks (for those constraints that need this) */ - if (exist_object(ct->tar) == 0) { - ct->tar = NULL; - curcon->flag |= CONSTRAINT_DISABLE; - } - else if (ct->tar == owner) { - if (!get_named_bone(get_armature(owner), ct->subtarget)) { - curcon->flag |= CONSTRAINT_DISABLE; - } - } - - /* target checks for specific constraints */ - if (ELEM(curcon->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO)) { - if (ct->tar) { - if (ct->tar->type != OB_CURVE) { - ct->tar= NULL; - curcon->flag |= CONSTRAINT_DISABLE; - } - else { - Curve *cu= ct->tar->data; - - /* auto-set 'Path' setting on curve so this works */ - cu->flag |= CU_PATH; - } - } - } - } - - /* free any temporary targets */ - if (cti->flush_constraint_targets) - cti->flush_constraint_targets(curcon, &targets, 0); - } - } - } -} - -static void test_bonelist_constraints (Object *owner, ListBase *list) -{ - Bone *bone; - - for (bone = list->first; bone; bone = bone->next) { - test_constraints(owner, bone->name); - test_bonelist_constraints(owner, &bone->childbase); - } -} - -void object_test_constraints (Object *owner) -{ - test_constraints(owner, ""); - - if (owner->type==OB_ARMATURE) { - bArmature *arm= get_armature(owner); - - if (arm) - test_bonelist_constraints(owner, &arm->bonebase); - } -} - -/* ********************** CONSTRAINT-SPECIFIC STUFF ********************* */ - -/* ---------- Distance-Dependent Constraints ---------- */ -/* StretchTo, Limit Distance */ - -static int stretchto_poll(bContext *C) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_StretchToConstraint); - return (ptr.id.data && ptr.data); -} - -static int stretchto_reset_exec (bContext *C, wmOperator *op) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_StretchToConstraint); - - /* just set original length to 0.0, which will cause a reset on next recalc */ - RNA_float_set(&ptr, "original_length", 0.0f); - - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL); - return OPERATOR_FINISHED; -} - -void CONSTRAINT_OT_stretchto_reset (wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Reset Original Length"; - ot->idname= "CONSTRAINT_OT_stretchto_reset"; - ot->description= "Reset original length of bone for Stretch To Constraint."; - - ot->exec= stretchto_reset_exec; - ot->poll= stretchto_poll; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - - -static int limitdistance_reset_exec (bContext *C, wmOperator *op) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_LimitDistanceConstraint); - - /* just set distance to 0.0, which will cause a reset on next recalc */ - RNA_float_set(&ptr, "distance", 0.0f); - - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL); - return OPERATOR_FINISHED; -} - -static int limitdistance_poll(bContext *C) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_LimitDistanceConstraint); - return (ptr.id.data && ptr.data); -} - -void CONSTRAINT_OT_limitdistance_reset (wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Reset Distance"; - ot->idname= "CONSTRAINT_OT_limitdistance_reset"; - ot->description= "Reset limiting distance for Limit Distance Constraint."; - - ot->exec= limitdistance_reset_exec; - ot->poll= limitdistance_poll; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/* ------------- Child-Of Constraint ------------------ */ - -static int childof_poll(bContext *C) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint); - return (ptr.id.data && ptr.data); -} - -/* ChildOf Constraint - set inverse callback */ -static int childof_set_inverse_exec (bContext *C, wmOperator *op) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint); - Scene *scene= CTX_data_scene(C); - Object *ob= ptr.id.data; - bConstraint *con= ptr.data; - bChildOfConstraint *data= (bChildOfConstraint *)con->data; - bPoseChannel *pchan= NULL; - - /* try to find a pose channel */ - // TODO: get from context instead? - if (ob && ob->pose) - pchan= get_active_posechannel(ob); - - /* calculate/set inverse matrix */ - if (pchan) { - float pmat[4][4], cinf; - float imat[4][4], tmat[4][4]; - - /* make copy of pchan's original pose-mat (for use later) */ - Mat4CpyMat4(pmat, pchan->pose_mat); - - /* disable constraint for pose to be solved without it */ - cinf= con->enforce; - con->enforce= 0.0f; - - /* solve pose without constraint */ - where_is_pose(scene, ob); - - /* determine effect of constraint by removing the newly calculated - * pchan->pose_mat from the original pchan->pose_mat, thus determining - * the effect of the constraint - */ - Mat4Invert(imat, pchan->pose_mat); - Mat4MulMat4(tmat, imat, pmat); - Mat4Invert(data->invmat, tmat); - - /* recalculate pose with new inv-mat */ - con->enforce= cinf; - where_is_pose(scene, ob); - } - else if (ob) { - Object workob; - /* use what_does_parent to find inverse - just like for normal parenting. - * NOTE: what_does_parent uses a static workob defined in object.c - */ - what_does_parent(scene, ob, &workob); - Mat4Invert(data->invmat, workob.obmat); - } - else - Mat4One(data->invmat); - - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); - - return OPERATOR_FINISHED; -} - -void CONSTRAINT_OT_childof_set_inverse (wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Set Inverse"; - ot->idname= "CONSTRAINT_OT_childof_set_inverse"; - ot->description= "Set inverse correction for ChildOf constraint."; - - ot->exec= childof_set_inverse_exec; - ot->poll= childof_poll; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/* ChildOf Constraint - clear inverse callback */ -static int childof_clear_inverse_exec (bContext *C, wmOperator *op) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint); - Object *ob= ptr.id.data; - bConstraint *con= ptr.data; - bChildOfConstraint *data= (bChildOfConstraint *)con->data; - - /* simply clear the matrix */ - Mat4One(data->invmat); - - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); - - return OPERATOR_FINISHED; -} - -void CONSTRAINT_OT_childof_clear_inverse (wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Clear Inverse"; - ot->idname= "CONSTRAINT_OT_childof_clear_inverse"; - ot->description= "Clear inverse correction for ChildOf constraint."; - - ot->exec= childof_clear_inverse_exec; - ot->poll= childof_poll; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/***************************** BUTTONS ****************************/ - -/* Rename the given constraint, con already has the new name */ -void ED_object_constraint_rename(Object *ob, bConstraint *con, char *oldname) -{ - bConstraint *tcon; - ListBase *conlist= NULL; - int from_object= 0; - - /* get context by searching for con (primitive...) */ - for (tcon= ob->constraints.first; tcon; tcon= tcon->next) { - if (tcon==con) - break; - } - - if (tcon) { - conlist= &ob->constraints; - from_object= 1; - } - else if (ob->pose) { - bPoseChannel *pchan; - - for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) { - for (tcon= pchan->constraints.first; tcon; tcon= tcon->next) { - if (tcon==con) - break; - } - if (tcon) - break; - } - - if (tcon) { - conlist= &pchan->constraints; - } - } - - if (conlist==NULL) { - printf("rename constraint failed\n"); /* should not happen in UI */ - return; - } - - /* first make sure it's a unique name within context */ - unique_constraint_name(con, conlist); -} - - - - -void ED_object_constraint_set_active(Object *ob, bConstraint *con) -{ - ListBase *lb; - bConstraint *origcon= con; - - /* lets be nice and escape if its active already */ - if(con && (con->flag & CONSTRAINT_ACTIVE)) - return ; - - lb= get_active_constraints(ob); - if(lb == NULL) - return; - - for(con= lb->first; con; con= con->next) { - if(con==origcon) con->flag |= CONSTRAINT_ACTIVE; - else con->flag &= ~CONSTRAINT_ACTIVE; - } -} - -static int constraint_poll(bContext *C) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint); - return (ptr.id.data && ptr.data); -} - -static int constraint_delete_exec (bContext *C, wmOperator *op) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint); - Object *ob= ptr.id.data; - bConstraint *con= ptr.data; - ListBase *lb; - - /* remove constraint itself */ - lb= get_active_constraints(ob); - free_constraint_data(con); - BLI_freelinkN(lb, con); - - ED_object_constraint_set_active(ob, NULL); - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); - - return OPERATOR_FINISHED; -} - -void CONSTRAINT_OT_delete (wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Delete Constraint"; - ot->idname= "CONSTRAINT_OT_delete"; - ot->description= "Remove constraitn from constraint stack."; - - /* callbacks */ - ot->exec= constraint_delete_exec; - ot->poll= constraint_poll; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int constraint_move_down_exec (bContext *C, wmOperator *op) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint); - Object *ob= ptr.id.data; - bConstraint *con= ptr.data; - - if (con->next) { - ListBase *conlist= get_active_constraints(ob); - bConstraint *nextCon= con->next; - - /* insert the nominated constraint after the one that used to be after it */ - BLI_remlink(conlist, con); - BLI_insertlinkafter(conlist, nextCon, con); - - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); - - return OPERATOR_FINISHED; - } - - return OPERATOR_CANCELLED; -} - -void CONSTRAINT_OT_move_down (wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Move Constraint Down"; - ot->idname= "CONSTRAINT_OT_move_down"; - ot->description= "Move constraint down constraint stack."; - - /* callbacks */ - ot->exec= constraint_move_down_exec; - ot->poll= constraint_poll; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - - -static int constraint_move_up_exec (bContext *C, wmOperator *op) -{ - PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint); - Object *ob= ptr.id.data; - bConstraint *con= ptr.data; - - if (con->prev) { - ListBase *conlist= get_active_constraints(ob); - bConstraint *prevCon= con->prev; - - /* insert the nominated constraint before the one that used to be before it */ - BLI_remlink(conlist, con); - BLI_insertlinkbefore(conlist, prevCon, con); - - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); - - return OPERATOR_FINISHED; - } - - return OPERATOR_CANCELLED; -} - -void CONSTRAINT_OT_move_up (wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Move Constraint Up"; - ot->idname= "CONSTRAINT_OT_move_up"; - ot->description= "Move constraint up constraint stack."; - - /* callbacks */ - ot->exec= constraint_move_up_exec; - ot->poll= constraint_poll; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/***************************** OPERATORS ****************************/ - -/************************ remove constraint operators *********************/ - -static int pose_constraints_clear_exec(bContext *C, wmOperator *op) -{ - Object *ob= CTX_data_active_object(C); - - /* free constraints for all selected bones */ - CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) - { - free_constraints(&pchan->constraints); - } - CTX_DATA_END; - - /* do updates */ - DAG_id_flush_update(&ob->id, OB_RECALC_OB); - WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_CONSTRAINT|NA_REMOVED, ob); - - return OPERATOR_FINISHED; -} - -void POSE_OT_constraints_clear(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Clear Constraints"; - ot->idname= "POSE_OT_constraints_clear"; - ot->description= "Clear all the constraints for the selected bones."; - - /* callbacks */ - ot->exec= pose_constraints_clear_exec; - ot->poll= ED_operator_posemode; // XXX - do we want to ensure there are selected bones too? -} - - -static int object_constraints_clear_exec(bContext *C, wmOperator *op) -{ - Object *ob= CTX_data_active_object(C); - - /* do freeing */ - // TODO: we should free constraints for all selected objects instead (to be more consistent with bones) - free_constraints(&ob->constraints); - - /* do updates */ - DAG_id_flush_update(&ob->id, OB_RECALC_OB); - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_constraints_clear(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Clear Constraints"; - ot->idname= "OBJECT_OT_constraints_clear"; - ot->description= "Clear all the constraints for the active Object only."; - - /* callbacks */ - ot->exec= object_constraints_clear_exec; - ot->poll= ED_operator_object_active; -} - -/************************ add constraint operators *********************/ - -/* get the Object and/or PoseChannel to use as target */ -static short get_new_constraint_target(bContext *C, int con_type, Object **tar_ob, bPoseChannel **tar_pchan, short add) -{ - Object *obact= CTX_data_active_object(C); - bPoseChannel *pchanact= get_active_posechannel(obact); - short only_curve= 0, only_mesh= 0, only_ob= 0; - short found= 0; - - /* clear tar_ob and tar_pchan fields before use - * - assume for now that both always exist... - */ - *tar_ob= NULL; - *tar_pchan= NULL; - - /* check if constraint type doesn't requires a target - * - if so, no need to get any targets - */ - switch (con_type) { - /* no-target constraints --------------------------- */ - /* null constraint - shouldn't even be added! */ - case CONSTRAINT_TYPE_NULL: - /* limit constraints - no targets needed */ - case CONSTRAINT_TYPE_LOCLIMIT: - case CONSTRAINT_TYPE_ROTLIMIT: - case CONSTRAINT_TYPE_SIZELIMIT: - return 0; - - /* restricted target-type constraints -------------- */ - /* NOTE: for these, we cannot try to add a target object if no valid ones are found, since that doesn't work */ - /* curve-based constraints - set the only_curve and only_ob flags */ - case CONSTRAINT_TYPE_TRACKTO: - case CONSTRAINT_TYPE_CLAMPTO: - case CONSTRAINT_TYPE_FOLLOWPATH: - only_curve= 1; - only_ob= 1; - add= 0; - break; - - /* mesh only? */ - case CONSTRAINT_TYPE_SHRINKWRAP: - only_mesh= 1; - only_ob= 1; - add= 0; - break; - - /* object only - add here is ok? */ - case CONSTRAINT_TYPE_RIGIDBODYJOINT: - only_ob= 1; - break; - } - - /* if the active Object is Armature, and we can search for bones, do so... */ - if ((obact->type == OB_ARMATURE) && (only_ob == 0)) { - /* search in list of selected Pose-Channels for target */ - CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) - { - /* just use the first one that we encounter, as long as it is not the active one */ - if (pchan != pchanact) { - *tar_ob= obact; - *tar_pchan= pchan; - found= 1; - - break; - } - } - CTX_DATA_END; - } - - /* if not yet found, try selected Objects... */ - if (found == 0) { - /* search in selected objects context */ - CTX_DATA_BEGIN(C, Object*, ob, selected_objects) - { - /* just use the first object we encounter (that isn't the active object) - * and which fulfills the criteria for the object-target that we've got - */ - if ( (ob != obact) && - ((!only_curve) || (ob->type == OB_CURVE)) && - ((!only_mesh) || (ob->type == OB_MESH)) ) - { - /* set target */ - *tar_ob= ob; - found= 1; - - /* perform some special operations on the target */ - if (only_curve) { - /* Curve-Path option must be enabled for follow-path constraints to be able to work */ - Curve *cu= (Curve *)ob->data; - cu->flag |= CU_PATH; - } - - break; - } - } - CTX_DATA_END; - } - - /* if still not found, add a new empty to act as a target (if allowed) */ - if ((found == 0) && (add)) { - Scene *scene= CTX_data_scene(C); - Base *base= BASACT, *newbase=NULL; - Object *obt; - - /* add new target object */ - obt= add_object(scene, OB_EMPTY); - - /* set layers OK */ - newbase= BASACT; - newbase->lay= base->lay; - obt->lay= newbase->lay; - - /* transform cent to global coords for loc */ - if (pchanact) { - /* since by default, IK targets the tip of the last bone, use the tip of the active PoseChannel - * if adding a target for an IK Constraint - */ - if (con_type == CONSTRAINT_TYPE_KINEMATIC) - VecMat4MulVecfl(obt->loc, obact->obmat, pchanact->pose_tail); - else - VecMat4MulVecfl(obt->loc, obact->obmat, pchanact->pose_head); - } - else - VECCOPY(obt->loc, obact->obmat[3]); - - /* restore, add_object sets active */ - BASACT= base; - base->flag |= SELECT; - - /* make our new target the new object */ - *tar_ob= obt; - found= 1; - } - - /* return whether there's any target */ - return found; -} - -/* used by add constraint operators to add the constraint required */ -static int constraint_add_exec(bContext *C, wmOperator *op, Object *ob, ListBase *list, int type, short setTarget) -{ - Scene *scene= CTX_data_scene(C); - bPoseChannel *pchan= get_active_posechannel(ob); - bConstraint *con; - - /* check if constraint to be added is valid for the given constraints stack */ - if (type == CONSTRAINT_TYPE_NULL) { - return OPERATOR_CANCELLED; - } - if ( (type == CONSTRAINT_TYPE_RIGIDBODYJOINT) && (list != &ob->constraints) ) { - BKE_report(op->reports, RPT_ERROR, "Rigid Body Joint Constraint can only be added to Objects."); - return OPERATOR_CANCELLED; - } - if ( (type == CONSTRAINT_TYPE_KINEMATIC) && ((!pchan) || (list != &pchan->constraints)) ) { - BKE_report(op->reports, RPT_ERROR, "IK Constraint can only be added to Bones."); - return OPERATOR_CANCELLED; - } - - /* create a new constraint of the type requried, and add it to the active/given constraints list */ - con = add_new_constraint(type); - - if (list) { - bConstraint *coniter; - - /* add new constraint to end of list of constraints before ensuring that it has a unique name - * (otherwise unique-naming code will fail, since it assumes element exists in list) - */ - BLI_addtail(list, con); - unique_constraint_name(con, list); - - /* if the target list is a list on some PoseChannel belonging to a proxy-protected - * Armature layer, we must tag newly added constraints with a flag which allows them - * to persist after proxy syncing has been done - */ - if (proxylocked_constraints_owner(ob, pchan)) - con->flag |= CONSTRAINT_PROXY_LOCAL; - - /* make this constraint the active one - * - since constraint was added at end of stack, we can just go - * through deactivating all previous ones - */ - con->flag |= CONSTRAINT_ACTIVE; - for (coniter= con->prev; coniter; coniter= coniter->prev) - coniter->flag &= ~CONSTRAINT_ACTIVE; - } - - /* get the first selected object/bone, and make that the target - * - apart from the buttons-window add buttons, we shouldn't add in this way - */ - if (setTarget) { - Object *tar_ob= NULL; - bPoseChannel *tar_pchan= NULL; - - /* get the target objects, adding them as need be */ - if (get_new_constraint_target(C, type, &tar_ob, &tar_pchan, 1)) { - /* method of setting target depends on the type of target we've got - * - by default, just set the first target (distinction here is only for multiple-targetted constraints) - */ - if (tar_pchan) - set_constraint_nth_target(con, tar_ob, tar_pchan->name, 0); - else - set_constraint_nth_target(con, tar_ob, "", 0); - } - } - - /* do type-specific tweaking to the constraint settings */ - switch (type) { - case CONSTRAINT_TYPE_CHILDOF: - { - /* if this constraint is being added to a posechannel, make sure - * the constraint gets evaluated in pose-space */ - if (ob->mode & OB_MODE_POSE) { - con->ownspace = CONSTRAINT_SPACE_POSE; - con->flag |= CONSTRAINT_SPACEONCE; - } - } - break; - - case CONSTRAINT_TYPE_PYTHON: // FIXME: this code is not really valid anymore - { - char *menustr; - int scriptint= 0; -#ifndef DISABLE_PYTHON - /* popup a list of usable scripts */ - menustr = buildmenu_pyconstraints(NULL, &scriptint); - scriptint = pupmenu(menustr); - MEM_freeN(menustr); - - /* only add constraint if a script was chosen */ - if (scriptint) { - /* add constraint */ - validate_pyconstraint_cb(con->data, &scriptint); - - /* make sure target allowance is set correctly */ - BPY_pyconstraint_update(ob, con); - } -#endif - } - default: - break; - } - - /* make sure all settings are valid - similar to above checks, but sometimes can be wrong */ - object_test_constraints(ob); - - if (ob->pose) - update_pose_constraint_flags(ob->pose); - - - /* force depsgraph to get recalculated since new relationships added */ - DAG_scene_sort(scene); /* sort order of objects */ - - if ((ob->type==OB_ARMATURE) && (pchan)) { - ob->pose->flag |= POSE_RECALC; /* sort pose channels */ - DAG_id_flush_update(&ob->id, OB_RECALC_DATA|OB_RECALC_OB); - } - else - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - - /* notifiers for updates */ - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, ob); - - return OPERATOR_FINISHED; -} - -/* ------------------ */ - -/* dummy operator callback */ -static int object_constraint_add_exec(bContext *C, wmOperator *op) -{ - ScrArea *sa= CTX_wm_area(C); - Object *ob; - int type= RNA_enum_get(op->ptr, "type"); - short with_targets= 0; - - /* get active object from context */ - if (sa->spacetype == SPACE_BUTS) - ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; - else - ob= CTX_data_active_object(C); - - if (!ob) { - BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to."); - return OPERATOR_CANCELLED; - } - - /* hack: set constraint targets from selected objects in context is allowed when - * operator name included 'with_targets', since the menu doesn't allow multiple properties - */ - if (strstr(op->idname, "with_targets")) - with_targets= 1; - - return constraint_add_exec(C, op, ob, &ob->constraints, type, with_targets); -} - -/* dummy operator callback */ -static int pose_constraint_add_exec(bContext *C, wmOperator *op) -{ - ScrArea *sa= CTX_wm_area(C); - Object *ob; - int type= RNA_enum_get(op->ptr, "type"); - short with_targets= 0; - - /* get active object from context */ - if (sa->spacetype == SPACE_BUTS) - ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; - else - ob= CTX_data_active_object(C); - - if (!ob) { - BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to."); - return OPERATOR_CANCELLED; - } - - /* hack: set constraint targets from selected objects in context is allowed when - * operator name included 'with_targets', since the menu doesn't allow multiple properties - */ - if (strstr(op->idname, "with_targets")) - with_targets= 1; - - return constraint_add_exec(C, op, ob, get_active_constraints(ob), type, with_targets); -} - -/* ------------------ */ - -void OBJECT_OT_constraint_add(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Add Constraint"; - ot->description = "Add a constraint to the active object."; - ot->idname= "OBJECT_OT_constraint_add"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_constraint_add_exec; - ot->poll= ED_operator_object_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - /* properties */ - RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", ""); -} - -void OBJECT_OT_constraint_add_with_targets(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Add Constraint (with Targets)"; - ot->description = "Add a constraint to the active object, with target (where applicable) set to the selected Objects/Bones."; - ot->idname= "OBJECT_OT_constraint_add_with_targets"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_constraint_add_exec; - ot->poll= ED_operator_object_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - /* properties */ - RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", ""); -} - -void POSE_OT_constraint_add(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Add Constraint"; - ot->description = "Add a constraint to the active bone."; - ot->idname= "POSE_OT_constraint_add"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= pose_constraint_add_exec; - ot->poll= ED_operator_posemode; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - /* properties */ - RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", ""); -} - -void POSE_OT_constraint_add_with_targets(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Add Constraint (with Targets)"; - ot->description = "Add a constraint to the active bone, with target (where applicable) set to the selected Objects/Bones."; - ot->idname= "POSE_OT_constraint_add_with_targets"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= pose_constraint_add_exec; - ot->poll= ED_operator_posemode; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - /* properties */ - RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", ""); -} - -/************************ IK Constraint operators *********************/ -/* NOTE: only for Pose-Channels */ -// TODO: should these be here, or back in editors/armature/poseobject.c again? - -/* present menu with options + validation for targets to use */ -static int pose_ik_add_invoke(bContext *C, wmOperator *op, wmEvent *evt) -{ - Object *ob= CTX_data_active_object(C); - bPoseChannel *pchan= get_active_posechannel(ob); - bConstraint *con= NULL; - - uiPopupMenu *pup; - uiLayout *layout; - Object *tar_ob= NULL; - bPoseChannel *tar_pchan= NULL; - - /* must have active bone */ - if (ELEM(NULL, ob, pchan)) { - BKE_report(op->reports, RPT_ERROR, "Must have active bone to add IK Constraint to."); - return OPERATOR_CANCELLED; - } - - /* bone must not have any constraints already */ - for (con= pchan->constraints.first; con; con= con->next) { - if (con->type==CONSTRAINT_TYPE_KINEMATIC) break; - } - if (con) { - BKE_report(op->reports, RPT_ERROR, "Bone already has IK Constraint."); - return OPERATOR_CANCELLED; - } - - /* prepare popup menu to choose targetting options */ - pup= uiPupMenuBegin(C, "Add IK", 0); - layout= uiPupMenuLayout(pup); - - /* the type of targets we'll set determines the menu entries to show... */ - if (get_new_constraint_target(C, CONSTRAINT_TYPE_KINEMATIC, &tar_ob, &tar_pchan, 0)) { - /* bone target, or object target? - * - the only thing that matters is that we want a target... - */ - if (tar_pchan) - uiItemBooleanO(layout, "To Active Bone", 0, "POSE_OT_ik_add", "with_targets", 1); - else - uiItemBooleanO(layout, "To Active Object", 0, "POSE_OT_ik_add", "with_targets", 1); - } - else { - /* we have a choice of adding to a new empty, or not setting any target (targetless IK) */ - uiItemBooleanO(layout, "To New Empty Object", 0, "POSE_OT_ik_add", "with_targets", 1); - uiItemBooleanO(layout, "Without Targets", 0, "POSE_OT_ik_add", "with_targets", 0); - } - - /* finish building the menu, and process it (should result in calling self again) */ - uiPupMenuEnd(C, pup); - - return OPERATOR_CANCELLED; -} - -/* call constraint_add_exec() to add the IK constraint */ -static int pose_ik_add_exec(bContext *C, wmOperator *op) -{ - Object *ob= CTX_data_active_object(C); - int with_targets= RNA_boolean_get(op->ptr, "with_targets"); - - /* add the constraint - all necessary checks should have been done by the invoke() callback already... */ - return constraint_add_exec(C, op, ob, get_active_constraints(ob), CONSTRAINT_TYPE_KINEMATIC, with_targets); -} - -void POSE_OT_ik_add(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Add IK to Bone"; - ot->description= "Add IK Constraint to the active Bone."; - ot->idname= "POSE_OT_ik_add"; - - /* api callbacks */ - ot->invoke= pose_ik_add_invoke; - ot->exec= pose_ik_add_exec; - ot->poll= ED_operator_posemode; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - /* properties */ - RNA_def_boolean(ot->srna, "with_targets", 1, "With Targets", "Assign IK Constraint with targets derived from the select bones/objects"); -} - -/* ------------------ */ - -/* remove IK constraints from selected bones */ -static int pose_ik_clear_exec(bContext *C, wmOperator *op) -{ - Object *ob= CTX_data_active_object(C); - - /* only remove IK Constraints */ - CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) - { - bConstraint *con, *next; - - // TODO: should we be checking if these contraints were local before we try and remove them? - for (con= pchan->constraints.first; con; con= next) { - next= con->next; - if (con->type==CONSTRAINT_TYPE_KINEMATIC) { - free_constraint_data(con); - BLI_freelinkN(&pchan->constraints, con); - } - } - pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET); - } - CTX_DATA_END; - - /* */ - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - - /* note, notifier might evolve */ - WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob); - - return OPERATOR_FINISHED; -} - -void POSE_OT_ik_clear(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Remove IK"; - ot->description= "Remove all IK Constraints from selected bones."; - ot->idname= "POSE_OT_ik_clear"; - - /* api callbacks */ - ot->exec= pose_ik_clear_exec; - ot->poll= ED_operator_posemode; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} diff --git a/source/blender/editors/object/editgroup.c b/source/blender/editors/object/editgroup.c deleted file mode 100644 index 9a184892e71..00000000000 --- a/source/blender/editors/object/editgroup.c +++ /dev/null @@ -1,257 +0,0 @@ -/** - * $Id$ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) Blender Foundation - * All rights reserved. - * - * The Original Code is: all of this file. - * - * Contributor(s): none yet. - * - * ***** END GPL LICENSE BLOCK ***** - */ - -#include - -#include "MEM_guardedalloc.h" - -#include "BLI_blenlib.h" -#include "BLI_arithb.h" - -#include "DNA_group_types.h" -#include "DNA_object_types.h" -#include "DNA_scene_types.h" -#include "DNA_view3d_types.h" - -#include "BKE_depsgraph.h" -#include "BKE_group.h" -#include "BKE_global.h" -#include "BKE_context.h" -#include "BKE_main.h" -#include "BKE_report.h" - -#include "ED_view3d.h" -#include "ED_space_api.h" -#include "ED_screen.h" -#include "ED_types.h" -#include "ED_util.h" - -#include "UI_interface.h" -#include "UI_resources.h" - -#include "WM_api.h" -#include "WM_types.h" - -#include "RNA_access.h" -#include "RNA_define.h" - -#include "object_intern.h" - -static int objects_add_active_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - Object *ob= OBACT, *obt; - Group *group; - int ok = 0; - - if (!ob) return OPERATOR_CANCELLED; - - /* linking to same group requires its own loop so we can avoid - looking up the active objects groups each time */ - - group= G.main->group.first; - while(group) { - if(object_in_group(ob, group)) { - /* Assign groups to selected objects */ - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - obt= base->object; - add_to_group(group, obt); - obt->flag |= OB_FROMGROUP; - base->flag |= OB_FROMGROUP; - base->object->recalc= OB_RECALC_OB; - ok = 1; - } - CTX_DATA_END; - } - group= group->id.next; - } - - if (!ok) BKE_report(op->reports, RPT_ERROR, "Active Object contains no groups"); - - DAG_scene_sort(CTX_data_scene(C)); - - WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL); - - return OPERATOR_FINISHED; - -} - -void GROUP_OT_objects_add_active(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Add Selected To Active Group"; - ot->description = "Add the object to an object group that contains the active object."; - ot->idname= "GROUP_OT_objects_add_active"; - - /* api callbacks */ - ot->exec= objects_add_active_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int objects_remove_active_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - Object *ob= OBACT, *obt; - Group *group; - int ok = 0; - - if (!ob) return OPERATOR_CANCELLED; - - /* linking to same group requires its own loop so we can avoid - looking up the active objects groups each time */ - - group= G.main->group.first; - while(group) { - if(object_in_group(ob, group)) { - /* Assign groups to selected objects */ - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - obt= base->object; - rem_from_group(group, obt); - obt->flag &= ~OB_FROMGROUP; - base->flag &= ~OB_FROMGROUP; - base->object->recalc= OB_RECALC_OB; - ok = 1; - } - CTX_DATA_END; - } - group= group->id.next; - } - - if (!ok) BKE_report(op->reports, RPT_ERROR, "Active Object contains no groups"); - - DAG_scene_sort(CTX_data_scene(C)); - - WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL); - - return OPERATOR_FINISHED; - -} - -void GROUP_OT_objects_remove_active(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Remove Selected From Active Group"; - ot->description = "Remove the object from an object group that contains the active object."; - ot->idname= "GROUP_OT_objects_remove_active"; - - /* api callbacks */ - ot->exec= objects_remove_active_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int group_remove_exec(bContext *C, wmOperator *op) -{ - Group *group= NULL; - - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - group = NULL; - while( (group = find_group(base->object, group)) ) { - rem_from_group(group, base->object); - } - base->object->flag &= ~OB_FROMGROUP; - base->flag &= ~OB_FROMGROUP; - base->object->recalc= OB_RECALC_OB; - } - CTX_DATA_END; - - DAG_scene_sort(CTX_data_scene(C)); - - WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL); - - return OPERATOR_FINISHED; - -} - -void GROUP_OT_objects_remove(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Remove From Groups"; - ot->description = "Remove selected objects from all groups."; - ot->idname= "GROUP_OT_objects_remove"; - - /* api callbacks */ - ot->exec= group_remove_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int group_create_exec(bContext *C, wmOperator *op) -{ - Group *group= NULL; - char gid[32]; //group id - - RNA_string_get(op->ptr, "GID", gid); - - group= add_group(gid); - - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - add_to_group(group, base->object); - base->object->flag |= OB_FROMGROUP; - base->flag |= OB_FROMGROUP; - base->object->recalc= OB_RECALC_OB; - } - CTX_DATA_END; - - DAG_scene_sort(CTX_data_scene(C)); - - WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL); - - return OPERATOR_FINISHED; - -} - -void GROUP_OT_group_create(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Create New Group"; - ot->description = "Create an object group."; - ot->idname= "GROUP_OT_group_create"; - - /* api callbacks */ - ot->exec= group_create_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_string(ot->srna, "GID", "Group", 32, "Name", "Name of the new group"); -} - diff --git a/source/blender/editors/object/editkey.c b/source/blender/editors/object/editkey.c deleted file mode 100644 index 2ec3edd846a..00000000000 --- a/source/blender/editors/object/editkey.c +++ /dev/null @@ -1,539 +0,0 @@ -/** - * $Id$ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * Contributor(s): Blender Foundation, shapekey support - * - * ***** END GPL LICENSE BLOCK ***** - */ - -#include -#include - -#ifndef WIN32 -#include -#else -#include -#endif - -#include "MEM_guardedalloc.h" - -#include "BLI_blenlib.h" -#include "BLI_arithb.h" - -#include "DNA_action_types.h" -#include "DNA_curve_types.h" -#include "DNA_ipo_types.h" -#include "DNA_key_types.h" -#include "DNA_lattice_types.h" -#include "DNA_mesh_types.h" -#include "DNA_meshdata_types.h" -#include "DNA_object_types.h" -#include "DNA_scene_types.h" -#include "DNA_screen_types.h" -#include "DNA_space_types.h" -#include "DNA_userdef_types.h" -#include "DNA_view2d_types.h" - -#include "BKE_action.h" -#include "BKE_anim.h" -#include "BKE_context.h" -#include "BKE_curve.h" -#include "BKE_depsgraph.h" -#include "BKE_global.h" -#include "BKE_ipo.h" -#include "BKE_key.h" -#include "BKE_library.h" -#include "BKE_main.h" -#include "BKE_mesh.h" -#include "BKE_object.h" -#include "BKE_utildefines.h" - -#include "BLO_sys_types.h" // for intptr_t support - -#include "ED_object.h" - -#include "RNA_access.h" - -#include "WM_api.h" -#include "WM_types.h" - -#include "object_intern.h" - -#if 0 // XXX old animation system -static void default_key_ipo(Scene *scene, Key *key) -{ - IpoCurve *icu; - BezTriple *bezt; - - key->ipo= add_ipo(scene, "KeyIpo", ID_KE); - - icu= MEM_callocN(sizeof(IpoCurve), "ipocurve"); - - icu->blocktype= ID_KE; - icu->adrcode= KEY_SPEED; - icu->flag= IPO_VISIBLE|IPO_SELECT|IPO_AUTO_HORIZ; - set_icu_vars(icu); - - BLI_addtail( &(key->ipo->curve), icu); - - icu->bezt= bezt= MEM_callocN(2*sizeof(BezTriple), "defaultipo"); - icu->totvert= 2; - - bezt->hide= IPO_BEZ; - bezt->f1=bezt->f2= bezt->f3= SELECT; - bezt->h1= bezt->h2= HD_AUTO; - bezt++; - bezt->vec[1][0]= 100.0; - bezt->vec[1][1]= 1.0; - bezt->hide= IPO_BEZ; - bezt->f1=bezt->f2= bezt->f3= SELECT; - bezt->h1= bezt->h2= HD_AUTO; - - calchandles_ipocurve(icu); -} -#endif // XXX old animation system - - -/************************* Mesh ************************/ - -void mesh_to_key(Mesh *me, KeyBlock *kb) -{ - MVert *mvert; - float *fp; - int a; - - if(me->totvert==0) return; - - if(kb->data) MEM_freeN(kb->data); - - kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data"); - kb->totelem= me->totvert; - - mvert= me->mvert; - fp= kb->data; - for(a=0; atotelem; a++, fp+=3, mvert++) { - VECCOPY(fp, mvert->co); - - } -} - -void key_to_mesh(KeyBlock *kb, Mesh *me) -{ - MVert *mvert; - float *fp; - int a, tot; - - mvert= me->mvert; - fp= kb->data; - - tot= MIN2(kb->totelem, me->totvert); - - for(a=0; aco, fp); - } -} - -static KeyBlock *add_keyblock(Scene *scene, Key *key) -{ - KeyBlock *kb; - float curpos= -0.1; - int tot; - - kb= key->block.last; - if(kb) curpos= kb->pos; - - kb= MEM_callocN(sizeof(KeyBlock), "Keyblock"); - BLI_addtail(&key->block, kb); - kb->type= KEY_CARDINAL; - - tot= BLI_countlist(&key->block); - if(tot==1) strcpy(kb->name, "Basis"); - else sprintf(kb->name, "Key %d", tot-1); - - // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey) - kb->adrcode= tot-1; - - key->totkey++; - if(key->totkey==1) key->refkey= kb; - - kb->slidermin= 0.0f; - kb->slidermax= 1.0f; - - // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor... - if(key->type == KEY_RELATIVE) - kb->pos= curpos+0.1; - else { -#if 0 // XXX old animation system - curpos= bsystem_time(scene, 0, (float)CFRA, 0.0); - if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) { - curpos /= 100.0; - } - kb->pos= curpos; - - sort_keys(key); -#endif // XXX old animation system - } - return kb; -} - -void insert_meshkey(Scene *scene, Mesh *me, short rel) -{ - Key *key; - KeyBlock *kb; - - if(me->key==NULL) { - me->key= add_key( (ID *)me); - - if(rel) - me->key->type = KEY_RELATIVE; -// else -// default_key_ipo(scene, me->key); // XXX old animation system - } - key= me->key; - - kb= add_keyblock(scene, key); - - mesh_to_key(me, kb); -} - -/************************* Lattice ************************/ - -void latt_to_key(Lattice *lt, KeyBlock *kb) -{ - BPoint *bp; - float *fp; - int a, tot; - - tot= lt->pntsu*lt->pntsv*lt->pntsw; - if(tot==0) return; - - if(kb->data) MEM_freeN(kb->data); - - kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data"); - kb->totelem= tot; - - bp= lt->def; - fp= kb->data; - for(a=0; atotelem; a++, fp+=3, bp++) { - VECCOPY(fp, bp->vec); - } -} - -void key_to_latt(KeyBlock *kb, Lattice *lt) -{ - BPoint *bp; - float *fp; - int a, tot; - - bp= lt->def; - fp= kb->data; - - tot= lt->pntsu*lt->pntsv*lt->pntsw; - tot= MIN2(kb->totelem, tot); - - for(a=0; avec, fp); - } - -} - -/* exported to python... hrms, should not, use object levels! (ton) */ -void insert_lattkey(Scene *scene, Lattice *lt, short rel) -{ - Key *key; - KeyBlock *kb; - - if(lt->key==NULL) { - lt->key= add_key( (ID *)lt); -// default_key_ipo(scene, lt->key); // XXX old animation system - } - key= lt->key; - - kb= add_keyblock(scene, key); - - latt_to_key(lt, kb); -} - -/************************* Curve ************************/ - -void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb) -{ - Nurb *nu; - BezTriple *bezt; - BPoint *bp; - float *fp; - int a, tot; - - /* count */ - tot= count_curveverts(nurb); - if(tot==0) return; - - if(kb->data) MEM_freeN(kb->data); - - kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data"); - kb->totelem= tot; - - nu= nurb->first; - fp= kb->data; - while(nu) { - - if(nu->bezt) { - bezt= nu->bezt; - a= nu->pntsu; - while(a--) { - VECCOPY(fp, bezt->vec[0]); - fp+= 3; - VECCOPY(fp, bezt->vec[1]); - fp+= 3; - VECCOPY(fp, bezt->vec[2]); - fp+= 3; - fp[0]= bezt->alfa; - fp+= 3; /* alphas */ - bezt++; - } - } - else { - bp= nu->bp; - a= nu->pntsu*nu->pntsv; - while(a--) { - VECCOPY(fp, bp->vec); - fp[3]= bp->alfa; - - fp+= 4; - bp++; - } - } - nu= nu->next; - } -} - -void key_to_curve(KeyBlock *kb, Curve *cu, ListBase *nurb) -{ - Nurb *nu; - BezTriple *bezt; - BPoint *bp; - float *fp; - int a, tot; - - nu= nurb->first; - fp= kb->data; - - tot= count_curveverts(nurb); - - tot= MIN2(kb->totelem, tot); - - while(nu && tot>0) { - - if(nu->bezt) { - bezt= nu->bezt; - a= nu->pntsu; - while(a-- && tot>0) { - VECCOPY(bezt->vec[0], fp); - fp+= 3; - VECCOPY(bezt->vec[1], fp); - fp+= 3; - VECCOPY(bezt->vec[2], fp); - fp+= 3; - bezt->alfa= fp[0]; - fp+= 3; /* alphas */ - - tot-= 3; - bezt++; - } - } - else { - bp= nu->bp; - a= nu->pntsu*nu->pntsv; - while(a-- && tot>0) { - VECCOPY(bp->vec, fp); - bp->alfa= fp[3]; - - fp+= 4; - tot--; - bp++; - } - } - nu= nu->next; - } -} - - -void insert_curvekey(Scene *scene, Curve *cu, short rel) -{ - Key *key; - KeyBlock *kb; - - if(cu->key==NULL) { - cu->key= add_key( (ID *)cu); - - if(rel) - cu->key->type = KEY_RELATIVE; -// else -// default_key_ipo(scene, cu->key); // XXX old animation system - } - key= cu->key; - - kb= add_keyblock(scene, key); - - if(cu->editnurb->first) curve_to_key(cu, kb, cu->editnurb); - else curve_to_key(cu, kb, &cu->nurb); -} - -/*********************** add shape key ***********************/ - -void ED_object_shape_key_add(bContext *C, Scene *scene, Object *ob) -{ - Key *key; - - if(ob->type==OB_MESH) insert_meshkey(scene, ob->data, 1); - else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob->data, 1); - else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob->data, 1); - - key= ob_get_key(ob); - ob->shapenr= BLI_countlist(&key->block); - - WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob); -} - -/*********************** remove shape key ***********************/ - -int ED_object_shape_key_remove(bContext *C, Scene *scene, Object *ob) -{ - Main *bmain= CTX_data_main(C); - KeyBlock *kb, *rkb; - Key *key; - //IpoCurve *icu; - - key= ob_get_key(ob); - if(key==NULL) - return 0; - - kb= BLI_findlink(&key->block, ob->shapenr-1); - - if(kb) { - for(rkb= key->block.first; rkb; rkb= rkb->next) - if(rkb->relative == ob->shapenr-1) - rkb->relative= 0; - - BLI_remlink(&key->block, kb); - key->totkey--; - if(key->refkey== kb) - key->refkey= key->block.first; - - if(kb->data) MEM_freeN(kb->data); - MEM_freeN(kb); - - for(kb= key->block.first; kb; kb= kb->next) - if(kb->adrcode>=ob->shapenr) - kb->adrcode--; - -#if 0 // XXX old animation system - if(key->ipo) { - - for(icu= key->ipo->curve.first; icu; icu= icu->next) { - if(icu->adrcode==ob->shapenr-1) { - BLI_remlink(&key->ipo->curve, icu); - free_ipo_curve(icu); - break; - } - } - for(icu= key->ipo->curve.first; icu; icu= icu->next) - if(icu->adrcode>=ob->shapenr) - icu->adrcode--; - } -#endif // XXX old animation system - - if(ob->shapenr>1) ob->shapenr--; - } - - if(key->totkey==0) { - if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL; - else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL; - else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL; - - free_libblock_us(&(bmain->key), key); - } - - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob); - - return 1; -} - -/********************** shape key operators *********************/ - -static int shape_key_poll(bContext *C) -{ - Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; - ID *data= (ob)? ob->data: NULL; - return (ob && !ob->id.lib && data && !data->lib); -} - -static int shape_key_add_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; - - ED_object_shape_key_add(C, scene, ob); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_shape_key_add(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Add Shape Key"; - ot->idname= "OBJECT_OT_shape_key_add"; - - /* api callbacks */ - ot->poll= shape_key_poll; - ot->exec= shape_key_add_exec; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int shape_key_remove_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; - - if(!ED_object_shape_key_remove(C, scene, ob)) - return OPERATOR_CANCELLED; - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_shape_key_remove(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Remove Shape Key"; - ot->idname= "OBJECT_OT_shape_key_remove"; - - /* api callbacks */ - ot->poll= shape_key_poll; - ot->exec= shape_key_remove_exec; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - diff --git a/source/blender/editors/object/editlattice.c b/source/blender/editors/object/editlattice.c deleted file mode 100644 index 3ec1f3af014..00000000000 --- a/source/blender/editors/object/editlattice.c +++ /dev/null @@ -1,388 +0,0 @@ -/** - * $Id$ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * Contributor(s): Blender Foundation - * - * ***** END GPL LICENSE BLOCK ***** - */ - -#include -#include -#include - -#include "MEM_guardedalloc.h" - -#include "BLI_blenlib.h" -#include "BLI_arithb.h" - -#include "DNA_curve_types.h" -#include "DNA_key_types.h" -#include "DNA_lattice_types.h" -#include "DNA_meshdata_types.h" -#include "DNA_object_types.h" -#include "DNA_scene_types.h" -#include "DNA_view3d_types.h" - -#include "BKE_armature.h" -#include "BKE_context.h" -#include "BKE_depsgraph.h" -#include "BKE_global.h" -#include "BKE_key.h" -#include "BKE_lattice.h" -#include "BKE_mesh.h" -#include "BKE_utildefines.h" - -#include "ED_object.h" -#include "ED_screen.h" -#include "ED_view3d.h" -#include "ED_util.h" - -#include "WM_api.h" -#include "WM_types.h" - -#include "object_intern.h" - -/********************** Load/Make/Free ********************/ - -void free_editLatt(Object *ob) -{ - Lattice *lt= ob->data; - - if(lt->editlatt) { - if(lt->editlatt->def) - MEM_freeN(lt->editlatt->def); - if(lt->editlatt->dvert) - free_dverts(lt->editlatt->dvert, lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw); - - MEM_freeN(lt->editlatt); - lt->editlatt= NULL; - } -} - -void make_editLatt(Object *obedit) -{ - Lattice *lt= obedit->data; - KeyBlock *actkey; - - free_editLatt(obedit); - - lt= obedit->data; - - actkey= ob_get_keyblock(obedit); - if(actkey) - key_to_latt(actkey, lt); - - lt->editlatt= MEM_dupallocN(lt); - lt->editlatt->def= MEM_dupallocN(lt->def); - - if(lt->dvert) { - int tot= lt->pntsu*lt->pntsv*lt->pntsw; - lt->editlatt->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert"); - copy_dverts(lt->editlatt->dvert, lt->dvert, tot); - } -} - -void load_editLatt(Object *obedit) -{ - Lattice *lt; - KeyBlock *actkey; - BPoint *bp; - float *fp; - int tot; - - lt= obedit->data; - - actkey= ob_get_keyblock(obedit); - - if(actkey) { - /* active key: vertices */ - tot= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; - - if(actkey->data) MEM_freeN(actkey->data); - - fp=actkey->data= MEM_callocN(lt->key->elemsize*tot, "actkey->data"); - actkey->totelem= tot; - - bp= lt->editlatt->def; - while(tot--) { - VECCOPY(fp, bp->vec); - fp+= 3; - bp++; - } - } - else { - MEM_freeN(lt->def); - - lt->def= MEM_dupallocN(lt->editlatt->def); - - lt->flag= lt->editlatt->flag; - - lt->pntsu= lt->editlatt->pntsu; - lt->pntsv= lt->editlatt->pntsv; - lt->pntsw= lt->editlatt->pntsw; - - lt->typeu= lt->editlatt->typeu; - lt->typev= lt->editlatt->typev; - lt->typew= lt->editlatt->typew; - } - - if(lt->dvert) { - free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw); - lt->dvert= NULL; - } - - if(lt->editlatt->dvert) { - int tot= lt->pntsu*lt->pntsv*lt->pntsw; - - lt->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert"); - copy_dverts(lt->dvert, lt->editlatt->dvert, tot); - } -} - -/************************** Operators *************************/ - -static void setflagsLatt(Object *obedit, int flag) -{ - Lattice *lt= obedit->data; - BPoint *bp; - int a; - - bp= lt->editlatt->def; - - a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; - - while(a--) { - if(bp->hide==0) { - bp->f1= flag; - } - bp++; - } -} - -int de_select_all_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - Lattice *lt= obedit->data; - BPoint *bp; - int a, deselect= 0; - - bp= lt->editlatt->def; - a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; - - while(a--) { - if(bp->hide==0) { - if(bp->f1) { - deselect= 1; - break; - } - } - bp++; - } - - if(deselect) - setflagsLatt(obedit, 0); - else - setflagsLatt(obedit, 1); - - WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data); - - return OPERATOR_FINISHED; -} - -void LATTICE_OT_select_all_toggle(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Select or Deselect All"; - ot->idname= "LATTICE_OT_select_all_toggle"; - - /* api callbacks */ - ot->exec= de_select_all_exec; - ot->poll= ED_operator_editlattice; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -int make_regular_poll(bContext *C) -{ - Object *ob; - - if(ED_operator_editlattice(C)) return 1; - - ob= CTX_data_active_object(C); - return (ob && ob->type==OB_LATTICE); -} - -int make_regular_exec(bContext *C, wmOperator *op) -{ - Object *ob= CTX_data_edit_object(C); - Lattice *lt; - - if(ob) { - lt= ob->data; - resizelattice(lt->editlatt, lt->pntsu, lt->pntsv, lt->pntsw, NULL); - } - else { - ob= CTX_data_active_object(C); - lt= ob->data; - resizelattice(lt, lt->pntsu, lt->pntsv, lt->pntsw, NULL); - } - - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data); - - return OPERATOR_FINISHED; -} - -void LATTICE_OT_make_regular(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Make Regular"; - ot->idname= "LATTICE_OT_make_regular"; - - /* api callbacks */ - ot->exec= make_regular_exec; - ot->poll= make_regular_poll; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/****************************** Mouse Selection *************************/ - -static void findnearestLattvert__doClosest(void *userData, BPoint *bp, int x, int y) -{ - struct { BPoint *bp; short dist, select, mval[2]; } *data = userData; - float temp = abs(data->mval[0]-x) + abs(data->mval[1]-y); - - if((bp->f1 & SELECT)==data->select) - temp += 5; - - if(tempdist) { - data->dist = temp; - - data->bp = bp; - } -} - -static BPoint *findnearestLattvert(ViewContext *vc, short mval[2], int sel) -{ - /* sel==1: selected gets a disadvantage */ - /* in nurb and bezt or bp the nearest is written */ - /* return 0 1 2: handlepunt */ - struct { BPoint *bp; short dist, select, mval[2]; } data = {0}; - - data.dist = 100; - data.select = sel; - data.mval[0]= mval[0]; - data.mval[1]= mval[1]; - - lattice_foreachScreenVert(vc, findnearestLattvert__doClosest, &data); - - return data.bp; -} - -void mouse_lattice(bContext *C, short mval[2], int extend) -{ - ViewContext vc; - BPoint *bp= NULL; - - view3d_set_viewcontext(C, &vc); - bp= findnearestLattvert(&vc, mval, 1); - - if(bp) { - if(extend==0) { - setflagsLatt(vc.obedit, 0); - bp->f1 |= SELECT; - } - else - bp->f1 ^= SELECT; /* swap */ - - WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc.obedit->data); - } -} - -/******************************** Undo *************************/ - -typedef struct UndoLattice { - BPoint *def; - int pntsu, pntsv, pntsw; -} UndoLattice; - -static void undoLatt_to_editLatt(void *data, void *edata) -{ - UndoLattice *ult= (UndoLattice*)data; - Lattice *editlatt= (Lattice *)edata; - int a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw; - - memcpy(editlatt->def, ult->def, a*sizeof(BPoint)); -} - -static void *editLatt_to_undoLatt(void *edata) -{ - UndoLattice *ult= MEM_callocN(sizeof(UndoLattice), "UndoLattice"); - Lattice *editlatt= (Lattice *)edata; - - ult->def= MEM_dupallocN(editlatt->def); - ult->pntsu= editlatt->pntsu; - ult->pntsv= editlatt->pntsv; - ult->pntsw= editlatt->pntsw; - - return ult; -} - -static void free_undoLatt(void *data) -{ - UndoLattice *ult= (UndoLattice*)data; - - if(ult->def) MEM_freeN(ult->def); - MEM_freeN(ult); -} - -static int validate_undoLatt(void *data, void *edata) -{ - UndoLattice *ult= (UndoLattice*)data; - Lattice *editlatt= (Lattice *)edata; - - return (ult->pntsu == editlatt->pntsu && - ult->pntsv == editlatt->pntsv && - ult->pntsw == editlatt->pntsw); -} - -static void *get_editlatt(bContext *C) -{ - Object *obedit= CTX_data_edit_object(C); - - if(obedit && obedit->type==OB_LATTICE) { - Lattice *lt= obedit->data; - return lt->editlatt; - } - - return NULL; -} - -/* and this is all the undo system needs to know */ -void undo_push_lattice(bContext *C, char *name) -{ - undo_editmode_push(C, name, get_editlatt, free_undoLatt, undoLatt_to_editLatt, editLatt_to_undoLatt, validate_undoLatt); -} - diff --git a/source/blender/editors/object/object_add.c b/source/blender/editors/object/object_add.c new file mode 100644 index 00000000000..eaf7d41b9fe --- /dev/null +++ b/source/blender/editors/object/object_add.c @@ -0,0 +1,1451 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * Contributor(s): Blender Foundation, 2002-2008 full recode + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include + +#include "MEM_guardedalloc.h" + +#include "DNA_action_types.h" +#include "DNA_curve_types.h" +#include "DNA_group_types.h" +#include "DNA_material_types.h" +#include "DNA_mesh_types.h" +#include "DNA_meta_types.h" +#include "DNA_object_fluidsim.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" +#include "DNA_screen_types.h" +#include "DNA_userdef_types.h" +#include "DNA_view3d_types.h" +#include "DNA_vfont_types.h" + +#include "BLI_arithb.h" +#include "BLI_listbase.h" + +#include "BKE_anim.h" +#include "BKE_armature.h" +#include "BKE_constraint.h" +#include "BKE_context.h" +#include "BKE_curve.h" +#include "BKE_customdata.h" +#include "BKE_depsgraph.h" +#include "BKE_DerivedMesh.h" +#include "BKE_displist.h" +#include "BKE_global.h" +#include "BKE_group.h" +#include "BKE_lattice.h" +#include "BKE_library.h" +#include "BKE_main.h" +#include "BKE_material.h" +#include "BKE_mball.h" +#include "BKE_mesh.h" +#include "BKE_modifier.h" +#include "BKE_object.h" +#include "BKE_particle.h" +#include "BKE_report.h" +#include "BKE_sca.h" +#include "BKE_scene.h" +#include "BKE_texture.h" +#include "BKE_utildefines.h" + +#include "RNA_access.h" +#include "RNA_define.h" +#include "RNA_enum_types.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_anim_api.h" +#include "ED_armature.h" +#include "ED_curve.h" +#include "ED_mball.h" +#include "ED_mesh.h" +#include "ED_object.h" +#include "ED_screen.h" +#include "ED_transform.h" + +#include "UI_interface.h" +#include "UI_resources.h" + +#include "object_intern.h" + +/************************** Exported *****************************/ + +void ED_object_base_init_from_view(bContext *C, Base *base) +{ + View3D *v3d= CTX_wm_view3d(C); + Scene *scene= CTX_data_scene(C); + Object *ob= base->object; + + if (scene==NULL) + return; + + if (v3d==NULL) { + base->lay = scene->lay; + VECCOPY(ob->loc, scene->cursor); + } + else { + if (v3d->localview) { + base->lay= ob->lay= v3d->layact | v3d->lay; + VECCOPY(ob->loc, v3d->cursor); + } + else { + base->lay= ob->lay= v3d->layact; + VECCOPY(ob->loc, scene->cursor); + } + + if (U.flag & USER_ADD_VIEWALIGNED) { + ARegion *ar= CTX_wm_region(C); + if(ar) { + RegionView3D *rv3d= ar->regiondata; + + rv3d->viewquat[0]= -rv3d->viewquat[0]; + QuatToEul(rv3d->viewquat, ob->rot); + rv3d->viewquat[0]= -rv3d->viewquat[0]; + } + } + } + where_is_object(scene, ob); +} + +/********************* Add Object Operator ********************/ + +void add_object_draw(Scene *scene, View3D *v3d, int type) /* for toolbox or menus, only non-editmode stuff */ +{ + /* keep here to get things compile, remove later */ +} + +/* for object add primitive operators */ +static Object *object_add_type(bContext *C, int type) +{ + Scene *scene= CTX_data_scene(C); + Object *ob; + + /* for as long scene has editmode... */ + if (CTX_data_edit_object(C)) + ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */ + + /* deselects all, sets scene->basact */ + ob= add_object(scene, type); + /* editor level activate, notifiers */ + ED_base_object_activate(C, BASACT); + + /* more editor stuff */ + ED_object_base_init_from_view(C, BASACT); + + DAG_scene_sort(scene); + + return ob; +} + +/* for object add operator */ +static int object_add_exec(bContext *C, wmOperator *op) +{ + object_add_type(C, RNA_int_get(op->ptr, "type")); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Object"; + ot->description = "Add an object to the scene."; + ot->idname= "OBJECT_OT_add"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_add_exec; + + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", object_type_items, 0, "Type", ""); +} + +/* ***************** add primitives *************** */ +/* ****** work both in and outside editmode ****** */ + +static EnumPropertyItem prop_mesh_types[] = { + {0, "PLANE", ICON_MESH_PLANE, "Plane", ""}, + {1, "CUBE", ICON_MESH_CUBE, "Cube", ""}, + {2, "CIRCLE", ICON_MESH_CIRCLE, "Circle", ""}, + {3, "UVSPHERE", ICON_MESH_UVSPHERE, "UVsphere", ""}, + {4, "ICOSPHERE", ICON_MESH_ICOSPHERE, "Icosphere", ""}, + {5, "CYLINDER", ICON_MESH_TUBE, "Cylinder", ""}, + {6, "CONE", ICON_MESH_CONE, "Cone", ""}, + {0, "", 0, NULL, NULL}, + {7, "GRID", ICON_MESH_GRID, "Grid", ""}, + {8, "MONKEY", ICON_MESH_MONKEY, "Monkey", ""}, + {0, NULL, 0, NULL, NULL} +}; + +static int object_add_mesh_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + int newob= 0; + + if(obedit==NULL || obedit->type!=OB_MESH) { + object_add_type(C, OB_MESH); + ED_object_enter_editmode(C, EM_DO_UNDO); + newob = 1; + } + else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + + switch(RNA_enum_get(op->ptr, "type")) { + case 0: + WM_operator_name_call(C, "MESH_OT_primitive_plane_add", WM_OP_INVOKE_REGION_WIN, NULL); + break; + case 1: + WM_operator_name_call(C, "MESH_OT_primitive_cube_add", WM_OP_INVOKE_REGION_WIN, NULL); + break; + case 2: + WM_operator_name_call(C, "MESH_OT_primitive_circle_add", WM_OP_INVOKE_REGION_WIN, NULL); + break; + case 3: + WM_operator_name_call(C, "MESH_OT_primitive_uv_sphere_add", WM_OP_INVOKE_REGION_WIN, NULL); + break; + case 4: + WM_operator_name_call(C, "MESH_OT_primitive_ico_sphere_add", WM_OP_INVOKE_REGION_WIN, NULL); + break; + case 5: + WM_operator_name_call(C, "MESH_OT_primitive_cylinder_add", WM_OP_INVOKE_REGION_WIN, NULL); + break; + case 6: + WM_operator_name_call(C, "MESH_OT_primitive_cone_add", WM_OP_INVOKE_REGION_WIN, NULL); + break; + case 7: + WM_operator_name_call(C, "MESH_OT_primitive_grid_add", WM_OP_INVOKE_REGION_WIN, NULL); + break; + case 8: + WM_operator_name_call(C, "MESH_OT_primitive_monkey_add", WM_OP_INVOKE_REGION_WIN, NULL); + break; + } + /* userdef */ + if (newob && (U.flag & USER_ADD_EDITMODE)==0) { + ED_object_exit_editmode(C, EM_FREEDATA); + } + + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); + + return OPERATOR_FINISHED; +} + + +void OBJECT_OT_mesh_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Mesh"; + ot->description = "Add a mesh object to the scene."; + ot->idname= "OBJECT_OT_mesh_add"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_add_mesh_exec; + + ot->poll= ED_operator_scene_editable; + + /* flags: no register or undo, this operator calls operators */ + ot->flag= 0; //OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", prop_mesh_types, 0, "Primitive", ""); +} + +static EnumPropertyItem prop_curve_types[] = { + {CU_BEZIER|CU_PRIM_CURVE, "BEZIER_CURVE", ICON_CURVE_BEZCURVE, "Bezier Curve", ""}, + {CU_BEZIER|CU_PRIM_CIRCLE, "BEZIER_CIRCLE", ICON_CURVE_BEZCIRCLE, "Bezier Circle", ""}, + {CU_NURBS|CU_PRIM_CURVE, "NURBS_CURVE", ICON_CURVE_NCURVE, "NURBS Curve", ""}, + {CU_NURBS|CU_PRIM_CIRCLE, "NURBS_CIRCLE", ICON_CURVE_NCIRCLE, "NURBS Circle", ""}, + {CU_NURBS|CU_PRIM_PATH, "PATH", ICON_CURVE_PATH, "Path", ""}, + {0, NULL, 0, NULL, NULL} +}; + +static int object_add_curve_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + ListBase *editnurb; + Nurb *nu; + int newob= 0; + + if(obedit==NULL || obedit->type!=OB_CURVE) { + object_add_type(C, OB_CURVE); + ED_object_enter_editmode(C, 0); + newob = 1; + } + else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + + obedit= CTX_data_edit_object(C); + nu= add_nurbs_primitive(C, RNA_enum_get(op->ptr, "type"), newob); + editnurb= curve_get_editcurve(obedit); + BLI_addtail(editnurb, nu); + + /* userdef */ + if (newob && (U.flag & USER_ADD_EDITMODE)==0) { + ED_object_enter_editmode(C, 0); + ED_object_exit_editmode(C, EM_FREEDATA); + } + + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); + + return OPERATOR_FINISHED; +} + +static int object_add_curve_invoke(bContext *C, wmOperator *op, wmEvent *event) +{ + Object *obedit= CTX_data_edit_object(C); + uiPopupMenu *pup; + uiLayout *layout; + + pup= uiPupMenuBegin(C, op->type->name, 0); + layout= uiPupMenuLayout(pup); + if(!obedit || obedit->type == OB_CURVE) + uiItemsEnumO(layout, op->type->idname, "type"); + else + uiItemsEnumO(layout, "OBJECT_OT_surface_add", "type"); + uiPupMenuEnd(C, pup); + + return OPERATOR_CANCELLED; +} + +void OBJECT_OT_curve_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Curve"; + ot->description = "Add a curve object to the scene."; + ot->idname= "OBJECT_OT_curve_add"; + + /* api callbacks */ + ot->invoke= object_add_curve_invoke; + ot->exec= object_add_curve_exec; + + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", prop_curve_types, 0, "Primitive", ""); +} + +static EnumPropertyItem prop_surface_types[]= { + {CU_PRIM_CURVE|CU_NURBS, "NURBS_CURVE", ICON_SURFACE_NCURVE, "NURBS Curve", ""}, + {CU_PRIM_CIRCLE|CU_NURBS, "NURBS_CIRCLE", ICON_SURFACE_NCIRCLE, "NURBS Circle", ""}, + {CU_PRIM_PATCH|CU_NURBS, "NURBS_SURFACE", ICON_SURFACE_NSURFACE, "NURBS Surface", ""}, + {CU_PRIM_TUBE|CU_NURBS, "NURBS_TUBE", ICON_SURFACE_NTUBE, "NURBS Tube", ""}, + {CU_PRIM_SPHERE|CU_NURBS, "NURBS_SPHERE", ICON_SURFACE_NSPHERE, "NURBS Sphere", ""}, + {CU_PRIM_DONUT|CU_NURBS, "NURBS_DONUT", ICON_SURFACE_NDONUT, "NURBS Donut", ""}, + {0, NULL, 0, NULL, NULL} +}; + +static int object_add_surface_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + ListBase *editnurb; + Nurb *nu; + int newob= 0; + + if(obedit==NULL || obedit->type!=OB_SURF) { + object_add_type(C, OB_SURF); + ED_object_enter_editmode(C, 0); + newob = 1; + } + else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + + obedit= CTX_data_edit_object(C); + nu= add_nurbs_primitive(C, RNA_enum_get(op->ptr, "type"), newob); + editnurb= curve_get_editcurve(obedit); + BLI_addtail(editnurb, nu); + + /* userdef */ + if (newob && (U.flag & USER_ADD_EDITMODE)==0) { + ED_object_exit_editmode(C, EM_FREEDATA); + } + + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_surface_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Surface"; + ot->description = "Add a surface object to the scene."; + ot->idname= "OBJECT_OT_surface_add"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_add_surface_exec; + + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", prop_surface_types, 0, "Primitive", ""); +} + +static EnumPropertyItem prop_metaball_types[]= { + {MB_BALL, "MBALL_BALL", ICON_META_BALL, "Meta Ball", ""}, + {MB_TUBE, "MBALL_TUBE", ICON_META_TUBE, "Meta Tube", ""}, + {MB_PLANE, "MBALL_PLANE", ICON_META_PLANE, "Meta Plane", ""}, + {MB_CUBE, "MBALL_CUBE", ICON_META_CUBE, "Meta Cube", ""}, + {MB_ELIPSOID, "MBALL_ELLIPSOID", ICON_META_ELLIPSOID, "Meta Ellipsoid", ""}, + {0, NULL, 0, NULL, NULL} +}; + +static int object_metaball_add_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + MetaBall *mball; + MetaElem *elem; + int newob= 0; + + if(obedit==NULL || obedit->type!=OB_MBALL) { + object_add_type(C, OB_MBALL); + ED_object_enter_editmode(C, 0); + newob = 1; + } + else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + + obedit= CTX_data_edit_object(C); + elem= (MetaElem*)add_metaball_primitive(C, RNA_enum_get(op->ptr, "type"), newob); + mball= (MetaBall*)obedit->data; + BLI_addtail(mball->editelems, elem); + + /* userdef */ + if (newob && (U.flag & USER_ADD_EDITMODE)==0) { + ED_object_exit_editmode(C, EM_FREEDATA); + } + + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); + + return OPERATOR_FINISHED; +} + +static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *event) +{ + Object *obedit= CTX_data_edit_object(C); + uiPopupMenu *pup; + uiLayout *layout; + + pup= uiPupMenuBegin(C, op->type->name, 0); + layout= uiPupMenuLayout(pup); + if(!obedit || obedit->type == OB_MBALL) + uiItemsEnumO(layout, op->type->idname, "type"); + else + uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type"); + uiPupMenuEnd(C, pup); + + return OPERATOR_CANCELLED; +} + +void OBJECT_OT_metaball_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Metaball"; + ot->description= "Add an metaball object to the scene."; + ot->idname= "OBJECT_OT_metaball_add"; + + /* api callbacks */ + ot->invoke= object_metaball_add_invoke; + ot->exec= object_metaball_add_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", prop_metaball_types, 0, "Primitive", ""); +} +static int object_add_text_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + + if(obedit && obedit->type==OB_FONT) + return OPERATOR_CANCELLED; + + object_add_type(C, OB_FONT); + obedit= CTX_data_active_object(C); + + if(U.flag & USER_ADD_EDITMODE) + ED_object_enter_editmode(C, 0); + + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_text_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Text"; + ot->description = "Add a text object to the scene"; + ot->idname= "OBJECT_OT_text_add"; + + /* api callbacks */ + ot->exec= object_add_text_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int object_armature_add_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + View3D *v3d= CTX_wm_view3d(C); + RegionView3D *rv3d= NULL; + int newob= 0; + + if ((obedit==NULL) || (obedit->type != OB_ARMATURE)) { + object_add_type(C, OB_ARMATURE); + ED_object_enter_editmode(C, 0); + newob = 1; + } + else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + + if(v3d) + rv3d= CTX_wm_region(C)->regiondata; + + /* v3d and rv3d are allowed to be NULL */ + add_primitive_bone(CTX_data_scene(C), v3d, rv3d); + + /* userdef */ + if (newob && (U.flag & USER_ADD_EDITMODE)==0) { + ED_object_exit_editmode(C, EM_FREEDATA); + } + + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_armature_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Armature"; + ot->description = "Add an armature object to the scene."; + ot->idname= "OBJECT_OT_armature_add"; + + /* api callbacks */ + ot->exec= object_armature_add_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int object_primitive_add_invoke(bContext *C, wmOperator *op, wmEvent *event) +{ + uiPopupMenu *pup= uiPupMenuBegin(C, "Add Object", 0); + uiLayout *layout= uiPupMenuLayout(pup); + + uiItemMenuEnumO(layout, "Mesh", ICON_OUTLINER_OB_MESH, "OBJECT_OT_mesh_add", "type"); + uiItemMenuEnumO(layout, "Curve", ICON_OUTLINER_OB_CURVE, "OBJECT_OT_curve_add", "type"); + uiItemMenuEnumO(layout, "Surface", ICON_OUTLINER_OB_SURFACE, "OBJECT_OT_surface_add", "type"); + uiItemMenuEnumO(layout, NULL, ICON_OUTLINER_OB_META, "OBJECT_OT_metaball_add", "type"); + uiItemO(layout, "Text", ICON_OUTLINER_OB_FONT, "OBJECT_OT_text_add"); + uiItemS(layout); + uiItemO(layout, "Armature", ICON_OUTLINER_OB_ARMATURE, "OBJECT_OT_armature_add"); + uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_LATTICE, "OBJECT_OT_add", "type", OB_LATTICE); + uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_EMPTY, "OBJECT_OT_add", "type", OB_EMPTY); + uiItemS(layout); + uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_CAMERA, "OBJECT_OT_add", "type", OB_CAMERA); + uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_LAMP, "OBJECT_OT_add", "type", OB_LAMP); + + uiPupMenuEnd(C, pup); + + /* this operator is only for a menu, not used further */ + return OPERATOR_CANCELLED; +} + +/* only used as menu */ +void OBJECT_OT_primitive_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Primitive"; + ot->description = "Add a primitive object."; + ot->idname= "OBJECT_OT_primitive_add"; + + /* api callbacks */ + ot->invoke= object_primitive_add_invoke; + + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= 0; +} + +/**************************** Delete Object *************************/ + +/* remove base from a specific scene */ +/* note: now unlinks constraints as well */ +void ED_base_object_free_and_unlink(Scene *scene, Base *base) +{ + BLI_remlink(&scene->base, base); + free_libblock_us(&G.main->object, base->object); + if(scene->basact==base) scene->basact= NULL; + MEM_freeN(base); +} + +static int object_delete_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + int islamp= 0; + + if(CTX_data_edit_object(C)) + return OPERATOR_CANCELLED; + + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + + if(base->object->type==OB_LAMP) islamp= 1; + + /* remove from current scene only */ + ED_base_object_free_and_unlink(scene, base); + } + CTX_DATA_END; + + if(islamp) reshadeall_displist(scene); /* only frees displist */ + + DAG_scene_sort(scene); + ED_anim_dag_flush_update(C); + + WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C)); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_delete(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Delete"; + ot->description = "Delete selected objects."; + ot->idname= "OBJECT_OT_delete"; + + /* api callbacks */ + ot->invoke= WM_operator_confirm; + ot->exec= object_delete_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/**************************** Copy Utilities ******************************/ + +static void copy_object__forwardModifierLinks(void *userData, Object *ob, + ID **idpoin) +{ + /* this is copied from ID_NEW; it might be better to have a macro */ + if(*idpoin && (*idpoin)->newid) *idpoin = (*idpoin)->newid; +} + +/* after copying objects, copied data should get new pointers */ +static void copy_object_set_idnew(bContext *C, int dupflag) +{ + Object *ob; + Material *ma, *mao; + ID *id; +#if 0 // XXX old animation system + Ipo *ipo; + bActionStrip *strip; +#endif // XXX old animation system + int a; + + /* XXX check object pointers */ + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + ob= base->object; + relink_constraints(&ob->constraints); + if (ob->pose){ + bPoseChannel *chan; + for (chan = ob->pose->chanbase.first; chan; chan=chan->next){ + relink_constraints(&chan->constraints); + } + } + modifiers_foreachIDLink(ob, copy_object__forwardModifierLinks, NULL); + ID_NEW(ob->parent); + ID_NEW(ob->track); + ID_NEW(ob->proxy); + ID_NEW(ob->proxy_group); + +#if 0 // XXX old animation system + for(strip= ob->nlastrips.first; strip; strip= strip->next) { + bActionModifier *amod; + for(amod= strip->modifiers.first; amod; amod= amod->next) + ID_NEW(amod->ob); + } +#endif // XXX old animation system + } + CTX_DATA_END; + + /* materials */ + if( dupflag & USER_DUP_MAT) { + mao= G.main->mat.first; + while(mao) { + if(mao->id.newid) { + + ma= (Material *)mao->id.newid; + + if(dupflag & USER_DUP_TEX) { + for(a=0; amtex[a]) { + id= (ID *)ma->mtex[a]->tex; + if(id) { + ID_NEW_US(ma->mtex[a]->tex) + else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex); + id->us--; + } + } + } + } +#if 0 // XXX old animation system + id= (ID *)ma->ipo; + if(id) { + ID_NEW_US(ma->ipo) + else ma->ipo= copy_ipo(ma->ipo); + id->us--; + } +#endif // XXX old animation system + } + mao= mao->id.next; + } + } + +#if 0 // XXX old animation system + /* lamps */ + if( dupflag & USER_DUP_IPO) { + Lamp *la= G.main->lamp.first; + while(la) { + if(la->id.newid) { + Lamp *lan= (Lamp *)la->id.newid; + id= (ID *)lan->ipo; + if(id) { + ID_NEW_US(lan->ipo) + else lan->ipo= copy_ipo(lan->ipo); + id->us--; + } + } + la= la->id.next; + } + } + + /* ipos */ + ipo= G.main->ipo.first; + while(ipo) { + if(ipo->id.lib==NULL && ipo->id.newid) { + Ipo *ipon= (Ipo *)ipo->id.newid; + IpoCurve *icu; + for(icu= ipon->curve.first; icu; icu= icu->next) { + if(icu->driver) { + ID_NEW(icu->driver->ob); + } + } + } + ipo= ipo->id.next; + } +#endif // XXX old animation system + + set_sca_new_poins(); + + clear_id_newpoins(); +} + +/********************* Make Duplicates Real ************************/ + +static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base) +{ + Base *basen; + Object *ob; + ListBase *lb; + DupliObject *dob; + + if(!base && !(base = BASACT)) + return; + + if(!(base->object->transflag & OB_DUPLI)) + return; + + lb= object_duplilist(scene, base->object); + + for(dob= lb->first; dob; dob= dob->next) { + ob= copy_object(dob->ob); + /* font duplis can have a totcol without material, we get them from parent + * should be implemented better... + */ + if(ob->mat==NULL) ob->totcol= 0; + + basen= MEM_dupallocN(base); + basen->flag &= ~OB_FROMDUPLI; + BLI_addhead(&scene->base, basen); /* addhead: othwise eternal loop */ + basen->object= ob; + ob->ipo= NULL; /* make sure apply works */ + ob->parent= ob->track= NULL; + ob->disp.first= ob->disp.last= NULL; + ob->transflag &= ~OB_DUPLI; + + Mat4CpyMat4(ob->obmat, dob->mat); + ED_object_apply_obmat(ob); + } + + copy_object_set_idnew(C, 0); + + free_object_duplilist(lb); + + base->object->transflag &= ~OB_DUPLI; +} + +static int object_duplicates_make_real_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + + clear_id_newpoins(); + + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + make_object_duplilist_real(C, scene, base); + } + CTX_DATA_END; + + DAG_scene_sort(scene); + ED_anim_dag_flush_update(C); + WM_event_add_notifier(C, NC_SCENE, scene); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_duplicates_make_real(wmOperatorType *ot) +{ + + /* identifiers */ + ot->name= "Make Duplicates Real"; + ot->description = "Make dupli objects attached to this object real."; + ot->idname= "OBJECT_OT_duplicates_make_real"; + + /* api callbacks */ + ot->invoke= WM_operator_confirm; + ot->exec= object_duplicates_make_real_exec; + + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/**************************** Convert **************************/ + +static EnumPropertyItem convert_target_items[]= { + {OB_CURVE, "CURVE", 0, "Curve", ""}, + {OB_MESH, "MESH", 0, "Mesh", ""}, + {0, NULL, 0, NULL, NULL}}; + +static void curvetomesh(Scene *scene, Object *ob) +{ + Curve *cu= ob->data; + + if(cu->disp.first==0) + makeDispListCurveTypes(scene, ob, 0); /* force creation */ + + nurbs_to_mesh(ob); /* also does users */ + + if(ob->type == OB_MESH) + object_free_modifiers(ob); +} + +static int convert_poll(bContext *C) +{ + Object *obact= CTX_data_active_object(C); + Scene *scene= CTX_data_scene(C); + + return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT)); +} + +static int convert_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Base *basen=NULL, *basact, *basedel=NULL; + Object *ob, *ob1, *obact= CTX_data_active_object(C); + DerivedMesh *dm; + Curve *cu; + Nurb *nu; + MetaBall *mb; + Mesh *me; + int target= RNA_enum_get(op->ptr, "target"); + int keep_original= RNA_boolean_get(op->ptr, "keep_original"); + int a; + + /* don't forget multiple users! */ + + /* reset flags */ + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + ob= base->object; + ob->flag &= ~OB_DONE; + } + CTX_DATA_END; + + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + ob= base->object; + + if(ob->flag & OB_DONE) + continue; + else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */ + ob->flag |= OB_DONE; + basedel = base; + + ob1= copy_object(ob); + ob1->recalc |= OB_RECALC; + + basen= MEM_mallocN(sizeof(Base), "duplibase"); + *basen= *base; + BLI_addhead(&scene->base, basen); /* addhead: otherwise eternal loop */ + basen->object= ob1; + basen->flag |= SELECT; + base->flag &= ~SELECT; + ob->flag &= ~SELECT; + + /* decrement original mesh's usage count */ + me= ob1->data; + me->id.us--; + + /* make a new copy of the mesh */ + ob1->data= copy_mesh(me); + + /* make new mesh data from the original copy */ + dm= mesh_get_derived_final(scene, ob1, CD_MASK_MESH); + /* dm= mesh_create_derived_no_deform(ob1, NULL); this was called original (instead of get_derived). man o man why! (ton) */ + + DM_to_mesh(dm, ob1->data); + + dm->release(dm); + object_free_modifiers(ob1); /* after derivedmesh calls! */ + } + else if(ob->type==OB_FONT) { + ob->flag |= OB_DONE; + + ob->type= OB_CURVE; + cu= ob->data; + + if(cu->vfont) { + cu->vfont->id.us--; + cu->vfont= 0; + } + if(cu->vfontb) { + cu->vfontb->id.us--; + cu->vfontb= 0; + } + if(cu->vfonti) { + cu->vfonti->id.us--; + cu->vfonti= 0; + } + if(cu->vfontbi) { + cu->vfontbi->id.us--; + cu->vfontbi= 0; + } + /* other users */ + if(cu->id.us>1) { + for(ob1= G.main->object.first; ob1; ob1=ob1->id.next) { + if(ob1->data==cu) { + ob1->type= OB_CURVE; + ob1->recalc |= OB_RECALC; + } + } + } + + for(nu=cu->nurb.first; nu; nu=nu->next) + nu->charidx= 0; + + if(target == OB_MESH) + curvetomesh(scene, ob); + } + else if(ELEM(ob->type, OB_CURVE, OB_SURF)) { + ob->flag |= OB_DONE; + + if(target == OB_MESH) + curvetomesh(scene, ob); + } + else if(ob->type==OB_MBALL) { + ob= find_basis_mball(scene, ob); + + if(ob->disp.first && !(ob->flag & OB_DONE)) { + ob->flag |= OB_DONE; + basedel = base; + + ob1= copy_object(ob); + ob1->recalc |= OB_RECALC; + + basen= MEM_mallocN(sizeof(Base), "duplibase"); + *basen= *base; + BLI_addhead(&scene->base, basen); /* addhead: otherwise eternal loop */ + basen->object= ob1; + basen->flag |= SELECT; + basedel->flag &= ~SELECT; + ob->flag &= ~SELECT; + + mb= ob1->data; + mb->id.us--; + + ob1->data= add_mesh("Mesh"); + ob1->type= OB_MESH; + + me= ob1->data; + me->totcol= mb->totcol; + if(ob1->totcol) { + me->mat= MEM_dupallocN(mb->mat); + for(a=0; atotcol; a++) id_us_plus((ID *)me->mat[a]); + } + + mball_to_mesh(&ob->disp, ob1->data); + + /* So we can see the wireframe */ + BASACT= basen; // XXX hm + } + else + continue; + } + else + continue; + + /* If the original object is active then make this object active */ + if(basen) { + if(ob == obact) { + ED_base_object_activate(C, basen); + basact = basen; + } + + basen= NULL; + } + + /* delete original if needed */ + if(basedel) { + if(!keep_original) + ED_base_object_free_and_unlink(scene, basedel); + + basedel = NULL; + } + } + CTX_DATA_END; + + /* delete object should renew depsgraph */ + if(!keep_original) + DAG_scene_sort(scene); + + /* texspace and normals */ + if(!basen) BASACT= NULL; // XXX base; + +// XXX ED_object_enter_editmode(C, 0); +// XXX exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */ + BASACT= basact; + + DAG_scene_sort(scene); + WM_event_add_notifier(C, NC_SCENE, scene); + + return OPERATOR_FINISHED; +} + +static int convert_invoke(bContext *C, wmOperator *op, wmEvent *event) +{ + Object *obact= CTX_data_active_object(C); + uiPopupMenu *pup; + uiLayout *layout; + char *title; + + if(obact->type==OB_FONT) { + pup= uiPupMenuBegin(C, "Convert Font to", 0); + layout= uiPupMenuLayout(pup); + + uiItemEnumO(layout, "Curve", 0, op->type->idname, "target", OB_CURVE); + } + else { + if(obact->type == OB_MBALL) + title= "Convert Metaball to"; + else if(obact->type == OB_CURVE) + title= "Convert Curve to"; + else if(obact->type == OB_SURF) + title= "Convert Nurbs Surface to"; + else if(obact->type == OB_MESH) + title= "Convert Modifiers to"; + else + return OPERATOR_CANCELLED; + + pup= uiPupMenuBegin(C, title, 0); + layout= uiPupMenuLayout(pup); + } + + uiItemBooleanO(layout, "Mesh (keep original)", 0, op->type->idname, "keep_original", 1); + uiItemBooleanO(layout, "Mesh (delete original)", 0, op->type->idname, "keep_original", 0); + + uiPupMenuEnd(C, pup); + + return OPERATOR_CANCELLED; +} + +void OBJECT_OT_convert(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Convert"; + ot->description = "Convert selected objects to another type."; + ot->idname= "OBJECT_OT_convert"; + + /* api callbacks */ + ot->invoke= convert_invoke; + ot->exec= convert_exec; + ot->poll= convert_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to."); + RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them."); +} + +/************************** Add Duplicate **********************/ + +/* + dupflag: a flag made from constants declared in DNA_userdef_types.h + The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data. + U.dupflag for default operations or you can construct a flag as python does + if the dupflag is 0 then no data will be copied (linked duplicate) */ + +/* used below, assumes id.new is correct */ +/* leaves selection of base/object unaltered */ +static Base *object_add_duplicate_internal(Scene *scene, Base *base, int dupflag) +{ + Base *basen= NULL; + Material ***matarar; + Object *ob, *obn; + ID *id; + int a, didit; + + ob= base->object; + if(ob->mode & OB_MODE_POSE) { + ; /* nothing? */ + } + else { + obn= copy_object(ob); + obn->recalc |= OB_RECALC; + + basen= MEM_mallocN(sizeof(Base), "duplibase"); + *basen= *base; + BLI_addhead(&scene->base, basen); /* addhead: prevent eternal loop */ + basen->object= obn; + + if(basen->flag & OB_FROMGROUP) { + Group *group; + for(group= G.main->group.first; group; group= group->id.next) { + if(object_in_group(ob, group)) + add_to_group(group, obn); + } + obn->flag |= OB_FROMGROUP; /* this flag is unset with copy_object() */ + } + + /* duplicates using userflags */ +#if 0 // XXX old animation system + if(dupflag & USER_DUP_IPO) { + bConstraintChannel *chan; + id= (ID *)obn->ipo; + + if(id) { + ID_NEW_US( obn->ipo) + else obn->ipo= copy_ipo(obn->ipo); + id->us--; + } + /* Handle constraint ipos */ + for (chan=obn->constraintChannels.first; chan; chan=chan->next){ + id= (ID *)chan->ipo; + if(id) { + ID_NEW_US( chan->ipo) + else chan->ipo= copy_ipo(chan->ipo); + id->us--; + } + } + } + if(dupflag & USER_DUP_ACT){ /* Not buttons in the UI to modify this, add later? */ + id= (ID *)obn->action; + if (id){ + ID_NEW_US(obn->action) + else{ + obn->action= copy_action(obn->action); + } + id->us--; + } + } +#endif // XXX old animation system + if(dupflag & USER_DUP_MAT) { + for(a=0; atotcol; a++) { + id= (ID *)obn->mat[a]; + if(id) { + ID_NEW_US(obn->mat[a]) + else obn->mat[a]= copy_material(obn->mat[a]); + id->us--; + } + } + } + if(dupflag & USER_DUP_PSYS) { + ParticleSystem *psys; + for(psys=obn->particlesystem.first; psys; psys=psys->next) { + id= (ID*) psys->part; + if(id) { + ID_NEW_US(psys->part) + else psys->part= psys_copy_settings(psys->part); + id->us--; + } + } + } + + id= obn->data; + didit= 0; + + switch(obn->type) { + case OB_MESH: + if(dupflag & USER_DUP_MESH) { + ID_NEW_US2( obn->data ) + else { + obn->data= copy_mesh(obn->data); + + if(obn->fluidsimSettings) { + obn->fluidsimSettings->orgMesh = (Mesh *)obn->data; + } + + didit= 1; + } + id->us--; + } + break; + case OB_CURVE: + if(dupflag & USER_DUP_CURVE) { + ID_NEW_US2(obn->data ) + else { + obn->data= copy_curve(obn->data); + didit= 1; + } + id->us--; + } + break; + case OB_SURF: + if(dupflag & USER_DUP_SURF) { + ID_NEW_US2( obn->data ) + else { + obn->data= copy_curve(obn->data); + didit= 1; + } + id->us--; + } + break; + case OB_FONT: + if(dupflag & USER_DUP_FONT) { + ID_NEW_US2( obn->data ) + else { + obn->data= copy_curve(obn->data); + didit= 1; + } + id->us--; + } + break; + case OB_MBALL: + if(dupflag & USER_DUP_MBALL) { + ID_NEW_US2(obn->data ) + else { + obn->data= copy_mball(obn->data); + didit= 1; + } + id->us--; + } + break; + case OB_LAMP: + if(dupflag & USER_DUP_LAMP) { + ID_NEW_US2(obn->data ) + else obn->data= copy_lamp(obn->data); + id->us--; + } + break; + + case OB_ARMATURE: + obn->recalc |= OB_RECALC_DATA; + if(obn->pose) obn->pose->flag |= POSE_RECALC; + + if(dupflag & USER_DUP_ARM) { + ID_NEW_US2(obn->data ) + else { + obn->data= copy_armature(obn->data); + armature_rebuild_pose(obn, obn->data); + didit= 1; + } + id->us--; + } + + break; + + case OB_LATTICE: + if(dupflag!=0) { + ID_NEW_US2(obn->data ) + else obn->data= copy_lattice(obn->data); + id->us--; + } + break; + case OB_CAMERA: + if(dupflag!=0) { + ID_NEW_US2(obn->data ) + else obn->data= copy_camera(obn->data); + id->us--; + } + break; + } + + if(dupflag & USER_DUP_MAT) { + matarar= give_matarar(obn); + if(didit && matarar) { + for(a=0; atotcol; a++) { + id= (ID *)(*matarar)[a]; + if(id) { + ID_NEW_US( (*matarar)[a] ) + else (*matarar)[a]= copy_material((*matarar)[a]); + + id->us--; + } + } + } + } + } + return basen; +} + +/* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */ +/* leaves selection of base/object unaltered */ +Base *ED_object_add_duplicate(Scene *scene, Base *base, int dupflag) +{ + Base *basen; + + clear_id_newpoins(); + clear_sca_new_poins(); /* sensor/contr/act */ + + basen= object_add_duplicate_internal(scene, base, dupflag); + + DAG_scene_sort(scene); + + return basen; +} + +/* contextual operator dupli */ +static int duplicate_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + int linked= RNA_boolean_get(op->ptr, "linked"); + int dupflag= (linked)? 0: U.dupflag; + + clear_id_newpoins(); + clear_sca_new_poins(); /* sensor/contr/act */ + + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + Base *basen= object_add_duplicate_internal(scene, base, dupflag); + + /* note that this is safe to do with this context iterator, + the list is made in advance */ + ED_base_object_select(base, BA_DESELECT); + + /* new object becomes active */ + if(BASACT==base) + ED_base_object_activate(C, basen); + + } + CTX_DATA_END; + + copy_object_set_idnew(C, dupflag); + + DAG_scene_sort(scene); + ED_anim_dag_flush_update(C); + + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_duplicate(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Duplicate"; + ot->description = "Duplicate selected objects."; + ot->idname= "OBJECT_OT_duplicate"; + + /* api callbacks */ + ot->exec= duplicate_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* to give to transform */ + RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data."); + RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX); +} + +/**************************** Join *************************/ + +static int join_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Object *ob= CTX_data_active_object(C); + + if(scene->obedit) { + BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode."); + return OPERATOR_CANCELLED; + } + else if(!ob) { + BKE_report(op->reports, RPT_ERROR, "Can't join unless there is an active object."); + return OPERATOR_CANCELLED; + } + else if(object_data_is_libdata(ob)) { + BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata."); + return OPERATOR_CANCELLED; + } + + if(ob->type == OB_MESH) + return join_mesh_exec(C, op); + else if(ELEM(ob->type, OB_CURVE, OB_SURF)) + return join_curve_exec(C, op); + else if(ob->type == OB_ARMATURE) + return join_armature_exec(C, op); + + BKE_report(op->reports, RPT_ERROR, "This object type doesn't support joining."); + + return OPERATOR_CANCELLED; +} + +void OBJECT_OT_join(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Join"; + ot->description = "Join selected objects into active object."; + ot->idname= "OBJECT_OT_join"; + + /* api callbacks */ + ot->exec= join_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + diff --git a/source/blender/editors/object/object_constraint.c b/source/blender/editors/object/object_constraint.c new file mode 100644 index 00000000000..9b073ed5878 --- /dev/null +++ b/source/blender/editors/object/object_constraint.c @@ -0,0 +1,1416 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): Joshua Leung, Blender Foundation + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include +#include + +#include "MEM_guardedalloc.h" + +#include "BLI_blenlib.h" +#include "BLI_arithb.h" +#include "BLI_dynstr.h" + +#include "DNA_action_types.h" +#include "DNA_armature_types.h" +#include "DNA_constraint_types.h" +#include "DNA_curve_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" +#include "DNA_screen_types.h" +#include "DNA_text_types.h" +#include "DNA_view3d_types.h" + +#include "BKE_action.h" +#include "BKE_armature.h" +#include "BKE_constraint.h" +#include "BKE_context.h" +#include "BKE_depsgraph.h" +#include "BKE_global.h" +#include "BKE_main.h" +#include "BKE_object.h" +#include "BKE_report.h" +#include "BKE_utildefines.h" + +#ifndef DISABLE_PYTHON +#include "BPY_extern.h" +#endif + +#include "WM_api.h" +#include "WM_types.h" + +#include "RNA_access.h" +#include "RNA_define.h" +#include "RNA_enum_types.h" +#include "RNA_types.h" + +#include "ED_object.h" +#include "ED_screen.h" + +#include "UI_interface.h" + +#include "object_intern.h" + +/* -------------- Get Active Constraint Data ---------------------- */ + +/* if object in posemode, active bone constraints, else object constraints */ +ListBase *get_active_constraints (Object *ob) +{ + if (ob == NULL) + return NULL; + + if (ob->mode & OB_MODE_POSE) { + bPoseChannel *pchan; + + pchan = get_active_posechannel(ob); + if (pchan) + return &pchan->constraints; + } + else + return &ob->constraints; + + return NULL; +} + +/* single constraint */ +bConstraint *get_active_constraint (Object *ob) +{ + ListBase *lb= get_active_constraints(ob); + + if (lb) { + bConstraint *con; + + for (con= lb->first; con; con=con->next) { + if (con->flag & CONSTRAINT_ACTIVE) + return con; + } + } + + return NULL; +} +/* -------------- Constraint Management (Add New, Remove, Rename) -------------------- */ +/* ------------- PyConstraints ------------------ */ + +/* this callback sets the text-file to be used for selected menu item */ +void validate_pyconstraint_cb (void *arg1, void *arg2) +{ + bPythonConstraint *data = arg1; + Text *text= NULL; + int index = *((int *)arg2); + int i; + + /* exception for no script */ + if (index) { + /* innovative use of a for...loop to search */ + for (text=G.main->text.first, i=1; text && index!=i; i++, text=text->id.next); + } + data->text = text; +} + +#ifndef DISABLE_PYTHON +/* this returns a string for the list of usable pyconstraint script names */ +char *buildmenu_pyconstraints (Text *con_text, int *pyconindex) +{ + DynStr *pupds= BLI_dynstr_new(); + Text *text; + char *str; + char buf[64]; + int i; + + /* add title first */ + sprintf(buf, "Scripts: %%t|[None]%%x0|"); + BLI_dynstr_append(pupds, buf); + + /* init active-index first */ + if (con_text == NULL) + *pyconindex= 0; + + /* loop through markers, adding them */ + for (text=G.main->text.first, i=1; text; i++, text=text->id.next) { + /* this is important to ensure that right script is shown as active */ + if (text == con_text) *pyconindex = i; + + /* only include valid pyconstraint scripts */ + if (BPY_is_pyconstraint(text)) { + BLI_dynstr_append(pupds, text->id.name+2); + + sprintf(buf, "%%x%d", i); + BLI_dynstr_append(pupds, buf); + + if (text->id.next) + BLI_dynstr_append(pupds, "|"); + } + } + + /* convert to normal MEM_malloc'd string */ + str= BLI_dynstr_get_cstring(pupds); + BLI_dynstr_free(pupds); + + return str; +} +#endif /* DISABLE_PYTHON */ + +/* this callback gets called when the 'refresh' button of a pyconstraint gets pressed */ +void update_pyconstraint_cb (void *arg1, void *arg2) +{ + Object *owner= (Object *)arg1; + bConstraint *con= (bConstraint *)arg2; +#ifndef DISABLE_PYTHON + if (owner && con) + BPY_pyconstraint_update(owner, con); +#endif +} + +/* Creates a new constraint, initialises its data, and returns it */ +bConstraint *add_new_constraint (short type) +{ + bConstraint *con; + bConstraintTypeInfo *cti; + + con = MEM_callocN(sizeof(bConstraint), "Constraint"); + + /* Set up a generic constraint datablock */ + con->type = type; + con->flag |= CONSTRAINT_EXPAND; + con->enforce = 1.0f; + + /* Load the data for it */ + cti = constraint_get_typeinfo(con); + if (cti) { + con->data = MEM_callocN(cti->size, cti->structName); + + /* only constraints that change any settings need this */ + if (cti->new_data) + cti->new_data(con->data); + + /* set the name based on the type of constraint */ + strcpy(con->name, cti->name); + } + else + strcpy(con->name, "Const"); + + return con; +} + +/* Adds the given constraint to the Object-level set of constraints for the given Object */ +void add_constraint_to_object (bConstraint *con, Object *ob) +{ + ListBase *list; + list = &ob->constraints; + + if (list) { + unique_constraint_name(con, list); + BLI_addtail(list, con); + + if (proxylocked_constraints_owner(ob, NULL)) + con->flag |= CONSTRAINT_PROXY_LOCAL; + + con->flag |= CONSTRAINT_ACTIVE; + for (con= con->prev; con; con= con->prev) + con->flag &= ~CONSTRAINT_ACTIVE; + } +} + +/* helper function for add_constriant - sets the last target for the active constraint */ +static void set_constraint_nth_target (bConstraint *con, Object *target, char subtarget[], int index) +{ + bConstraintTypeInfo *cti= constraint_get_typeinfo(con); + ListBase targets = {NULL, NULL}; + bConstraintTarget *ct; + int num_targets, i; + + if (cti && cti->get_constraint_targets) { + cti->get_constraint_targets(con, &targets); + num_targets= BLI_countlist(&targets); + + if (index < 0) { + if (abs(index) < num_targets) + index= num_targets - abs(index); + else + index= num_targets - 1; + } + else if (index >= num_targets) { + index= num_targets - 1; + } + + for (ct=targets.first, i=0; ct; ct= ct->next, i++) { + if (i == index) { + ct->tar= target; + strcpy(ct->subtarget, subtarget); + break; + } + } + + if (cti->flush_constraint_targets) + cti->flush_constraint_targets(con, &targets, 0); + } +} + +/* ------------- Constraint Sanity Testing ------------------- */ + +/* checks validity of object pointers, and NULLs, + * if Bone doesnt exist it sets the CONSTRAINT_DISABLE flag + */ +static void test_constraints (Object *owner, const char substring[]) +{ + bConstraint *curcon; + ListBase *conlist= NULL; + int type; + + if (owner==NULL) return; + + /* Check parents */ + if (strlen(substring)) { + switch (owner->type) { + case OB_ARMATURE: + type = CONSTRAINT_OBTYPE_BONE; + break; + default: + type = CONSTRAINT_OBTYPE_OBJECT; + break; + } + } + else + type = CONSTRAINT_OBTYPE_OBJECT; + + /* Get the constraint list for this object */ + switch (type) { + case CONSTRAINT_OBTYPE_OBJECT: + conlist = &owner->constraints; + break; + case CONSTRAINT_OBTYPE_BONE: + { + Bone *bone; + bPoseChannel *chan; + + bone = get_named_bone( ((bArmature *)owner->data), substring ); + chan = get_pose_channel(owner->pose, substring); + if (bone && chan) { + conlist = &chan->constraints; + } + } + break; + } + + /* Check all constraints - is constraint valid? */ + if (conlist) { + for (curcon = conlist->first; curcon; curcon=curcon->next) { + bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon); + ListBase targets = {NULL, NULL}; + bConstraintTarget *ct; + + /* clear disabled-flag first */ + curcon->flag &= ~CONSTRAINT_DISABLE; + + if (curcon->type == CONSTRAINT_TYPE_KINEMATIC) { + bKinematicConstraint *data = curcon->data; + + /* bad: we need a separate set of checks here as poletarget is + * optional... otherwise poletarget must exist too or else + * the constraint is deemed invalid + */ + if (exist_object(data->tar) == 0) { + data->tar = NULL; + curcon->flag |= CONSTRAINT_DISABLE; + } + else if (data->tar == owner) { + if (!get_named_bone(get_armature(owner), data->subtarget)) { + curcon->flag |= CONSTRAINT_DISABLE; + } + } + + if (data->poletar) { + if (exist_object(data->poletar) == 0) { + data->poletar = NULL; + curcon->flag |= CONSTRAINT_DISABLE; + } + else if (data->poletar == owner) { + if (!get_named_bone(get_armature(owner), data->polesubtarget)) { + curcon->flag |= CONSTRAINT_DISABLE; + } + } + } + + /* targets have already been checked for this */ + continue; + } + else if (curcon->type == CONSTRAINT_TYPE_ACTION) { + bActionConstraint *data = curcon->data; + + /* validate action */ + if (data->act == NULL) + curcon->flag |= CONSTRAINT_DISABLE; + } + else if (curcon->type == CONSTRAINT_TYPE_FOLLOWPATH) { + bFollowPathConstraint *data = curcon->data; + + /* don't allow track/up axes to be the same */ + if (data->upflag==data->trackflag) + curcon->flag |= CONSTRAINT_DISABLE; + if (data->upflag+3==data->trackflag) + curcon->flag |= CONSTRAINT_DISABLE; + } + else if (curcon->type == CONSTRAINT_TYPE_TRACKTO) { + bTrackToConstraint *data = curcon->data; + + /* don't allow track/up axes to be the same */ + if (data->reserved2==data->reserved1) + curcon->flag |= CONSTRAINT_DISABLE; + if (data->reserved2+3==data->reserved1) + curcon->flag |= CONSTRAINT_DISABLE; + } + else if (curcon->type == CONSTRAINT_TYPE_LOCKTRACK) { + bLockTrackConstraint *data = curcon->data; + + if (data->lockflag==data->trackflag) + curcon->flag |= CONSTRAINT_DISABLE; + if (data->lockflag+3==data->trackflag) + curcon->flag |= CONSTRAINT_DISABLE; + } + + /* Check targets for constraints */ + if (cti && cti->get_constraint_targets) { + cti->get_constraint_targets(curcon, &targets); + + /* disable and clear constraints targets that are incorrect */ + for (ct= targets.first; ct; ct= ct->next) { + /* general validity checks (for those constraints that need this) */ + if (exist_object(ct->tar) == 0) { + ct->tar = NULL; + curcon->flag |= CONSTRAINT_DISABLE; + } + else if (ct->tar == owner) { + if (!get_named_bone(get_armature(owner), ct->subtarget)) { + curcon->flag |= CONSTRAINT_DISABLE; + } + } + + /* target checks for specific constraints */ + if (ELEM(curcon->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO)) { + if (ct->tar) { + if (ct->tar->type != OB_CURVE) { + ct->tar= NULL; + curcon->flag |= CONSTRAINT_DISABLE; + } + else { + Curve *cu= ct->tar->data; + + /* auto-set 'Path' setting on curve so this works */ + cu->flag |= CU_PATH; + } + } + } + } + + /* free any temporary targets */ + if (cti->flush_constraint_targets) + cti->flush_constraint_targets(curcon, &targets, 0); + } + } + } +} + +static void test_bonelist_constraints (Object *owner, ListBase *list) +{ + Bone *bone; + + for (bone = list->first; bone; bone = bone->next) { + test_constraints(owner, bone->name); + test_bonelist_constraints(owner, &bone->childbase); + } +} + +void object_test_constraints (Object *owner) +{ + test_constraints(owner, ""); + + if (owner->type==OB_ARMATURE) { + bArmature *arm= get_armature(owner); + + if (arm) + test_bonelist_constraints(owner, &arm->bonebase); + } +} + +/* ********************** CONSTRAINT-SPECIFIC STUFF ********************* */ + +/* ---------- Distance-Dependent Constraints ---------- */ +/* StretchTo, Limit Distance */ + +static int stretchto_poll(bContext *C) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_StretchToConstraint); + return (ptr.id.data && ptr.data); +} + +static int stretchto_reset_exec (bContext *C, wmOperator *op) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_StretchToConstraint); + + /* just set original length to 0.0, which will cause a reset on next recalc */ + RNA_float_set(&ptr, "original_length", 0.0f); + + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL); + return OPERATOR_FINISHED; +} + +void CONSTRAINT_OT_stretchto_reset (wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Reset Original Length"; + ot->idname= "CONSTRAINT_OT_stretchto_reset"; + ot->description= "Reset original length of bone for Stretch To Constraint."; + + ot->exec= stretchto_reset_exec; + ot->poll= stretchto_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + + +static int limitdistance_reset_exec (bContext *C, wmOperator *op) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_LimitDistanceConstraint); + + /* just set distance to 0.0, which will cause a reset on next recalc */ + RNA_float_set(&ptr, "distance", 0.0f); + + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL); + return OPERATOR_FINISHED; +} + +static int limitdistance_poll(bContext *C) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_LimitDistanceConstraint); + return (ptr.id.data && ptr.data); +} + +void CONSTRAINT_OT_limitdistance_reset (wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Reset Distance"; + ot->idname= "CONSTRAINT_OT_limitdistance_reset"; + ot->description= "Reset limiting distance for Limit Distance Constraint."; + + ot->exec= limitdistance_reset_exec; + ot->poll= limitdistance_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/* ------------- Child-Of Constraint ------------------ */ + +static int childof_poll(bContext *C) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint); + return (ptr.id.data && ptr.data); +} + +/* ChildOf Constraint - set inverse callback */ +static int childof_set_inverse_exec (bContext *C, wmOperator *op) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint); + Scene *scene= CTX_data_scene(C); + Object *ob= ptr.id.data; + bConstraint *con= ptr.data; + bChildOfConstraint *data= (bChildOfConstraint *)con->data; + bPoseChannel *pchan= NULL; + + /* try to find a pose channel */ + // TODO: get from context instead? + if (ob && ob->pose) + pchan= get_active_posechannel(ob); + + /* calculate/set inverse matrix */ + if (pchan) { + float pmat[4][4], cinf; + float imat[4][4], tmat[4][4]; + + /* make copy of pchan's original pose-mat (for use later) */ + Mat4CpyMat4(pmat, pchan->pose_mat); + + /* disable constraint for pose to be solved without it */ + cinf= con->enforce; + con->enforce= 0.0f; + + /* solve pose without constraint */ + where_is_pose(scene, ob); + + /* determine effect of constraint by removing the newly calculated + * pchan->pose_mat from the original pchan->pose_mat, thus determining + * the effect of the constraint + */ + Mat4Invert(imat, pchan->pose_mat); + Mat4MulMat4(tmat, imat, pmat); + Mat4Invert(data->invmat, tmat); + + /* recalculate pose with new inv-mat */ + con->enforce= cinf; + where_is_pose(scene, ob); + } + else if (ob) { + Object workob; + /* use what_does_parent to find inverse - just like for normal parenting. + * NOTE: what_does_parent uses a static workob defined in object.c + */ + what_does_parent(scene, ob, &workob); + Mat4Invert(data->invmat, workob.obmat); + } + else + Mat4One(data->invmat); + + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); + + return OPERATOR_FINISHED; +} + +void CONSTRAINT_OT_childof_set_inverse (wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Set Inverse"; + ot->idname= "CONSTRAINT_OT_childof_set_inverse"; + ot->description= "Set inverse correction for ChildOf constraint."; + + ot->exec= childof_set_inverse_exec; + ot->poll= childof_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/* ChildOf Constraint - clear inverse callback */ +static int childof_clear_inverse_exec (bContext *C, wmOperator *op) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint); + Object *ob= ptr.id.data; + bConstraint *con= ptr.data; + bChildOfConstraint *data= (bChildOfConstraint *)con->data; + + /* simply clear the matrix */ + Mat4One(data->invmat); + + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); + + return OPERATOR_FINISHED; +} + +void CONSTRAINT_OT_childof_clear_inverse (wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Clear Inverse"; + ot->idname= "CONSTRAINT_OT_childof_clear_inverse"; + ot->description= "Clear inverse correction for ChildOf constraint."; + + ot->exec= childof_clear_inverse_exec; + ot->poll= childof_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/***************************** BUTTONS ****************************/ + +/* Rename the given constraint, con already has the new name */ +void ED_object_constraint_rename(Object *ob, bConstraint *con, char *oldname) +{ + bConstraint *tcon; + ListBase *conlist= NULL; + int from_object= 0; + + /* get context by searching for con (primitive...) */ + for (tcon= ob->constraints.first; tcon; tcon= tcon->next) { + if (tcon==con) + break; + } + + if (tcon) { + conlist= &ob->constraints; + from_object= 1; + } + else if (ob->pose) { + bPoseChannel *pchan; + + for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) { + for (tcon= pchan->constraints.first; tcon; tcon= tcon->next) { + if (tcon==con) + break; + } + if (tcon) + break; + } + + if (tcon) { + conlist= &pchan->constraints; + } + } + + if (conlist==NULL) { + printf("rename constraint failed\n"); /* should not happen in UI */ + return; + } + + /* first make sure it's a unique name within context */ + unique_constraint_name(con, conlist); +} + + + + +void ED_object_constraint_set_active(Object *ob, bConstraint *con) +{ + ListBase *lb; + bConstraint *origcon= con; + + /* lets be nice and escape if its active already */ + if(con && (con->flag & CONSTRAINT_ACTIVE)) + return ; + + lb= get_active_constraints(ob); + if(lb == NULL) + return; + + for(con= lb->first; con; con= con->next) { + if(con==origcon) con->flag |= CONSTRAINT_ACTIVE; + else con->flag &= ~CONSTRAINT_ACTIVE; + } +} + +static int constraint_poll(bContext *C) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint); + return (ptr.id.data && ptr.data); +} + +static int constraint_delete_exec (bContext *C, wmOperator *op) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint); + Object *ob= ptr.id.data; + bConstraint *con= ptr.data; + ListBase *lb; + + /* remove constraint itself */ + lb= get_active_constraints(ob); + free_constraint_data(con); + BLI_freelinkN(lb, con); + + ED_object_constraint_set_active(ob, NULL); + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); + + return OPERATOR_FINISHED; +} + +void CONSTRAINT_OT_delete (wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Delete Constraint"; + ot->idname= "CONSTRAINT_OT_delete"; + ot->description= "Remove constraitn from constraint stack."; + + /* callbacks */ + ot->exec= constraint_delete_exec; + ot->poll= constraint_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int constraint_move_down_exec (bContext *C, wmOperator *op) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint); + Object *ob= ptr.id.data; + bConstraint *con= ptr.data; + + if (con->next) { + ListBase *conlist= get_active_constraints(ob); + bConstraint *nextCon= con->next; + + /* insert the nominated constraint after the one that used to be after it */ + BLI_remlink(conlist, con); + BLI_insertlinkafter(conlist, nextCon, con); + + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); + + return OPERATOR_FINISHED; + } + + return OPERATOR_CANCELLED; +} + +void CONSTRAINT_OT_move_down (wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Move Constraint Down"; + ot->idname= "CONSTRAINT_OT_move_down"; + ot->description= "Move constraint down constraint stack."; + + /* callbacks */ + ot->exec= constraint_move_down_exec; + ot->poll= constraint_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + + +static int constraint_move_up_exec (bContext *C, wmOperator *op) +{ + PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint); + Object *ob= ptr.id.data; + bConstraint *con= ptr.data; + + if (con->prev) { + ListBase *conlist= get_active_constraints(ob); + bConstraint *prevCon= con->prev; + + /* insert the nominated constraint before the one that used to be before it */ + BLI_remlink(conlist, con); + BLI_insertlinkbefore(conlist, prevCon, con); + + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob); + + return OPERATOR_FINISHED; + } + + return OPERATOR_CANCELLED; +} + +void CONSTRAINT_OT_move_up (wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Move Constraint Up"; + ot->idname= "CONSTRAINT_OT_move_up"; + ot->description= "Move constraint up constraint stack."; + + /* callbacks */ + ot->exec= constraint_move_up_exec; + ot->poll= constraint_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/***************************** OPERATORS ****************************/ + +/************************ remove constraint operators *********************/ + +static int pose_constraints_clear_exec(bContext *C, wmOperator *op) +{ + Object *ob= CTX_data_active_object(C); + + /* free constraints for all selected bones */ + CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) + { + free_constraints(&pchan->constraints); + } + CTX_DATA_END; + + /* do updates */ + DAG_id_flush_update(&ob->id, OB_RECALC_OB); + WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_CONSTRAINT|NA_REMOVED, ob); + + return OPERATOR_FINISHED; +} + +void POSE_OT_constraints_clear(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Clear Constraints"; + ot->idname= "POSE_OT_constraints_clear"; + ot->description= "Clear all the constraints for the selected bones."; + + /* callbacks */ + ot->exec= pose_constraints_clear_exec; + ot->poll= ED_operator_posemode; // XXX - do we want to ensure there are selected bones too? +} + + +static int object_constraints_clear_exec(bContext *C, wmOperator *op) +{ + Object *ob= CTX_data_active_object(C); + + /* do freeing */ + // TODO: we should free constraints for all selected objects instead (to be more consistent with bones) + free_constraints(&ob->constraints); + + /* do updates */ + DAG_id_flush_update(&ob->id, OB_RECALC_OB); + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_constraints_clear(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Clear Constraints"; + ot->idname= "OBJECT_OT_constraints_clear"; + ot->description= "Clear all the constraints for the active Object only."; + + /* callbacks */ + ot->exec= object_constraints_clear_exec; + ot->poll= ED_operator_object_active; +} + +/************************ add constraint operators *********************/ + +/* get the Object and/or PoseChannel to use as target */ +static short get_new_constraint_target(bContext *C, int con_type, Object **tar_ob, bPoseChannel **tar_pchan, short add) +{ + Object *obact= CTX_data_active_object(C); + bPoseChannel *pchanact= get_active_posechannel(obact); + short only_curve= 0, only_mesh= 0, only_ob= 0; + short found= 0; + + /* clear tar_ob and tar_pchan fields before use + * - assume for now that both always exist... + */ + *tar_ob= NULL; + *tar_pchan= NULL; + + /* check if constraint type doesn't requires a target + * - if so, no need to get any targets + */ + switch (con_type) { + /* no-target constraints --------------------------- */ + /* null constraint - shouldn't even be added! */ + case CONSTRAINT_TYPE_NULL: + /* limit constraints - no targets needed */ + case CONSTRAINT_TYPE_LOCLIMIT: + case CONSTRAINT_TYPE_ROTLIMIT: + case CONSTRAINT_TYPE_SIZELIMIT: + return 0; + + /* restricted target-type constraints -------------- */ + /* NOTE: for these, we cannot try to add a target object if no valid ones are found, since that doesn't work */ + /* curve-based constraints - set the only_curve and only_ob flags */ + case CONSTRAINT_TYPE_TRACKTO: + case CONSTRAINT_TYPE_CLAMPTO: + case CONSTRAINT_TYPE_FOLLOWPATH: + only_curve= 1; + only_ob= 1; + add= 0; + break; + + /* mesh only? */ + case CONSTRAINT_TYPE_SHRINKWRAP: + only_mesh= 1; + only_ob= 1; + add= 0; + break; + + /* object only - add here is ok? */ + case CONSTRAINT_TYPE_RIGIDBODYJOINT: + only_ob= 1; + break; + } + + /* if the active Object is Armature, and we can search for bones, do so... */ + if ((obact->type == OB_ARMATURE) && (only_ob == 0)) { + /* search in list of selected Pose-Channels for target */ + CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) + { + /* just use the first one that we encounter, as long as it is not the active one */ + if (pchan != pchanact) { + *tar_ob= obact; + *tar_pchan= pchan; + found= 1; + + break; + } + } + CTX_DATA_END; + } + + /* if not yet found, try selected Objects... */ + if (found == 0) { + /* search in selected objects context */ + CTX_DATA_BEGIN(C, Object*, ob, selected_objects) + { + /* just use the first object we encounter (that isn't the active object) + * and which fulfills the criteria for the object-target that we've got + */ + if ( (ob != obact) && + ((!only_curve) || (ob->type == OB_CURVE)) && + ((!only_mesh) || (ob->type == OB_MESH)) ) + { + /* set target */ + *tar_ob= ob; + found= 1; + + /* perform some special operations on the target */ + if (only_curve) { + /* Curve-Path option must be enabled for follow-path constraints to be able to work */ + Curve *cu= (Curve *)ob->data; + cu->flag |= CU_PATH; + } + + break; + } + } + CTX_DATA_END; + } + + /* if still not found, add a new empty to act as a target (if allowed) */ + if ((found == 0) && (add)) { + Scene *scene= CTX_data_scene(C); + Base *base= BASACT, *newbase=NULL; + Object *obt; + + /* add new target object */ + obt= add_object(scene, OB_EMPTY); + + /* set layers OK */ + newbase= BASACT; + newbase->lay= base->lay; + obt->lay= newbase->lay; + + /* transform cent to global coords for loc */ + if (pchanact) { + /* since by default, IK targets the tip of the last bone, use the tip of the active PoseChannel + * if adding a target for an IK Constraint + */ + if (con_type == CONSTRAINT_TYPE_KINEMATIC) + VecMat4MulVecfl(obt->loc, obact->obmat, pchanact->pose_tail); + else + VecMat4MulVecfl(obt->loc, obact->obmat, pchanact->pose_head); + } + else + VECCOPY(obt->loc, obact->obmat[3]); + + /* restore, add_object sets active */ + BASACT= base; + base->flag |= SELECT; + + /* make our new target the new object */ + *tar_ob= obt; + found= 1; + } + + /* return whether there's any target */ + return found; +} + +/* used by add constraint operators to add the constraint required */ +static int constraint_add_exec(bContext *C, wmOperator *op, Object *ob, ListBase *list, int type, short setTarget) +{ + Scene *scene= CTX_data_scene(C); + bPoseChannel *pchan= get_active_posechannel(ob); + bConstraint *con; + + /* check if constraint to be added is valid for the given constraints stack */ + if (type == CONSTRAINT_TYPE_NULL) { + return OPERATOR_CANCELLED; + } + if ( (type == CONSTRAINT_TYPE_RIGIDBODYJOINT) && (list != &ob->constraints) ) { + BKE_report(op->reports, RPT_ERROR, "Rigid Body Joint Constraint can only be added to Objects."); + return OPERATOR_CANCELLED; + } + if ( (type == CONSTRAINT_TYPE_KINEMATIC) && ((!pchan) || (list != &pchan->constraints)) ) { + BKE_report(op->reports, RPT_ERROR, "IK Constraint can only be added to Bones."); + return OPERATOR_CANCELLED; + } + + /* create a new constraint of the type requried, and add it to the active/given constraints list */ + con = add_new_constraint(type); + + if (list) { + bConstraint *coniter; + + /* add new constraint to end of list of constraints before ensuring that it has a unique name + * (otherwise unique-naming code will fail, since it assumes element exists in list) + */ + BLI_addtail(list, con); + unique_constraint_name(con, list); + + /* if the target list is a list on some PoseChannel belonging to a proxy-protected + * Armature layer, we must tag newly added constraints with a flag which allows them + * to persist after proxy syncing has been done + */ + if (proxylocked_constraints_owner(ob, pchan)) + con->flag |= CONSTRAINT_PROXY_LOCAL; + + /* make this constraint the active one + * - since constraint was added at end of stack, we can just go + * through deactivating all previous ones + */ + con->flag |= CONSTRAINT_ACTIVE; + for (coniter= con->prev; coniter; coniter= coniter->prev) + coniter->flag &= ~CONSTRAINT_ACTIVE; + } + + /* get the first selected object/bone, and make that the target + * - apart from the buttons-window add buttons, we shouldn't add in this way + */ + if (setTarget) { + Object *tar_ob= NULL; + bPoseChannel *tar_pchan= NULL; + + /* get the target objects, adding them as need be */ + if (get_new_constraint_target(C, type, &tar_ob, &tar_pchan, 1)) { + /* method of setting target depends on the type of target we've got + * - by default, just set the first target (distinction here is only for multiple-targetted constraints) + */ + if (tar_pchan) + set_constraint_nth_target(con, tar_ob, tar_pchan->name, 0); + else + set_constraint_nth_target(con, tar_ob, "", 0); + } + } + + /* do type-specific tweaking to the constraint settings */ + switch (type) { + case CONSTRAINT_TYPE_CHILDOF: + { + /* if this constraint is being added to a posechannel, make sure + * the constraint gets evaluated in pose-space */ + if (ob->mode & OB_MODE_POSE) { + con->ownspace = CONSTRAINT_SPACE_POSE; + con->flag |= CONSTRAINT_SPACEONCE; + } + } + break; + + case CONSTRAINT_TYPE_PYTHON: // FIXME: this code is not really valid anymore + { + char *menustr; + int scriptint= 0; +#ifndef DISABLE_PYTHON + /* popup a list of usable scripts */ + menustr = buildmenu_pyconstraints(NULL, &scriptint); + // XXX scriptint = pupmenu(menustr); + MEM_freeN(menustr); + + /* only add constraint if a script was chosen */ + if (scriptint) { + /* add constraint */ + validate_pyconstraint_cb(con->data, &scriptint); + + /* make sure target allowance is set correctly */ + BPY_pyconstraint_update(ob, con); + } +#endif + } + default: + break; + } + + /* make sure all settings are valid - similar to above checks, but sometimes can be wrong */ + object_test_constraints(ob); + + if (ob->pose) + update_pose_constraint_flags(ob->pose); + + + /* force depsgraph to get recalculated since new relationships added */ + DAG_scene_sort(scene); /* sort order of objects */ + + if ((ob->type==OB_ARMATURE) && (pchan)) { + ob->pose->flag |= POSE_RECALC; /* sort pose channels */ + DAG_id_flush_update(&ob->id, OB_RECALC_DATA|OB_RECALC_OB); + } + else + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + + /* notifiers for updates */ + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, ob); + + return OPERATOR_FINISHED; +} + +/* ------------------ */ + +/* dummy operator callback */ +static int object_constraint_add_exec(bContext *C, wmOperator *op) +{ + ScrArea *sa= CTX_wm_area(C); + Object *ob; + int type= RNA_enum_get(op->ptr, "type"); + short with_targets= 0; + + /* get active object from context */ + if (sa->spacetype == SPACE_BUTS) + ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; + else + ob= CTX_data_active_object(C); + + if (!ob) { + BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to."); + return OPERATOR_CANCELLED; + } + + /* hack: set constraint targets from selected objects in context is allowed when + * operator name included 'with_targets', since the menu doesn't allow multiple properties + */ + if (strstr(op->idname, "with_targets")) + with_targets= 1; + + return constraint_add_exec(C, op, ob, &ob->constraints, type, with_targets); +} + +/* dummy operator callback */ +static int pose_constraint_add_exec(bContext *C, wmOperator *op) +{ + ScrArea *sa= CTX_wm_area(C); + Object *ob; + int type= RNA_enum_get(op->ptr, "type"); + short with_targets= 0; + + /* get active object from context */ + if (sa->spacetype == SPACE_BUTS) + ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; + else + ob= CTX_data_active_object(C); + + if (!ob) { + BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to."); + return OPERATOR_CANCELLED; + } + + /* hack: set constraint targets from selected objects in context is allowed when + * operator name included 'with_targets', since the menu doesn't allow multiple properties + */ + if (strstr(op->idname, "with_targets")) + with_targets= 1; + + return constraint_add_exec(C, op, ob, get_active_constraints(ob), type, with_targets); +} + +/* ------------------ */ + +void OBJECT_OT_constraint_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Constraint"; + ot->description = "Add a constraint to the active object."; + ot->idname= "OBJECT_OT_constraint_add"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_constraint_add_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", ""); +} + +void OBJECT_OT_constraint_add_with_targets(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Constraint (with Targets)"; + ot->description = "Add a constraint to the active object, with target (where applicable) set to the selected Objects/Bones."; + ot->idname= "OBJECT_OT_constraint_add_with_targets"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_constraint_add_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", ""); +} + +void POSE_OT_constraint_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Constraint"; + ot->description = "Add a constraint to the active bone."; + ot->idname= "POSE_OT_constraint_add"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= pose_constraint_add_exec; + ot->poll= ED_operator_posemode; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", ""); +} + +void POSE_OT_constraint_add_with_targets(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Constraint (with Targets)"; + ot->description = "Add a constraint to the active bone, with target (where applicable) set to the selected Objects/Bones."; + ot->idname= "POSE_OT_constraint_add_with_targets"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= pose_constraint_add_exec; + ot->poll= ED_operator_posemode; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", ""); +} + +/************************ IK Constraint operators *********************/ +/* NOTE: only for Pose-Channels */ +// TODO: should these be here, or back in editors/armature/poseobject.c again? + +/* present menu with options + validation for targets to use */ +static int pose_ik_add_invoke(bContext *C, wmOperator *op, wmEvent *evt) +{ + Object *ob= CTX_data_active_object(C); + bPoseChannel *pchan= get_active_posechannel(ob); + bConstraint *con= NULL; + + uiPopupMenu *pup; + uiLayout *layout; + Object *tar_ob= NULL; + bPoseChannel *tar_pchan= NULL; + + /* must have active bone */ + if (ELEM(NULL, ob, pchan)) { + BKE_report(op->reports, RPT_ERROR, "Must have active bone to add IK Constraint to."); + return OPERATOR_CANCELLED; + } + + /* bone must not have any constraints already */ + for (con= pchan->constraints.first; con; con= con->next) { + if (con->type==CONSTRAINT_TYPE_KINEMATIC) break; + } + if (con) { + BKE_report(op->reports, RPT_ERROR, "Bone already has IK Constraint."); + return OPERATOR_CANCELLED; + } + + /* prepare popup menu to choose targetting options */ + pup= uiPupMenuBegin(C, "Add IK", 0); + layout= uiPupMenuLayout(pup); + + /* the type of targets we'll set determines the menu entries to show... */ + if (get_new_constraint_target(C, CONSTRAINT_TYPE_KINEMATIC, &tar_ob, &tar_pchan, 0)) { + /* bone target, or object target? + * - the only thing that matters is that we want a target... + */ + if (tar_pchan) + uiItemBooleanO(layout, "To Active Bone", 0, "POSE_OT_ik_add", "with_targets", 1); + else + uiItemBooleanO(layout, "To Active Object", 0, "POSE_OT_ik_add", "with_targets", 1); + } + else { + /* we have a choice of adding to a new empty, or not setting any target (targetless IK) */ + uiItemBooleanO(layout, "To New Empty Object", 0, "POSE_OT_ik_add", "with_targets", 1); + uiItemBooleanO(layout, "Without Targets", 0, "POSE_OT_ik_add", "with_targets", 0); + } + + /* finish building the menu, and process it (should result in calling self again) */ + uiPupMenuEnd(C, pup); + + return OPERATOR_CANCELLED; +} + +/* call constraint_add_exec() to add the IK constraint */ +static int pose_ik_add_exec(bContext *C, wmOperator *op) +{ + Object *ob= CTX_data_active_object(C); + int with_targets= RNA_boolean_get(op->ptr, "with_targets"); + + /* add the constraint - all necessary checks should have been done by the invoke() callback already... */ + return constraint_add_exec(C, op, ob, get_active_constraints(ob), CONSTRAINT_TYPE_KINEMATIC, with_targets); +} + +void POSE_OT_ik_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add IK to Bone"; + ot->description= "Add IK Constraint to the active Bone."; + ot->idname= "POSE_OT_ik_add"; + + /* api callbacks */ + ot->invoke= pose_ik_add_invoke; + ot->exec= pose_ik_add_exec; + ot->poll= ED_operator_posemode; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_boolean(ot->srna, "with_targets", 1, "With Targets", "Assign IK Constraint with targets derived from the select bones/objects"); +} + +/* ------------------ */ + +/* remove IK constraints from selected bones */ +static int pose_ik_clear_exec(bContext *C, wmOperator *op) +{ + Object *ob= CTX_data_active_object(C); + + /* only remove IK Constraints */ + CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) + { + bConstraint *con, *next; + + // TODO: should we be checking if these contraints were local before we try and remove them? + for (con= pchan->constraints.first; con; con= next) { + next= con->next; + if (con->type==CONSTRAINT_TYPE_KINEMATIC) { + free_constraint_data(con); + BLI_freelinkN(&pchan->constraints, con); + } + } + pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET); + } + CTX_DATA_END; + + /* */ + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + + /* note, notifier might evolve */ + WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob); + + return OPERATOR_FINISHED; +} + +void POSE_OT_ik_clear(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Remove IK"; + ot->description= "Remove all IK Constraints from selected bones."; + ot->idname= "POSE_OT_ik_clear"; + + /* api callbacks */ + ot->exec= pose_ik_clear_exec; + ot->poll= ED_operator_posemode; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + diff --git a/source/blender/editors/object/object_edit.c b/source/blender/editors/object/object_edit.c index 31a604a79df..daa63da03db 100644 --- a/source/blender/editors/object/object_edit.c +++ b/source/blender/editors/object/object_edit.c @@ -145,8 +145,6 @@ static void error() {} static void waitcursor(int val) {} static int pupmenu(const char *msg) {return 0;} -static int pupmenu_col(const char *msg, int val) {return 0;} -static int okee(const char *msg) {return 0;} /* port over here */ static bContext *C; @@ -156,6636 +154,1433 @@ static void error_libdata() {} /* --------------------------------- */ -/* simple API for object selection, rather than just using the flag - * this takes into account the 'restrict selection in 3d view' flag. - * deselect works always, the restriction just prevents selection */ - -/* Note: send a NC_SCENE|ND_OB_SELECT notifier yourself! */ - -void ED_base_object_select(Base *base, short mode) -{ - if (base) { - if (mode==BA_SELECT) { - if (!(base->object->restrictflag & OB_RESTRICT_SELECT)) - if (mode==BA_SELECT) base->flag |= SELECT; - } - else if (mode==BA_DESELECT) { - base->flag &= ~SELECT; - } - base->object->flag= base->flag; - } -} - -/* also to set active NULL */ -void ED_base_object_activate(bContext *C, Base *base) +void ED_object_apply_obmat(Object *ob) { - Scene *scene= CTX_data_scene(C); - Base *tbase; + float mat[3][3], imat[3][3], tmat[3][3]; - /* sets scene->basact */ - BASACT= base; + /* from obmat to loc rot size */ - if(base) { - - /* XXX old signals, remember to handle notifiers now! */ - // select_actionchannel_by_name(base->object->action, "Object", 1); - - /* disable temporal locks */ - for(tbase=FIRSTBASE; tbase; tbase= tbase->next) { - if(base!=tbase && (tbase->object->shapeflag & OB_SHAPE_TEMPLOCK)) { - tbase->object->shapeflag &= ~OB_SHAPE_TEMPLOCK; - DAG_id_flush_update(&tbase->object->id, OB_RECALC_DATA); - } - } - WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene); - } - else - WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, NULL); -} + if(ob==NULL) return; + Mat3CpyMat4(mat, ob->obmat); + + VECCOPY(ob->loc, ob->obmat[3]); + Mat3ToEul(mat, ob->rot); + EulToMat3(ob->rot, tmat); -/* exported */ -void ED_object_base_init_from_view(bContext *C, Base *base) -{ - View3D *v3d= CTX_wm_view3d(C); - Scene *scene= CTX_data_scene(C); - Object *ob= base->object; + Mat3Inv(imat, tmat); - if (scene==NULL) - return; + Mat3MulMat3(tmat, imat, mat); + + ob->size[0]= tmat[0][0]; + ob->size[1]= tmat[1][1]; + ob->size[2]= tmat[2][2]; - if (v3d==NULL) { - base->lay = scene->lay; - VECCOPY(ob->loc, scene->cursor); - } - else { - if (v3d->localview) { - base->lay= ob->lay= v3d->layact | v3d->lay; - VECCOPY(ob->loc, v3d->cursor); - } - else { - base->lay= ob->lay= v3d->layact; - VECCOPY(ob->loc, scene->cursor); - } - - if (U.flag & USER_ADD_VIEWALIGNED) { - ARegion *ar= CTX_wm_region(C); - if(ar) { - RegionView3D *rv3d= ar->regiondata; - - rv3d->viewquat[0]= -rv3d->viewquat[0]; - QuatToEul(rv3d->viewquat, ob->rot); - rv3d->viewquat[0]= -rv3d->viewquat[0]; - } - } - } - where_is_object(scene, ob); -} - -/* ******************* add object operator ****************** */ - -static EnumPropertyItem prop_object_types[] = { - {OB_MESH, "MESH", 0, "Mesh", ""}, - {OB_CURVE, "CURVE", 0, "Curve", ""}, - {OB_SURF, "SURFACE", 0, "Surface", ""}, - {OB_MBALL, "META", 0, "Meta", ""}, - {OB_FONT, "TEXT", 0, "Text", ""}, - {0, "", 0, NULL, NULL}, - {OB_ARMATURE, "ARMATURE", 0, "Armature", ""}, - {OB_LATTICE, "LATTICE", 0, "Lattice", ""}, - {OB_EMPTY, "EMPTY", 0, "Empty", ""}, - {0, "", 0, NULL, NULL}, - {OB_CAMERA, "CAMERA", 0, "Camera", ""}, - {OB_LAMP, "LAMP", 0, "Lamp", ""}, - {0, NULL, 0, NULL, NULL} -}; - - - -void add_object_draw(Scene *scene, View3D *v3d, int type) /* for toolbox or menus, only non-editmode stuff */ -{ - /* keep here to get things compile, remove later */ } -/* for object add primitive operators */ -static Object *object_add_type(bContext *C, int type) +/* ********* clear/set restrict view *********/ +static int object_restrictview_clear_exec(bContext *C, wmOperator *op) { + ScrArea *sa= CTX_wm_area(C); + View3D *v3d= sa->spacedata.first; Scene *scene= CTX_data_scene(C); - Object *ob; - - /* for as long scene has editmode... */ - if (CTX_data_edit_object(C)) - ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */ + Base *base; + int changed = 0; - /* deselects all, sets scene->basact */ - ob= add_object(scene, type); - /* editor level activate, notifiers */ - ED_base_object_activate(C, BASACT); - - /* more editor stuff */ - ED_object_base_init_from_view(C, BASACT); - - DAG_scene_sort(scene); - - return ob; -} + /* XXX need a context loop to handle such cases */ + for(base = FIRSTBASE; base; base=base->next){ + if((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) { + base->flag |= SELECT; + base->object->flag = base->flag; + base->object->restrictflag &= ~OB_RESTRICT_VIEW; + changed = 1; + } + } + if (changed) { + DAG_scene_sort(scene); + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene); + } -/* for object add operator */ -static int object_add_exec(bContext *C, wmOperator *op) -{ - object_add_type(C, RNA_int_get(op->ptr, "type")); - return OPERATOR_FINISHED; } -void OBJECT_OT_object_add(wmOperatorType *ot) +void OBJECT_OT_restrictview_clear(wmOperatorType *ot) { + /* identifiers */ - ot->name= "Add Object"; - ot->description = "Add an object to the scene."; - ot->idname= "OBJECT_OT_object_add"; + ot->name= "Clear Restrict View"; + ot->description = "Reveal the object by setting the restrictview flag."; + ot->idname= "OBJECT_OT_restrictview_clear"; /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_add_exec; - - ot->poll= ED_operator_scene_editable; + ot->exec= object_restrictview_clear_exec; + ot->poll= ED_operator_view3d_active; /* flags */ ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_object_types, 0, "Type", ""); } -/* ***************** add primitives *************** */ -/* ****** work both in and outside editmode ****** */ - -static EnumPropertyItem prop_mesh_types[] = { - {0, "PLANE", ICON_MESH_PLANE, "Plane", ""}, - {1, "CUBE", ICON_MESH_CUBE, "Cube", ""}, - {2, "CIRCLE", ICON_MESH_CIRCLE, "Circle", ""}, - {3, "UVSPHERE", ICON_MESH_UVSPHERE, "UVsphere", ""}, - {4, "ICOSPHERE", ICON_MESH_ICOSPHERE, "Icosphere", ""}, - {5, "CYLINDER", ICON_MESH_TUBE, "Cylinder", ""}, - {6, "CONE", ICON_MESH_CONE, "Cone", ""}, - {0, "", 0, NULL, NULL}, - {7, "GRID", ICON_MESH_GRID, "Grid", ""}, - {8, "MONKEY", ICON_MESH_MONKEY, "Monkey", ""}, - {0, NULL, 0, NULL, NULL} -}; - -static int object_add_mesh_exec(bContext *C, wmOperator *op) +static int object_restrictview_set_exec(bContext *C, wmOperator *op) { - Object *obedit= CTX_data_edit_object(C); - int newob= 0; + Scene *scene= CTX_data_scene(C); + short changed = 0; + int unselected= RNA_boolean_get(op->ptr, "unselected"); - if(obedit==NULL || obedit->type!=OB_MESH) { - object_add_type(C, OB_MESH); - ED_object_enter_editmode(C, EM_DO_UNDO); - newob = 1; - } - else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); - - switch(RNA_enum_get(op->ptr, "type")) { - case 0: - WM_operator_name_call(C, "MESH_OT_primitive_plane_add", WM_OP_INVOKE_REGION_WIN, NULL); - break; - case 1: - WM_operator_name_call(C, "MESH_OT_primitive_cube_add", WM_OP_INVOKE_REGION_WIN, NULL); - break; - case 2: - WM_operator_name_call(C, "MESH_OT_primitive_circle_add", WM_OP_INVOKE_REGION_WIN, NULL); - break; - case 3: - WM_operator_name_call(C, "MESH_OT_primitive_uv_sphere_add", WM_OP_INVOKE_REGION_WIN, NULL); - break; - case 4: - WM_operator_name_call(C, "MESH_OT_primitive_ico_sphere_add", WM_OP_INVOKE_REGION_WIN, NULL); - break; - case 5: - WM_operator_name_call(C, "MESH_OT_primitive_cylinder_add", WM_OP_INVOKE_REGION_WIN, NULL); - break; - case 6: - WM_operator_name_call(C, "MESH_OT_primitive_cone_add", WM_OP_INVOKE_REGION_WIN, NULL); - break; - case 7: - WM_operator_name_call(C, "MESH_OT_primitive_grid_add", WM_OP_INVOKE_REGION_WIN, NULL); - break; - case 8: - WM_operator_name_call(C, "MESH_OT_primitive_monkey_add", WM_OP_INVOKE_REGION_WIN, NULL); - break; + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + if(!unselected) { + if (base->flag & SELECT){ + base->flag &= ~SELECT; + base->object->flag = base->flag; + base->object->restrictflag |= OB_RESTRICT_VIEW; + changed = 1; + if (base==BASACT) { + ED_base_object_activate(C, NULL); + } + } + } + else { + if (!(base->flag & SELECT)){ + base->object->restrictflag |= OB_RESTRICT_VIEW; + changed = 1; + } + } } - /* userdef */ - if (newob && (U.flag & USER_ADD_EDITMODE)==0) { - ED_object_exit_editmode(C, EM_FREEDATA); + CTX_DATA_END; + + if (changed) { + DAG_scene_sort(scene); + + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); + } - - WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); - + return OPERATOR_FINISHED; } - -void OBJECT_OT_mesh_add(wmOperatorType *ot) +void OBJECT_OT_restrictview_set(wmOperatorType *ot) { /* identifiers */ - ot->name= "Add Mesh"; - ot->description = "Add a mesh object to the scene."; - ot->idname= "OBJECT_OT_mesh_add"; + ot->name= "Set Restrict View"; + ot->description = "Hide the object by setting the restrictview flag."; + ot->idname= "OBJECT_OT_restrictview_set"; /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_add_mesh_exec; + ot->exec= object_restrictview_set_exec; + ot->poll= ED_operator_view3d_active; - ot->poll= ED_operator_scene_editable; + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - /* flags: no register or undo, this operator calls operators */ - ot->flag= 0; //OPTYPE_REGISTER|OPTYPE_UNDO; + RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects."); - RNA_def_enum(ot->srna, "type", prop_mesh_types, 0, "Primitive", ""); } -static EnumPropertyItem prop_curve_types[] = { - {CU_BEZIER|CU_PRIM_CURVE, "BEZIER_CURVE", ICON_CURVE_BEZCURVE, "Bezier Curve", ""}, - {CU_BEZIER|CU_PRIM_CIRCLE, "BEZIER_CIRCLE", ICON_CURVE_BEZCIRCLE, "Bezier Circle", ""}, - {CU_NURBS|CU_PRIM_CURVE, "NURBS_CURVE", ICON_CURVE_NCURVE, "NURBS Curve", ""}, - {CU_NURBS|CU_PRIM_CIRCLE, "NURBS_CIRCLE", ICON_CURVE_NCIRCLE, "NURBS Circle", ""}, - {CU_NURBS|CU_PRIM_PATH, "PATH", ICON_CURVE_PATH, "Path", ""}, - {0, NULL, 0, NULL, NULL} -}; -static int object_add_curve_exec(bContext *C, wmOperator *op) +/* ******************* toggle editmode operator ***************** */ + +void ED_object_exit_editmode(bContext *C, int flag) { + Scene *scene= CTX_data_scene(C); Object *obedit= CTX_data_edit_object(C); - ListBase *editnurb; - Nurb *nu; - int newob= 0; - - if(obedit==NULL || obedit->type!=OB_CURVE) { - object_add_type(C, OB_CURVE); - ED_object_enter_editmode(C, 0); - newob = 1; - } - else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + int freedata = flag & EM_FREEDATA; - obedit= CTX_data_edit_object(C); - nu= add_nurbs_primitive(C, RNA_enum_get(op->ptr, "type"), newob); - editnurb= curve_get_editcurve(obedit); - BLI_addtail(editnurb, nu); + if(obedit==NULL) return; - /* userdef */ - if (newob && (U.flag & USER_ADD_EDITMODE)==0) { - ED_object_exit_editmode(C, EM_FREEDATA); + if(flag & EM_WAITCURSOR) waitcursor(1); + if(obedit->type==OB_MESH) { + Mesh *me= obedit->data; + +// if(EM_texFaceCheck()) + +// if(retopo_mesh_paint_check()) +// retopo_end_okee(); + + if(me->edit_mesh->totvert>MESH_MAX_VERTS) { + error("Too many vertices"); + return; + } + load_editMesh(scene, obedit); + + if(freedata) { + free_editMesh(me->edit_mesh); + MEM_freeN(me->edit_mesh); + me->edit_mesh= NULL; + } + + if(obedit->restore_mode & OB_MODE_WEIGHT_PAINT) + mesh_octree_table(obedit, NULL, NULL, 'e'); + } + else if (obedit->type==OB_ARMATURE) { + ED_armature_from_edit(obedit); + if(freedata) + ED_armature_edit_free(obedit); + } + else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) { + load_editNurb(obedit); + if(freedata) free_editNurb(obedit); + } + else if(obedit->type==OB_FONT && freedata) { + load_editText(obedit); + if(freedata) free_editText(obedit); + } + else if(obedit->type==OB_LATTICE) { + load_editLatt(obedit); + if(freedata) free_editLatt(obedit); + } + else if(obedit->type==OB_MBALL) { + load_editMball(obedit); + if(freedata) free_editMball(obedit); } - - WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); - - return OPERATOR_FINISHED; -} - -static int object_add_curve_invoke(bContext *C, wmOperator *op, wmEvent *event) -{ - Object *obedit= CTX_data_edit_object(C); - uiPopupMenu *pup; - uiLayout *layout; - - pup= uiPupMenuBegin(C, op->type->name, 0); - layout= uiPupMenuLayout(pup); - if(!obedit || obedit->type == OB_CURVE) - uiItemsEnumO(layout, op->type->idname, "type"); - else - uiItemsEnumO(layout, "OBJECT_OT_surface_add", "type"); - uiPupMenuEnd(C, pup); - - return OPERATOR_CANCELLED; -} -void OBJECT_OT_curve_add(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Add Curve"; - ot->description = "Add a curve object to the scene."; - ot->idname= "OBJECT_OT_curve_add"; - - /* api callbacks */ - ot->invoke= object_add_curve_invoke; - ot->exec= object_add_curve_exec; - - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_curve_types, 0, "Primitive", ""); -} + /* freedata only 0 now on file saves */ + if(freedata) { + /* for example; displist make is different in editmode */ + scene->obedit= NULL; // XXX for context + + BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_DEPSGRAPH); -static EnumPropertyItem prop_surface_types[]= { - {CU_PRIM_CURVE|CU_NURBS, "NURBS_CURVE", ICON_SURFACE_NCURVE, "NURBS Curve", ""}, - {CU_PRIM_CIRCLE|CU_NURBS, "NURBS_CIRCLE", ICON_SURFACE_NCIRCLE, "NURBS Circle", ""}, - {CU_PRIM_PATCH|CU_NURBS, "NURBS_SURFACE", ICON_SURFACE_NSURFACE, "NURBS Surface", ""}, - {CU_PRIM_TUBE|CU_NURBS, "NURBS_TUBE", ICON_SURFACE_NTUBE, "NURBS Tube", ""}, - {CU_PRIM_SPHERE|CU_NURBS, "NURBS_SPHERE", ICON_SURFACE_NSPHERE, "NURBS Sphere", ""}, - {CU_PRIM_DONUT|CU_NURBS, "NURBS_DONUT", ICON_SURFACE_NDONUT, "NURBS Donut", ""}, - {0, NULL, 0, NULL, NULL} -}; - -static int object_add_surface_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - ListBase *editnurb; - Nurb *nu; - int newob= 0; + /* also flush ob recalc, doesn't take much overhead, but used for particles */ + DAG_id_flush_update(&obedit->id, OB_RECALC_OB|OB_RECALC_DATA); - if(obedit==NULL || obedit->type!=OB_SURF) { - object_add_type(C, OB_SURF); - ED_object_enter_editmode(C, 0); - newob = 1; - } - else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + ED_undo_push(C, "Editmode"); - obedit= CTX_data_edit_object(C); - nu= add_nurbs_primitive(C, RNA_enum_get(op->ptr, "type"), newob); - editnurb= curve_get_editcurve(obedit); - BLI_addtail(editnurb, nu); + if(flag & EM_WAITCURSOR) waitcursor(0); - /* userdef */ - if (newob && (U.flag & USER_ADD_EDITMODE)==0) { - ED_object_exit_editmode(C, EM_FREEDATA); + WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene); } - - WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); - - return OPERATOR_FINISHED; + + obedit->mode &= ~OB_MODE_EDIT; + ED_object_toggle_modes(C, obedit->restore_mode); } -void OBJECT_OT_surface_add(wmOperatorType *ot) + +void ED_object_enter_editmode(bContext *C, int flag) { - /* identifiers */ - ot->name= "Add Surface"; - ot->description = "Add a surface object to the scene."; - ot->idname= "OBJECT_OT_surface_add"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_add_surface_exec; + Scene *scene= CTX_data_scene(C); + Base *base= CTX_data_active_base(C); + Object *ob; + ScrArea *sa= CTX_wm_area(C); + View3D *v3d= NULL; + int ok= 0; - ot->poll= ED_operator_scene_editable; + if(scene->id.lib) return; + if(base==NULL) return; - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + if(sa && sa->spacetype==SPACE_VIEW3D) + v3d= sa->spacedata.first; - RNA_def_enum(ot->srna, "type", prop_surface_types, 0, "Primitive", ""); -} + if(v3d && (base->lay & v3d->lay)==0) return; + else if(!v3d && (base->lay & scene->lay)==0) return; -static EnumPropertyItem prop_metaball_types[]= { - {MB_BALL, "MBALL_BALL", ICON_META_BALL, "Meta Ball", ""}, - {MB_TUBE, "MBALL_TUBE", ICON_META_TUBE, "Meta Tube", ""}, - {MB_PLANE, "MBALL_PLANE", ICON_META_PLANE, "Meta Plane", ""}, - {MB_CUBE, "MBALL_CUBE", ICON_META_CUBE, "Meta Cube", ""}, - {MB_ELIPSOID, "MBALL_ELLIPSOID", ICON_META_ELLIPSOID, "Meta Ellipsoid", ""}, - {0, NULL, 0, NULL, NULL} -}; + ob = base->object; -static int object_metaball_add_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - MetaBall *mball; - MetaElem *elem; - int newob= 0; - - if(obedit==NULL || obedit->type!=OB_MBALL) { - object_add_type(C, OB_MBALL); - ED_object_enter_editmode(C, 0); - newob = 1; - } - else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); - - obedit= CTX_data_edit_object(C); - elem= (MetaElem*)add_metaball_primitive(C, RNA_enum_get(op->ptr, "type"), newob); - mball= (MetaBall*)obedit->data; - BLI_addtail(mball->editelems, elem); + if(ob==NULL) return; + if(ob->data==NULL) return; - /* userdef */ - if (newob && (U.flag & USER_ADD_EDITMODE)==0) { - ED_object_exit_editmode(C, EM_FREEDATA); + if (object_data_is_libdata(ob)) { + error_libdata(); + return; } - WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); - - return OPERATOR_FINISHED; -} - -static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *event) -{ - Object *obedit= CTX_data_edit_object(C); - uiPopupMenu *pup; - uiLayout *layout; - - pup= uiPupMenuBegin(C, op->type->name, 0); - layout= uiPupMenuLayout(pup); - if(!obedit || obedit->type == OB_MBALL) - uiItemsEnumO(layout, op->type->idname, "type"); - else - uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type"); - uiPupMenuEnd(C, pup); - - return OPERATOR_CANCELLED; -} - -void OBJECT_OT_metaball_add(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Metaball"; - ot->description= "Add an metaball object to the scene."; - ot->idname= "OBJECT_OT_metaball_add"; + if(flag & EM_WAITCURSOR) waitcursor(1); - /* api callbacks */ - ot->invoke= object_metaball_add_invoke; - ot->exec= object_metaball_add_exec; - ot->poll= ED_operator_scene_editable; + ob->restore_mode = ob->mode; + ED_object_toggle_modes(C, ob->mode); - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_metaball_types, 0, "Primitive", ""); -} -static int object_add_text_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); + ob->mode |= OB_MODE_EDIT; - if(obedit && obedit->type==OB_FONT) - return OPERATOR_CANCELLED; - - object_add_type(C, OB_FONT); - obedit= CTX_data_active_object(C); + if(ob->type==OB_MESH) { + Mesh *me= ob->data; + + if(me->pv) mesh_pmv_off(ob, me); + ok= 1; + scene->obedit= ob; // context sees this + + make_editMesh(scene, ob); - if(U.flag & USER_ADD_EDITMODE) - ED_object_enter_editmode(C, 0); - - WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); - - return OPERATOR_FINISHED; -} + WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MESH, scene); + } + else if (ob->type==OB_ARMATURE){ + bArmature *arm= base->object->data; + if (!arm) return; + /* + * The function object_data_is_libdata make a problem here, the + * check for ob->proxy return 0 and let blender enter to edit mode + * this causa a crash when you try leave the edit mode. + * The problem is that i can't remove the ob->proxy check from + * object_data_is_libdata that prevent the bugfix #6614, so + * i add this little hack here. + */ + if(arm->id.lib) { + error_libdata(); + return; + } + ok=1; + scene->obedit= ob; + ED_armature_to_edit(ob); + /* to ensure all goes in restposition and without striding */ + DAG_id_flush_update(&ob->id, OB_RECALC); -void OBJECT_OT_text_add(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Add Text"; - ot->description = "Add a text object to the scene"; - ot->idname= "OBJECT_OT_text_add"; - - /* api callbacks */ - ot->exec= object_add_text_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} + WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_ARMATURE, scene); + } + else if(ob->type==OB_FONT) { + scene->obedit= ob; // XXX for context + ok= 1; + make_editText(ob); -static int object_armature_add_exec(bContext *C, wmOperator *op) -{ - Object *obedit= CTX_data_edit_object(C); - View3D *v3d= CTX_wm_view3d(C); - RegionView3D *rv3d= NULL; - int newob= 0; - - if ((obedit==NULL) || (obedit->type != OB_ARMATURE)) { - object_add_type(C, OB_ARMATURE); - ED_object_enter_editmode(C, 0); - newob = 1; + WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_TEXT, scene); } - else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); - - if(v3d) - rv3d= CTX_wm_region(C)->regiondata; - - /* v3d and rv3d are allowed to be NULL */ - add_primitive_bone(CTX_data_scene(C), v3d, rv3d); + else if(ob->type==OB_MBALL) { + scene->obedit= ob; // XXX for context + ok= 1; + make_editMball(ob); - /* userdef */ - if (newob && (U.flag & USER_ADD_EDITMODE)==0) { - ED_object_exit_editmode(C, EM_FREEDATA); + WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MBALL, scene); + } + else if(ob->type==OB_LATTICE) { + scene->obedit= ob; // XXX for context + ok= 1; + make_editLatt(ob); + + WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_LATTICE, scene); + } + else if(ob->type==OB_SURF || ob->type==OB_CURVE) { + ok= 1; + scene->obedit= ob; // XXX for context + make_editNurb(ob); + + WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_CURVE, scene); } - WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit); + if(ok) { + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + } + else { + scene->obedit= NULL; // XXX for context + WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene); + } - return OPERATOR_FINISHED; + if(flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode"); + if(flag & EM_WAITCURSOR) waitcursor(0); } -void OBJECT_OT_armature_add(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Add Armature"; - ot->description = "Add an armature object to the scene."; - ot->idname= "OBJECT_OT_armature_add"; +static int editmode_toggle_exec(bContext *C, wmOperator *op) +{ - /* api callbacks */ - ot->exec= object_armature_add_exec; - ot->poll= ED_operator_scene_editable; + if(!CTX_data_edit_object(C)) + ED_object_enter_editmode(C, EM_WAITCURSOR); + else + ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + return OPERATOR_FINISHED; } -static int object_primitive_add_invoke(bContext *C, wmOperator *op, wmEvent *event) +static int editmode_toggle_poll(bContext *C) { - uiPopupMenu *pup= uiPupMenuBegin(C, "Add Object", 0); - uiLayout *layout= uiPupMenuLayout(pup); - - uiItemMenuEnumO(layout, "Mesh", ICON_OUTLINER_OB_MESH, "OBJECT_OT_mesh_add", "type"); - uiItemMenuEnumO(layout, "Curve", ICON_OUTLINER_OB_CURVE, "OBJECT_OT_curve_add", "type"); - uiItemMenuEnumO(layout, "Surface", ICON_OUTLINER_OB_SURFACE, "OBJECT_OT_surface_add", "type"); - uiItemMenuEnumO(layout, NULL, ICON_OUTLINER_OB_META, "OBJECT_OT_metaball_add", "type"); - uiItemO(layout, "Text", ICON_OUTLINER_OB_FONT, "OBJECT_OT_text_add"); - uiItemS(layout); - uiItemO(layout, "Armature", ICON_OUTLINER_OB_ARMATURE, "OBJECT_OT_armature_add"); - uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_LATTICE, "OBJECT_OT_object_add", "type", OB_LATTICE); - uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_EMPTY, "OBJECT_OT_object_add", "type", OB_EMPTY); - uiItemS(layout); - uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_CAMERA, "OBJECT_OT_object_add", "type", OB_CAMERA); - uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_LAMP, "OBJECT_OT_object_add", "type", OB_LAMP); - - uiPupMenuEnd(C, pup); - - /* this operator is only for a menu, not used further */ - return OPERATOR_CANCELLED; + Object *ob = CTX_data_active_object(C); + + return ob && (ob->type == OB_MESH || ob->type == OB_ARMATURE || + ob->type == OB_FONT || ob->type == OB_MBALL || + ob->type == OB_LATTICE || ob->type == OB_SURF || + ob->type == OB_CURVE); } -/* only used as menu */ -void OBJECT_OT_primitive_add(wmOperatorType *ot) +void OBJECT_OT_editmode_toggle(wmOperatorType *ot) { + /* identifiers */ - ot->name= "Add Primitive"; - ot->description = "Add a primitive object."; - ot->idname= "OBJECT_OT_primitive_add"; + ot->name= "Toggle Editmode"; + ot->description = "Toggle object's editmode."; + ot->idname= "OBJECT_OT_editmode_toggle"; /* api callbacks */ - ot->invoke= object_primitive_add_invoke; + ot->exec= editmode_toggle_exec; - ot->poll= ED_operator_scene_editable; + ot->poll= editmode_toggle_poll; /* flags */ - ot->flag= 0; + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; } +/* *************************** */ -/* ******************************* */ - -/* remove base from a specific scene */ -/* note: now unlinks constraints as well */ -void ED_base_object_free_and_unlink(Scene *scene, Base *base) -{ - BLI_remlink(&scene->base, base); - free_libblock_us(&G.main->object, base->object); - if(scene->basact==base) scene->basact= NULL; - MEM_freeN(base); -} - -static int object_delete_exec(bContext *C, wmOperator *op) +static int posemode_exec(bContext *C, wmOperator *op) { - Scene *scene= CTX_data_scene(C); - int islamp= 0; - - if(CTX_data_edit_object(C)) - return OPERATOR_CANCELLED; + Base *base= CTX_data_active_base(C); - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - - if(base->object->type==OB_LAMP) islamp= 1; + if(base->object->type==OB_ARMATURE) { + if(base->object==CTX_data_edit_object(C)) { + ED_object_exit_editmode(C, EM_FREEDATA); + ED_armature_enter_posemode(C, base); + } + else if(base->object->mode & OB_MODE_POSE) + ED_armature_exit_posemode(C, base); + else + ED_armature_enter_posemode(C, base); - /* remove from current scene only */ - ED_base_object_free_and_unlink(scene, base); + return OPERATOR_FINISHED; } - CTX_DATA_END; - - if(islamp) reshadeall_displist(scene); /* only frees displist */ - - DAG_scene_sort(scene); - ED_anim_dag_flush_update(C); - WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C)); - - return OPERATOR_FINISHED; + return OPERATOR_PASS_THROUGH; } -void OBJECT_OT_delete(wmOperatorType *ot) +void OBJECT_OT_posemode_toggle(wmOperatorType *ot) { - /* identifiers */ - ot->name= "Delete"; - ot->description = "Delete selected objects."; - ot->idname= "OBJECT_OT_delete"; + ot->name= "Toggle Pose Mode"; + ot->idname= "OBJECT_OT_posemode_toggle"; + ot->description= "Enables or disables posing/selecting bones"; /* api callbacks */ - ot->invoke= WM_operator_confirm; - ot->exec= object_delete_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + ot->exec= posemode_exec; + ot->poll= ED_operator_object_active; + /* flag */ + ot->flag= OPTYPE_REGISTER; } +/* *********************** */ -static void single_object_users__forwardModifierLinks(void *userData, Object *ob, Object **obpoin) +void check_editmode(int type) { - ID_NEW(*obpoin); -} + Object *obedit= NULL; // XXX + + if (obedit==NULL || obedit->type==type) return; -static void copy_object__forwardModifierLinks(void *userData, Object *ob, - ID **idpoin) -{ - /* this is copied from ID_NEW; it might be better to have a macro */ - if(*idpoin && (*idpoin)->newid) *idpoin = (*idpoin)->newid; +// XXX ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */ } +#if 0 +// XXX should be in view3d? -/* after copying objects, copied data should get new pointers */ -static void copy_object_set_idnew(Scene *scene, View3D *v3d, int dupflag) +/* context: ob = lamp */ +/* code should be replaced with proper (custom) transform handles for lamp properties */ +static void spot_interactive(Object *ob, int mode) { - Base *base; - Object *ob; - Material *ma, *mao; - ID *id; -#if 0 // XXX old animation system - Ipo *ipo; - bActionStrip *strip; -#endif // XXX old animation system - int a; - - /* XXX check object pointers */ - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB_BGMODE(v3d, base)) { - ob= base->object; - relink_constraints(&ob->constraints); - if (ob->pose){ - bPoseChannel *chan; - for (chan = ob->pose->chanbase.first; chan; chan=chan->next){ - relink_constraints(&chan->constraints); - } - } - modifiers_foreachIDLink(ob, copy_object__forwardModifierLinks, NULL); - ID_NEW(ob->parent); - ID_NEW(ob->track); - ID_NEW(ob->proxy); - ID_NEW(ob->proxy_group); - -#if 0 // XXX old animation system - for(strip= ob->nlastrips.first; strip; strip= strip->next) { - bActionModifier *amod; - for(amod= strip->modifiers.first; amod; amod= amod->next) - ID_NEW(amod->ob); - } -#endif // XXX old animation system - } - } - - /* materials */ - if( dupflag & USER_DUP_MAT) { - mao= G.main->mat.first; - while(mao) { - if(mao->id.newid) { - - ma= (Material *)mao->id.newid; - - if(dupflag & USER_DUP_TEX) { - for(a=0; amtex[a]) { - id= (ID *)ma->mtex[a]->tex; - if(id) { - ID_NEW_US(ma->mtex[a]->tex) - else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex); - id->us--; - } - } - } - } -#if 0 // XXX old animation system - id= (ID *)ma->ipo; - if(id) { - ID_NEW_US(ma->ipo) - else ma->ipo= copy_ipo(ma->ipo); - id->us--; - } -#endif // XXX old animation system - } - mao= mao->id.next; - } - } + Lamp *la= ob->data; + float transfac, dx, dy, ratio, origval; + int keep_running= 1, center2d[2]; + short mval[2], mvalo[2]; -#if 0 // XXX old animation system - /* lamps */ - if( dupflag & USER_DUP_IPO) { - Lamp *la= G.main->lamp.first; - while(la) { - if(la->id.newid) { - Lamp *lan= (Lamp *)la->id.newid; - id= (ID *)lan->ipo; - if(id) { - ID_NEW_US(lan->ipo) - else lan->ipo= copy_ipo(lan->ipo); - id->us--; - } - } - la= la->id.next; - } - } +// getmouseco_areawin(mval); +// getmouseco_areawin(mvalo); - /* ipos */ - ipo= G.main->ipo.first; - while(ipo) { - if(ipo->id.lib==NULL && ipo->id.newid) { - Ipo *ipon= (Ipo *)ipo->id.newid; - IpoCurve *icu; - for(icu= ipon->curve.first; icu; icu= icu->next) { - if(icu->driver) { - ID_NEW(icu->driver->ob); - } - } - } - ipo= ipo->id.next; + project_int(ob->obmat[3], center2d); + if( center2d[0] > 100000 ) { /* behind camera */ +// center2d[0]= curarea->winx/2; +// center2d[1]= curarea->winy/2; } -#endif // XXX old animation system - - set_sca_new_poins(); - - clear_id_newpoins(); - -} -static int return_editmesh_indexar(EditMesh *em, int *tot, int **indexar, float *cent) -{ - EditVert *eve; - int *index, nr, totvert=0; - - for(eve= em->verts.first; eve; eve= eve->next) { - if(eve->f & SELECT) totvert++; - } - if(totvert==0) return 0; - - *indexar= index= MEM_mallocN(4*totvert, "hook indexar"); - *tot= totvert; - nr= 0; - cent[0]= cent[1]= cent[2]= 0.0; +// helpline(mval, center2d); - for(eve= em->verts.first; eve; eve= eve->next) { - if(eve->f & SELECT) { - *index= nr; index++; - VecAddf(cent, cent, eve->co); - } - nr++; - } + /* ratio is like scaling */ + dx = (float)(center2d[0] - mval[0]); + dy = (float)(center2d[1] - mval[1]); + transfac = (float)sqrt( dx*dx + dy*dy); + if(transfac==0.0f) transfac= 1.0f; - VecMulf(cent, 1.0f/(float)totvert); - - return totvert; -} - -static int return_editmesh_vgroup(Object *obedit, EditMesh *em, char *name, float *cent) -{ - MDeformVert *dvert; - EditVert *eve; - int i, totvert=0; - - cent[0]= cent[1]= cent[2]= 0.0; + if(mode==1) + origval= la->spotsize; + else if(mode==2) + origval= la->dist; + else if(mode==3) + origval= la->clipsta; + else + origval= la->clipend; - if(obedit->actdef) { + while (keep_running>0) { - /* find the vertices */ - for(eve= em->verts.first; eve; eve= eve->next) { - dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT); - - if(dvert) { - for(i=0; itotweight; i++){ - if(dvert->dw[i].def_nr == (obedit->actdef-1)) { - totvert++; - VecAddf(cent, cent, eve->co); - } - } - } - } - if(totvert) { - bDeformGroup *defGroup = BLI_findlink(&obedit->defbase, obedit->actdef-1); - strcpy(name, defGroup->name); - VecMulf(cent, 1.0f/(float)totvert); - return 1; +// getmouseco_areawin(mval); + + /* essential for idling subloop */ + if(mval[0]==mvalo[0] && mval[1]==mvalo[1]) { + PIL_sleep_ms(2); } - } - - return 0; -} + else { + char str[32]; + + dx = (float)(center2d[0] - mval[0]); + dy = (float)(center2d[1] - mval[1]); + ratio = (float)(sqrt( dx*dx + dy*dy))/transfac; + + /* do the trick */ + + if(mode==1) { /* spot */ + la->spotsize = ratio*origval; + CLAMP(la->spotsize, 1.0f, 180.0f); + sprintf(str, "Spot size %.2f\n", la->spotsize); + } + else if(mode==2) { /* dist */ + la->dist = ratio*origval; + CLAMP(la->dist, 0.01f, 5000.0f); + sprintf(str, "Distance %.2f\n", la->dist); + } + else if(mode==3) { /* sta */ + la->clipsta = ratio*origval; + CLAMP(la->clipsta, 0.001f, 5000.0f); + sprintf(str, "Distance %.2f\n", la->clipsta); + } + else if(mode==4) { /* end */ + la->clipend = ratio*origval; + CLAMP(la->clipend, 0.1f, 5000.0f); + sprintf(str, "Clip End %.2f\n", la->clipend); + } -static void select_editmesh_hook(Object *ob, HookModifierData *hmd) -{ - Mesh *me= ob->data; - EditMesh *em= BKE_mesh_get_editmesh(me); - EditVert *eve; - int index=0, nr=0; - - if (hmd->indexar == NULL) - return; - - for(eve= em->verts.first; eve; eve= eve->next, nr++) { - if(nr==hmd->indexar[index]) { - eve->f |= SELECT; - if(index < hmd->totindex-1) index++; - } - } - EM_select_flush(em); + /* cleanup */ + mvalo[0]= mval[0]; + mvalo[1]= mval[1]; + + /* handle shaded mode */ +// XXX shade_buttons_change_3d(); - BKE_mesh_end_editmesh(me, em); -} + /* DRAW */ + headerprint(str); + force_draw_plus(SPACE_BUTS, 0); -static int return_editlattice_indexar(Lattice *editlatt, int *tot, int **indexar, float *cent) -{ - BPoint *bp; - int *index, nr, totvert=0, a; - - /* count */ - a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw; - bp= editlatt->def; - while(a--) { - if(bp->f1 & SELECT) { - if(bp->hide==0) totvert++; +// helpline(mval, center2d); } - bp++; - } - - if(totvert==0) return 0; - - *indexar= index= MEM_mallocN(4*totvert, "hook indexar"); - *tot= totvert; - nr= 0; - cent[0]= cent[1]= cent[2]= 0.0; - - a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw; - bp= editlatt->def; - while(a--) { - if(bp->f1 & SELECT) { - if(bp->hide==0) { - *index= nr; index++; - VecAddf(cent, cent, bp->vec); + + while( qtest() ) { + short val; + unsigned short event= extern_qread(&val); + + switch (event){ + case ESCKEY: + case RIGHTMOUSE: + keep_running= 0; + break; + case LEFTMOUSE: + case SPACEKEY: + case PADENTER: + case RETKEY: + if(val) + keep_running= -1; + break; } } - bp++; - nr++; } - - VecMulf(cent, 1.0f/(float)totvert); - - return totvert; -} -static void select_editlattice_hook(Object *obedit, HookModifierData *hmd) -{ - Lattice *lt= obedit->data; - BPoint *bp; - int index=0, nr=0, a; - - /* count */ - a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; - bp= lt->editlatt->def; - while(a--) { - if(hmd->indexar[index]==nr) { - bp->f1 |= SELECT; - if(index < hmd->totindex-1) index++; - } - nr++; - bp++; + if(keep_running==0) { + if(mode==1) + la->spotsize= origval; + else if(mode==2) + la->dist= origval; + else if(mode==3) + la->clipsta= origval; + else + la->clipend= origval; } + } +#endif -static int return_editcurve_indexar(Object *obedit, int *tot, int **indexar, float *cent) +void special_editmenu(Scene *scene, View3D *v3d) { - ListBase *editnurb= curve_get_editcurve(obedit); - Nurb *nu; - BPoint *bp; - BezTriple *bezt; - int *index, a, nr, totvert=0; - - for(nu= editnurb->first; nu; nu= nu->next) { - if(nu->type == CU_BEZIER) { - bezt= nu->bezt; - a= nu->pntsu; - while(a--) { - if(bezt->f1 & SELECT) totvert++; - if(bezt->f2 & SELECT) totvert++; - if(bezt->f3 & SELECT) totvert++; - bezt++; - } - } - else { - bp= nu->bp; - a= nu->pntsu*nu->pntsv; - while(a--) { - if(bp->f1 & SELECT) totvert++; - bp++; - } - } - } - if(totvert==0) return 0; +// XXX static short numcuts= 2; + Object *ob= OBACT; + Object *obedit= NULL; // XXX + int nr,ret=0; - *indexar= index= MEM_mallocN(4*totvert, "hook indexar"); - *tot= totvert; - nr= 0; - cent[0]= cent[1]= cent[2]= 0.0; + if(ob==NULL) return; - for(nu= editnurb->first; nu; nu= nu->next) { - if(nu->type == CU_BEZIER) { - bezt= nu->bezt; - a= nu->pntsu; - while(a--) { - if(bezt->f1 & SELECT) { - *index= nr; index++; - VecAddf(cent, cent, bezt->vec[0]); - } - nr++; - if(bezt->f2 & SELECT) { - *index= nr; index++; - VecAddf(cent, cent, bezt->vec[1]); - } - nr++; - if(bezt->f3 & SELECT) { - *index= nr; index++; - VecAddf(cent, cent, bezt->vec[2]); + if(obedit==NULL) { + + if(ob->mode & OB_MODE_POSE) { +// XXX pose_special_editmenu(); + } + else if(paint_facesel_test(ob)) { + Mesh *me= get_mesh(ob); + MTFace *tface; + MFace *mface; + int a; + + if(me==0 || me->mtface==0) return; + + nr= pupmenu("Specials%t|Set Tex%x1| Shared%x2| Light%x3| Invisible%x4| Collision%x5| TwoSide%x6|Clr Tex%x7| Shared%x8| Light%x9| Invisible%x10| Collision%x11| TwoSide%x12"); + + tface= me->mtface; + mface= me->mface; + for(a=me->totface; a>0; a--, tface++, mface++) { + if(mface->flag & ME_FACE_SEL) { + switch(nr) { + case 1: + tface->mode |= TF_TEX; break; + case 2: + tface->mode |= TF_SHAREDCOL; break; + case 3: + tface->mode |= TF_LIGHT; break; + case 4: + tface->mode |= TF_INVISIBLE; break; + case 5: + tface->mode |= TF_DYNAMIC; break; + case 6: + tface->mode |= TF_TWOSIDE; break; + case 7: + tface->mode &= ~TF_TEX; + tface->tpage= 0; + break; + case 8: + tface->mode &= ~TF_SHAREDCOL; break; + case 9: + tface->mode &= ~TF_LIGHT; break; + case 10: + tface->mode &= ~TF_INVISIBLE; break; + case 11: + tface->mode &= ~TF_DYNAMIC; break; + case 12: + tface->mode &= ~TF_TWOSIDE; break; + } } - nr++; - bezt++; } + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); } - else { - bp= nu->bp; - a= nu->pntsu*nu->pntsv; - while(a--) { - if(bp->f1 & SELECT) { - *index= nr; index++; - VecAddf(cent, cent, bp->vec); - } - nr++; - bp++; + else if(ob->mode & OB_MODE_VERTEX_PAINT) { + Mesh *me= get_mesh(ob); + + if(me==0 || (me->mcol==NULL && me->mtface==NULL) ) return; + + nr= pupmenu("Specials%t|Shared VertexCol%x1"); + if(nr==1) { + +// XXX do_shared_vertexcol(me); + + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); } } - } - - VecMulf(cent, 1.0f/(float)totvert); - - return totvert; -} - -void ED_object_apply_obmat(Object *ob) -{ - float mat[3][3], imat[3][3], tmat[3][3]; - - /* from obmat to loc rot size */ - - if(ob==NULL) return; - Mat3CpyMat4(mat, ob->obmat); - - VECCOPY(ob->loc, ob->obmat[3]); - - Mat3ToEul(mat, ob->rot); - EulToMat3(ob->rot, tmat); - - Mat3Inv(imat, tmat); - - Mat3MulMat3(tmat, imat, mat); - - ob->size[0]= tmat[0][0]; - ob->size[1]= tmat[1][1]; - ob->size[2]= tmat[2][2]; - -} + else if(ob->mode & OB_MODE_WEIGHT_PAINT) { + Object *par= modifiers_isDeformedByArmature(ob); -int object_hook_index_array(Object *obedit, int *tot, int **indexar, char *name, float *cent_r) -{ - *indexar= NULL; - *tot= 0; - name[0]= 0; - - switch(obedit->type) { - case OB_MESH: - { - Mesh *me= obedit->data; - EditMesh *em = BKE_mesh_get_editmesh(me); + if(par && (par->mode & OB_MODE_POSE)) { + nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2"); - /* check selected vertices first */ - if( return_editmesh_indexar(em, tot, indexar, cent_r)) { - BKE_mesh_end_editmesh(me, em); - return 1; - } else { - int ret = return_editmesh_vgroup(obedit, em, name, cent_r); - BKE_mesh_end_editmesh(me, em); - return ret; +// XXX if(nr==1 || nr==2) +// XXX pose_adds_vgroups(ob, (nr == 2)); } } - case OB_CURVE: - case OB_SURF: - return return_editcurve_indexar(obedit, tot, indexar, cent_r); - case OB_LATTICE: - { - Lattice *lt= obedit->data; - return return_editlattice_indexar(lt->editlatt, tot, indexar, cent_r); - } - default: - return 0; - } -} + else if(ob->mode & OB_MODE_PARTICLE_EDIT) { +#if 0 + // XXX + ParticleSystem *psys = PE_get_current(ob); + ParticleEditSettings *pset = PE_settings(); -static void select_editcurve_hook(Object *obedit, HookModifierData *hmd) -{ - ListBase *editnurb= curve_get_editcurve(obedit); - Nurb *nu; - BPoint *bp; - BezTriple *bezt; - int index=0, a, nr=0; - - for(nu= editnurb->first; nu; nu= nu->next) { - if(nu->type == CU_BEZIER) { - bezt= nu->bezt; - a= nu->pntsu; - while(a--) { - if(nr == hmd->indexar[index]) { - bezt->f1 |= SELECT; - if(indextotindex-1) index++; - } - nr++; - if(nr == hmd->indexar[index]) { - bezt->f2 |= SELECT; - if(indextotindex-1) index++; - } - nr++; - if(nr == hmd->indexar[index]) { - bezt->f3 |= SELECT; - if(indextotindex-1) index++; - } - nr++; - - bezt++; - } - } - else { - bp= nu->bp; - a= nu->pntsu*nu->pntsv; - while(a--) { - if(nr == hmd->indexar[index]) { - bp->f1 |= SELECT; - if(indextotindex-1) index++; - } - nr++; - bp++; - } - } - } -} - -void object_hook_select(Object *ob, HookModifierData *hmd) -{ - if(ob->type==OB_MESH) select_editmesh_hook(ob, hmd); - else if(ob->type==OB_LATTICE) select_editlattice_hook(ob, hmd); - else if(ob->type==OB_CURVE) select_editcurve_hook(ob, hmd); - else if(ob->type==OB_SURF) select_editcurve_hook(ob, hmd); -} - - -void add_hook(Scene *scene, View3D *v3d, int mode) -{ - ModifierData *md = NULL; - HookModifierData *hmd = NULL; - Object *ob=NULL; - Object *obedit= scene->obedit; // XXX get from context - - if(obedit==NULL) return; - - /* preconditions */ - if(mode==2) { /* selected object */ - Base *base; - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - if(base!=BASACT) { - ob= base->object; - break; - } - } - } - if(ob==NULL) { - error("Requires selected Object"); - return; - } - } - else if(mode!=1) { - int maxlen=0, a, nr; - char *cp; - - /* make pupmenu with hooks */ - for(md=obedit->modifiers.first; md; md= md->next) { - if (md->type==eModifierType_Hook) - maxlen+=32; - } - - if(maxlen==0) { - error("Object has no hooks yet"); - return; - } - - cp= MEM_callocN(maxlen+32, "temp string"); - if(mode==3) strcpy(cp, "Remove %t|"); - else if(mode==4) strcpy(cp, "Reassign %t|"); - else if(mode==5) strcpy(cp, "Select %t|"); - else if(mode==6) strcpy(cp, "Clear Offset %t|"); - - for(md=obedit->modifiers.first; md; md= md->next) { - if (md->type==eModifierType_Hook) { - strcat(cp, md->name); - strcat(cp, " |"); - } - } - - nr= pupmenu(cp); - MEM_freeN(cp); - - if(nr<1) return; - - a= 1; - for(md=obedit->modifiers.first; md; md=md->next) { - if (md->type==eModifierType_Hook) { - if(a==nr) break; - a++; - } - } - - hmd = (HookModifierData*) md; - ob= hmd->object; - } - - /* do it, new hooks or reassign */ - if(mode==1 || mode==2 || mode==4) { - float cent[3]; - int tot, ok, *indexar; - char name[32]; - - ok = object_hook_index_array(obedit, &tot, &indexar, name, cent); - - if(ok==0) { - error("Requires selected vertices or active Vertex Group"); - } - else { - - if(mode==1) { - Base *base= BASACT, *newbase; - - ob= add_object(scene, OB_EMPTY); - /* set layers OK */ - newbase= BASACT; - newbase->lay= base->lay; - ob->lay= newbase->lay; - - /* transform cent to global coords for loc */ - VecMat4MulVecfl(ob->loc, obedit->obmat, cent); - - /* restore, add_object sets active */ - BASACT= base; - } - /* if mode is 2 or 4, ob has been set */ - - /* new hook */ - if(mode==1 || mode==2) { - ModifierData *md = obedit->modifiers.first; - - while (md && modifierType_getInfo(md->type)->type==eModifierTypeType_OnlyDeform) { - md = md->next; - } - - hmd = (HookModifierData*) modifier_new(eModifierType_Hook); - BLI_insertlinkbefore(&obedit->modifiers, md, hmd); - sprintf(hmd->modifier.name, "Hook-%s", ob->id.name+2); - } - else if (hmd->indexar) MEM_freeN(hmd->indexar); /* reassign, hook was set */ - - hmd->object= ob; - hmd->indexar= indexar; - VECCOPY(hmd->cent, cent); - hmd->totindex= tot; - BLI_strncpy(hmd->name, name, 32); - - // TODO: need to take into account bone targets here too now... - if(mode==1 || mode==2) { - /* matrix calculus */ - /* vert x (obmat x hook->imat) x hook->obmat x ob->imat */ - /* (parentinv ) */ - - where_is_object(scene, ob); - - Mat4Invert(ob->imat, ob->obmat); - /* apparently this call goes from right to left... */ - Mat4MulSerie(hmd->parentinv, ob->imat, obedit->obmat, NULL, - NULL, NULL, NULL, NULL, NULL); - } - } - } - else if(mode==3) { /* remove */ - BLI_remlink(&obedit->modifiers, md); - modifier_free(md); - } - else if(mode==5) { /* select */ - // FIXME: this is now OBJECT_OT_hook_select - object_hook_select(obedit, hmd); - } - else if(mode==6) { /* clear offset */ - // FIXME: this is now OBJECT_OT_hook_reset operator - where_is_object(scene, ob); /* ob is hook->parent */ - - Mat4Invert(ob->imat, ob->obmat); - /* this call goes from right to left... */ - Mat4MulSerie(hmd->parentinv, ob->imat, obedit->obmat, NULL, - NULL, NULL, NULL, NULL, NULL); - } - - DAG_scene_sort(scene); -} - -void add_hook_menu(Scene *scene, View3D *v3d) -{ - Object *obedit= scene->obedit; // XXX get from context - int mode; - - if(obedit==NULL) return; - - if(modifiers_findByType(obedit, eModifierType_Hook)) - mode= pupmenu("Hooks %t|Add, To New Empty %x1|Add, To Selected Object %x2|Remove... %x3|Reassign... %x4|Select... %x5|Clear Offset...%x6"); - else - mode= pupmenu("Hooks %t|Add, New Empty %x1|Add, To Selected Object %x2"); - - if(mode<1) return; - - /* do operations */ - add_hook(scene, v3d, mode); -} - - - -/* use this when the loc/size/rot of the parent has changed but the children should stay in the same place - * apply-size-rot or object center for eg */ -static void ignore_parent_tx(Scene *scene, Object *ob ) -{ - Object workob; - Object *ob_child; - - /* a change was made, adjust the children to compensate */ - for (ob_child=G.main->object.first; ob_child; ob_child=ob_child->id.next) { - if (ob_child->parent == ob) { - ED_object_apply_obmat(ob_child); - what_does_parent(scene, ob_child, &workob); - Mat4Invert(ob_child->parentinv, workob.obmat); - } - } -} - -/* ******************** clear parent operator ******************* */ - -static EnumPropertyItem prop_clear_parent_types[] = { - {0, "CLEAR", 0, "Clear Parent", ""}, - {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""}, - {2, "CLEAR_INVERSE", 0, "Clear Parent Inverse", ""}, - {0, NULL, 0, NULL, NULL} -}; - -/* note, poll should check for editable scene */ -static int parent_clear_exec(bContext *C, wmOperator *op) -{ - int type= RNA_enum_get(op->ptr, "type"); - - CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { - - if(type == 0) { - ob->parent= NULL; - } - else if(type == 1) { - ob->parent= NULL; - ob->track= NULL; - ED_object_apply_obmat(ob); - } - else if(type == 2) - Mat4One(ob->parentinv); - - ob->recalc |= OB_RECALC; - } - CTX_DATA_END; - - DAG_scene_sort(CTX_data_scene(C)); - ED_anim_dag_flush_update(C); - WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_parent_clear(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Clear Parent"; - ot->description = "Clear the object's parenting."; - ot->idname= "OBJECT_OT_parent_clear"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= parent_clear_exec; - - ot->poll= ED_operator_object_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", ""); -} - -/* ******************** clear track operator ******************* */ - - -static EnumPropertyItem prop_clear_track_types[] = { - {0, "CLEAR", 0, "Clear Track", ""}, - {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""}, - {0, NULL, 0, NULL, NULL} -}; - -/* note, poll should check for editable scene */ -static int object_track_clear_exec(bContext *C, wmOperator *op) -{ - int type= RNA_enum_get(op->ptr, "type"); - - if(CTX_data_edit_object(C)) { - BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode"); - return OPERATOR_CANCELLED; - } - CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { - ob->track= NULL; - ob->recalc |= OB_RECALC; - - if(type == 1) - ED_object_apply_obmat(ob); - } - CTX_DATA_END; - - DAG_scene_sort(CTX_data_scene(C)); - ED_anim_dag_flush_update(C); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_track_clear(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Clear track"; - ot->description = "Clear tracking constraint or flag from object."; - ot->idname= "OBJECT_OT_track_clear"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_track_clear_exec; - - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", ""); -} - -/* *****************Selection Operators******************* */ -static EnumPropertyItem prop_select_types[] = { - {0, "EXCLUSIVE", 0, "Exclusive", ""}, - {1, "EXTEND", 0, "Extend", ""}, - {0, NULL, 0, NULL, NULL} -}; - -/* ****** Select by Type ****** */ - -static int object_select_by_type_exec(bContext *C, wmOperator *op) -{ - short obtype, seltype; - - obtype = RNA_enum_get(op->ptr, "type"); - seltype = RNA_enum_get(op->ptr, "seltype"); - - if (seltype == 0) { - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - ED_base_object_select(base, BA_DESELECT); - } - CTX_DATA_END; - } - - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - if(base->object->type==obtype) { - ED_base_object_select(base, BA_SELECT); - } - } - CTX_DATA_END; - - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_select_by_type(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Select By Type"; - ot->description = "Select all visible objects that are of a type."; - ot->idname= "OBJECT_OT_select_by_type"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_select_by_type_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "seltype", prop_select_types, 0, "Selection", "Extend selection or clear selection then select"); - RNA_def_enum(ot->srna, "type", prop_object_types, 1, "Type", ""); - -} -/* ****** selection by links *******/ - -static EnumPropertyItem prop_select_linked_types[] = { - {1, "IPO", 0, "Object IPO", ""}, // XXX depreceated animation system stuff... - {2, "OBDATA", 0, "Ob Data", ""}, - {3, "MATERIAL", 0, "Material", ""}, - {4, "TEXTURE", 0, "Texture", ""}, - {5, "DUPGROUP", 0, "Dupligroup", ""}, - {6, "PARTICLE", 0, "Particle System", ""}, - {0, NULL, 0, NULL, NULL} -}; - -static int object_select_linked_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - Object *ob; - void *obdata = NULL; - Material *mat = NULL, *mat1; - Tex *tex=0; - int a, b; - int nr = RNA_enum_get(op->ptr, "type"); - short changed = 0, seltype; - /* events (nr): - * Object Ipo: 1 - * ObData: 2 - * Current Material: 3 - * Current Texture: 4 - * DupliGroup: 5 - * PSys: 6 - */ - - seltype = RNA_enum_get(op->ptr, "seltype"); - - if (seltype == 0) { - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - ED_base_object_select(base, BA_DESELECT); - } - CTX_DATA_END; - } - - ob= OBACT; - if(ob==0){ - BKE_report(op->reports, RPT_ERROR, "No Active Object"); - return OPERATOR_CANCELLED; - } - - if(nr==1) { - // XXX old animation system - //ipo= ob->ipo; - //if(ipo==0) return OPERATOR_CANCELLED; - return OPERATOR_CANCELLED; - } - else if(nr==2) { - if(ob->data==0) return OPERATOR_CANCELLED; - obdata= ob->data; - } - else if(nr==3 || nr==4) { - mat= give_current_material(ob, ob->actcol); - if(mat==0) return OPERATOR_CANCELLED; - if(nr==4) { - if(mat->mtex[ (int)mat->texact ]) tex= mat->mtex[ (int)mat->texact ]->tex; - if(tex==0) return OPERATOR_CANCELLED; - } - } - else if(nr==5) { - if(ob->dup_group==NULL) return OPERATOR_CANCELLED; - } - else if(nr==6) { - if(ob->particlesystem.first==NULL) return OPERATOR_CANCELLED; - } - else return OPERATOR_CANCELLED; - - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - if(nr==1) { - // XXX old animation system - //if(base->object->ipo==ipo) base->flag |= SELECT; - //changed = 1; - } - else if(nr==2) { - if(base->object->data==obdata) base->flag |= SELECT; - changed = 1; - } - else if(nr==3 || nr==4) { - ob= base->object; - - for(a=1; a<=ob->totcol; a++) { - mat1= give_current_material(ob, a); - if(nr==3) { - if(mat1==mat) base->flag |= SELECT; - changed = 1; - } - else if(mat1 && nr==4) { - for(b=0; bmtex[b]) { - if(tex==mat1->mtex[b]->tex) { - base->flag |= SELECT; - changed = 1; - break; - } - } - } - } - } - } - else if(nr==5) { - if(base->object->dup_group==ob->dup_group) { - base->flag |= SELECT; - changed = 1; - } - } - else if(nr==6) { - /* loop through other, then actives particles*/ - ParticleSystem *psys; - ParticleSystem *psys_act; - - for(psys=base->object->particlesystem.first; psys; psys=psys->next) { - for(psys_act=ob->particlesystem.first; psys_act; psys_act=psys_act->next) { - if (psys->part == psys_act->part) { - base->flag |= SELECT; - changed = 1; - break; - } - } - - if (base->flag & SELECT) { - break; - } - } - } - base->object->flag= base->flag; - } - CTX_DATA_END; - - if (changed) { - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); - return OPERATOR_FINISHED; - } - - return OPERATOR_CANCELLED; -} - -void OBJECT_OT_select_linked(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Select Linked"; - ot->description = "Select all visible objects that are linked."; - ot->idname= "OBJECT_OT_select_linked"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_select_linked_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_select_linked_types, 0, "Type", ""); - RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); - -} - -/* ****** selection grouped *******/ - -static EnumPropertyItem prop_select_grouped_types[] = { - {1, "CHILDREN_RECURSIVE", 0, "Children", ""}, // XXX depreceated animation system stuff... - {2, "CHILDREN", 0, "Immediate Children", ""}, - {3, "PARENT", 0, "Parent", ""}, - {4, "SIBLINGS", 0, "Siblings", "Shared Parent"}, - {5, "TYPE", 0, "Type", "Shared object type"}, - {6, "LAYER", 0, "Layer", "Shared layers"}, - {7, "GROUP", 0, "Group", "Shared group"}, - {8, "HOOK", 0, "Hook", ""}, - {9, "PASS", 0, "Pass", "Render pass Index"}, - {10, "COLOR", 0, "Color", "Object Color"}, - {11, "PROPERTIES", 0, "Properties", "Game Properties"}, - {0, NULL, 0, NULL, NULL} -}; - - -static short select_grouped_children(bContext *C, Object *ob, int recursive) -{ - short changed = 0; - - CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { - if (ob == base->object->parent) { - if (!(base->flag & SELECT)) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - - if (recursive) - changed |= select_grouped_children(C, base->object, 1); - } - } - CTX_DATA_END; - return changed; -} - -static short select_grouped_parent(bContext *C) /* Makes parent active and de-selected OBACT */ -{ - Scene *scene= CTX_data_scene(C); - View3D *v3d= CTX_wm_view3d(C); - - short changed = 0; - Base *baspar, *basact= CTX_data_active_base(C); - - if (!basact || !(basact->object->parent)) return 0; /* we know OBACT is valid */ - - baspar= object_in_scene(basact->object->parent, scene); - - /* can be NULL if parent in other scene */ - if(baspar && BASE_SELECTABLE(v3d, baspar)) { - ED_base_object_select(basact, BA_DESELECT); - ED_base_object_select(baspar, BA_SELECT); - ED_base_object_activate(C, baspar); - changed = 1; - } - return changed; -} - - -#define GROUP_MENU_MAX 24 -static short select_grouped_group(bContext *C, Object *ob) /* Select objects in the same group as the active */ -{ - short changed = 0; - Group *group, *ob_groups[GROUP_MENU_MAX]; - //char str[10 + (24*GROUP_MENU_MAX)]; - //char *p = str; - int group_count=0; //, menu, i; - - for ( group=G.main->group.first; - group && group_count < GROUP_MENU_MAX; - group=group->id.next - ) { - if (object_in_group (ob, group)) { - ob_groups[group_count] = group; - group_count++; - } - } - - if (!group_count) - return 0; - - else if (group_count == 1) { - group = ob_groups[0]; - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - if (!(base->flag & SELECT) && object_in_group(base->object, group)) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - } - CTX_DATA_END; - return changed; - } -#if 0 // XXX hows this work in 2.5? - /* build the menu. */ - p += sprintf(str, "Groups%%t"); - for (i=0; iid.name+2, i); - } - - menu = pupmenu (str); - if (menu == -1) - return 0; - - group = ob_groups[menu]; - for (base= FIRSTBASE; base; base= base->next) { - if (!(base->flag & SELECT) && object_in_group(base->object, group)) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - } -#endif - return changed; -} - -static short select_grouped_object_hooks(bContext *C, Object *ob) -{ - Scene *scene= CTX_data_scene(C); - View3D *v3d= CTX_wm_view3d(C); - - short changed = 0; - Base *base; - ModifierData *md; - HookModifierData *hmd; - - for (md = ob->modifiers.first; md; md=md->next) { - if (md->type==eModifierType_Hook) { - hmd= (HookModifierData*) md; - if (hmd->object && !(hmd->object->flag & SELECT)) { - base= object_in_scene(hmd->object, scene); - if (base && (BASE_SELECTABLE(v3d, base))) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - } - } - } - return changed; -} - -/* Select objects woth the same parent as the active (siblings), - * parent can be NULL also */ -static short select_grouped_siblings(bContext *C, Object *ob) -{ - short changed = 0; - - CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { - if ((base->object->parent==ob->parent) && !(base->flag & SELECT)) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - } - CTX_DATA_END; - return changed; -} - -static short select_grouped_type(bContext *C, Object *ob) -{ - short changed = 0; - - CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { - if ((base->object->type == ob->type) && !(base->flag & SELECT)) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - } - CTX_DATA_END; - return changed; -} - -static short select_grouped_layer(bContext *C, Object *ob) -{ - char changed = 0; - - CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { - if ((base->lay & ob->lay) && !(base->flag & SELECT)) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - } - CTX_DATA_END; - return changed; -} - -static short select_grouped_index_object(bContext *C, Object *ob) -{ - char changed = 0; - - CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { - if ((base->object->index == ob->index) && !(base->flag & SELECT)) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - } - CTX_DATA_END; - return changed; -} - -static short select_grouped_color(bContext *C, Object *ob) -{ - char changed = 0; - - CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { - if (!(base->flag & SELECT) && (FloatCompare(base->object->col, ob->col, 0.005f))) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - } - CTX_DATA_END; - return changed; -} - -static short objects_share_gameprop(Object *a, Object *b) -{ - bProperty *prop; - /*make a copy of all its properties*/ - - for( prop= a->prop.first; prop; prop = prop->next ) { - if ( get_ob_property(b, prop->name) ) - return 1; - } - return 0; -} - -static short select_grouped_gameprops(bContext *C, Object *ob) -{ - char changed = 0; - - CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { - if (!(base->flag & SELECT) && (objects_share_gameprop(base->object, ob))) { - ED_base_object_select(base, BA_SELECT); - changed = 1; - } - } - CTX_DATA_END; - return changed; -} - -static int object_select_grouped_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - Object *ob; - int nr = RNA_enum_get(op->ptr, "type"); - short changed = 0, seltype; - - seltype = RNA_enum_get(op->ptr, "seltype"); - - if (seltype == 0) { - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - ED_base_object_select(base, BA_DESELECT); - } - CTX_DATA_END; - } - - ob= OBACT; - if(ob==0){ - BKE_report(op->reports, RPT_ERROR, "No Active Object"); - return OPERATOR_CANCELLED; - } - - if(nr==1) changed = select_grouped_children(C, ob, 1); - else if(nr==2) changed = select_grouped_children(C, ob, 0); - else if(nr==3) changed = select_grouped_parent(C); - else if(nr==4) changed = select_grouped_siblings(C, ob); - else if(nr==5) changed = select_grouped_type(C, ob); - else if(nr==6) changed = select_grouped_layer(C, ob); - else if(nr==7) changed = select_grouped_group(C, ob); - else if(nr==8) changed = select_grouped_object_hooks(C, ob); - else if(nr==9) changed = select_grouped_index_object(C, ob); - else if(nr==10) changed = select_grouped_color(C, ob); - else if(nr==11) changed = select_grouped_gameprops(C, ob); - - if (changed) { - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); - return OPERATOR_FINISHED; - } - - return OPERATOR_CANCELLED; -} - -void OBJECT_OT_select_grouped(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Select Grouped"; - ot->description = "Select all visible objects grouped by various properties."; - ot->idname= "OBJECT_OT_select_grouped"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_select_grouped_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_select_grouped_types, 0, "Type", ""); - RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); - -} - -/* ****** selection by layer *******/ - -static int object_select_by_layer_exec(bContext *C, wmOperator *op) -{ - unsigned int layernum; - short seltype; - - seltype = RNA_enum_get(op->ptr, "seltype"); - layernum = RNA_int_get(op->ptr, "layer"); - - if (seltype == 0) { - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - ED_base_object_select(base, BA_DESELECT); - } - CTX_DATA_END; - } - - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - if(base->lay == (1<< (layernum -1))) - ED_base_object_select(base, BA_SELECT); - } - CTX_DATA_END; - - /* undo? */ - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_select_by_layer(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "select by layer"; - ot->description = "Select all visible objects on a layer."; - ot->idname= "OBJECT_OT_select_by_layer"; - - /* api callbacks */ - /*ot->invoke = XXX - need a int grid popup*/ - ot->exec= object_select_by_layer_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_int(ot->srna, "layer", 1, 1, 20, "Layer", "", 1, 20); - RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); -} - -/* ****** invert selection *******/ -static int object_select_inverse_exec(bContext *C, wmOperator *op) -{ - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - if (base->flag & SELECT) - ED_base_object_select(base, BA_DESELECT); - else - ED_base_object_select(base, BA_SELECT); - } - CTX_DATA_END; - - /* undo? */ - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_select_inverse(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Select Inverse"; - ot->description = "Invert selection of all visible objects."; - ot->idname= "OBJECT_OT_select_inverse"; - - /* api callbacks */ - ot->exec= object_select_inverse_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - -} -/* ****** (de)select All *******/ - -static int object_select_de_select_all_exec(bContext *C, wmOperator *op) -{ - - int a=0, ok=0; - - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - if (base->flag & SELECT) { - ok= a= 1; - break; - } - else ok=1; - } - CTX_DATA_END; - - if (!ok) return OPERATOR_PASS_THROUGH; - - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - if (a) ED_base_object_select(base, BA_DESELECT); - else ED_base_object_select(base, BA_SELECT); - } - CTX_DATA_END; - - /* undo? */ - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_select_all_toggle(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "deselect all"; - ot->description = "(de)select all visible objects in scene."; - ot->idname= "OBJECT_OT_select_all_toggle"; - - /* api callbacks */ - ot->exec= object_select_de_select_all_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - -} -/* ****** select mirror *******/ -/* finds the best possible flipped name. For renaming; check for unique names afterwards */ -/* if strip_number: removes number extensions */ -void object_flip_name (char *name) -{ - int len; - char prefix[128]={""}; /* The part before the facing */ - char suffix[128]={""}; /* The part after the facing */ - char replace[128]={""}; /* The replacement string */ - char number[128]={""}; /* The number extension string */ - char *index=NULL; - - len= strlen(name); - if(len<3) return; // we don't do names like .R or .L - - /* We first check the case with a .### extension, let's find the last period */ - if(isdigit(name[len-1])) { - index= strrchr(name, '.'); // last occurrance - if (index && isdigit(index[1]) ) { // doesnt handle case bone.1abc2 correct..., whatever! - strcpy(number, index); - *index= 0; - len= strlen(name); - } - } - - strcpy (prefix, name); - -#define IS_SEPARATOR(a) ((a)=='.' || (a)==' ' || (a)=='-' || (a)=='_') - - /* first case; separator . - _ with extensions r R l L */ - if( IS_SEPARATOR(name[len-2]) ) { - switch(name[len-1]) { - case 'l': - prefix[len-1]= 0; - strcpy(replace, "r"); - break; - case 'r': - prefix[len-1]= 0; - strcpy(replace, "l"); - break; - case 'L': - prefix[len-1]= 0; - strcpy(replace, "R"); - break; - case 'R': - prefix[len-1]= 0; - strcpy(replace, "L"); - break; - } - } - /* case; beginning with r R l L , with separator after it */ - else if( IS_SEPARATOR(name[1]) ) { - switch(name[0]) { - case 'l': - strcpy(replace, "r"); - strcpy(suffix, name+1); - prefix[0]= 0; - break; - case 'r': - strcpy(replace, "l"); - strcpy(suffix, name+1); - prefix[0]= 0; - break; - case 'L': - strcpy(replace, "R"); - strcpy(suffix, name+1); - prefix[0]= 0; - break; - case 'R': - strcpy(replace, "L"); - strcpy(suffix, name+1); - prefix[0]= 0; - break; - } - } - else if(len > 5) { - /* hrms, why test for a separator? lets do the rule 'ultimate left or right' */ - index = BLI_strcasestr(prefix, "right"); - if (index==prefix || index==prefix+len-5) { - if(index[0]=='r') - strcpy (replace, "left"); - else { - if(index[1]=='I') - strcpy (replace, "LEFT"); - else - strcpy (replace, "Left"); - } - *index= 0; - strcpy (suffix, index+5); - } - else { - index = BLI_strcasestr(prefix, "left"); - if (index==prefix || index==prefix+len-4) { - if(index[0]=='l') - strcpy (replace, "right"); - else { - if(index[1]=='E') - strcpy (replace, "RIGHT"); - else - strcpy (replace, "Right"); - } - *index= 0; - strcpy (suffix, index+4); - } - } - } - -#undef IS_SEPARATOR - - sprintf (name, "%s%s%s%s", prefix, replace, suffix, number); -} - -static int object_select_mirror_exec(bContext *C, wmOperator *op) -{ - char tmpname[32]; - short seltype; - - seltype = RNA_enum_get(op->ptr, "seltype"); - - CTX_DATA_BEGIN(C, Base*, primbase, selected_bases) { - - strcpy(tmpname, primbase->object->id.name+2); - object_flip_name(tmpname); - - CTX_DATA_BEGIN(C, Base*, secbase, visible_bases) { - if(!strcmp(secbase->object->id.name+2, tmpname)) { - ED_base_object_select(secbase, BA_SELECT); - } - } - CTX_DATA_END; - - if (seltype == 0) ED_base_object_select(primbase, BA_DESELECT); - - } - CTX_DATA_END; - - /* undo? */ - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_select_mirror(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Select Mirror"; - ot->description = "Select the Mirror objects of the selected object eg. L.sword -> R.sword"; - ot->idname= "OBJECT_OT_select_mirror"; - - /* api callbacks */ - ot->exec= object_select_mirror_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); -} -/* ****** random selection *******/ - -static int object_select_random_exec(bContext *C, wmOperator *op) -{ - float percent; - short seltype; - - seltype = RNA_enum_get(op->ptr, "seltype"); - - if (seltype == 0) { - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - ED_base_object_select(base, BA_DESELECT); - } - CTX_DATA_END; - } - percent = RNA_float_get(op->ptr, "percent"); - - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - if (BLI_frand() < percent) { - ED_base_object_select(base, BA_SELECT); - } - } - CTX_DATA_END; - - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_select_random(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Random select"; - ot->description = "Set select on random visible objects."; - ot->idname= "OBJECT_OT_select_random"; - - /* api callbacks */ - /*ot->invoke= object_select_random_invoke XXX - need a number popup ;*/ - ot->exec = object_select_random_exec; - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_float_percentage(ot->srna, "percent", 0.5f, 0.0f, 1.0f, "Percent", "percentage of objects to randomly select", 0.0001f, 1.0f); - RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); -} - -/* ******** Clear object Translation *********** */ - -static int object_location_clear_exec(bContext *C, wmOperator *op) -{ - int armature_clear= 0; - - CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { - if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) { - if ((ob->protectflag & OB_LOCK_LOCX)==0) - ob->loc[0]= ob->dloc[0]= 0.0f; - if ((ob->protectflag & OB_LOCK_LOCY)==0) - ob->loc[1]= ob->dloc[1]= 0.0f; - if ((ob->protectflag & OB_LOCK_LOCZ)==0) - ob->loc[2]= ob->dloc[2]= 0.0f; - } - ob->recalc |= OB_RECALC_OB; - } - CTX_DATA_END; - - if(armature_clear==0) /* in this case flush was done */ - ED_anim_dag_flush_update(C); - - WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); - - return OPERATOR_FINISHED; -} - - -void OBJECT_OT_location_clear(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Clear Location"; - ot->description = "Clear the object's location."; - ot->idname= "OBJECT_OT_location_clear"; - - /* api callbacks */ - ot->invoke= WM_operator_confirm; - ot->exec= object_location_clear_exec; - ot->poll= ED_operator_object_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int object_rotation_clear_exec(bContext *C, wmOperator *op) -{ - int armature_clear= 0; - - CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { - if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) { - /* eulers can only get cleared if they are not protected */ - if ((ob->protectflag & OB_LOCK_ROTX)==0) - ob->rot[0]= ob->drot[0]= 0.0f; - if ((ob->protectflag & OB_LOCK_ROTY)==0) - ob->rot[1]= ob->drot[1]= 0.0f; - if ((ob->protectflag & OB_LOCK_ROTZ)==0) - ob->rot[2]= ob->drot[2]= 0.0f; - } - ob->recalc |= OB_RECALC_OB; - } - CTX_DATA_END; - - if(armature_clear==0) /* in this case flush was done */ - ED_anim_dag_flush_update(C); - - WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); - - return OPERATOR_FINISHED; -} - - -void OBJECT_OT_rotation_clear(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Clear Rotation"; - ot->description = "Clear the object's rotation."; - ot->idname= "OBJECT_OT_rotation_clear"; - - /* api callbacks */ - ot->invoke= WM_operator_confirm; - ot->exec= object_rotation_clear_exec; - ot->poll= ED_operator_object_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int object_scale_clear_exec(bContext *C, wmOperator *op) -{ - int armature_clear= 0; - - CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { - if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) { - if ((ob->protectflag & OB_LOCK_SCALEX)==0) { - ob->dsize[0]= 0.0f; - ob->size[0]= 1.0f; - } - if ((ob->protectflag & OB_LOCK_SCALEY)==0) { - ob->dsize[1]= 0.0f; - ob->size[1]= 1.0f; - } - if ((ob->protectflag & OB_LOCK_SCALEZ)==0) { - ob->dsize[2]= 0.0f; - ob->size[2]= 1.0f; - } - } - ob->recalc |= OB_RECALC_OB; - } - CTX_DATA_END; - - if(armature_clear==0) /* in this case flush was done */ - ED_anim_dag_flush_update(C); - - WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_scale_clear(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Clear Scale"; - ot->description = "Clear the object's scale."; - ot->idname= "OBJECT_OT_scale_clear"; - - /* api callbacks */ - ot->invoke= WM_operator_confirm; - ot->exec= object_scale_clear_exec; - ot->poll= ED_operator_object_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int object_origin_clear_exec(bContext *C, wmOperator *op) -{ - float *v1, *v3, mat[3][3]; - int armature_clear= 0; - - CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { - if(ob->parent) { - v1= ob->loc; - v3= ob->parentinv[3]; - - Mat3CpyMat4(mat, ob->parentinv); - VECCOPY(v3, v1); - v3[0]= -v3[0]; - v3[1]= -v3[1]; - v3[2]= -v3[2]; - Mat3MulVecfl(mat, v3); - } - ob->recalc |= OB_RECALC_OB; - } - CTX_DATA_END; - - if(armature_clear==0) /* in this case flush was done */ - ED_anim_dag_flush_update(C); - - WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_origin_clear(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Clear Origin"; - ot->description = "Clear the object's origin."; - ot->idname= "OBJECT_OT_origin_clear"; - - /* api callbacks */ - ot->invoke= WM_operator_confirm; - ot->exec= object_origin_clear_exec; - ot->poll= ED_operator_object_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/* ********* clear/set restrict view *********/ -static int object_restrictview_clear_exec(bContext *C, wmOperator *op) -{ - ScrArea *sa= CTX_wm_area(C); - View3D *v3d= sa->spacedata.first; - Scene *scene= CTX_data_scene(C); - Base *base; - int changed = 0; - - /* XXX need a context loop to handle such cases */ - for(base = FIRSTBASE; base; base=base->next){ - if((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) { - base->flag |= SELECT; - base->object->flag = base->flag; - base->object->restrictflag &= ~OB_RESTRICT_VIEW; - changed = 1; - } - } - if (changed) { - DAG_scene_sort(scene); - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene); - } - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_restrictview_clear(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Clear Restrict View"; - ot->description = "Reveal the object by setting the restrictview flag."; - ot->idname= "OBJECT_OT_restrictview_clear"; - - /* api callbacks */ - ot->exec= object_restrictview_clear_exec; - ot->poll= ED_operator_view3d_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int object_restrictview_set_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - short changed = 0; - int unselected= RNA_boolean_get(op->ptr, "unselected"); - - CTX_DATA_BEGIN(C, Base*, base, visible_bases) { - if(!unselected) { - if (base->flag & SELECT){ - base->flag &= ~SELECT; - base->object->flag = base->flag; - base->object->restrictflag |= OB_RESTRICT_VIEW; - changed = 1; - if (base==BASACT) { - ED_base_object_activate(C, NULL); - } - } - } - else { - if (!(base->flag & SELECT)){ - base->object->restrictflag |= OB_RESTRICT_VIEW; - changed = 1; - } - } - } - CTX_DATA_END; - - if (changed) { - DAG_scene_sort(scene); - - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); - - } - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_restrictview_set(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Set Restrict View"; - ot->description = "Hide the object by setting the restrictview flag."; - ot->idname= "OBJECT_OT_restrictview_set"; - - /* api callbacks */ - ot->exec= object_restrictview_set_exec; - ot->poll= ED_operator_view3d_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects."); - -} -/* ************* Slow Parent ******************* */ -static int object_slowparent_set_exec(bContext *C, wmOperator *op) -{ - - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - - if(base->object->parent) base->object->partype |= PARSLOW; - base->object->recalc |= OB_RECALC_OB; - - } - CTX_DATA_END; - - ED_anim_dag_flush_update(C); - - WM_event_add_notifier(C, NC_SCENE, CTX_data_scene(C)); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_slowparent_set(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Set Slow Parent"; - ot->description = "Set the object's slow parent."; - ot->idname= "OBJECT_OT_slow_parent_set"; - - /* api callbacks */ - ot->invoke= WM_operator_confirm; - ot->exec= object_slowparent_set_exec; - ot->poll= ED_operator_view3d_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -static int object_slowparent_clear_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - if(base->object->parent) { - if(base->object->partype & PARSLOW) { - base->object->partype -= PARSLOW; - where_is_object(scene, base->object); - base->object->partype |= PARSLOW; - base->object->recalc |= OB_RECALC_OB; - } - } - - } - CTX_DATA_END; - - ED_anim_dag_flush_update(C); - - WM_event_add_notifier(C, NC_SCENE, CTX_data_scene(C)); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_slowparent_clear(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Clear Slow Parent"; - ot->description = "Clear the object's slow parent."; - ot->idname= "OBJECT_OT_slow_parent_clear"; - - /* api callbacks */ - ot->invoke= WM_operator_confirm; - ot->exec= object_slowparent_clear_exec; - ot->poll= ED_operator_view3d_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} -/* ******************** **************** */ - -/* only in edit mode */ -void make_vertex_parent(Scene *scene, Object *obedit, View3D *v3d) -{ - EditVert *eve; - Base *base; - Curve *cu= obedit->data; - Nurb *nu; - BezTriple *bezt; - BPoint *bp; - Object *par, *ob; - int a, v1=0, v2=0, v3=0, v4=0, nr=1; - - /* we need 1 to 3 selected vertices */ - - if(obedit->type==OB_MESH) { - Mesh *me= obedit->data; - EditMesh *em = BKE_mesh_get_editmesh(me); - - eve= em->verts.first; - while(eve) { - if(eve->f & 1) { - if(v1==0) v1= nr; - else if(v2==0) v2= nr; - else if(v3==0) v3= nr; - else if(v4==0) v4= nr; - else break; - } - nr++; - eve= eve->next; - } - - BKE_mesh_end_editmesh(me, em); - } - else if(ELEM(obedit->type, OB_SURF, OB_CURVE)) { - ListBase *editnurb= curve_get_editcurve(obedit); - - nu= editnurb->first; - while(nu) { - if(nu->type == CU_BEZIER) { - bezt= nu->bezt; - a= nu->pntsu; - while(a--) { - if(BEZSELECTED_HIDDENHANDLES(cu, bezt)) { - if(v1==0) v1= nr; - else if(v2==0) v2= nr; - else if(v3==0) v3= nr; - else if(v4==0) v4= nr; - else break; - } - nr++; - bezt++; - } - } - else { - bp= nu->bp; - a= nu->pntsu*nu->pntsv; - while(a--) { - if(bp->f1 & SELECT) { - if(v1==0) v1= nr; - else if(v2==0) v2= nr; - else if(v3==0) v3= nr; - else if(v4==0) v4= nr; - else break; - } - nr++; - bp++; - } - } - nu= nu->next; - } - } - else if(obedit->type==OB_LATTICE) { - Lattice *lt= obedit->data; - - a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; - bp= lt->editlatt->def; - while(a--) { - if(bp->f1 & SELECT) { - if(v1==0) v1= nr; - else if(v2==0) v2= nr; - else if(v3==0) v3= nr; - else if(v4==0) v4= nr; - else break; - } - nr++; - bp++; - } - } - - if(v4 || !((v1 && v2==0 && v3==0) || (v1 && v2 && v3)) ) { - error("Select either 1 or 3 vertices to parent to"); - return; - } - - if(okee("Make vertex parent")==0) return; - - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - if(base!=BASACT) { - - ob= base->object; - ob->recalc |= OB_RECALC; - par= BASACT->object->parent; - - while(par) { - if(par==ob) break; - par= par->parent; - } - if(par) { - error("Loop in parents"); - } - else { - Object workob; - - ob->parent= BASACT->object; - if(v3) { - ob->partype= PARVERT3; - ob->par1= v1-1; - ob->par2= v2-1; - ob->par3= v3-1; - - /* inverse parent matrix */ - what_does_parent(scene, ob, &workob); - Mat4Invert(ob->parentinv, workob.obmat); - } - else { - ob->partype= PARVERT1; - ob->par1= v1-1; - - /* inverse parent matrix */ - what_does_parent(scene, ob, &workob); - Mat4Invert(ob->parentinv, workob.obmat); - } - } - } - } - } - - DAG_scene_sort(scene); -} - - -/* ******************** make proxy operator *********************** */ - -/* present menu listing the possible objects within the group to proxify */ -static void proxy_group_objects_menu (bContext *C, wmOperator *op, Object *ob, Group *group) -{ - uiPopupMenu *pup; - uiLayout *layout; - GroupObject *go; - int len=0; - - /* check if there are any objects within the group to assign for */ - for (go= group->gobject.first; go; go= go->next) { - if (go->ob) len++; - } - if (len==0) return; - - /* now create the menu to draw */ - pup= uiPupMenuBegin(C, "Make Proxy For:", 0); - layout= uiPupMenuLayout(pup); - - for (go= group->gobject.first; go; go= go->next) { - if (go->ob) { - PointerRNA props_ptr; - - /* create operator menu item with relevant properties filled in */ - props_ptr= uiItemFullO(layout, go->ob->id.name+2, 0, op->idname, NULL, WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS); - RNA_string_set(&props_ptr, "object", go->ob->id.name+2); - RNA_string_set(&props_ptr, "group_object", go->ob->id.name+2); - } - } - - /* display the menu, and be done */ - uiPupMenuEnd(C, pup); -} - -/* set the object to proxify */ -static int make_proxy_invoke (bContext *C, wmOperator *op, wmEvent *evt) -{ - Scene *scene= CTX_data_scene(C); - Object *ob= CTX_data_active_object(C); - - /* sanity checks */ - if (!scene || scene->id.lib || !ob) - return OPERATOR_CANCELLED; - - /* Get object to work on - use a menu if we need to... */ - if (ob->dup_group && ob->dup_group->id.lib) { - /* gives menu with list of objects in group */ - proxy_group_objects_menu(C, op, ob, ob->dup_group); - } - else if (ob->id.lib) { - uiPopupMenu *pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION); - uiLayout *layout= uiPupMenuLayout(pup); - PointerRNA props_ptr; - - /* create operator menu item with relevant properties filled in */ - props_ptr= uiItemFullO(layout, op->type->name, 0, op->idname, NULL, WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS); - RNA_string_set(&props_ptr, "object", ob->id.name+2); - - /* present the menu and be done... */ - uiPupMenuEnd(C, pup); - } - else { - /* error.. cannot continue */ - BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group"); - } - - /* this invoke just calls another instance of this operator... */ - return OPERATOR_CANCELLED; -} - -static int make_proxy_exec (bContext *C, wmOperator *op) -{ - Object *ob=NULL, *gob=NULL; - Scene *scene= CTX_data_scene(C); - char ob_name[21], gob_name[21]; - - /* get object and group object - * - firstly names - * - then pointers from context - */ - RNA_string_get(op->ptr, "object", ob_name); - RNA_string_get(op->ptr, "group_object", gob_name); - - if (gob_name[0]) { - Group *group; - GroupObject *go; - - /* active object is group object... */ - // FIXME: we should get the nominated name instead - gob= CTX_data_active_object(C); - group= gob->dup_group; - - /* find the object to affect */ - for (go= group->gobject.first; go; go= go->next) { - if ((go->ob) && strcmp(go->ob->id.name+2, gob_name)==0) { - ob= go->ob; - break; - } - } - } - else { - /* just use the active object for now */ - // FIXME: we should get the nominated name instead - ob= CTX_data_active_object(C); - } - - if (ob) { - Object *newob; - Base *newbase, *oldbase= BASACT; - char name[32]; - - /* Add new object for the proxy */ - newob= add_object(scene, OB_EMPTY); - if (gob) - strcpy(name, gob->id.name+2); - else - strcpy(name, ob->id.name+2); - strcat(name, "_proxy"); - rename_id(&newob->id, name); - - /* set layers OK */ - newbase= BASACT; /* add_object sets active... */ - newbase->lay= oldbase->lay; - newob->lay= newbase->lay; - - /* remove base, leave user count of object, it gets linked in object_make_proxy */ - if (gob==NULL) { - BLI_remlink(&scene->base, oldbase); - MEM_freeN(oldbase); - } - - object_make_proxy(newob, ob, gob); - - /* depsgraph flushes are needed for the new data */ - DAG_scene_sort(scene); - DAG_id_flush_update(&newob->id, OB_RECALC); - - WM_event_add_notifier(C, NC_OBJECT, NULL); - } - else { - BKE_report(op->reports, RPT_ERROR, "No object to make proxy for"); - return OPERATOR_CANCELLED; - } - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_proxy_make (wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Make Proxy"; - ot->idname= "OBJECT_OT_proxy_make"; - ot->description= "Add empty object to become local replacement data of a library-linked object"; - - /* callbacks */ - ot->invoke= make_proxy_invoke; - ot->exec= make_proxy_exec; - ot->poll= ED_operator_object_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - /* properties */ - RNA_def_string(ot->srna, "object", "", 19, "Proxy Object", "Name of lib-linked/grouped object to make a proxy for."); - RNA_def_string(ot->srna, "group_object", "", 19, "Group Object", "Name of group instancer (if applicable)."); -} - -/* ******************** make parent operator *********************** */ - -#define PAR_OBJECT 0 -#define PAR_ARMATURE 1 -#define PAR_ARMATURE_NAME 2 -#define PAR_ARMATURE_ENVELOPE 3 -#define PAR_ARMATURE_AUTO 4 -#define PAR_BONE 5 -#define PAR_CURVE 6 -#define PAR_FOLLOW 7 -#define PAR_PATH_CONST 8 -#define PAR_LATTICE 9 -#define PAR_VERTEX 10 -#define PAR_TRIA 11 - -static EnumPropertyItem prop_make_parent_types[] = { - {PAR_OBJECT, "OBJECT", 0, "Object", ""}, - {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""}, - {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, " With Empty Groups", ""}, - {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, " With Automatic Weights", ""}, - {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, " With Envelope Weights", ""}, - {PAR_BONE, "BONE", 0, "Bone", ""}, - {PAR_CURVE, "CURVE", 0, "Curve Deform", ""}, - {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""}, - {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""}, - {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""}, - {PAR_VERTEX, "VERTEX", 0, "Vertex", ""}, - {PAR_TRIA, "TRIA", 0, "Triangle", ""}, - {0, NULL, 0, NULL, NULL} -}; - -static int test_parent_loop(Object *par, Object *ob) -{ - /* test if 'ob' is a parent somewhere in par's parents */ - - if(par == NULL) return 0; - if(ob == par) return 1; - - return test_parent_loop(par->parent, ob); -} - -void ED_object_parent(Object *ob, Object *par, int type, const char *substr) -{ - if(!par || test_parent_loop(par, ob)) { - ob->parent= NULL; - ob->partype= PAROBJECT; - ob->parsubstr[0]= 0; - return; - } - - /* this could use some more checks */ - - ob->parent= par; - ob->partype &= ~PARTYPE; - ob->partype |= type; - BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr)); -} - -static int parent_set_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - Object *par= CTX_data_active_object(C); - bPoseChannel *pchan= NULL; - int partype= RNA_enum_get(op->ptr, "type"); - int pararm= ELEM4(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO); - - par->recalc |= OB_RECALC_OB; - - /* preconditions */ - if(partype==PAR_FOLLOW || partype==PAR_PATH_CONST) { - if(par->type!=OB_CURVE) - return OPERATOR_CANCELLED; - else { - Curve *cu= par->data; - - if((cu->flag & CU_PATH)==0) { - cu->flag |= CU_PATH|CU_FOLLOW; - makeDispListCurveTypes(scene, par, 0); /* force creation of path data */ - } - else cu->flag |= CU_FOLLOW; - - /* fall back on regular parenting now */ - partype= PAR_OBJECT; - } - } - else if(partype==PAR_BONE) { - pchan= get_active_posechannel(par); - - if(pchan==NULL) { - error("No active Bone"); - return OPERATOR_CANCELLED; - } - } - - /* context itterator */ - CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { - - if(ob!=par) { - - if( test_parent_loop(par, ob) ) { - error("Loop in parents"); - } - else { - Object workob; - - /* apply transformation of previous parenting */ - ED_object_apply_obmat(ob); - - ob->parent= par; - - /* handle types */ - if (pchan) - strcpy (ob->parsubstr, pchan->name); - else - ob->parsubstr[0]= 0; - - /* constraint */ - if(partype==PAR_PATH_CONST) { - bConstraint *con; - bFollowPathConstraint *data; - float cmat[4][4], vec[3]; - - con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH); - strcpy (con->name, "AutoPath"); - - data = con->data; - data->tar = par; - - add_constraint_to_object(con, ob); - - get_constraint_target_matrix(con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra - give_timeoffset(ob)); - VecSubf(vec, ob->obmat[3], cmat[3]); - - ob->loc[0] = vec[0]; - ob->loc[1] = vec[1]; - } - else if(pararm && ob->type==OB_MESH && par->type == OB_ARMATURE) { - if(partype == PAR_ARMATURE_NAME) - create_vgroups_from_armature(scene, ob, par, ARM_GROUPS_NAME); - else if(partype == PAR_ARMATURE_ENVELOPE) - create_vgroups_from_armature(scene, ob, par, ARM_GROUPS_ENVELOPE); - else if(partype == PAR_ARMATURE_AUTO) - create_vgroups_from_armature(scene, ob, par, ARM_GROUPS_AUTO); - - /* get corrected inverse */ - ob->partype= PAROBJECT; - what_does_parent(scene, ob, &workob); - - ob->partype= PARSKEL; - - Mat4Invert(ob->parentinv, workob.obmat); - } - else { - /* calculate inverse parent matrix */ - what_does_parent(scene, ob, &workob); - Mat4Invert(ob->parentinv, workob.obmat); - } - - ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA; - - if( ELEM(partype, PAR_CURVE, PAR_LATTICE) || pararm ) - ob->partype= PARSKEL; /* note, dna define, not operator property */ - else - ob->partype= PAROBJECT; /* note, dna define, not operator property */ - } - } - } - CTX_DATA_END; - - DAG_scene_sort(CTX_data_scene(C)); - ED_anim_dag_flush_update(C); - WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); - - return OPERATOR_FINISHED; -} - -static int parent_set_invoke(bContext *C, wmOperator *op, wmEvent *event) -{ - Object *ob= CTX_data_active_object(C); - uiPopupMenu *pup= uiPupMenuBegin(C, "Set Parent To", 0); - uiLayout *layout= uiPupMenuLayout(pup); - - uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT); - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_OBJECT); - - /* ob becomes parent, make the associated menus */ - if(ob->type==OB_ARMATURE) { - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE); - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE_NAME); - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE_ENVELOPE); - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE_AUTO); - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_BONE); - } - else if(ob->type==OB_CURVE) { - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_CURVE); - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_FOLLOW); - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_PATH_CONST); - } - else if(ob->type == OB_LATTICE) { - uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_LATTICE); - } - - uiPupMenuEnd(C, pup); - - return OPERATOR_CANCELLED; -} - - -void OBJECT_OT_parent_set(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Make Parent"; - ot->description = "Set the object's parenting."; - ot->idname= "OBJECT_OT_parent_set"; - - /* api callbacks */ - ot->invoke= parent_set_invoke; - ot->exec= parent_set_exec; - - ot->poll= ED_operator_object_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", ""); -} - -/* *** make track ***** */ -static EnumPropertyItem prop_make_track_types[] = { - {1, "TRACKTO", 0, "TrackTo Constraint", ""}, - {2, "LOCKTRACK", 0, "LockTrack Constraint", ""}, - {3, "OLDTRACK", 0, "Old Track", ""}, - {0, NULL, 0, NULL, NULL} -}; - -static int track_set_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - int type= RNA_enum_get(op->ptr, "type"); - - if(type == 1) { - bConstraint *con; - bTrackToConstraint *data; - - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - if(base!=BASACT) { - con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO); - strcpy (con->name, "AutoTrack"); - - data = con->data; - data->tar = BASACT->object; - base->object->recalc |= OB_RECALC; - - /* Lamp and Camera track differently by default */ - if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) { - data->reserved1 = TRACK_nZ; - data->reserved2 = UP_Y; - } - - add_constraint_to_object(con, base->object); - } - } - CTX_DATA_END; - } - else if(type == 2) { - bConstraint *con; - bLockTrackConstraint *data; - - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - if(base!=BASACT) { - con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK); - strcpy (con->name, "AutoTrack"); - - data = con->data; - data->tar = BASACT->object; - base->object->recalc |= OB_RECALC; - - /* Lamp and Camera track differently by default */ - if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) { - data->trackflag = TRACK_nZ; - data->lockflag = LOCK_Y; - } - - add_constraint_to_object(con, base->object); - } - } - CTX_DATA_END; - } - else { - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - if(base!=BASACT) { - base->object->track= BASACT->object; - base->object->recalc |= OB_RECALC; - } - } - CTX_DATA_END; - } - DAG_scene_sort(CTX_data_scene(C)); - ED_anim_dag_flush_update(C); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_track_set(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Make Track"; - ot->description = "Make the object track another object, either by constraint or old way or locked track."; - ot->idname= "OBJECT_OT_track_set"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= track_set_exec; - - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", ""); -} - -/* ************* Make Dupli Real ********* */ -static void make_object_duplilist_real(Scene *scene, View3D *v3d, Base *base) -{ - Base *basen; - Object *ob; - ListBase *lb; - DupliObject *dob; - - if(!base && !(base = BASACT)) - return; - - if(!(base->object->transflag & OB_DUPLI)) - return; - - lb= object_duplilist(scene, base->object); - - for(dob= lb->first; dob; dob= dob->next) { - ob= copy_object(dob->ob); - /* font duplis can have a totcol without material, we get them from parent - * should be implemented better... - */ - if(ob->mat==NULL) ob->totcol= 0; - - basen= MEM_dupallocN(base); - basen->flag &= ~OB_FROMDUPLI; - BLI_addhead(&scene->base, basen); /* addhead: othwise eternal loop */ - basen->object= ob; - ob->ipo= NULL; /* make sure apply works */ - ob->parent= ob->track= NULL; - ob->disp.first= ob->disp.last= NULL; - ob->transflag &= ~OB_DUPLI; - - Mat4CpyMat4(ob->obmat, dob->mat); - ED_object_apply_obmat(ob); - } - - copy_object_set_idnew(scene, v3d, 0); - - free_object_duplilist(lb); - - base->object->transflag &= ~OB_DUPLI; -} - - -static int object_duplicates_make_real_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - ScrArea *sa= CTX_wm_area(C); - View3D *v3d= sa->spacedata.first; - - clear_id_newpoins(); - - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - make_object_duplilist_real(scene, v3d, base); - } - CTX_DATA_END; - - DAG_scene_sort(CTX_data_scene(C)); - ED_anim_dag_flush_update(C); - WM_event_add_notifier(C, NC_SCENE, CTX_data_scene(C)); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_duplicates_make_real(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Make Duplicates Real"; - ot->description = "Make dupli objects attached to this object real."; - ot->idname= "OBJECT_OT_duplicates_make_real"; - - /* api callbacks */ - ot->invoke= WM_operator_confirm; - ot->exec= object_duplicates_make_real_exec; - - ot->poll= ED_operator_scene_editable; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} -/* ******************* Set Object Center ********************** */ - -static EnumPropertyItem prop_set_center_types[] = { - {0, "CENTER", 0, "ObData to Center", "Move object data around Object center"}, - {1, "CENTERNEW", 0, "Center New", "Move Object center to center of object data"}, - {2, "CENTERCURSOR", 0, "Center Cursor", "Move Object Center to position of the 3d cursor"}, - {0, NULL, 0, NULL, NULL} -}; - -/* 0 == do center, 1 == center new, 2 == center cursor */ -static int object_center_set_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - ScrArea *sa= CTX_wm_area(C); - View3D *v3d= sa->spacedata.first; - Object *obedit= CTX_data_edit_object(C); - Object *ob; - Mesh *me, *tme; - Curve *cu; -/* BezTriple *bezt; - BPoint *bp; */ - Nurb *nu, *nu1; - EditVert *eve; - float cent[3], centn[3], min[3], max[3], omat[3][3]; - int a, total= 0; - int centermode = RNA_enum_get(op->ptr, "type"); - - /* keep track of what is changed */ - int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0; - MVert *mvert; - - if(scene->id.lib || v3d==NULL){ - BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed on Lib data"); - return OPERATOR_CANCELLED; - } - if (obedit && centermode > 0) { - BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode"); - return OPERATOR_CANCELLED; - } - cent[0]= cent[1]= cent[2]= 0.0; - - if(obedit) { - - INIT_MINMAX(min, max); - - if(obedit->type==OB_MESH) { - Mesh *me= obedit->data; - EditMesh *em = BKE_mesh_get_editmesh(me); - - for(eve= em->verts.first; eve; eve= eve->next) { - if(v3d->around==V3D_CENTROID) { - total++; - VECADD(cent, cent, eve->co); - } - else { - DO_MINMAX(eve->co, min, max); - } - } - - if(v3d->around==V3D_CENTROID) { - VecMulf(cent, 1.0f/(float)total); - } - else { - cent[0]= (min[0]+max[0])/2.0f; - cent[1]= (min[1]+max[1])/2.0f; - cent[2]= (min[2]+max[2])/2.0f; - } - - for(eve= em->verts.first; eve; eve= eve->next) { - VecSubf(eve->co, eve->co, cent); - } - - recalc_editnormals(em); - tot_change++; - DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); - BKE_mesh_end_editmesh(me, em); - } - } - - /* reset flags */ - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - base->object->flag &= ~OB_DONE; - } - CTX_DATA_END; - - for (me= G.main->mesh.first; me; me= me->id.next) { - me->flag &= ~ME_ISDONE; - } - - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - if((base->object->flag & OB_DONE)==0) { - base->object->flag |= OB_DONE; - - if(obedit==NULL && (me=get_mesh(base->object)) ) { - if (me->id.lib) { - tot_lib_error++; - } else { - if(centermode==2) { - VECCOPY(cent, give_cursor(scene, v3d)); - Mat4Invert(base->object->imat, base->object->obmat); - Mat4MulVecfl(base->object->imat, cent); - } else { - INIT_MINMAX(min, max); - mvert= me->mvert; - for(a=0; atotvert; a++, mvert++) { - DO_MINMAX(mvert->co, min, max); - } - - cent[0]= (min[0]+max[0])/2.0f; - cent[1]= (min[1]+max[1])/2.0f; - cent[2]= (min[2]+max[2])/2.0f; - } - - mvert= me->mvert; - for(a=0; atotvert; a++, mvert++) { - VecSubf(mvert->co, mvert->co, cent); - } - - if (me->key) { - KeyBlock *kb; - for (kb=me->key->block.first; kb; kb=kb->next) { - float *fp= kb->data; - - for (a=0; atotelem; a++, fp+=3) { - VecSubf(fp, fp, cent); - } - } - } - - me->flag |= ME_ISDONE; - - if(centermode) { - Mat3CpyMat4(omat, base->object->obmat); - - VECCOPY(centn, cent); - Mat3MulVecfl(omat, centn); - base->object->loc[0]+= centn[0]; - base->object->loc[1]+= centn[1]; - base->object->loc[2]+= centn[2]; - - where_is_object(scene, base->object); - ignore_parent_tx(scene, base->object); - - /* other users? */ - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - ob = base->object; - if((ob->flag & OB_DONE)==0) { - tme= get_mesh(ob); - - if(tme==me) { - - ob->flag |= OB_DONE; - ob->recalc= OB_RECALC_OB|OB_RECALC_DATA; - - Mat3CpyMat4(omat, ob->obmat); - VECCOPY(centn, cent); - Mat3MulVecfl(omat, centn); - ob->loc[0]+= centn[0]; - ob->loc[1]+= centn[1]; - ob->loc[2]+= centn[2]; - - where_is_object(scene, ob); - ignore_parent_tx(scene, ob); - - if(tme && (tme->flag & ME_ISDONE)==0) { - mvert= tme->mvert; - for(a=0; atotvert; a++, mvert++) { - VecSubf(mvert->co, mvert->co, cent); - } - - if (tme->key) { - KeyBlock *kb; - for (kb=tme->key->block.first; kb; kb=kb->next) { - float *fp= kb->data; - - for (a=0; atotelem; a++, fp+=3) { - VecSubf(fp, fp, cent); - } - } - } - - tme->flag |= ME_ISDONE; - } - } - } - - ob= ob->id.next; - } - CTX_DATA_END; - } - tot_change++; - } - } - else if (ELEM(base->object->type, OB_CURVE, OB_SURF)) { - - /* weak code here... (ton) */ - if(obedit==base->object) { - ListBase *editnurb= curve_get_editcurve(obedit); - - nu1= editnurb->first; - cu= obedit->data; - } - else { - cu= base->object->data; - nu1= cu->nurb.first; - } - - if (cu->id.lib) { - tot_lib_error++; - } else { - if(centermode==2) { - VECCOPY(cent, give_cursor(scene, v3d)); - Mat4Invert(base->object->imat, base->object->obmat); - Mat4MulVecfl(base->object->imat, cent); - - /* don't allow Z change if curve is 2D */ - if( !( cu->flag & CU_3D ) ) - cent[2] = 0.0; - } - else { - INIT_MINMAX(min, max); - - nu= nu1; - while(nu) { - minmaxNurb(nu, min, max); - nu= nu->next; - } - - cent[0]= (min[0]+max[0])/2.0f; - cent[1]= (min[1]+max[1])/2.0f; - cent[2]= (min[2]+max[2])/2.0f; - } - - nu= nu1; - while(nu) { - if(nu->type == CU_BEZIER) { - a= nu->pntsu; - while (a--) { - VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent); - VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent); - VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent); - } - } - else { - a= nu->pntsu*nu->pntsv; - while (a--) - VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent); - } - nu= nu->next; - } - - if(centermode && obedit==0) { - Mat3CpyMat4(omat, base->object->obmat); - - Mat3MulVecfl(omat, cent); - base->object->loc[0]+= cent[0]; - base->object->loc[1]+= cent[1]; - base->object->loc[2]+= cent[2]; - - where_is_object(scene, base->object); - ignore_parent_tx(scene, base->object); - } - - tot_change++; - if(obedit) { - if (centermode==0) { - DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); - } - break; - } - } - } - else if(base->object->type==OB_FONT) { - /* get from bb */ - - cu= base->object->data; - - if(cu->bb==0) { - /* do nothing*/ - } else if (cu->id.lib) { - tot_lib_error++; - } else { - cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]); - cu->yof= -0.5f -0.5f*( cu->bb->vec[0][1] - cu->bb->vec[2][1]); /* extra 0.5 is the height o above line */ - - /* not really ok, do this better once! */ - cu->xof /= cu->fsize; - cu->yof /= cu->fsize; - - tot_change++; - } - } - else if(base->object->type==OB_ARMATURE) { - bArmature *arm = base->object->data; - - if (arm->id.lib) { - tot_lib_error++; - } else if(arm->id.us>1) { - /*error("Can't apply to a multi user armature"); - return;*/ - tot_multiuser_arm_error++; - } else { - /* Function to recenter armatures in editarmature.c - * Bone + object locations are handled there. - */ - docenter_armature(scene, v3d, base->object, centermode); - tot_change++; - - where_is_object(scene, base->object); - ignore_parent_tx(scene, base->object); - - if(obedit) - break; - } - } - base->object->recalc= OB_RECALC_OB|OB_RECALC_DATA; - } - } - CTX_DATA_END; - - if (tot_change) { - ED_anim_dag_flush_update(C); - } - - /* Warn if any errors occured */ - if (tot_lib_error+tot_multiuser_arm_error) { - BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change); - if (tot_lib_error) - BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects",tot_lib_error); - if (tot_multiuser_arm_error) - BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)",tot_multiuser_arm_error); - } - - return OPERATOR_FINISHED; -} -void OBJECT_OT_center_set(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Set Center"; - ot->description = "Set the object's center, by either moving the data, or set to center of data, or use 3d cursor"; - ot->idname= "OBJECT_OT_center_set"; - - /* api callbacks */ - ot->invoke= WM_menu_invoke; - ot->exec= object_center_set_exec; - - ot->poll= ED_operator_view3d_active; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; - - RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", ""); -} -/* ******************* toggle editmode operator ***************** */ - -void ED_object_exit_editmode(bContext *C, int flag) -{ - Scene *scene= CTX_data_scene(C); - Object *obedit= CTX_data_edit_object(C); - int freedata = flag & EM_FREEDATA; - - if(obedit==NULL) return; - - if(flag & EM_WAITCURSOR) waitcursor(1); - if(obedit->type==OB_MESH) { - Mesh *me= obedit->data; - -// if(EM_texFaceCheck()) - -// if(retopo_mesh_paint_check()) -// retopo_end_okee(); - - if(me->edit_mesh->totvert>MESH_MAX_VERTS) { - error("Too many vertices"); - return; - } - load_editMesh(scene, obedit); - - if(freedata) { - free_editMesh(me->edit_mesh); - MEM_freeN(me->edit_mesh); - me->edit_mesh= NULL; - } - - if(obedit->restore_mode & OB_MODE_WEIGHT_PAINT) - mesh_octree_table(obedit, NULL, NULL, 'e'); - } - else if (obedit->type==OB_ARMATURE) { - ED_armature_from_edit(scene, obedit); - if(freedata) - ED_armature_edit_free(obedit); - } - else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) { - load_editNurb(obedit); - if(freedata) free_editNurb(obedit); - } - else if(obedit->type==OB_FONT && freedata) { - load_editText(obedit); - if(freedata) free_editText(obedit); - } - else if(obedit->type==OB_LATTICE) { - load_editLatt(obedit); - if(freedata) free_editLatt(obedit); - } - else if(obedit->type==OB_MBALL) { - load_editMball(obedit); - if(freedata) free_editMball(obedit); - } - - /* freedata only 0 now on file saves */ - if(freedata) { - /* for example; displist make is different in editmode */ - scene->obedit= NULL; // XXX for context - - BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_DEPSGRAPH); - - /* also flush ob recalc, doesn't take much overhead, but used for particles */ - DAG_id_flush_update(&obedit->id, OB_RECALC_OB|OB_RECALC_DATA); - - ED_undo_push(C, "Editmode"); - - if(flag & EM_WAITCURSOR) waitcursor(0); - - WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene); - } - - obedit->mode &= ~OB_MODE_EDIT; - ED_object_toggle_modes(C, obedit->restore_mode); -} - - -void ED_object_enter_editmode(bContext *C, int flag) -{ - Scene *scene= CTX_data_scene(C); - Base *base= CTX_data_active_base(C); - Object *ob; - ScrArea *sa= CTX_wm_area(C); - View3D *v3d= NULL; - int ok= 0; - - if(scene->id.lib) return; - if(base==NULL) return; - - if(sa && sa->spacetype==SPACE_VIEW3D) - v3d= sa->spacedata.first; - - if(v3d && (base->lay & v3d->lay)==0) return; - else if(!v3d && (base->lay & scene->lay)==0) return; - - ob = base->object; - - if(ob==NULL) return; - if(ob->data==NULL) return; - - if (object_data_is_libdata(ob)) { - error_libdata(); - return; - } - - if(flag & EM_WAITCURSOR) waitcursor(1); - - ob->restore_mode = ob->mode; - ED_object_toggle_modes(C, ob->mode); - - ob->mode |= OB_MODE_EDIT; - - if(ob->type==OB_MESH) { - Mesh *me= ob->data; - - if(me->pv) mesh_pmv_off(ob, me); - ok= 1; - scene->obedit= ob; // context sees this - - make_editMesh(scene, ob); - - WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MESH, scene); - } - else if (ob->type==OB_ARMATURE){ - bArmature *arm= base->object->data; - if (!arm) return; - /* - * The function object_data_is_libdata make a problem here, the - * check for ob->proxy return 0 and let blender enter to edit mode - * this causa a crash when you try leave the edit mode. - * The problem is that i can't remove the ob->proxy check from - * object_data_is_libdata that prevent the bugfix #6614, so - * i add this little hack here. - */ - if(arm->id.lib) { - error_libdata(); - return; - } - ok=1; - scene->obedit= ob; - ED_armature_to_edit(ob); - /* to ensure all goes in restposition and without striding */ - DAG_id_flush_update(&ob->id, OB_RECALC); - - WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_ARMATURE, scene); - } - else if(ob->type==OB_FONT) { - scene->obedit= ob; // XXX for context - ok= 1; - make_editText(ob); - - WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_TEXT, scene); - } - else if(ob->type==OB_MBALL) { - scene->obedit= ob; // XXX for context - ok= 1; - make_editMball(ob); - - WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MBALL, scene); - } - else if(ob->type==OB_LATTICE) { - scene->obedit= ob; // XXX for context - ok= 1; - make_editLatt(ob); - - WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_LATTICE, scene); - } - else if(ob->type==OB_SURF || ob->type==OB_CURVE) { - ok= 1; - scene->obedit= ob; // XXX for context - make_editNurb(ob); - - WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_CURVE, scene); - } - - if(ok) { - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - } - else { - scene->obedit= NULL; // XXX for context - WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene); - } - - if(flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode"); - if(flag & EM_WAITCURSOR) waitcursor(0); -} - -static int editmode_toggle_exec(bContext *C, wmOperator *op) -{ - - if(!CTX_data_edit_object(C)) - ED_object_enter_editmode(C, EM_WAITCURSOR); - else - ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); - - return OPERATOR_FINISHED; -} - -static int editmode_toggle_poll(bContext *C) -{ - Object *ob = CTX_data_active_object(C); - - return ob && (ob->type == OB_MESH || ob->type == OB_ARMATURE || - ob->type == OB_FONT || ob->type == OB_MBALL || - ob->type == OB_LATTICE || ob->type == OB_SURF || - ob->type == OB_CURVE); -} - -void OBJECT_OT_editmode_toggle(wmOperatorType *ot) -{ - - /* identifiers */ - ot->name= "Toggle Editmode"; - ot->description = "Toggle object's editmode."; - ot->idname= "OBJECT_OT_editmode_toggle"; - - /* api callbacks */ - ot->exec= editmode_toggle_exec; - - ot->poll= editmode_toggle_poll; - - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; -} - -/* *************************** */ - -static int posemode_exec(bContext *C, wmOperator *op) -{ - Base *base= CTX_data_active_base(C); - - if(base->object->type==OB_ARMATURE) { - if(base->object==CTX_data_edit_object(C)) { - ED_object_exit_editmode(C, EM_FREEDATA); - ED_armature_enter_posemode(C, base); - } - else if(base->object->mode & OB_MODE_POSE) - ED_armature_exit_posemode(C, base); - else - ED_armature_enter_posemode(C, base); - - return OPERATOR_FINISHED; - } - - return OPERATOR_PASS_THROUGH; -} - -void OBJECT_OT_posemode_toggle(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Toggle Pose Mode"; - ot->idname= "OBJECT_OT_posemode_toggle"; - ot->description= "Enables or disables posing/selecting bones"; - - /* api callbacks */ - ot->exec= posemode_exec; - ot->poll= ED_operator_object_active; - - /* flag */ - ot->flag= OPTYPE_REGISTER; -} - -/* *********************** */ - -void check_editmode(int type) -{ - Object *obedit= NULL; // XXX - - if (obedit==NULL || obedit->type==type) return; - -// XXX ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */ -} -void movetolayer(Scene *scene, View3D *v3d) -{ - Base *base; - unsigned int lay= 0, local; - int islamp= 0; - - if(scene->id.lib) return; - - for(base= FIRSTBASE; base; base= base->next) { - if (TESTBASE(v3d, base)) lay |= base->lay; - } - if(lay==0) return; - lay &= 0xFFFFFF; - - if(lay==0) return; - - if(v3d->localview) { - /* now we can move out of localview. */ - if (!okee("Move from localview")) return; - for(base= FIRSTBASE; base; base= base->next) { - if (TESTBASE(v3d, base)) { - lay= base->lay & ~v3d->lay; - base->lay= lay; - base->object->lay= lay; - base->object->flag &= ~SELECT; - base->flag &= ~SELECT; - if(base->object->type==OB_LAMP) islamp= 1; - } - } - } else { -// XXX if( movetolayer_buts(&lay, NULL)==0 ) return; - - /* normal non localview operation */ - for(base= FIRSTBASE; base; base= base->next) { - if (TESTBASE(v3d, base)) { - /* upper byte is used for local view */ - local= base->lay & 0xFF000000; - base->lay= lay + local; - base->object->lay= lay; - if(base->object->type==OB_LAMP) islamp= 1; - } - } - } - if(islamp) reshadeall_displist(scene); /* only frees */ - - /* warning, active object may be hidden now */ - - DAG_scene_sort(scene); - -} - - -#if 0 -// XXX should be in view3d? - -/* context: ob = lamp */ -/* code should be replaced with proper (custom) transform handles for lamp properties */ -static void spot_interactive(Object *ob, int mode) -{ - Lamp *la= ob->data; - float transfac, dx, dy, ratio, origval; - int keep_running= 1, center2d[2]; - short mval[2], mvalo[2]; - -// getmouseco_areawin(mval); -// getmouseco_areawin(mvalo); - - project_int(ob->obmat[3], center2d); - if( center2d[0] > 100000 ) { /* behind camera */ -// center2d[0]= curarea->winx/2; -// center2d[1]= curarea->winy/2; - } - -// helpline(mval, center2d); - - /* ratio is like scaling */ - dx = (float)(center2d[0] - mval[0]); - dy = (float)(center2d[1] - mval[1]); - transfac = (float)sqrt( dx*dx + dy*dy); - if(transfac==0.0f) transfac= 1.0f; - - if(mode==1) - origval= la->spotsize; - else if(mode==2) - origval= la->dist; - else if(mode==3) - origval= la->clipsta; - else - origval= la->clipend; - - while (keep_running>0) { - -// getmouseco_areawin(mval); - - /* essential for idling subloop */ - if(mval[0]==mvalo[0] && mval[1]==mvalo[1]) { - PIL_sleep_ms(2); - } - else { - char str[32]; - - dx = (float)(center2d[0] - mval[0]); - dy = (float)(center2d[1] - mval[1]); - ratio = (float)(sqrt( dx*dx + dy*dy))/transfac; - - /* do the trick */ - - if(mode==1) { /* spot */ - la->spotsize = ratio*origval; - CLAMP(la->spotsize, 1.0f, 180.0f); - sprintf(str, "Spot size %.2f\n", la->spotsize); - } - else if(mode==2) { /* dist */ - la->dist = ratio*origval; - CLAMP(la->dist, 0.01f, 5000.0f); - sprintf(str, "Distance %.2f\n", la->dist); - } - else if(mode==3) { /* sta */ - la->clipsta = ratio*origval; - CLAMP(la->clipsta, 0.001f, 5000.0f); - sprintf(str, "Distance %.2f\n", la->clipsta); - } - else if(mode==4) { /* end */ - la->clipend = ratio*origval; - CLAMP(la->clipend, 0.1f, 5000.0f); - sprintf(str, "Clip End %.2f\n", la->clipend); - } - - /* cleanup */ - mvalo[0]= mval[0]; - mvalo[1]= mval[1]; - - /* handle shaded mode */ -// XXX shade_buttons_change_3d(); - - /* DRAW */ - headerprint(str); - force_draw_plus(SPACE_BUTS, 0); - -// helpline(mval, center2d); - } - - while( qtest() ) { - short val; - unsigned short event= extern_qread(&val); - - switch (event){ - case ESCKEY: - case RIGHTMOUSE: - keep_running= 0; - break; - case LEFTMOUSE: - case SPACEKEY: - case PADENTER: - case RETKEY: - if(val) - keep_running= -1; - break; - } - } - } - - if(keep_running==0) { - if(mode==1) - la->spotsize= origval; - else if(mode==2) - la->dist= origval; - else if(mode==3) - la->clipsta= origval; - else - la->clipend= origval; - } - -} -#endif - -void special_editmenu(Scene *scene, View3D *v3d) -{ -// XXX static short numcuts= 2; - Object *ob= OBACT; - Object *obedit= NULL; // XXX - int nr,ret=0; - - if(ob==NULL) return; - - if(obedit==NULL) { - - if(ob->mode & OB_MODE_POSE) { -// XXX pose_special_editmenu(); - } - else if(paint_facesel_test(ob)) { - Mesh *me= get_mesh(ob); - MTFace *tface; - MFace *mface; - int a; - - if(me==0 || me->mtface==0) return; - - nr= pupmenu("Specials%t|Set Tex%x1| Shared%x2| Light%x3| Invisible%x4| Collision%x5| TwoSide%x6|Clr Tex%x7| Shared%x8| Light%x9| Invisible%x10| Collision%x11| TwoSide%x12"); - - tface= me->mtface; - mface= me->mface; - for(a=me->totface; a>0; a--, tface++, mface++) { - if(mface->flag & ME_FACE_SEL) { - switch(nr) { - case 1: - tface->mode |= TF_TEX; break; - case 2: - tface->mode |= TF_SHAREDCOL; break; - case 3: - tface->mode |= TF_LIGHT; break; - case 4: - tface->mode |= TF_INVISIBLE; break; - case 5: - tface->mode |= TF_DYNAMIC; break; - case 6: - tface->mode |= TF_TWOSIDE; break; - case 7: - tface->mode &= ~TF_TEX; - tface->tpage= 0; - break; - case 8: - tface->mode &= ~TF_SHAREDCOL; break; - case 9: - tface->mode &= ~TF_LIGHT; break; - case 10: - tface->mode &= ~TF_INVISIBLE; break; - case 11: - tface->mode &= ~TF_DYNAMIC; break; - case 12: - tface->mode &= ~TF_TWOSIDE; break; - } - } - } - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - } - else if(ob->mode & OB_MODE_VERTEX_PAINT) { - Mesh *me= get_mesh(ob); - - if(me==0 || (me->mcol==NULL && me->mtface==NULL) ) return; - - nr= pupmenu("Specials%t|Shared VertexCol%x1"); - if(nr==1) { - -// XXX do_shared_vertexcol(me); - - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - } - } - else if(ob->mode & OB_MODE_WEIGHT_PAINT) { - Object *par= modifiers_isDeformedByArmature(ob); - - if(par && (par->mode & OB_MODE_POSE)) { - nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2"); - -// XXX if(nr==1 || nr==2) -// XXX pose_adds_vgroups(ob, (nr == 2)); - } - } - else if(ob->mode & OB_MODE_PARTICLE_EDIT) { -#if 0 - // XXX - ParticleSystem *psys = PE_get_current(ob); - ParticleEditSettings *pset = PE_settings(); - - if(!psys) - return; - - if(pset->selectmode & SCE_SELECT_POINT) - nr= pupmenu("Specials%t|Rekey%x1|Subdivide%x2|Select First%x3|Select Last%x4|Remove Doubles%x5"); - else - nr= pupmenu("Specials%t|Rekey%x1|Remove Doubles%x5"); - - switch(nr) { - case 1: -// XXX if(button(&pset->totrekey, 2, 100, "Number of Keys:")==0) return; - waitcursor(1); - PE_rekey(); - break; - case 2: - PE_subdivide(); - break; - case 3: - PE_select_root(); - break; - case 4: - PE_select_tip(); - break; - case 5: - PE_remove_doubles(); - break; - } - - DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); - - if(nr>0) waitcursor(0); -#endif - } - else { - Base *base, *base_select= NULL; - - /* Get the active object mesh. */ - Mesh *me= get_mesh(ob); - - /* Booleans, if the active object is a mesh... */ - if (me && ob->id.lib==NULL) { - - /* Bring up a little menu with the boolean operation choices on. */ - nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6"); - - if (nr > 0) { - /* user has made a choice of a menu element. - All of the boolean functions require 2 mesh objects - we search through the object list to find the other - selected item and make sure it is distinct and a mesh. */ - - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - if(base->object != ob) base_select= base; - } - } - - if (base_select) { - if (get_mesh(base_select->object)) { - if(nr <= 3){ - waitcursor(1); -// XXX ret = NewBooleanMesh(BASACT,base_select,nr); - if (ret==0) { - error("An internal error occurred"); - } else if(ret==-1) { - error("Selected meshes must have faces to perform boolean operations"); - } else if (ret==-2) { - error("Both meshes must be a closed mesh"); - } - waitcursor(0); - } else { - BooleanModifierData *bmd = NULL; - bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean); - BLI_addtail(&ob->modifiers, bmd); - bmd->object = base_select->object; - bmd->modifier.mode |= eModifierMode_Realtime; - switch(nr){ - case 4: bmd->operation = eBooleanModifierOp_Intersect; break; - case 5: bmd->operation = eBooleanModifierOp_Union; break; - case 6: bmd->operation = eBooleanModifierOp_Difference; break; - } -// XXX do_common_editbuts(B_CHANGEDEP); - } - } else { - error("Please select 2 meshes"); - } - } else { - error("Please select 2 meshes"); - } - } - - } - else if (ob->type == OB_FONT) { - /* removed until this gets a decent implementation (ton) */ -/* nr= pupmenu("Split %t|Characters%x1"); - if (nr > 0) { - switch(nr) { - case 1: split_font(); - } - } -*/ - } - } - } - else if(obedit->type==OB_MESH) { - } - else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) { - } - else if(obedit->type==OB_ARMATURE) { - nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Switch Direction%x7|Flip Left-Right Names%x3|%l|AutoName Left-Right%x4|AutoName Front-Back%x5|AutoName Top-Bottom%x6"); -// if(nr==1) -// XXX subdivide_armature(1); - if(nr==2) { -// XXX if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return; - waitcursor(1); -// XXX subdivide_armature(numcuts); - } -// else if(nr==3) -// XXX armature_flip_names(); - else if(ELEM3(nr, 4, 5, 6)) { -// XXX armature_autoside_names(nr-4); - } -// else if(nr == 7) -// XXX switch_direction_armature(); - } - else if(obedit->type==OB_LATTICE) { - Lattice *lt= obedit->data; - static float weight= 1.0f; - { // XXX -// XXX if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) { - int a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; - BPoint *bp= lt->editlatt->def; - - while(a--) { - if(bp->f1 & SELECT) - bp->weight= weight; - bp++; - } - } - } - -} - -static void curvetomesh(Scene *scene, Object *ob) -{ - Curve *cu; - DispList *dl; - - ob->flag |= OB_DONE; - cu= ob->data; - - dl= cu->disp.first; - if(dl==0) makeDispListCurveTypes(scene, ob, 0); /* force creation */ - - nurbs_to_mesh(ob); /* also does users */ - if (ob->type != OB_MESH) { - error("can't convert curve to mesh"); - } else { - object_free_modifiers(ob); - } -} - -void convertmenu(Scene *scene, View3D *v3d) -{ - Base *base, *basen=NULL, *basact, *basedel=NULL; - Object *obact, *ob, *ob1; - Object *obedit= NULL; // XXX - Curve *cu; - Nurb *nu; - MetaBall *mb; - Mesh *me; - int ok=0, nr = 0, a; - - if(scene->id.lib) return; - - obact= OBACT; - if (obact == NULL) return; - if(!obact->flag & SELECT) return; - if(obedit) return; - - basact= BASACT; /* will be restored */ - - if(obact->type==OB_FONT) { - nr= pupmenu("Convert Font to%t|Curve%x1|Curve (Single filling group)%x2|Mesh%x3"); - if(nr>0) ok= 1; - } - else if(obact->type==OB_MBALL) { - nr= pupmenu("Convert Metaball to%t|Mesh (keep original)%x1|Mesh (Delete Original)%x2"); - if(nr>0) ok= 1; - } - else if(obact->type==OB_CURVE) { - nr= pupmenu("Convert Curve to%t|Mesh"); - if(nr>0) ok= 1; - } - else if(obact->type==OB_SURF) { - nr= pupmenu("Convert Nurbs Surface to%t|Mesh"); - if(nr>0) ok= 1; - } - else if(obact->type==OB_MESH) { - nr= pupmenu("Convert Modifiers to%t|Mesh (Keep Original)%x1|Mesh (Delete Original)%x2"); - if(nr>0) ok= 1; - } - if(ok==0) return; - - /* don't forget multiple users! */ - - /* reset flags */ - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - base->object->flag &= ~OB_DONE; - } - } - - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - - ob= base->object; - - if(ob->flag & OB_DONE); - else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */ - DerivedMesh *dm; - - basedel = base; - - ob->flag |= OB_DONE; - - ob1= copy_object(ob); - ob1->recalc |= OB_RECALC; - - basen= MEM_mallocN(sizeof(Base), "duplibase"); - *basen= *base; - BLI_addhead(&scene->base, basen); /* addhead: otherwise eternal loop */ - basen->object= ob1; - basen->flag |= SELECT; - base->flag &= ~SELECT; - ob->flag &= ~SELECT; - - /* decrement original mesh's usage count */ - me= ob1->data; - me->id.us--; - - /* make a new copy of the mesh */ - ob1->data= copy_mesh(me); - - /* make new mesh data from the original copy */ - dm= mesh_get_derived_final(scene, ob1, CD_MASK_MESH); - /* dm= mesh_create_derived_no_deform(ob1, NULL); this was called original (instead of get_derived). man o man why! (ton) */ - - DM_to_mesh(dm, ob1->data); - - dm->release(dm); - object_free_modifiers(ob1); /* after derivedmesh calls! */ - - /* If the original object is active then make this object active */ - if (ob == obact) { - // XXX ED_base_object_activate(C, basen); - basact = basen; - } - } - else if(ob->type==OB_FONT) { - ob->flag |= OB_DONE; - - ob->type= OB_CURVE; - cu= ob->data; - - if(cu->vfont) { - cu->vfont->id.us--; - cu->vfont= 0; - } - if(cu->vfontb) { - cu->vfontb->id.us--; - cu->vfontb= 0; - } - if(cu->vfonti) { - cu->vfonti->id.us--; - cu->vfonti= 0; - } - if(cu->vfontbi) { - cu->vfontbi->id.us--; - cu->vfontbi= 0; - } - /* other users */ - if(cu->id.us>1) { - ob1= G.main->object.first; - while(ob1) { - if(ob1->data==cu) { - ob1->type= OB_CURVE; - ob1->recalc |= OB_RECALC; - } - ob1= ob1->id.next; - } - } - if (nr==2 || nr==3) { - nu= cu->nurb.first; - while(nu) { - nu->charidx= 0; - nu= nu->next; - } - } - if (nr==3) { - curvetomesh(scene, ob); - } - } - else if(ELEM(ob->type, OB_CURVE, OB_SURF)) { - if(nr==1) { - curvetomesh(scene, ob); - } - } - else if(ob->type==OB_MBALL) { - - if(nr==1 || nr == 2) { - ob= find_basis_mball(scene, ob); - - if(ob->disp.first && !(ob->flag&OB_DONE)) { - basedel = base; - - ob->flag |= OB_DONE; - - ob1= copy_object(ob); - ob1->recalc |= OB_RECALC; - - basen= MEM_mallocN(sizeof(Base), "duplibase"); - *basen= *base; - BLI_addhead(&scene->base, basen); /* addhead: othwise eternal loop */ - basen->object= ob1; - basen->flag |= SELECT; - basedel->flag &= ~SELECT; - ob->flag &= ~SELECT; - - mb= ob1->data; - mb->id.us--; - - ob1->data= add_mesh("Mesh"); - ob1->type= OB_MESH; - - me= ob1->data; - me->totcol= mb->totcol; - if(ob1->totcol) { - me->mat= MEM_dupallocN(mb->mat); - for(a=0; atotcol; a++) id_us_plus((ID *)me->mat[a]); - } - - mball_to_mesh(&ob->disp, ob1->data); - - /* So we can see the wireframe */ - BASACT= basen; - - /* If the original object is active then make this object active */ - if (ob == obact) { - // XXX ED_base_object_activate(C, basen); - basact = basen; - } - - } - } - } - } - if(basedel != NULL && nr == 2) { - ED_base_object_free_and_unlink(scene, basedel); - } - basedel = NULL; - } - - /* delete object should renew depsgraph */ - if(nr==2) - DAG_scene_sort(scene); - - /* texspace and normals */ - if(!basen) BASACT= base; - -// XXX ED_object_enter_editmode(C, 0); -// XXX exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */ - BASACT= basact; - - - DAG_scene_sort(scene); -} - -/* Change subdivision or particle properties of mesh object ob, if level==-1 - * then toggle subsurf, else set to level set allows to toggle multiple - * selections */ - -static void object_has_subdivision_particles(Object *ob, int *havesubdiv, int *havepart, int depth) -{ - if(ob->type==OB_MESH) { - if(modifiers_findByType(ob, eModifierType_Subsurf)) - *havesubdiv= 1; - if(modifiers_findByType(ob, eModifierType_ParticleSystem)) - *havepart= 1; - } - - if(ob->dup_group && depth <= 4) { - GroupObject *go; - - for(go= ob->dup_group->gobject.first; go; go= go->next) - object_has_subdivision_particles(go->ob, havesubdiv, havepart, depth+1); - } -} - -static void object_flip_subdivison_particles(Scene *scene, Object *ob, int *set, int level, int mode, int particles, int depth) -{ - ModifierData *md; - - if(ob->type==OB_MESH) { - if(particles) { - for(md=ob->modifiers.first; md; md=md->next) { - if(md->type == eModifierType_ParticleSystem) { - ParticleSystemModifierData *psmd = (ParticleSystemModifierData*)md; - - if(*set == -1) - *set= psmd->modifier.mode&(mode); - - if (*set) - psmd->modifier.mode &= ~(mode); - else - psmd->modifier.mode |= (mode); - } - } - } - else { - md = modifiers_findByType(ob, eModifierType_Subsurf); - - if (md) { - SubsurfModifierData *smd = (SubsurfModifierData*) md; - - if (level == -1) { - if(*set == -1) - *set= smd->modifier.mode&(mode); - - if (*set) - smd->modifier.mode &= ~(mode); - else - smd->modifier.mode |= (mode); - } else { - smd->levels = level; - } - } - else if(depth == 0 && *set != 0) { - SubsurfModifierData *smd = (SubsurfModifierData*) modifier_new(eModifierType_Subsurf); - - BLI_addtail(&ob->modifiers, smd); - - if (level!=-1) { - smd->levels = level; - } - - if(*set == -1) - *set= 1; - } - } - - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - } - - if(ob->dup_group && depth<=4) { - GroupObject *go; - - for(go= ob->dup_group->gobject.first; go; go= go->next) - object_flip_subdivison_particles(scene, go->ob, set, level, mode, particles, depth+1); - } -} - -/* Change subdivision properties of mesh object ob, if -* level==-1 then toggle subsurf, else set to level. -*/ - -void flip_subdivison(Scene *scene, View3D *v3d, int level) -{ - Base *base; - int set= -1; - int mode, pupmode, particles= 0, havesubdiv= 0, havepart= 0; - int alt= 0; // XXX - - if(alt) - mode= eModifierMode_Realtime; - else - mode= eModifierMode_Render|eModifierMode_Realtime; - - if(level == -1) { - if (scene->obedit) { // XXX get from context - object_has_subdivision_particles(scene->obedit, &havesubdiv, &havepart, 0); - } else { - for(base= scene->base.first; base; base= base->next) { - if(((level==-1) && (TESTBASE(v3d, base))) || (TESTBASELIB(v3d, base))) { - object_has_subdivision_particles(base->object, &havesubdiv, &havepart, 0); - } - } - } - } - else - havesubdiv= 1; - - if(havesubdiv && havepart) { - pupmode= pupmenu("Switch%t|Subsurf %x1|Particle Systems %x2"); - if(pupmode <= 0) - return; - else if(pupmode == 2) - particles= 1; - } - else if(havepart) - particles= 1; - - if (scene->obedit) { // XXX get from context - object_flip_subdivison_particles(scene, scene->obedit, &set, level, mode, particles, 0); - } else { - for(base= scene->base.first; base; base= base->next) { - if(((level==-1) && (TESTBASE(v3d, base))) || (TESTBASELIB(v3d, base))) { - object_flip_subdivison_particles(scene, base->object, &set, level, mode, particles, 0); - } - } - } - - ED_anim_dag_flush_update(C); -} - -static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob) -{ - bProperty *prop; - Base *base; - int nr, tot=0; - char *str; - - prop= ob->prop.first; - while(prop) { - tot++; - prop= prop->next; - } - - str= MEM_callocN(50 + 33*tot, "copymenu prop"); - - if (tot) - strcpy(str, "Copy Property %t|Replace All|Merge All|%l"); - else - strcpy(str, "Copy Property %t|Clear All (no properties on active)"); - - tot= 0; - prop= ob->prop.first; - while(prop) { - tot++; - strcat(str, "|"); - strcat(str, prop->name); - prop= prop->next; - } - - nr= pupmenu(str); - - if ( nr==1 || nr==2 ) { - for(base= FIRSTBASE; base; base= base->next) { - if((base != BASACT) &&(TESTBASELIB(v3d, base))) { - if (nr==1) { /* replace */ - copy_properties( &base->object->prop, &ob->prop ); - } else { - for(prop = ob->prop.first; prop; prop= prop->next ) { - set_ob_property(base->object, prop); - } - } - } - } - } else if(nr>0) { - prop = BLI_findlink(&ob->prop, nr-4); /* account for first 3 menu items & menu index starting at 1*/ - - if(prop) { - for(base= FIRSTBASE; base; base= base->next) { - if((base != BASACT) &&(TESTBASELIB(v3d, base))) { - set_ob_property(base->object, prop); - } - } - } - } - MEM_freeN(str); - -} - -static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob) -{ - Base *base; - - for(base= FIRSTBASE; base; base= base->next) { - if(base->object != ob) { - if(TESTBASELIB(v3d, base)) { - - /* first: free all logic */ - free_sensors(&base->object->sensors); - unlink_controllers(&base->object->controllers); - free_controllers(&base->object->controllers); - unlink_actuators(&base->object->actuators); - free_actuators(&base->object->actuators); - - /* now copy it, this also works without logicbricks! */ - clear_sca_new_poins_ob(ob); - copy_sensors(&base->object->sensors, &ob->sensors); - copy_controllers(&base->object->controllers, &ob->controllers); - copy_actuators(&base->object->actuators, &ob->actuators); - set_sca_new_poins_ob(base->object); - - /* some menu settings */ - base->object->scavisflag= ob->scavisflag; - base->object->scaflag= ob->scaflag; - - /* set the initial state */ - base->object->state= ob->state; - base->object->init_state= ob->init_state; - } - } - } -} - -static void copymenu_modifiers(Scene *scene, View3D *v3d, Object *ob) -{ - Base *base; - int i, event; - char str[512]; - char *errorstr= NULL; - - strcpy(str, "Copy Modifiers %t"); - - sprintf(str+strlen(str), "|All%%x%d|%%l", NUM_MODIFIER_TYPES); - - for (i=eModifierType_None+1; iflags&eModifierTypeFlag_AcceptsCVs) || - (ob->type==OB_MESH && (mti->flags&eModifierTypeFlag_AcceptsMesh))) { - sprintf(str+strlen(str), "|%s%%x%d", mti->name, i); - } - } - - event = pupmenu(str); - if(event<=0) return; - - for (base= FIRSTBASE; base; base= base->next) { - if(base->object != ob) { - if(TESTBASELIB(v3d, base)) { - ModifierData *md; - - base->object->recalc |= OB_RECALC_OB|OB_RECALC_DATA; - - if (base->object->type==ob->type) { - /* copy all */ - if (event==NUM_MODIFIER_TYPES) { - object_free_modifiers(base->object); - - for (md=ob->modifiers.first; md; md=md->next) { - ModifierData *nmd = NULL; - - if(ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance)) continue; - - if(md->type == eModifierType_Collision) - continue; - - nmd = modifier_new(md->type); - modifier_copyData(md, nmd); - BLI_addtail(&base->object->modifiers, nmd); - } - - copy_object_particlesystems(base->object, ob); - copy_object_softbody(base->object, ob); - } else { - /* copy specific types */ - ModifierData *md, *mdn; - - /* remove all with type 'event' */ - for (md=base->object->modifiers.first; md; md=mdn) { - mdn= md->next; - if(md->type==event) { - BLI_remlink(&base->object->modifiers, md); - modifier_free(md); - } - } - - /* copy all with type 'event' */ - for (md=ob->modifiers.first; md; md=md->next) { - if (md->type==event) { - - mdn = modifier_new(event); - BLI_addtail(&base->object->modifiers, mdn); - - modifier_copyData(md, mdn); - } - } - - if(event == eModifierType_ParticleSystem) { - object_free_particlesystems(base->object); - copy_object_particlesystems(base->object, ob); - } - else if(event == eModifierType_Softbody) { - object_free_softbody(base->object); - copy_object_softbody(base->object, ob); - } - } - } - else - errorstr= "Did not copy modifiers to other Object types"; - } - } - } - -// if(errorstr) notice(errorstr); - - DAG_scene_sort(scene); - -} - -/* both pointers should exist */ -static void copy_texture_space(Object *to, Object *ob) -{ - float *poin1= NULL, *poin2= NULL; - int texflag= 0; - - if(ob->type==OB_MESH) { - texflag= ((Mesh *)ob->data)->texflag; - poin2= ((Mesh *)ob->data)->loc; - } - else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) { - texflag= ((Curve *)ob->data)->texflag; - poin2= ((Curve *)ob->data)->loc; - } - else if(ob->type==OB_MBALL) { - texflag= ((MetaBall *)ob->data)->texflag; - poin2= ((MetaBall *)ob->data)->loc; - } - else - return; - - if(to->type==OB_MESH) { - ((Mesh *)to->data)->texflag= texflag; - poin1= ((Mesh *)to->data)->loc; - } - else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) { - ((Curve *)to->data)->texflag= texflag; - poin1= ((Curve *)to->data)->loc; - } - else if(to->type==OB_MBALL) { - ((MetaBall *)to->data)->texflag= texflag; - poin1= ((MetaBall *)to->data)->loc; - } - else - return; - - memcpy(poin1, poin2, 9*sizeof(float)); /* this was noted in DNA_mesh, curve, mball */ - - if(to->type==OB_MESH) ; - else if(to->type==OB_MBALL) tex_space_mball(to); - else tex_space_curve(to->data); - -} - -void copy_attr(Scene *scene, View3D *v3d, short event) -{ - Object *ob; - Base *base; - Curve *cu, *cu1; - Nurb *nu; - int do_scene_sort= 0; - - if(scene->id.lib) return; - - if(!(ob=OBACT)) return; - - if(scene->obedit) { // XXX get from context - /* obedit_copymenu(); */ - return; - } - if(event==9) { - copymenu_properties(scene, v3d, ob); - return; - } - else if(event==10) { - copymenu_logicbricks(scene, v3d, ob); - return; - } - else if(event==24) { - copymenu_modifiers(scene, v3d, ob); - return; - } - - for(base= FIRSTBASE; base; base= base->next) { - if(base != BASACT) { - if(TESTBASELIB(v3d, base)) { - base->object->recalc |= OB_RECALC_OB; - - if(event==1) { /* loc */ - VECCOPY(base->object->loc, ob->loc); - VECCOPY(base->object->dloc, ob->dloc); - } - else if(event==2) { /* rot */ - VECCOPY(base->object->rot, ob->rot); - VECCOPY(base->object->drot, ob->drot); - /* Quats arnt used yet */ - /*VECCOPY(base->object->quat, ob->quat); - VECCOPY(base->object->dquat, ob->dquat);*/ - } - else if(event==3) { /* size */ - VECCOPY(base->object->size, ob->size); - VECCOPY(base->object->dsize, ob->dsize); - } - else if(event==4) { /* drawtype */ - base->object->dt= ob->dt; - base->object->dtx= ob->dtx; - base->object->empty_drawtype= ob->empty_drawtype; - base->object->empty_drawsize= ob->empty_drawsize; - } - else if(event==5) { /* time offs */ - base->object->sf= ob->sf; - } - else if(event==6) { /* dupli */ - base->object->dupon= ob->dupon; - base->object->dupoff= ob->dupoff; - base->object->dupsta= ob->dupsta; - base->object->dupend= ob->dupend; - - base->object->transflag &= ~OB_DUPLI; - base->object->transflag |= (ob->transflag & OB_DUPLI); - - base->object->dup_group= ob->dup_group; - if(ob->dup_group) - id_us_plus((ID *)ob->dup_group); - } - else if(event==7) { /* mass */ - base->object->mass= ob->mass; - } - else if(event==8) { /* damping */ - base->object->damping= ob->damping; - base->object->rdamping= ob->rdamping; - } - else if(event==11) { /* all physical attributes */ - base->object->gameflag = ob->gameflag; - base->object->inertia = ob->inertia; - base->object->formfactor = ob->formfactor; - base->object->damping= ob->damping; - base->object->rdamping= ob->rdamping; - base->object->min_vel= ob->min_vel; - base->object->max_vel= ob->max_vel; - if (ob->gameflag & OB_BOUNDS) { - base->object->boundtype = ob->boundtype; - } - base->object->margin= ob->margin; - base->object->bsoft= copy_bulletsoftbody(ob->bsoft); - - } - else if(event==17) { /* tex space */ - copy_texture_space(base->object, ob); - } - else if(event==18) { /* font settings */ - - if(base->object->type==ob->type) { - cu= ob->data; - cu1= base->object->data; - - cu1->spacemode= cu->spacemode; - cu1->spacing= cu->spacing; - cu1->linedist= cu->linedist; - cu1->shear= cu->shear; - cu1->fsize= cu->fsize; - cu1->xof= cu->xof; - cu1->yof= cu->yof; - cu1->textoncurve= cu->textoncurve; - cu1->wordspace= cu->wordspace; - cu1->ulpos= cu->ulpos; - cu1->ulheight= cu->ulheight; - if(cu1->vfont) cu1->vfont->id.us--; - cu1->vfont= cu->vfont; - id_us_plus((ID *)cu1->vfont); - if(cu1->vfontb) cu1->vfontb->id.us--; - cu1->vfontb= cu->vfontb; - id_us_plus((ID *)cu1->vfontb); - if(cu1->vfonti) cu1->vfonti->id.us--; - cu1->vfonti= cu->vfonti; - id_us_plus((ID *)cu1->vfonti); - if(cu1->vfontbi) cu1->vfontbi->id.us--; - cu1->vfontbi= cu->vfontbi; - id_us_plus((ID *)cu1->vfontbi); - - BKE_text_to_curve(scene, base->object, 0); /* needed? */ - - - strcpy(cu1->family, cu->family); - - base->object->recalc |= OB_RECALC_DATA; - } - } - else if(event==19) { /* bevel settings */ - - if(ELEM(base->object->type, OB_CURVE, OB_FONT)) { - cu= ob->data; - cu1= base->object->data; - - cu1->bevobj= cu->bevobj; - cu1->taperobj= cu->taperobj; - cu1->width= cu->width; - cu1->bevresol= cu->bevresol; - cu1->ext1= cu->ext1; - cu1->ext2= cu->ext2; - - base->object->recalc |= OB_RECALC_DATA; - } - } - else if(event==25) { /* curve resolution */ - - if(ELEM(base->object->type, OB_CURVE, OB_FONT)) { - cu= ob->data; - cu1= base->object->data; - - cu1->resolu= cu->resolu; - cu1->resolu_ren= cu->resolu_ren; - - nu= cu1->nurb.first; - - while(nu) { - nu->resolu= cu1->resolu; - nu= nu->next; - } - - base->object->recalc |= OB_RECALC_DATA; - } - } - else if(event==21){ - if (base->object->type==OB_MESH) { - ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf); - - if (md) { - ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf); - - if (!tmd) { - tmd = modifier_new(eModifierType_Subsurf); - BLI_addtail(&base->object->modifiers, tmd); - } - - modifier_copyData(md, tmd); - base->object->recalc |= OB_RECALC_DATA; - } - } - } - else if(event==22) { - /* Copy the constraint channels over */ - copy_constraints(&base->object->constraints, &ob->constraints); - - do_scene_sort= 1; - } - else if(event==23) { - base->object->softflag= ob->softflag; - if(base->object->soft) sbFree(base->object->soft); - - base->object->soft= copy_softbody(ob->soft); - - if (!modifiers_findByType(base->object, eModifierType_Softbody)) { - BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody)); - } - } - else if(event==26) { -#if 0 // XXX old animation system - copy_nlastrips(&base->object->nlastrips, &ob->nlastrips); -#endif // XXX old animation system - } - else if(event==27) { /* autosmooth */ - if (base->object->type==OB_MESH) { - Mesh *me= ob->data; - Mesh *cme= base->object->data; - cme->smoothresh= me->smoothresh; - if(me->flag & ME_AUTOSMOOTH) - cme->flag |= ME_AUTOSMOOTH; - else - cme->flag &= ~ME_AUTOSMOOTH; - } - } - else if(event==28) { /* UV orco */ - if(ELEM(base->object->type, OB_CURVE, OB_SURF)) { - cu= ob->data; - cu1= base->object->data; - - if(cu->flag & CU_UV_ORCO) - cu1->flag |= CU_UV_ORCO; - else - cu1->flag &= ~CU_UV_ORCO; - } - } - else if(event==29) { /* protected bits */ - base->object->protectflag= ob->protectflag; - } - else if(event==30) { /* index object */ - base->object->index= ob->index; - } - else if(event==31) { /* object color */ - QUATCOPY(base->object->col, ob->col); - } - } - } - } - - if(do_scene_sort) - DAG_scene_sort(scene); - - ED_anim_dag_flush_update(C); - -} - -void copy_attr_menu(Scene *scene, View3D *v3d) -{ - Object *ob; - short event; - char str[512]; - - if(!(ob=OBACT)) return; - - if (scene->obedit) { // XXX get from context -// if (ob->type == OB_MESH) -// XXX mesh_copy_menu(); - return; - } - - /* Object Mode */ - - /* If you change this menu, don't forget to update the menu in header_view3d.c - * view3d_edit_object_copyattrmenu() and in toolbox.c - */ - - strcpy(str, "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Draw Options%x4|Time Offset%x5|Dupli%x6|Object Color%x31|%l|Mass%x7|Damping%x8|All Physical Attributes%x11|Properties%x9|Logic Bricks%x10|Protected Transform%x29|%l"); - - strcat (str, "|Object Constraints%x22"); - strcat (str, "|NLA Strips%x26"); - -// XXX if (OB_SUPPORT_MATERIAL(ob)) { -// strcat(str, "|Texture Space%x17"); -// } - - if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19"); - if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28"); - - if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) { - strcat(str, "|Curve Resolution%x25"); - } - - if(ob->type==OB_MESH){ - strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27"); - } - - if(ob->soft) strcat(str, "|Soft Body Settings%x23"); - - strcat(str, "|Pass Index%x30"); - - if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){ - strcat(str, "|Modifiers ...%x24"); - } - - event= pupmenu(str); - if(event<= 0) return; - - copy_attr(scene, v3d, event); -} - - -void link_to_scene(unsigned short nr) -{ -#if 0 - Scene *sce= (Scene*) BLI_findlink(&G.main->scene, G.curscreen->scenenr-1); - Base *base, *nbase; - - if(sce==0) return; - if(sce->id.lib) return; - - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASE(v3d, base)) { - - nbase= MEM_mallocN( sizeof(Base), "newbase"); - *nbase= *base; - BLI_addhead( &(sce->base), nbase); - id_us_plus((ID *)base->object); - } - } -#endif -} - - -void make_links(Scene *scene, View3D *v3d, short event) -{ - Object *ob, *obt; - Base *base, *nbase, *sbase; - Scene *sce = NULL; - ID *id; - int a; - short nr=0; - char *strp; - - if(!(ob=OBACT)) return; - - if(event==1) { - IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->scene), 0, &nr); - - if(nr == -2) { - MEM_freeN(strp); - -// XXX activate_databrowse((ID *)scene, ID_SCE, 0, B_INFOSCE, &(G.curscreen->scenenr), link_to_scene ); - - return; - } - else { - event= pupmenu_col(strp, 20); - MEM_freeN(strp); - - if(event<= 0) return; - - nr= 1; - sce= G.main->scene.first; - while(sce) { - if(nr==event) break; - nr++; - sce= sce->id.next; - } - if(sce==scene) { - error("This is the current scene"); + if(!psys) return; - } - if(sce==0 || sce->id.lib) return; - - /* remember: is needed below */ - event= 1; - } - } - - /* All non group linking */ - for(base= FIRSTBASE; base; base= base->next) { - if(event==1 || base != BASACT) { - - obt= base->object; - - if(TESTBASE(v3d, base)) { - - if(event==1) { /* to scene */ - - /* test if already linked */ - sbase= sce->base.first; - while(sbase) { - if(sbase->object==base->object) break; - sbase= sbase->next; - } - if(sbase) { /* remove */ - continue; - } - - nbase= MEM_mallocN( sizeof(Base), "newbase"); - *nbase= *base; - BLI_addhead( &(sce->base), nbase); - id_us_plus((ID *)base->object); - } - } - if(TESTBASELIB(v3d, base)) { - if(event==2 || event==5) { /* obdata */ - if(ob->type==obt->type) { - - id= obt->data; - id->us--; - - id= ob->data; - id_us_plus(id); - obt->data= id; - - /* if amount of material indices changed: */ - test_object_materials(obt->data); - - obt->recalc |= OB_RECALC_DATA; - } - } - else if(event==4) { /* ob ipo */ -#if 0 // XXX old animation system - if(obt->ipo) obt->ipo->id.us--; - obt->ipo= ob->ipo; - if(obt->ipo) { - id_us_plus((ID *)obt->ipo); - do_ob_ipo(scene, obt); - } -#endif // XXX old animation system - } - else if(event==6) { - if(ob->dup_group) ob->dup_group->id.us--; - obt->dup_group= ob->dup_group; - if(obt->dup_group) { - id_us_plus((ID *)obt->dup_group); - obt->transflag |= OB_DUPLIGROUP; - } - } - else if(event==3) { /* materials */ - - /* new approach, using functions from kernel */ - for(a=0; atotcol; a++) { - Material *ma= give_current_material(ob, a+1); - assign_material(obt, ma, a+1); /* also works with ma==NULL */ - } - } - } - } - } - - ED_anim_dag_flush_update(C); - -} - -void make_links_menu(Scene *scene, View3D *v3d) -{ - Object *ob; - short event=0; - char str[140]; - - if(!(ob=OBACT)) return; - - strcpy(str, "Make Links %t|To Scene...%x1|%l|Object Ipo%x4"); - - if(ob->type==OB_MESH) - strcat(str, "|Mesh Data%x2|Materials%x3"); - else if(ob->type==OB_CURVE) - strcat(str, "|Curve Data%x2|Materials%x3"); - else if(ob->type==OB_FONT) - strcat(str, "|Text Data%x2|Materials%x3"); - else if(ob->type==OB_SURF) - strcat(str, "|Surface Data%x2|Materials%x3"); - else if(ob->type==OB_MBALL) - strcat(str, "|Materials%x3"); - else if(ob->type==OB_CAMERA) - strcat(str, "|Camera Data%x2"); - else if(ob->type==OB_LAMP) - strcat(str, "|Lamp Data%x2"); - else if(ob->type==OB_LATTICE) - strcat(str, "|Lattice Data%x2"); - else if(ob->type==OB_ARMATURE) - strcat(str, "|Armature Data%x2"); - - event= pupmenu(str); - - if(event<= 0) return; - - make_links(scene, v3d, event); -} -static void apply_objects_internal(Scene *scene, View3D *v3d, int apply_scale, int apply_rot ) -{ - Base *base, *basact; - Object *ob; - bArmature *arm; - Mesh *me; - Curve *cu; - Nurb *nu; - BPoint *bp; - BezTriple *bezt; - MVert *mvert; - float mat[3][3]; - int a, change = 0; - - if (!apply_scale && !apply_rot) { - /* do nothing? */ - error("Nothing to do!"); - return; - } - /* first check if we can execute */ - for (base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - ob= base->object; - if(ob->type==OB_MESH) { - me= ob->data; - - if(me->id.us>1) { - error("Can't apply to a multi user mesh, doing nothing."); - return; - } - } - else if (ob->type==OB_ARMATURE) { - arm= ob->data; - - if(arm->id.us>1) { - error("Can't apply to a multi user armature, doing nothing."); - return; - } - } - else if(ELEM(ob->type, OB_CURVE, OB_SURF)) { - cu= ob->data; - - if(cu->id.us>1) { - error("Can't apply to a multi user curve, doing nothing."); - return; - } - if(cu->key) { - error("Can't apply to a curve with vertex keys, doing nothing."); - return; - } - } - } - } - - /* now execute */ - basact= BASACT; - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - ob= base->object; + if(pset->selectmode & SCE_SELECT_POINT) + nr= pupmenu("Specials%t|Rekey%x1|Subdivide%x2|Select First%x3|Select Last%x4|Remove Doubles%x5"); + else + nr= pupmenu("Specials%t|Rekey%x1|Remove Doubles%x5"); - if(ob->type==OB_MESH) { - /* calculate matrix */ - if (apply_scale && apply_rot) - object_to_mat3(ob, mat); - else if (apply_scale) - object_scale_to_mat3(ob, mat); - else - object_rot_to_mat3(ob, mat); - - /* get object data */ - me= ob->data; - - /* adjust data */ - mvert= me->mvert; - for(a=0; atotvert; a++, mvert++) { - Mat3MulVecfl(mat, mvert->co); - } - - if (me->key) { - KeyBlock *kb; - - for (kb=me->key->block.first; kb; kb=kb->next) { - float *fp= kb->data; - - for (a=0; atotelem; a++, fp+=3) - Mat3MulVecfl(mat, fp); - } - } - - /* adjust transforms */ - if (apply_scale) - ob->size[0]= ob->size[1]= ob->size[2]= 1.0f; - if (apply_rot) - ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0f; - /*QuatOne(ob->quat);*/ /* Quats arnt used yet */ - - where_is_object(scene, ob); - - /* texspace and normals */ - BASACT= base; -// XXX ED_object_enter_editmode(C, 0); -// XXX ED_object_exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */ - BASACT= basact; - - change = 1; - } - else if (ob->type==OB_ARMATURE) { - if (apply_scale && apply_rot) - object_to_mat3(ob, mat); - else if (apply_scale) - object_scale_to_mat3(ob, mat); - else - object_rot_to_mat3(ob, mat); - arm= ob->data; - - /* see checks above */ -// XXX apply_rot_armature(ob, mat); - - /* Reset the object's transforms */ - if (apply_scale) - ob->size[0]= ob->size[1]= ob->size[2]= 1.0; - if (apply_rot) - ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0; - /*QuatOne(ob->quat); (not used anymore)*/ - - where_is_object(scene, ob); - - change = 1; + switch(nr) { + case 1: +// XXX if(button(&pset->totrekey, 2, 100, "Number of Keys:")==0) return; + waitcursor(1); + PE_rekey(); + break; + case 2: + PE_subdivide(); + break; + case 3: + PE_select_root(); + break; + case 4: + PE_select_tip(); + break; + case 5: + PE_remove_doubles(); + break; } - else if(ELEM(ob->type, OB_CURVE, OB_SURF)) { - float scale; - if (apply_scale && apply_rot) - object_to_mat3(ob, mat); - else if (apply_scale) - object_scale_to_mat3(ob, mat); - else - object_rot_to_mat3(ob, mat); - scale = Mat3ToScalef(mat); - cu= ob->data; - - /* see checks above */ + + DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + + if(nr>0) waitcursor(0); +#endif + } + else { + Base *base, *base_select= NULL; + + /* Get the active object mesh. */ + Mesh *me= get_mesh(ob); + + /* Booleans, if the active object is a mesh... */ + if (me && ob->id.lib==NULL) { - nu= cu->nurb.first; - while(nu) { - if(nu->type == CU_BEZIER) { - a= nu->pntsu; - bezt= nu->bezt; - while(a--) { - Mat3MulVecfl(mat, bezt->vec[0]); - Mat3MulVecfl(mat, bezt->vec[1]); - Mat3MulVecfl(mat, bezt->vec[2]); - bezt->radius *= scale; - bezt++; + /* Bring up a little menu with the boolean operation choices on. */ + nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6"); + + if (nr > 0) { + /* user has made a choice of a menu element. + All of the boolean functions require 2 mesh objects + we search through the object list to find the other + selected item and make sure it is distinct and a mesh. */ + + for(base= FIRSTBASE; base; base= base->next) { + if(TESTBASELIB(v3d, base)) { + if(base->object != ob) base_select= base; } } - else { - a= nu->pntsu*nu->pntsv; - bp= nu->bp; - while(a--) { - Mat3MulVecfl(mat, bp->vec); - bp++; + + if (base_select) { + if (get_mesh(base_select->object)) { + if(nr <= 3){ + waitcursor(1); +// XXX ret = NewBooleanMesh(BASACT,base_select,nr); + if (ret==0) { + error("An internal error occurred"); + } else if(ret==-1) { + error("Selected meshes must have faces to perform boolean operations"); + } else if (ret==-2) { + error("Both meshes must be a closed mesh"); + } + waitcursor(0); + } else { + BooleanModifierData *bmd = NULL; + bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean); + BLI_addtail(&ob->modifiers, bmd); + bmd->object = base_select->object; + bmd->modifier.mode |= eModifierMode_Realtime; + switch(nr){ + case 4: bmd->operation = eBooleanModifierOp_Intersect; break; + case 5: bmd->operation = eBooleanModifierOp_Union; break; + case 6: bmd->operation = eBooleanModifierOp_Difference; break; + } +// XXX do_common_editbuts(B_CHANGEDEP); + } + } else { + error("Please select 2 meshes"); } + } else { + error("Please select 2 meshes"); } - nu= nu->next; } - if (apply_scale) - ob->size[0]= ob->size[1]= ob->size[2]= 1.0; - if (apply_rot) - ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0; - /*QuatOne(ob->quat); (quats arnt used anymore)*/ - - where_is_object(scene, ob); - - /* texspace and normals */ - BASACT= base; -// XXX ED_object_enter_editmode(C, 0); -// XXX ED_object_exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */ - BASACT= basact; - - change = 1; - } else { - continue; + } - - ignore_parent_tx(scene, ob); + else if (ob->type == OB_FONT) { + /* removed until this gets a decent implementation (ton) */ +/* nr= pupmenu("Split %t|Characters%x1"); + if (nr > 0) { + switch(nr) { + case 1: split_font(); + } + } +*/ + } } } - if (change) { + else if(obedit->type==OB_MESH) { + } + else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) { + } + else if(obedit->type==OB_ARMATURE) { + nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Switch Direction%x7|Flip Left-Right Names%x3|%l|AutoName Left-Right%x4|AutoName Front-Back%x5|AutoName Top-Bottom%x6"); +// if(nr==1) +// XXX subdivide_armature(1); + if(nr==2) { +// XXX if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return; + waitcursor(1); +// XXX subdivide_armature(numcuts); + } +// else if(nr==3) +// XXX armature_flip_names(); + else if(ELEM3(nr, 4, 5, 6)) { +// XXX armature_autoside_names(nr-4); + } +// else if(nr == 7) +// XXX switch_direction_armature(); + } + else if(obedit->type==OB_LATTICE) { + Lattice *lt= obedit->data; + static float weight= 1.0f; + { // XXX +// XXX if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) { + int a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; + BPoint *bp= lt->editlatt->def; + + while(a--) { + if(bp->f1 & SELECT) + bp->weight= weight; + bp++; + } + } } -} - -void apply_objects_locrot(Scene *scene, View3D *v3d) -{ - apply_objects_internal(scene, v3d, 1, 1); -} -void apply_objects_scale(Scene *scene, View3D *v3d) -{ - apply_objects_internal(scene, v3d, 1, 0); } -void apply_objects_rot(Scene *scene, View3D *v3d) -{ - apply_objects_internal(scene, v3d, 0, 1); -} +/* Change subdivision or particle properties of mesh object ob, if level==-1 + * then toggle subsurf, else set to level set allows to toggle multiple + * selections */ -void apply_objects_visual_tx( Scene *scene, View3D *v3d ) +static void object_has_subdivision_particles(Object *ob, int *havesubdiv, int *havepart, int depth) { - Base *base; - Object *ob; - int change = 0; - - for (base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - ob= base->object; - where_is_object(scene, ob); - VECCOPY(ob->loc, ob->obmat[3]); - Mat4ToSize(ob->obmat, ob->size); - Mat4ToEul(ob->obmat, ob->rot); - - where_is_object(scene, ob); - - change = 1; - } + if(ob->type==OB_MESH) { + if(modifiers_findByType(ob, eModifierType_Subsurf)) + *havesubdiv= 1; + if(modifiers_findByType(ob, eModifierType_ParticleSystem)) + *havepart= 1; } - if (change) { + + if(ob->dup_group && depth <= 4) { + GroupObject *go; + + for(go= ob->dup_group->gobject.first; go; go= go->next) + object_has_subdivision_particles(go->ob, havesubdiv, havepart, depth+1); } } -/* ************************************** */ +static void object_flip_subdivison_particles(Scene *scene, Object *ob, int *set, int level, int mode, int particles, int depth) +{ + ModifierData *md; + if(ob->type==OB_MESH) { + if(particles) { + for(md=ob->modifiers.first; md; md=md->next) { + if(md->type == eModifierType_ParticleSystem) { + ParticleSystemModifierData *psmd = (ParticleSystemModifierData*)md; -void single_object_users(Scene *scene, View3D *v3d, int flag) -{ - Base *base; - Object *ob, *obn; - - clear_sca_new_poins(); /* sensor/contr/act */ + if(*set == -1) + *set= psmd->modifier.mode&(mode); - /* duplicate (must set newid) */ - for(base= FIRSTBASE; base; base= base->next) { - ob= base->object; - - if( (base->flag & flag)==flag ) { - if(ob->id.lib==NULL && ob->id.us>1) { - /* base gets copy of object */ - obn= copy_object(ob); - base->object= obn; - ob->id.us--; + if (*set) + psmd->modifier.mode &= ~(mode); + else + psmd->modifier.mode |= (mode); + } } } - } - - ID_NEW(scene->camera); - if(v3d) ID_NEW(v3d->camera); - - /* object pointers */ - for(base= FIRSTBASE; base; base= base->next) { - ob= base->object; - if(ob->id.lib==NULL) { - relink_constraints(&base->object->constraints); - if (base->object->pose){ - bPoseChannel *chan; - for (chan = base->object->pose->chanbase.first; chan; chan=chan->next){ - relink_constraints(&chan->constraints); + else { + md = modifiers_findByType(ob, eModifierType_Subsurf); + + if (md) { + SubsurfModifierData *smd = (SubsurfModifierData*) md; + + if (level == -1) { + if(*set == -1) + *set= smd->modifier.mode&(mode); + + if (*set) + smd->modifier.mode &= ~(mode); + else + smd->modifier.mode |= (mode); + } else { + smd->levels = level; + } + } + else if(depth == 0 && *set != 0) { + SubsurfModifierData *smd = (SubsurfModifierData*) modifier_new(eModifierType_Subsurf); + + BLI_addtail(&ob->modifiers, smd); + + if (level!=-1) { + smd->levels = level; } + + if(*set == -1) + *set= 1; } - modifiers_foreachObjectLink(base->object, single_object_users__forwardModifierLinks, NULL); - - ID_NEW(ob->parent); - ID_NEW(ob->track); } + + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); } - set_sca_new_poins(); -} + if(ob->dup_group && depth<=4) { + GroupObject *go; -void new_id_matar(Material **matar, int totcol) -{ - ID *id; - int a; - - for(a=0; alib==0) { - if(id->newid) { - matar[a]= (Material *)id->newid; - id_us_plus(id->newid); - id->us--; - } - else if(id->us>1) { - matar[a]= copy_material(matar[a]); - id->us--; - id->newid= (ID *)matar[a]; - } - } + for(go= ob->dup_group->gobject.first; go; go= go->next) + object_flip_subdivison_particles(scene, go->ob, set, level, mode, particles, depth+1); } } -void single_obdata_users(Scene *scene, int flag) +/* Change subdivision properties of mesh object ob, if +* level==-1 then toggle subsurf, else set to level. +*/ + +void flip_subdivison(Scene *scene, View3D *v3d, int level) { - Object *ob; - Lamp *la; - Curve *cu; - //Camera *cam; Base *base; - Mesh *me; - ID *id; - int a; - - for(base= FIRSTBASE; base; base= base->next) { - ob= base->object; - if(ob->id.lib==NULL && (base->flag & flag)==flag ) { - id= ob->data; - - if(id && id->us>1 && id->lib==0) { - ob->recalc= OB_RECALC_DATA; - - switch(ob->type) { - case OB_LAMP: - if(id && id->us>1 && id->lib==NULL) { - ob->data= la= copy_lamp(ob->data); - for(a=0; amtex[a]) { - ID_NEW(la->mtex[a]->object); - } - } - } - break; - case OB_CAMERA: - ob->data= copy_camera(ob->data); - break; - case OB_MESH: - me= ob->data= copy_mesh(ob->data); - //if(me && me->key) - // ipo_idnew(me->key->ipo); /* drivers */ - break; - case OB_MBALL: - ob->data= copy_mball(ob->data); - break; - case OB_CURVE: - case OB_SURF: - case OB_FONT: - ob->data= cu= copy_curve(ob->data); - ID_NEW(cu->bevobj); - ID_NEW(cu->taperobj); - break; - case OB_LATTICE: - ob->data= copy_lattice(ob->data); - break; - case OB_ARMATURE: - ob->recalc |= OB_RECALC_DATA; - ob->data= copy_armature(ob->data); - armature_rebuild_pose(ob, ob->data); - break; - default: - printf("ERROR single_obdata_users: %s\n", id->name); - error("Read console"); - return; - } - - id->us--; - id->newid= ob->data; - - } - -#if 0 // XXX old animation system - id= (ID *)ob->action; - if (id && id->us>1 && id->lib==NULL){ - if(id->newid){ - ob->action= (bAction *)id->newid; - id_us_plus(id->newid); - } - else { - ob->action= copy_action(ob->action); - id->us--; - id->newid=(ID *)ob->action; - } - } - id= (ID *)ob->ipo; - if(id && id->us>1 && id->lib==NULL) { - if(id->newid) { - ob->ipo= (Ipo *)id->newid; - id_us_plus(id->newid); - } - else { - ob->ipo= copy_ipo(ob->ipo); - id->us--; - id->newid= (ID *)ob->ipo; - } - ipo_idnew(ob->ipo); /* drivers */ - } - /* other ipos */ - switch(ob->type) { - case OB_LAMP: - la= ob->data; - if(la->ipo && la->ipo->id.us>1) { - la->ipo->id.us--; - la->ipo= copy_ipo(la->ipo); - ipo_idnew(la->ipo); /* drivers */ - } - break; - case OB_CAMERA: - cam= ob->data; - if(cam->ipo && cam->ipo->id.us>1) { - cam->ipo->id.us--; - cam->ipo= copy_ipo(cam->ipo); - ipo_idnew(cam->ipo); /* drivers */ + int set= -1; + int mode, pupmode, particles= 0, havesubdiv= 0, havepart= 0; + int alt= 0; // XXX + + if(alt) + mode= eModifierMode_Realtime; + else + mode= eModifierMode_Render|eModifierMode_Realtime; + + if(level == -1) { + if (scene->obedit) { // XXX get from context + object_has_subdivision_particles(scene->obedit, &havesubdiv, &havepart, 0); + } else { + for(base= scene->base.first; base; base= base->next) { + if(((level==-1) && (TESTBASE(v3d, base))) || (TESTBASELIB(v3d, base))) { + object_has_subdivision_particles(base->object, &havesubdiv, &havepart, 0); } - break; } -#endif // XXX old animation system } } + else + havesubdiv= 1; - me= G.main->mesh.first; - while(me) { - ID_NEW(me->texcomesh); - me= me->id.next; + if(havesubdiv && havepart) { + pupmode= pupmenu("Switch%t|Subsurf %x1|Particle Systems %x2"); + if(pupmode <= 0) + return; + else if(pupmode == 2) + particles= 1; } -} + else if(havepart) + particles= 1; -void single_ipo_users(Scene *scene, int flag) -{ -#if 0 // XXX old animation system - Object *ob; - Base *base; - ID *id; - - for(base= FIRSTBASE; base; base= base->next) { - ob= base->object; - if(ob->id.lib==NULL && (flag==0 || (base->flag & SELECT)) ) { - ob->recalc= OB_RECALC_DATA; - - id= (ID *)ob->ipo; - if(id && id->us>1 && id->lib==NULL) { - ob->ipo= copy_ipo(ob->ipo); - id->us--; - ipo_idnew(ob->ipo); /* drivers */ + if (scene->obedit) { // XXX get from context + object_flip_subdivison_particles(scene, scene->obedit, &set, level, mode, particles, 0); + } else { + for(base= scene->base.first; base; base= base->next) { + if(((level==-1) && (TESTBASE(v3d, base))) || (TESTBASELIB(v3d, base))) { + object_flip_subdivison_particles(scene, base->object, &set, level, mode, particles, 0); } } } -#endif // XXX old animation system + + ED_anim_dag_flush_update(C); } - -void single_mat_users(Scene *scene, int flag) -{ - Object *ob; + +static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob) +{ + bProperty *prop; Base *base; - Material *ma, *man; - Tex *tex; - int a, b; + int nr, tot=0; + char *str; + prop= ob->prop.first; + while(prop) { + tot++; + prop= prop->next; + } - for(base= FIRSTBASE; base; base= base->next) { - ob= base->object; - if(ob->id.lib==NULL && (flag==0 || (base->flag & SELECT)) ) { + str= MEM_callocN(50 + 33*tot, "copymenu prop"); - for(a=1; a<=ob->totcol; a++) { - ma= give_current_material(ob, a); - if(ma) { - /* do not test for LIB_NEW: this functions guaranteed delivers single_users! */ - - if(ma->id.us>1) { - man= copy_material(ma); - - man->id.us= 0; - assign_material(ob, man, a); + if (tot) + strcpy(str, "Copy Property %t|Replace All|Merge All|%l"); + else + strcpy(str, "Copy Property %t|Clear All (no properties on active)"); -#if 0 // XXX old animation system - if(ma->ipo) { - man->ipo= copy_ipo(ma->ipo); - ma->ipo->id.us--; - ipo_idnew(ma->ipo); /* drivers */ - } -#endif // XXX old animation system - - for(b=0; bmtex[b] && ma->mtex[b]->tex) { - tex= ma->mtex[b]->tex; - if(tex->id.us>1) { - ma->mtex[b]->tex= copy_texture(tex); - tex->id.us--; - } - } - } - - } - } - } - } + tot= 0; + prop= ob->prop.first; + while(prop) { + tot++; + strcat(str, "|"); + strcat(str, prop->name); + prop= prop->next; } -} -void do_single_tex_user(Tex **from) -{ - Tex *tex, *texn; - - tex= *from; - if(tex==0) return; - - if(tex->id.newid) { - *from= (Tex *)tex->id.newid; - id_us_plus(tex->id.newid); - tex->id.us--; - } - else if(tex->id.us>1) { - texn= copy_texture(tex); - tex->id.newid= (ID *)texn; - tex->id.us--; - *from= texn; - } + nr= pupmenu(str); -} - -void single_tex_users_expand() -{ - /* only when 'parent' blocks are LIB_NEW */ - Material *ma; - Lamp *la; - World *wo; - int b; - - ma= G.main->mat.first; - while(ma) { - if(ma->id.flag & LIB_NEW) { - for(b=0; bmtex[b] && ma->mtex[b]->tex) { - do_single_tex_user( &(ma->mtex[b]->tex) ); + if ( nr==1 || nr==2 ) { + for(base= FIRSTBASE; base; base= base->next) { + if((base != BASACT) &&(TESTBASELIB(v3d, base))) { + if (nr==1) { /* replace */ + copy_properties( &base->object->prop, &ob->prop ); + } else { + for(prop = ob->prop.first; prop; prop= prop->next ) { + set_ob_property(base->object, prop); + } } } } - ma= ma->id.next; - } - - la= G.main->lamp.first; - while(la) { - if(la->id.flag & LIB_NEW) { - for(b=0; bmtex[b] && la->mtex[b]->tex) { - do_single_tex_user( &(la->mtex[b]->tex) ); + } else if(nr>0) { + prop = BLI_findlink(&ob->prop, nr-4); /* account for first 3 menu items & menu index starting at 1*/ + + if(prop) { + for(base= FIRSTBASE; base; base= base->next) { + if((base != BASACT) &&(TESTBASELIB(v3d, base))) { + set_ob_property(base->object, prop); } } } - la= la->id.next; } - wo= G.main->world.first; - while(wo) { - if(wo->id.flag & LIB_NEW) { - for(b=0; bmtex[b] && wo->mtex[b]->tex) { - do_single_tex_user( &(wo->mtex[b]->tex) ); - } + MEM_freeN(str); + +} + +static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob) +{ + Base *base; + + for(base= FIRSTBASE; base; base= base->next) { + if(base->object != ob) { + if(TESTBASELIB(v3d, base)) { + + /* first: free all logic */ + free_sensors(&base->object->sensors); + unlink_controllers(&base->object->controllers); + free_controllers(&base->object->controllers); + unlink_actuators(&base->object->actuators); + free_actuators(&base->object->actuators); + + /* now copy it, this also works without logicbricks! */ + clear_sca_new_poins_ob(ob); + copy_sensors(&base->object->sensors, &ob->sensors); + copy_controllers(&base->object->controllers, &ob->controllers); + copy_actuators(&base->object->actuators, &ob->actuators); + set_sca_new_poins_ob(base->object); + + /* some menu settings */ + base->object->scavisflag= ob->scavisflag; + base->object->scaflag= ob->scaflag; + + /* set the initial state */ + base->object->state= ob->state; + base->object->init_state= ob->init_state; } } - wo= wo->id.next; } } -void single_mat_users_expand(void) +static void copymenu_modifiers(Scene *scene, View3D *v3d, Object *ob) { - /* only when 'parent' blocks are LIB_NEW */ + Base *base; + int i, event; + char str[512]; + char *errorstr= NULL; - Object *ob; - Mesh *me; - Curve *cu; - MetaBall *mb; - Material *ma; - int a; - - ob= G.main->object.first; - while(ob) { - if(ob->id.flag & LIB_NEW) { - new_id_matar(ob->mat, ob->totcol); - } - ob= ob->id.next; - } + strcpy(str, "Copy Modifiers %t"); - me= G.main->mesh.first; - while(me) { - if(me->id.flag & LIB_NEW) { - new_id_matar(me->mat, me->totcol); - } - me= me->id.next; - } + sprintf(str+strlen(str), "|All%%x%d|%%l", NUM_MODIFIER_TYPES); - cu= G.main->curve.first; - while(cu) { - if(cu->id.flag & LIB_NEW) { - new_id_matar(cu->mat, cu->totcol); - } - cu= cu->id.next; - } + for (i=eModifierType_None+1; imball.first; - while(mb) { - if(mb->id.flag & LIB_NEW) { - new_id_matar(mb->mat, mb->totcol); - } - mb= mb->id.next; - } + if(ELEM3(i, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance)) continue; + + if(i == eModifierType_Collision) + continue; - /* material imats */ - ma= G.main->mat.first; - while(ma) { - if(ma->id.flag & LIB_NEW) { - for(a=0; amtex[a]) { - ID_NEW(ma->mtex[a]->object); - } - } + if ( (mti->flags&eModifierTypeFlag_AcceptsCVs) || + (ob->type==OB_MESH && (mti->flags&eModifierTypeFlag_AcceptsMesh))) { + sprintf(str+strlen(str), "|%s%%x%d", mti->name, i); } - ma= ma->id.next; } -} -void single_user(Scene *scene, View3D *v3d) -{ - int nr; - - if(scene->id.lib) return; + event = pupmenu(str); + if(event<=0) return; - clear_id_newpoins(); - - nr= pupmenu("Make Single User%t|Object|Object & ObData|Object & ObData & Materials+Tex|Materials+Tex|Ipos"); - if(nr>0) { - - if(nr==1) single_object_users(scene, v3d, 1); - - else if(nr==2) { - single_object_users(scene, v3d, 1); - single_obdata_users(scene, 1); - } - else if(nr==3) { - single_object_users(scene, v3d, 1); - single_obdata_users(scene, 1); - single_mat_users(scene, 1); /* also tex */ - - } - else if(nr==4) { - single_mat_users(scene, 1); - } - else if(nr==5) { - single_ipo_users(scene, 1); + for (base= FIRSTBASE; base; base= base->next) { + if(base->object != ob) { + if(TESTBASELIB(v3d, base)) { + ModifierData *md; + + base->object->recalc |= OB_RECALC_OB|OB_RECALC_DATA; + + if (base->object->type==ob->type) { + /* copy all */ + if (event==NUM_MODIFIER_TYPES) { + object_free_modifiers(base->object); + + for (md=ob->modifiers.first; md; md=md->next) { + ModifierData *nmd = NULL; + + if(ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance)) continue; + + if(md->type == eModifierType_Collision) + continue; + + nmd = modifier_new(md->type); + modifier_copyData(md, nmd); + BLI_addtail(&base->object->modifiers, nmd); + } + + copy_object_particlesystems(base->object, ob); + copy_object_softbody(base->object, ob); + } else { + /* copy specific types */ + ModifierData *md, *mdn; + + /* remove all with type 'event' */ + for (md=base->object->modifiers.first; md; md=mdn) { + mdn= md->next; + if(md->type==event) { + BLI_remlink(&base->object->modifiers, md); + modifier_free(md); + } + } + + /* copy all with type 'event' */ + for (md=ob->modifiers.first; md; md=md->next) { + if (md->type==event) { + + mdn = modifier_new(event); + BLI_addtail(&base->object->modifiers, mdn); + + modifier_copyData(md, mdn); + } + } + + if(event == eModifierType_ParticleSystem) { + object_free_particlesystems(base->object); + copy_object_particlesystems(base->object, ob); + } + else if(event == eModifierType_Softbody) { + object_free_softbody(base->object); + copy_object_softbody(base->object, ob); + } + } + } + else + errorstr= "Did not copy modifiers to other Object types"; + } } - - - clear_id_newpoins(); - } + +// if(errorstr) notice(errorstr); + + DAG_scene_sort(scene); + } -/* used for copying scenes */ -void ED_object_single_users(Scene *scene, int full) -{ - single_object_users(scene, NULL, 0); - - if(full) { - single_obdata_users(scene, 0); - single_mat_users_expand(); - single_tex_users_expand(); - } - - clear_id_newpoins(); -} - -/* ************************************************************* */ - -/* helper for below, ma was checked to be not NULL */ -static void make_local_makelocalmaterial(Material *ma) +/* both pointers should exist */ +static void copy_texture_space(Object *to, Object *ob) { - //ID *id; - int b; - - make_local_material(ma); + float *poin1= NULL, *poin2= NULL; + int texflag= 0; - for(b=0; bmtex[b] && ma->mtex[b]->tex) { - make_local_texture(ma->mtex[b]->tex); - } + if(ob->type==OB_MESH) { + texflag= ((Mesh *)ob->data)->texflag; + poin2= ((Mesh *)ob->data)->loc; + } + else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) { + texflag= ((Curve *)ob->data)->texflag; + poin2= ((Curve *)ob->data)->loc; + } + else if(ob->type==OB_MBALL) { + texflag= ((MetaBall *)ob->data)->texflag; + poin2= ((MetaBall *)ob->data)->loc; + } + else + return; + + if(to->type==OB_MESH) { + ((Mesh *)to->data)->texflag= texflag; + poin1= ((Mesh *)to->data)->loc; + } + else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) { + ((Curve *)to->data)->texflag= texflag; + poin1= ((Curve *)to->data)->loc; + } + else if(to->type==OB_MBALL) { + ((MetaBall *)to->data)->texflag= texflag; + poin1= ((MetaBall *)to->data)->loc; } + else + return; -#if 0 // XXX old animation system - id= (ID *)ma->ipo; - if(id && id->lib) make_local_ipo(ma->ipo); -#endif // XXX old animation system + memcpy(poin1, poin2, 9*sizeof(float)); /* this was noted in DNA_mesh, curve, mball */ + + if(to->type==OB_MESH) ; + else if(to->type==OB_MBALL) tex_space_mball(to); + else tex_space_curve(to->data); - /* nodetree? XXX */ } -void make_local(Scene *scene, View3D *v3d, int mode) +void copy_attr(Scene *scene, View3D *v3d, short event) { - Base *base; Object *ob; - //bActionStrip *strip; - ParticleSystem *psys; - Material *ma, ***matarar; - Lamp *la; - Curve *cu; - ID *id; - int a, b; + Base *base; + Curve *cu, *cu1; + Nurb *nu; + int do_scene_sort= 0; - /* WATCH: the function new_id(..) re-inserts the id block!!! */ if(scene->id.lib) return; + + if(!(ob=OBACT)) return; - if(mode==3) { - all_local(NULL, 0); /* NULL is all libs */ + if(scene->obedit) { // XXX get from context + /* obedit_copymenu(); */ return; } - else if(mode<1) return; - - clear_id_newpoins(); - - for(base= FIRSTBASE; base; base= base->next) { - if( TESTBASE(v3d, base) ) { - ob= base->object; - if(ob->id.lib) { - make_local_object(ob); - } - } + if(event==9) { + copymenu_properties(scene, v3d, ob); + return; } - - /* maybe object pointers */ - for(base= FIRSTBASE; base; base= base->next) { - if( TESTBASE(v3d, base) ) { - ob= base->object; - if(ob->id.lib==NULL) { - ID_NEW(ob->parent); - ID_NEW(ob->track); - } - } + else if(event==10) { + copymenu_logicbricks(scene, v3d, ob); + return; + } + else if(event==24) { + copymenu_modifiers(scene, v3d, ob); + return; } for(base= FIRSTBASE; base; base= base->next) { - if( TESTBASE(v3d, base) ) { - ob= base->object; - id= ob->data; - - if(id && mode>1) { + if(base != BASACT) { + if(TESTBASELIB(v3d, base)) { + base->object->recalc |= OB_RECALC_OB; - switch(ob->type) { - case OB_LAMP: - make_local_lamp((Lamp *)id); - - la= ob->data; -#if 0 // XXX old animation system - id= (ID *)la->ipo; - if(id && id->lib) make_local_ipo(la->ipo); -#endif // XXX old animation system - break; - case OB_CAMERA: - make_local_camera((Camera *)id); - break; - case OB_MESH: - make_local_mesh((Mesh *)id); - make_local_key( ((Mesh *)id)->key ); - break; - case OB_MBALL: - make_local_mball((MetaBall *)id); - break; - case OB_CURVE: - case OB_SURF: - case OB_FONT: - cu= (Curve *)id; - make_local_curve(cu); -#if 0 // XXX old animation system - id= (ID *)cu->ipo; - if(id && id->lib) make_local_ipo(cu->ipo); -#endif // XXX old animation system - make_local_key( cu->key ); - break; - case OB_LATTICE: - make_local_lattice((Lattice *)id); - make_local_key( ((Lattice *)id)->key ); - break; - case OB_ARMATURE: - make_local_armature ((bArmature *)id); - break; + if(event==1) { /* loc */ + VECCOPY(base->object->loc, ob->loc); + VECCOPY(base->object->dloc, ob->dloc); } - - for(psys=ob->particlesystem.first; psys; psys=psys->next) - make_local_particlesettings(psys->part); - } - -#if 0 // XXX old animation system - id= (ID *)ob->ipo; - if(id && id->lib) make_local_ipo(ob->ipo); - - id= (ID *)ob->action; - if(id && id->lib) make_local_action(ob->action); - - for(strip=ob->nlastrips.first; strip; strip=strip->next) { - if(strip->act && strip->act->id.lib) - make_local_action(strip->act); - } -#endif // XXX old animation system - } - } - - if(mode>1) { - for(base= FIRSTBASE; base; base= base->next) { - if( TESTBASE(v3d, base) ) { - ob= base->object; - if(ob->type==OB_LAMP) { - la= ob->data; - for(b=0; bmtex[b] && la->mtex[b]->tex) { - make_local_texture(la->mtex[b]->tex); - } - } + else if(event==2) { /* rot */ + VECCOPY(base->object->rot, ob->rot); + VECCOPY(base->object->drot, ob->drot); + /* Quats arnt used yet */ + /*VECCOPY(base->object->quat, ob->quat); + VECCOPY(base->object->dquat, ob->dquat);*/ } - else { - - for(a=0; atotcol; a++) { - ma= ob->mat[a]; - if(ma) - make_local_makelocalmaterial(ma); - } - - matarar= (Material ***)give_matarar(ob); - if (matarar) { - for(a=0; atotcol; a++) { - ma= (*matarar)[a]; - if(ma) - make_local_makelocalmaterial(ma); - } - } + else if(event==3) { /* size */ + VECCOPY(base->object->size, ob->size); + VECCOPY(base->object->dsize, ob->dsize); } - } - } - } - -} - -void make_local_menu(Scene *scene, View3D *v3d) -{ - int mode; - - /* If you modify this menu, please remember to update view3d_edit_object_makelocalmenu - * in header_view3d.c and the menu in toolbox.c - */ - - if(scene->id.lib) return; - - mode = pupmenu("Make Local%t|Selected Objects %x1|Selected Objects and Data %x2|All %x3"); - - if (mode <= 0) return; - - make_local(scene, v3d, mode); -} - -/* ************************ ADD DUPLICATE ******************** */ - -/* - dupflag: a flag made from constants declared in DNA_userdef_types.h - The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data. - U.dupflag for default operations or you can construct a flag as python does - if the dupflag is 0 then no data will be copied (linked duplicate) */ + else if(event==4) { /* drawtype */ + base->object->dt= ob->dt; + base->object->dtx= ob->dtx; + base->object->empty_drawtype= ob->empty_drawtype; + base->object->empty_drawsize= ob->empty_drawsize; + } + else if(event==5) { /* time offs */ + base->object->sf= ob->sf; + } + else if(event==6) { /* dupli */ + base->object->dupon= ob->dupon; + base->object->dupoff= ob->dupoff; + base->object->dupsta= ob->dupsta; + base->object->dupend= ob->dupend; + + base->object->transflag &= ~OB_DUPLI; + base->object->transflag |= (ob->transflag & OB_DUPLI); -/* used below, assumes id.new is correct */ -/* leaves selection of base/object unaltered */ -static Base *object_add_duplicate_internal(Scene *scene, Base *base, int dupflag) -{ - Base *basen= NULL; - Material ***matarar; - Object *ob, *obn; - ID *id; - int a, didit; - - ob= base->object; - if(ob->mode & OB_MODE_POSE) { - ; /* nothing? */ - } - else { - obn= copy_object(ob); - obn->recalc |= OB_RECALC; - - basen= MEM_mallocN(sizeof(Base), "duplibase"); - *basen= *base; - BLI_addhead(&scene->base, basen); /* addhead: prevent eternal loop */ - basen->object= obn; - - if(basen->flag & OB_FROMGROUP) { - Group *group; - for(group= G.main->group.first; group; group= group->id.next) { - if(object_in_group(ob, group)) - add_to_group(group, obn); - } - obn->flag |= OB_FROMGROUP; /* this flag is unset with copy_object() */ - } - - /* duplicates using userflags */ -#if 0 // XXX old animation system - if(dupflag & USER_DUP_IPO) { - bConstraintChannel *chan; - id= (ID *)obn->ipo; - - if(id) { - ID_NEW_US( obn->ipo) - else obn->ipo= copy_ipo(obn->ipo); - id->us--; - } - /* Handle constraint ipos */ - for (chan=obn->constraintChannels.first; chan; chan=chan->next){ - id= (ID *)chan->ipo; - if(id) { - ID_NEW_US( chan->ipo) - else chan->ipo= copy_ipo(chan->ipo); - id->us--; + base->object->dup_group= ob->dup_group; + if(ob->dup_group) + id_us_plus((ID *)ob->dup_group); } - } - } - if(dupflag & USER_DUP_ACT){ /* Not buttons in the UI to modify this, add later? */ - id= (ID *)obn->action; - if (id){ - ID_NEW_US(obn->action) - else{ - obn->action= copy_action(obn->action); + else if(event==7) { /* mass */ + base->object->mass= ob->mass; } - id->us--; - } - } -#endif // XXX old animation system - if(dupflag & USER_DUP_MAT) { - for(a=0; atotcol; a++) { - id= (ID *)obn->mat[a]; - if(id) { - ID_NEW_US(obn->mat[a]) - else obn->mat[a]= copy_material(obn->mat[a]); - id->us--; + else if(event==8) { /* damping */ + base->object->damping= ob->damping; + base->object->rdamping= ob->rdamping; } - } - } - if(dupflag & USER_DUP_PSYS) { - ParticleSystem *psys; - for(psys=obn->particlesystem.first; psys; psys=psys->next) { - id= (ID*) psys->part; - if(id) { - ID_NEW_US(psys->part) - else psys->part= psys_copy_settings(psys->part); - id->us--; + else if(event==11) { /* all physical attributes */ + base->object->gameflag = ob->gameflag; + base->object->inertia = ob->inertia; + base->object->formfactor = ob->formfactor; + base->object->damping= ob->damping; + base->object->rdamping= ob->rdamping; + base->object->min_vel= ob->min_vel; + base->object->max_vel= ob->max_vel; + if (ob->gameflag & OB_BOUNDS) { + base->object->boundtype = ob->boundtype; + } + base->object->margin= ob->margin; + base->object->bsoft= copy_bulletsoftbody(ob->bsoft); + } - } - } - - id= obn->data; - didit= 0; - - switch(obn->type) { - case OB_MESH: - if(dupflag & USER_DUP_MESH) { - ID_NEW_US2( obn->data ) - else { - obn->data= copy_mesh(obn->data); + else if(event==17) { /* tex space */ + copy_texture_space(base->object, ob); + } + else if(event==18) { /* font settings */ + + if(base->object->type==ob->type) { + cu= ob->data; + cu1= base->object->data; + + cu1->spacemode= cu->spacemode; + cu1->spacing= cu->spacing; + cu1->linedist= cu->linedist; + cu1->shear= cu->shear; + cu1->fsize= cu->fsize; + cu1->xof= cu->xof; + cu1->yof= cu->yof; + cu1->textoncurve= cu->textoncurve; + cu1->wordspace= cu->wordspace; + cu1->ulpos= cu->ulpos; + cu1->ulheight= cu->ulheight; + if(cu1->vfont) cu1->vfont->id.us--; + cu1->vfont= cu->vfont; + id_us_plus((ID *)cu1->vfont); + if(cu1->vfontb) cu1->vfontb->id.us--; + cu1->vfontb= cu->vfontb; + id_us_plus((ID *)cu1->vfontb); + if(cu1->vfonti) cu1->vfonti->id.us--; + cu1->vfonti= cu->vfonti; + id_us_plus((ID *)cu1->vfonti); + if(cu1->vfontbi) cu1->vfontbi->id.us--; + cu1->vfontbi= cu->vfontbi; + id_us_plus((ID *)cu1->vfontbi); + + BKE_text_to_curve(scene, base->object, 0); /* needed? */ + + + strcpy(cu1->family, cu->family); + + base->object->recalc |= OB_RECALC_DATA; + } + } + else if(event==19) { /* bevel settings */ + + if(ELEM(base->object->type, OB_CURVE, OB_FONT)) { + cu= ob->data; + cu1= base->object->data; - if(obn->fluidsimSettings) { - obn->fluidsimSettings->orgMesh = (Mesh *)obn->data; - } + cu1->bevobj= cu->bevobj; + cu1->taperobj= cu->taperobj; + cu1->width= cu->width; + cu1->bevresol= cu->bevresol; + cu1->ext1= cu->ext1; + cu1->ext2= cu->ext2; - didit= 1; + base->object->recalc |= OB_RECALC_DATA; } - id->us--; } - break; - case OB_CURVE: - if(dupflag & USER_DUP_CURVE) { - ID_NEW_US2(obn->data ) - else { - obn->data= copy_curve(obn->data); - didit= 1; + else if(event==25) { /* curve resolution */ + + if(ELEM(base->object->type, OB_CURVE, OB_FONT)) { + cu= ob->data; + cu1= base->object->data; + + cu1->resolu= cu->resolu; + cu1->resolu_ren= cu->resolu_ren; + + nu= cu1->nurb.first; + + while(nu) { + nu->resolu= cu1->resolu; + nu= nu->next; + } + + base->object->recalc |= OB_RECALC_DATA; } - id->us--; } - break; - case OB_SURF: - if(dupflag & USER_DUP_SURF) { - ID_NEW_US2( obn->data ) - else { - obn->data= copy_curve(obn->data); - didit= 1; + else if(event==21){ + if (base->object->type==OB_MESH) { + ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf); + + if (md) { + ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf); + + if (!tmd) { + tmd = modifier_new(eModifierType_Subsurf); + BLI_addtail(&base->object->modifiers, tmd); + } + + modifier_copyData(md, tmd); + base->object->recalc |= OB_RECALC_DATA; + } } - id->us--; } - break; - case OB_FONT: - if(dupflag & USER_DUP_FONT) { - ID_NEW_US2( obn->data ) - else { - obn->data= copy_curve(obn->data); - didit= 1; - } - id->us--; + else if(event==22) { + /* Copy the constraint channels over */ + copy_constraints(&base->object->constraints, &ob->constraints); + + do_scene_sort= 1; } - break; - case OB_MBALL: - if(dupflag & USER_DUP_MBALL) { - ID_NEW_US2(obn->data ) - else { - obn->data= copy_mball(obn->data); - didit= 1; + else if(event==23) { + base->object->softflag= ob->softflag; + if(base->object->soft) sbFree(base->object->soft); + + base->object->soft= copy_softbody(ob->soft); + + if (!modifiers_findByType(base->object, eModifierType_Softbody)) { + BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody)); } - id->us--; } - break; - case OB_LAMP: - if(dupflag & USER_DUP_LAMP) { - ID_NEW_US2(obn->data ) - else obn->data= copy_lamp(obn->data); - id->us--; + else if(event==26) { +#if 0 // XXX old animation system + copy_nlastrips(&base->object->nlastrips, &ob->nlastrips); +#endif // XXX old animation system } - break; - - case OB_ARMATURE: - obn->recalc |= OB_RECALC_DATA; - if(obn->pose) obn->pose->flag |= POSE_RECALC; - - if(dupflag & USER_DUP_ARM) { - ID_NEW_US2(obn->data ) - else { - obn->data= copy_armature(obn->data); - armature_rebuild_pose(obn, obn->data); - didit= 1; - } - id->us--; + else if(event==27) { /* autosmooth */ + if (base->object->type==OB_MESH) { + Mesh *me= ob->data; + Mesh *cme= base->object->data; + cme->smoothresh= me->smoothresh; + if(me->flag & ME_AUTOSMOOTH) + cme->flag |= ME_AUTOSMOOTH; + else + cme->flag &= ~ME_AUTOSMOOTH; } + } + else if(event==28) { /* UV orco */ + if(ELEM(base->object->type, OB_CURVE, OB_SURF)) { + cu= ob->data; + cu1= base->object->data; - break; - - case OB_LATTICE: - if(dupflag!=0) { - ID_NEW_US2(obn->data ) - else obn->data= copy_lattice(obn->data); - id->us--; + if(cu->flag & CU_UV_ORCO) + cu1->flag |= CU_UV_ORCO; + else + cu1->flag &= ~CU_UV_ORCO; + } } - break; - case OB_CAMERA: - if(dupflag!=0) { - ID_NEW_US2(obn->data ) - else obn->data= copy_camera(obn->data); - id->us--; + else if(event==29) { /* protected bits */ + base->object->protectflag= ob->protectflag; } - break; - } - - if(dupflag & USER_DUP_MAT) { - matarar= give_matarar(obn); - if(didit && matarar) { - for(a=0; atotcol; a++) { - id= (ID *)(*matarar)[a]; - if(id) { - ID_NEW_US( (*matarar)[a] ) - else (*matarar)[a]= copy_material((*matarar)[a]); - - id->us--; - } + else if(event==30) { /* index object */ + base->object->index= ob->index; + } + else if(event==31) { /* object color */ + QUATCOPY(base->object->col, ob->col); } } } } - return basen; -} + + if(do_scene_sort) + DAG_scene_sort(scene); -/* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */ -/* leaves selection of base/object unaltered */ -Base *ED_object_add_duplicate(Scene *scene, Base *base, int dupflag) -{ - Base *basen; + ED_anim_dag_flush_update(C); - clear_id_newpoins(); - clear_sca_new_poins(); /* sensor/contr/act */ - - basen= object_add_duplicate_internal(scene, base, dupflag); - - DAG_scene_sort(scene); - - return basen; } -/* contextual operator dupli */ -static int duplicate_exec(bContext *C, wmOperator *op) +void copy_attr_menu(Scene *scene, View3D *v3d) { - Scene *scene= CTX_data_scene(C); - View3D *v3d= CTX_wm_view3d(C); - int linked= RNA_boolean_get(op->ptr, "linked"); - int dupflag= (linked)? 0: U.dupflag; + Object *ob; + short event; + char str[512]; - clear_id_newpoins(); - clear_sca_new_poins(); /* sensor/contr/act */ + if(!(ob=OBACT)) return; - CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { - Base *basen= object_add_duplicate_internal(scene, base, dupflag); - - /* XXX context conflict maybe, itterator could solve this? */ - ED_base_object_select(base, BA_DESELECT); - /* new object becomes active */ - if(BASACT==base) - ED_base_object_activate(C, basen); - + if (scene->obedit) { // XXX get from context +// if (ob->type == OB_MESH) +// XXX mesh_copy_menu(); + return; } - CTX_DATA_END; - - /* XXX fix this for context */ - copy_object_set_idnew(scene, v3d, dupflag); - - DAG_scene_sort(scene); - ED_anim_dag_flush_update(C); - - WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene); - - return OPERATOR_FINISHED; -} - -static int duplicate_invoke(bContext *C, wmOperator *op, wmEvent *event) -{ - duplicate_exec(C, op); -// RNA_int_set(op->ptr, "mode", TFM_TRANSLATION); -// WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr); - - return OPERATOR_FINISHED; -} - -void OBJECT_OT_duplicate(wmOperatorType *ot) -{ + /* Object Mode */ - /* identifiers */ - ot->name= "Duplicate"; - ot->description = "Duplicate selected objects."; - ot->idname= "OBJECT_OT_duplicate"; + /* If you change this menu, don't forget to update the menu in header_view3d.c + * view3d_edit_object_copyattrmenu() and in toolbox.c + */ - /* api callbacks */ - ot->invoke= duplicate_invoke; - ot->exec= duplicate_exec; + strcpy(str, "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Draw Options%x4|Time Offset%x5|Dupli%x6|Object Color%x31|%l|Mass%x7|Damping%x8|All Physical Attributes%x11|Properties%x9|Logic Bricks%x10|Protected Transform%x29|%l"); - ot->poll= ED_operator_scene_editable; + strcat (str, "|Object Constraints%x22"); + strcat (str, "|NLA Strips%x26"); - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +// XXX if (OB_SUPPORT_MATERIAL(ob)) { +// strcat(str, "|Texture Space%x17"); +// } - /* to give to transform */ - RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data."); - RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX); -} - -/* ************************** JOIN *********************** */ - -static int join_exec(bContext *C, wmOperator *op) -{ - Scene *scene= CTX_data_scene(C); - Object *ob= CTX_data_active_object(C); - - if(scene->obedit) { - BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode."); - return OPERATOR_CANCELLED; - } - else if(!ob) { - BKE_report(op->reports, RPT_ERROR, "Can't join unless there is an active object."); - return OPERATOR_CANCELLED; - } - else if(object_data_is_libdata(ob)) { - BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata."); - return OPERATOR_CANCELLED; + if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19"); + if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28"); + + if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) { + strcat(str, "|Curve Resolution%x25"); } - if(ob->type == OB_MESH) - return join_mesh_exec(C, op); - else if(ELEM(ob->type, OB_CURVE, OB_SURF)) - return join_curve_exec(C, op); - else if(ob->type == OB_ARMATURE) - return join_armature_exec(C, op); - - BKE_report(op->reports, RPT_ERROR, "This object type doesn't support joining."); - - return OPERATOR_CANCELLED; -} + if(ob->type==OB_MESH){ + strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27"); + } -void OBJECT_OT_join(wmOperatorType *ot) -{ - /* identifiers */ - ot->name= "Join"; - ot->description = "Join selected objects into active object."; - ot->idname= "OBJECT_OT_join"; + if(ob->soft) strcat(str, "|Soft Body Settings%x23"); - /* api callbacks */ - ot->exec= join_exec; - ot->poll= ED_operator_scene_editable; + strcat(str, "|Pass Index%x30"); - /* flags */ - ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){ + strcat(str, "|Modifiers ...%x24"); + } + + event= pupmenu(str); + if(event<= 0) return; + + copy_attr(scene, v3d, event); } /********************** Smooth/Flat *********************/ @@ -7095,124 +1890,6 @@ void rand_timeoffs(Scene *scene, View3D *v3d) } - -void texspace_edit(Scene *scene, View3D *v3d) -{ - Base *base; - int nr=0; - - /* first test if from visible and selected objects - * texspacedraw is set: - */ - - if(scene->obedit) return; // XXX get from context - - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - break; - } - } - - if(base==0) { - return; - } - - nr= pupmenu("Texture Space %t|Grab/Move%x1|Size%x2"); - if(nr<1) return; - - for(base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - base->object->dtx |= OB_TEXSPACE; - } - } - - - if(nr==1) { -// XXX initTransform(TFM_TRANSLATION, CTX_TEXTURE); -// XXX Transform(); - } - else if(nr==2) { -// XXX initTransform(TFM_RESIZE, CTX_TEXTURE); -// XXX Transform(); - } - else if(nr==3) { -// XXX initTransform(TFM_ROTATION, CTX_TEXTURE); -// XXX Transform(); - } -} - -/* ******************************************************************** */ -/* Mirror function in Edit Mode */ - -void mirrormenu(void) -{ -// XXX initTransform(TFM_MIRROR, CTX_NO_PET); -// XXX Transform(); -} - -void hookmenu(Scene *scene, View3D *v3d) -{ - /* only called in object mode */ - short event, changed=0; - Object *ob; - Base *base; - ModifierData *md; - HookModifierData *hmd; - - event= pupmenu("Modify Hooks for Selected...%t|Reset Offset%x1|Recenter at Cursor%x2"); - if (event==-1) return; - if (event==2 && !(v3d)) { - error("Cannot perform this operation without a 3d view"); - return; - } - - for (base= FIRSTBASE; base; base= base->next) { - if(TESTBASELIB(v3d, base)) { - for (md = base->object->modifiers.first; md; md=md->next) { - if (md->type==eModifierType_Hook) { - ob = base->object; - hmd = (HookModifierData*) md; - - /* - * Copied from modifiers_cursorHookCenter and - * modifiers_clearHookOffset, should consolidate - * */ - - if (event==1) { - if(hmd->object) { - Mat4Invert(hmd->object->imat, hmd->object->obmat); - Mat4MulSerie(hmd->parentinv, hmd->object->imat, ob->obmat, NULL, NULL, NULL, NULL, NULL, NULL); - - changed= 1; - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - } - } else { - float *curs = give_cursor(scene, v3d); - float bmat[3][3], imat[3][3]; - - where_is_object(scene, ob); - - Mat3CpyMat4(bmat, ob->obmat); - Mat3Inv(imat, bmat); - - curs= give_cursor(scene, v3d); - hmd->cent[0]= curs[0]-ob->obmat[3][0]; - hmd->cent[1]= curs[1]-ob->obmat[3][1]; - hmd->cent[2]= curs[2]-ob->obmat[3][2]; - Mat3MulVecfl(imat, hmd->cent); - - changed= 1; - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - } - } - } - } - } - - if (changed) { - } -} - static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *ptr, int *free) { EnumPropertyItem *input = object_mode_items; @@ -7373,7 +2050,7 @@ void ED_object_toggle_modes(bContext *C, int mode) WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL); } -/* game property ops */ +/************************ Game Properties ***********************/ static int game_property_new(bContext *C, wmOperator *op) { @@ -7441,3 +2118,4 @@ void OBJECT_OT_game_property_remove(wmOperatorType *ot) RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX); } + diff --git a/source/blender/editors/object/object_group.c b/source/blender/editors/object/object_group.c new file mode 100644 index 00000000000..c83f3022c7c --- /dev/null +++ b/source/blender/editors/object/object_group.c @@ -0,0 +1,364 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) Blender Foundation + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include + +#include "MEM_guardedalloc.h" + +#include "BLI_blenlib.h" + +#include "DNA_group_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" + +#include "BKE_context.h" +#include "BKE_depsgraph.h" +#include "BKE_global.h" +#include "BKE_group.h" +#include "BKE_main.h" +#include "BKE_report.h" +#include "BKE_scene.h" + +#include "ED_screen.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "object_intern.h" + +/********************* 3d view operators ***********************/ + +static int objects_add_active_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Object *ob= OBACT, *obt; + Group *group; + int ok = 0; + + if(!ob) return OPERATOR_CANCELLED; + + /* linking to same group requires its own loop so we can avoid + looking up the active objects groups each time */ + + for(group= G.main->group.first; group; group=group->id.next) { + if(object_in_group(ob, group)) { + /* Assign groups to selected objects */ + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + obt= base->object; + add_to_group(group, obt); + obt->flag |= OB_FROMGROUP; + base->flag |= OB_FROMGROUP; + base->object->recalc= OB_RECALC_OB; + ok = 1; + } + CTX_DATA_END; + } + } + + if(!ok) BKE_report(op->reports, RPT_ERROR, "Active Object contains no groups"); + + DAG_scene_sort(scene); + WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GROUP_OT_objects_add_active(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Selected To Active Group"; + ot->description = "Add the object to an object group that contains the active object."; + ot->idname= "GROUP_OT_objects_add_active"; + + /* api callbacks */ + ot->exec= objects_add_active_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int objects_remove_active_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Object *ob= OBACT, *obt; + Group *group; + int ok = 0; + + if(!ob) return OPERATOR_CANCELLED; + + /* linking to same group requires its own loop so we can avoid + looking up the active objects groups each time */ + + for(group= G.main->group.first; group; group=group->id.next) { + if(object_in_group(ob, group)) { + /* Assign groups to selected objects */ + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + obt= base->object; + rem_from_group(group, obt); + obt->flag &= ~OB_FROMGROUP; + base->flag &= ~OB_FROMGROUP; + base->object->recalc= OB_RECALC_OB; + ok = 1; + } + CTX_DATA_END; + } + } + + if(!ok) BKE_report(op->reports, RPT_ERROR, "Active Object contains no groups"); + + DAG_scene_sort(scene); + WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GROUP_OT_objects_remove_active(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Remove Selected From Active Group"; + ot->description = "Remove the object from an object group that contains the active object."; + ot->idname= "GROUP_OT_objects_remove_active"; + + /* api callbacks */ + ot->exec= objects_remove_active_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int group_objects_remove_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Group *group= NULL; + + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + group = NULL; + while((group = find_group(base->object, group))) + rem_from_group(group, base->object); + + base->object->flag &= ~OB_FROMGROUP; + base->flag &= ~OB_FROMGROUP; + base->object->recalc= OB_RECALC_OB; + } + CTX_DATA_END; + + DAG_scene_sort(scene); + WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GROUP_OT_objects_remove(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Remove From Groups"; + ot->description = "Remove selected objects from all groups."; + ot->idname= "GROUP_OT_objects_remove"; + + /* api callbacks */ + ot->exec= group_objects_remove_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int group_create_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Group *group= NULL; + char gid[32]; //group id + + RNA_string_get(op->ptr, "GID", gid); + + group= add_group(gid); + + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + add_to_group(group, base->object); + base->object->flag |= OB_FROMGROUP; + base->flag |= OB_FROMGROUP; + base->object->recalc= OB_RECALC_OB; + } + CTX_DATA_END; + + DAG_scene_sort(scene); + WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GROUP_OT_group_create(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Create New Group"; + ot->description = "Create an object group."; + ot->idname= "GROUP_OT_group_create"; + + /* api callbacks */ + ot->exec= group_create_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_string(ot->srna, "GID", "Group", 32, "Name", "Name of the new group"); +} + +/****************** properties window 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; +} + diff --git a/source/blender/editors/object/object_hook.c b/source/blender/editors/object/object_hook.c new file mode 100644 index 00000000000..ab7bcbc989d --- /dev/null +++ b/source/blender/editors/object/object_hook.c @@ -0,0 +1,608 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * Contributor(s): Blender Foundation, 2002-2008 full recode + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include +#include + +#include "MEM_guardedalloc.h" + +#include "BLI_arithb.h" +#include "BLI_editVert.h" +#include "BLI_listbase.h" +#include "BLI_string.h" + +#include "DNA_curve_types.h" +#include "DNA_lattice_types.h" +#include "DNA_mesh_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_modifier_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" +#include "DNA_view3d_types.h" + +#include "BKE_customdata.h" +#include "BKE_depsgraph.h" +#include "BKE_mesh.h" +#include "BKE_modifier.h" +#include "BKE_object.h" + +#include "ED_curve.h" +#include "ED_mesh.h" +#include "ED_object.h" +#include "ED_view3d.h" + +#include "object_intern.h" + +/* XXX operators for this are not implemented yet */ + +static int return_editmesh_indexar(EditMesh *em, int *tot, int **indexar, float *cent) +{ + EditVert *eve; + int *index, nr, totvert=0; + + for(eve= em->verts.first; eve; eve= eve->next) { + if(eve->f & SELECT) totvert++; + } + if(totvert==0) return 0; + + *indexar= index= MEM_mallocN(4*totvert, "hook indexar"); + *tot= totvert; + nr= 0; + cent[0]= cent[1]= cent[2]= 0.0; + + for(eve= em->verts.first; eve; eve= eve->next) { + if(eve->f & SELECT) { + *index= nr; index++; + VecAddf(cent, cent, eve->co); + } + nr++; + } + + VecMulf(cent, 1.0f/(float)totvert); + + return totvert; +} + +static int return_editmesh_vgroup(Object *obedit, EditMesh *em, char *name, float *cent) +{ + MDeformVert *dvert; + EditVert *eve; + int i, totvert=0; + + cent[0]= cent[1]= cent[2]= 0.0; + + if(obedit->actdef) { + + /* find the vertices */ + for(eve= em->verts.first; eve; eve= eve->next) { + dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT); + + if(dvert) { + for(i=0; itotweight; i++){ + if(dvert->dw[i].def_nr == (obedit->actdef-1)) { + totvert++; + VecAddf(cent, cent, eve->co); + } + } + } + } + if(totvert) { + bDeformGroup *defGroup = BLI_findlink(&obedit->defbase, obedit->actdef-1); + strcpy(name, defGroup->name); + VecMulf(cent, 1.0f/(float)totvert); + return 1; + } + } + + return 0; +} + +static void select_editmesh_hook(Object *ob, HookModifierData *hmd) +{ + Mesh *me= ob->data; + EditMesh *em= BKE_mesh_get_editmesh(me); + EditVert *eve; + int index=0, nr=0; + + if (hmd->indexar == NULL) + return; + + for(eve= em->verts.first; eve; eve= eve->next, nr++) { + if(nr==hmd->indexar[index]) { + eve->f |= SELECT; + if(index < hmd->totindex-1) index++; + } + } + EM_select_flush(em); + + BKE_mesh_end_editmesh(me, em); +} + +static int return_editlattice_indexar(Lattice *editlatt, int *tot, int **indexar, float *cent) +{ + BPoint *bp; + int *index, nr, totvert=0, a; + + /* count */ + a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw; + bp= editlatt->def; + while(a--) { + if(bp->f1 & SELECT) { + if(bp->hide==0) totvert++; + } + bp++; + } + + if(totvert==0) return 0; + + *indexar= index= MEM_mallocN(4*totvert, "hook indexar"); + *tot= totvert; + nr= 0; + cent[0]= cent[1]= cent[2]= 0.0; + + a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw; + bp= editlatt->def; + while(a--) { + if(bp->f1 & SELECT) { + if(bp->hide==0) { + *index= nr; index++; + VecAddf(cent, cent, bp->vec); + } + } + bp++; + nr++; + } + + VecMulf(cent, 1.0f/(float)totvert); + + return totvert; +} + +static void select_editlattice_hook(Object *obedit, HookModifierData *hmd) +{ + Lattice *lt= obedit->data; + BPoint *bp; + int index=0, nr=0, a; + + /* count */ + a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; + bp= lt->editlatt->def; + while(a--) { + if(hmd->indexar[index]==nr) { + bp->f1 |= SELECT; + if(index < hmd->totindex-1) index++; + } + nr++; + bp++; + } +} + +static int return_editcurve_indexar(Object *obedit, int *tot, int **indexar, float *cent) +{ + ListBase *editnurb= curve_get_editcurve(obedit); + Nurb *nu; + BPoint *bp; + BezTriple *bezt; + int *index, a, nr, totvert=0; + + for(nu= editnurb->first; nu; nu= nu->next) { + if(nu->type == CU_BEZIER) { + bezt= nu->bezt; + a= nu->pntsu; + while(a--) { + if(bezt->f1 & SELECT) totvert++; + if(bezt->f2 & SELECT) totvert++; + if(bezt->f3 & SELECT) totvert++; + bezt++; + } + } + else { + bp= nu->bp; + a= nu->pntsu*nu->pntsv; + while(a--) { + if(bp->f1 & SELECT) totvert++; + bp++; + } + } + } + if(totvert==0) return 0; + + *indexar= index= MEM_mallocN(4*totvert, "hook indexar"); + *tot= totvert; + nr= 0; + cent[0]= cent[1]= cent[2]= 0.0; + + for(nu= editnurb->first; nu; nu= nu->next) { + if(nu->type == CU_BEZIER) { + bezt= nu->bezt; + a= nu->pntsu; + while(a--) { + if(bezt->f1 & SELECT) { + *index= nr; index++; + VecAddf(cent, cent, bezt->vec[0]); + } + nr++; + if(bezt->f2 & SELECT) { + *index= nr; index++; + VecAddf(cent, cent, bezt->vec[1]); + } + nr++; + if(bezt->f3 & SELECT) { + *index= nr; index++; + VecAddf(cent, cent, bezt->vec[2]); + } + nr++; + bezt++; + } + } + else { + bp= nu->bp; + a= nu->pntsu*nu->pntsv; + while(a--) { + if(bp->f1 & SELECT) { + *index= nr; index++; + VecAddf(cent, cent, bp->vec); + } + nr++; + bp++; + } + } + } + + VecMulf(cent, 1.0f/(float)totvert); + + return totvert; +} + +int object_hook_index_array(Object *obedit, int *tot, int **indexar, char *name, float *cent_r) +{ + *indexar= NULL; + *tot= 0; + name[0]= 0; + + switch(obedit->type) { + case OB_MESH: + { + Mesh *me= obedit->data; + EditMesh *em = BKE_mesh_get_editmesh(me); + + /* check selected vertices first */ + if( return_editmesh_indexar(em, tot, indexar, cent_r)) { + BKE_mesh_end_editmesh(me, em); + return 1; + } else { + int ret = return_editmesh_vgroup(obedit, em, name, cent_r); + BKE_mesh_end_editmesh(me, em); + return ret; + } + } + case OB_CURVE: + case OB_SURF: + return return_editcurve_indexar(obedit, tot, indexar, cent_r); + case OB_LATTICE: + { + Lattice *lt= obedit->data; + return return_editlattice_indexar(lt->editlatt, tot, indexar, cent_r); + } + default: + return 0; + } +} + +static void select_editcurve_hook(Object *obedit, HookModifierData *hmd) +{ + ListBase *editnurb= curve_get_editcurve(obedit); + Nurb *nu; + BPoint *bp; + BezTriple *bezt; + int index=0, a, nr=0; + + for(nu= editnurb->first; nu; nu= nu->next) { + if(nu->type == CU_BEZIER) { + bezt= nu->bezt; + a= nu->pntsu; + while(a--) { + if(nr == hmd->indexar[index]) { + bezt->f1 |= SELECT; + if(indextotindex-1) index++; + } + nr++; + if(nr == hmd->indexar[index]) { + bezt->f2 |= SELECT; + if(indextotindex-1) index++; + } + nr++; + if(nr == hmd->indexar[index]) { + bezt->f3 |= SELECT; + if(indextotindex-1) index++; + } + nr++; + + bezt++; + } + } + else { + bp= nu->bp; + a= nu->pntsu*nu->pntsv; + while(a--) { + if(nr == hmd->indexar[index]) { + bp->f1 |= SELECT; + if(indextotindex-1) index++; + } + nr++; + bp++; + } + } + } +} + +void object_hook_select(Object *ob, HookModifierData *hmd) +{ + if(ob->type==OB_MESH) select_editmesh_hook(ob, hmd); + else if(ob->type==OB_LATTICE) select_editlattice_hook(ob, hmd); + else if(ob->type==OB_CURVE) select_editcurve_hook(ob, hmd); + else if(ob->type==OB_SURF) select_editcurve_hook(ob, hmd); +} + + +void add_hook(Scene *scene, View3D *v3d, int mode) +{ + ModifierData *md = NULL; + HookModifierData *hmd = NULL; + Object *ob=NULL; + Object *obedit= scene->obedit; // XXX get from context + + if(obedit==NULL) return; + + /* preconditions */ + if(mode==2) { /* selected object */ + Base *base; + for(base= FIRSTBASE; base; base= base->next) { + if(TESTBASELIB(v3d, base)) { + if(base!=BASACT) { + ob= base->object; + break; + } + } + } + if(ob==NULL) { + // XXX error("Requires selected Object"); + return; + } + } + else if(mode!=1) { + int maxlen=0, a, nr; + char *cp; + + /* make pupmenu with hooks */ + for(md=obedit->modifiers.first; md; md= md->next) { + if (md->type==eModifierType_Hook) + maxlen+=32; + } + + if(maxlen==0) { + // XXX error("Object has no hooks yet"); + return; + } + + cp= MEM_callocN(maxlen+32, "temp string"); + if(mode==3) strcpy(cp, "Remove %t|"); + else if(mode==4) strcpy(cp, "Reassign %t|"); + else if(mode==5) strcpy(cp, "Select %t|"); + else if(mode==6) strcpy(cp, "Clear Offset %t|"); + + for(md=obedit->modifiers.first; md; md= md->next) { + if (md->type==eModifierType_Hook) { + strcat(cp, md->name); + strcat(cp, " |"); + } + } + + nr= 0; // XXX pupmenu(cp); + MEM_freeN(cp); + + if(nr<1) return; + + a= 1; + for(md=obedit->modifiers.first; md; md=md->next) { + if (md->type==eModifierType_Hook) { + if(a==nr) break; + a++; + } + } + + hmd = (HookModifierData*) md; + ob= hmd->object; + } + + /* do it, new hooks or reassign */ + if(mode==1 || mode==2 || mode==4) { + float cent[3]; + int tot, ok, *indexar; + char name[32]; + + ok = object_hook_index_array(obedit, &tot, &indexar, name, cent); + + if(ok==0) { + // XXX error("Requires selected vertices or active Vertex Group"); + } + else { + + if(mode==1) { + Base *base= BASACT, *newbase; + + ob= add_object(scene, OB_EMPTY); + /* set layers OK */ + newbase= BASACT; + newbase->lay= base->lay; + ob->lay= newbase->lay; + + /* transform cent to global coords for loc */ + VecMat4MulVecfl(ob->loc, obedit->obmat, cent); + + /* restore, add_object sets active */ + BASACT= base; + } + /* if mode is 2 or 4, ob has been set */ + + /* new hook */ + if(mode==1 || mode==2) { + ModifierData *md = obedit->modifiers.first; + + while (md && modifierType_getInfo(md->type)->type==eModifierTypeType_OnlyDeform) { + md = md->next; + } + + hmd = (HookModifierData*) modifier_new(eModifierType_Hook); + BLI_insertlinkbefore(&obedit->modifiers, md, hmd); + sprintf(hmd->modifier.name, "Hook-%s", ob->id.name+2); + } + else if (hmd->indexar) MEM_freeN(hmd->indexar); /* reassign, hook was set */ + + hmd->object= ob; + hmd->indexar= indexar; + VecCopyf(hmd->cent, cent); + hmd->totindex= tot; + BLI_strncpy(hmd->name, name, 32); + + // TODO: need to take into account bone targets here too now... + if(mode==1 || mode==2) { + /* matrix calculus */ + /* vert x (obmat x hook->imat) x hook->obmat x ob->imat */ + /* (parentinv ) */ + + where_is_object(scene, ob); + + Mat4Invert(ob->imat, ob->obmat); + /* apparently this call goes from right to left... */ + Mat4MulSerie(hmd->parentinv, ob->imat, obedit->obmat, NULL, + NULL, NULL, NULL, NULL, NULL); + } + } + } + else if(mode==3) { /* remove */ + BLI_remlink(&obedit->modifiers, md); + modifier_free(md); + } + else if(mode==5) { /* select */ + // FIXME: this is now OBJECT_OT_hook_select + object_hook_select(obedit, hmd); + } + else if(mode==6) { /* clear offset */ + // FIXME: this is now OBJECT_OT_hook_reset operator + where_is_object(scene, ob); /* ob is hook->parent */ + + Mat4Invert(ob->imat, ob->obmat); + /* this call goes from right to left... */ + Mat4MulSerie(hmd->parentinv, ob->imat, obedit->obmat, NULL, + NULL, NULL, NULL, NULL, NULL); + } + + DAG_scene_sort(scene); +} + +void add_hook_menu(Scene *scene, View3D *v3d) +{ + Object *obedit= scene->obedit; // XXX get from context + int mode; + + if(obedit==NULL) return; + + if(modifiers_findByType(obedit, eModifierType_Hook)) + mode= 0; // XXX pupmenu("Hooks %t|Add, To New Empty %x1|Add, To Selected Object %x2|Remove... %x3|Reassign... %x4|Select... %x5|Clear Offset...%x6"); + else + mode= 0; // XXX pupmenu("Hooks %t|Add, New Empty %x1|Add, To Selected Object %x2"); + + if(mode<1) return; + + /* do operations */ + add_hook(scene, v3d, mode); +} + +void hookmenu(Scene *scene, View3D *v3d) +{ + /* only called in object mode */ + short event, changed=0; + Object *ob; + Base *base; + ModifierData *md; + HookModifierData *hmd; + + event= 0; // XXX pupmenu("Modify Hooks for Selected...%t|Reset Offset%x1|Recenter at Cursor%x2"); + if (event==-1) return; + if (event==2 && !(v3d)) { + // XXX error("Cannot perform this operation without a 3d view"); + return; + } + + for (base= FIRSTBASE; base; base= base->next) { + if(TESTBASELIB(v3d, base)) { + for (md = base->object->modifiers.first; md; md=md->next) { + if (md->type==eModifierType_Hook) { + ob = base->object; + hmd = (HookModifierData*) md; + + /* + * Copied from modifiers_cursorHookCenter and + * modifiers_clearHookOffset, should consolidate + * */ + + if (event==1) { + if(hmd->object) { + Mat4Invert(hmd->object->imat, hmd->object->obmat); + Mat4MulSerie(hmd->parentinv, hmd->object->imat, ob->obmat, NULL, NULL, NULL, NULL, NULL, NULL); + + changed= 1; + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + } + } else { + float *curs = give_cursor(scene, v3d); + float bmat[3][3], imat[3][3]; + + where_is_object(scene, ob); + + Mat3CpyMat4(bmat, ob->obmat); + Mat3Inv(imat, bmat); + + curs= give_cursor(scene, v3d); + hmd->cent[0]= curs[0]-ob->obmat[3][0]; + hmd->cent[1]= curs[1]-ob->obmat[3][1]; + hmd->cent[2]= curs[2]-ob->obmat[3][2]; + Mat3MulVecfl(imat, hmd->cent); + + changed= 1; + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + } + } + } + } + } + + if (changed) { + } +} + diff --git a/source/blender/editors/object/object_intern.h b/source/blender/editors/object/object_intern.h index 8eaa388cdf0..7d52e9c7c56 100644 --- a/source/blender/editors/object/object_intern.h +++ b/source/blender/editors/object/object_intern.h @@ -38,15 +38,39 @@ struct HookModifierData; /* internal exports only */ +/* object_transform.c */ +void OBJECT_OT_location_clear(struct wmOperatorType *ot); +void OBJECT_OT_rotation_clear(struct wmOperatorType *ot); +void OBJECT_OT_scale_clear(struct wmOperatorType *ot); +void OBJECT_OT_origin_clear(struct wmOperatorType *ot); +void OBJECT_OT_visual_transform_apply(struct wmOperatorType *ot); +void OBJECT_OT_location_apply(struct wmOperatorType *ot); +void OBJECT_OT_scale_apply(struct wmOperatorType *ot); +void OBJECT_OT_rotation_apply(struct wmOperatorType *ot); +void OBJECT_OT_center_set(struct wmOperatorType *ot); -/* object_edit.c */ -void OBJECT_OT_mode_set(struct wmOperatorType *ot); -void OBJECT_OT_editmode_toggle(struct wmOperatorType *ot); -void OBJECT_OT_posemode_toggle(struct wmOperatorType *ot); +/* object_relations.c */ void OBJECT_OT_parent_set(struct wmOperatorType *ot); void OBJECT_OT_parent_clear(struct wmOperatorType *ot); +void OBJECT_OT_vertex_parent_set(struct wmOperatorType *ot); void OBJECT_OT_track_set(struct wmOperatorType *ot); void OBJECT_OT_track_clear(struct wmOperatorType *ot); +void OBJECT_OT_slow_parent_set(struct wmOperatorType *ot); +void OBJECT_OT_slow_parent_clear(struct wmOperatorType *ot); +void OBJECT_OT_make_local(struct wmOperatorType *ot); +void OBJECT_OT_move_to_layer(struct wmOperatorType *ot); + +/* object_edit.c */ +void OBJECT_OT_mode_set(struct wmOperatorType *ot); +void OBJECT_OT_editmode_toggle(struct wmOperatorType *ot); +void OBJECT_OT_posemode_toggle(struct wmOperatorType *ot); +void OBJECT_OT_restrictview_set(struct wmOperatorType *ot); +void OBJECT_OT_restrictview_clear(struct wmOperatorType *ot); +void OBJECT_OT_proxy_make(struct wmOperatorType *ot); +void OBJECT_OT_shade_smooth(struct wmOperatorType *ot); +void OBJECT_OT_shade_flat(struct wmOperatorType *ot); + +/* object_select.c */ void OBJECT_OT_select_all_toggle(struct wmOperatorType *ot); void OBJECT_OT_select_inverse(struct wmOperatorType *ot); void OBJECT_OT_select_random(struct wmOperatorType *ot); @@ -55,37 +79,28 @@ void OBJECT_OT_select_by_layer(struct wmOperatorType *ot); void OBJECT_OT_select_linked(struct wmOperatorType *ot); void OBJECT_OT_select_grouped(struct wmOperatorType *ot); void OBJECT_OT_select_mirror(struct wmOperatorType *ot); -void OBJECT_OT_location_clear(struct wmOperatorType *ot); -void OBJECT_OT_rotation_clear(struct wmOperatorType *ot); -void OBJECT_OT_scale_clear(struct wmOperatorType *ot); -void OBJECT_OT_origin_clear(struct wmOperatorType *ot); -void OBJECT_OT_restrictview_set(struct wmOperatorType *ot); -void OBJECT_OT_restrictview_clear(struct wmOperatorType *ot); -void OBJECT_OT_slowparent_set(struct wmOperatorType *ot); -void OBJECT_OT_slowparent_clear(struct wmOperatorType *ot); -void OBJECT_OT_center_set(struct wmOperatorType *ot); -void OBJECT_OT_duplicates_make_real(struct wmOperatorType *ot); -void OBJECT_OT_object_add(struct wmOperatorType *ot); -void OBJECT_OT_duplicate(struct wmOperatorType *ot); -void OBJECT_OT_delete(struct wmOperatorType *ot); -void OBJECT_OT_join(struct wmOperatorType *ot); -void OBJECT_OT_proxy_make(struct wmOperatorType *ot); -void OBJECT_OT_shade_smooth(struct wmOperatorType *ot); -void OBJECT_OT_shade_flat(struct wmOperatorType *ot); +/* object_add.c */ +void OBJECT_OT_add(struct wmOperatorType *ot); void OBJECT_OT_mesh_add(struct wmOperatorType *ot); void OBJECT_OT_curve_add(struct wmOperatorType *ot); void OBJECT_OT_surface_add(struct wmOperatorType *ot); -void OBJECT_OT_metaball_add(wmOperatorType *ot); +void OBJECT_OT_metaball_add(struct wmOperatorType *ot); void OBJECT_OT_text_add(struct wmOperatorType *ot); void OBJECT_OT_armature_add(struct wmOperatorType *ot); - /* only used as menu */ -void OBJECT_OT_primitive_add(struct wmOperatorType *ot); +void OBJECT_OT_primitive_add(struct wmOperatorType *ot); /* only used as menu */ + +void OBJECT_OT_duplicates_make_real(struct wmOperatorType *ot); +void OBJECT_OT_duplicate(struct wmOperatorType *ot); +void OBJECT_OT_delete(struct wmOperatorType *ot); +void OBJECT_OT_join(struct wmOperatorType *ot); +void OBJECT_OT_convert(struct wmOperatorType *ot); +/* object_hook.c */ int object_hook_index_array(Object *obedit, int *tot, int **indexar, char *name, float *cent_r); void object_hook_select(Object *obedit, struct HookModifierData *hmd); -/* editlattice.c */ +/* object_lattice.c */ void free_editLatt(Object *ob); void make_editLatt(Object *obedit); void load_editLatt(Object *obedit); @@ -94,7 +109,7 @@ void remake_editLatt(Object *obedit); void LATTICE_OT_select_all_toggle(struct wmOperatorType *ot); void LATTICE_OT_make_regular(struct wmOperatorType *ot); -/* editgroup.c */ +/* object_group.c */ void GROUP_OT_group_create(struct wmOperatorType *ot); void GROUP_OT_objects_remove(struct wmOperatorType *ot); void GROUP_OT_objects_add_active(struct wmOperatorType *ot); @@ -117,7 +132,7 @@ void OBJECT_OT_hook_select(struct wmOperatorType *ot); void OBJECT_OT_hook_assign(struct wmOperatorType *ot); void OBJECT_OT_explode_refresh(struct wmOperatorType *ot); -/* editconstraint.c */ +/* object_constraint.c */ void OBJECT_OT_constraint_add(struct wmOperatorType *ot); void OBJECT_OT_constraint_add_with_targets(struct wmOperatorType *ot); void POSE_OT_constraint_add(struct wmOperatorType *ot); @@ -148,11 +163,13 @@ void OBJECT_OT_vertex_group_select(struct wmOperatorType *ot); void OBJECT_OT_vertex_group_deselect(struct wmOperatorType *ot); void OBJECT_OT_vertex_group_copy_to_linked(struct wmOperatorType *ot); void OBJECT_OT_vertex_group_copy(struct wmOperatorType *ot); +void OBJECT_OT_vertex_group_menu(struct wmOperatorType *ot); +void OBJECT_OT_vertex_group_set_active(struct wmOperatorType *ot); void OBJECT_OT_game_property_new(struct wmOperatorType *ot); void OBJECT_OT_game_property_remove(struct wmOperatorType *ot); -/* editkey.c */ +/* object_shapekey.c */ void OBJECT_OT_shape_key_add(struct wmOperatorType *ot); void OBJECT_OT_shape_key_remove(struct wmOperatorType *ot); diff --git a/source/blender/editors/object/object_lattice.c b/source/blender/editors/object/object_lattice.c new file mode 100644 index 00000000000..b35d3908b43 --- /dev/null +++ b/source/blender/editors/object/object_lattice.c @@ -0,0 +1,382 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * Contributor(s): Blender Foundation + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include +#include +#include + +#include "MEM_guardedalloc.h" + +#include "DNA_curve_types.h" +#include "DNA_key_types.h" +#include "DNA_lattice_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" + +#include "BKE_context.h" +#include "BKE_depsgraph.h" +#include "BKE_key.h" +#include "BKE_lattice.h" +#include "BKE_mesh.h" +#include "BKE_utildefines.h" + +#include "ED_object.h" +#include "ED_screen.h" +#include "ED_view3d.h" +#include "ED_util.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "object_intern.h" + +/********************** Load/Make/Free ********************/ + +void free_editLatt(Object *ob) +{ + Lattice *lt= ob->data; + + if(lt->editlatt) { + if(lt->editlatt->def) + MEM_freeN(lt->editlatt->def); + if(lt->editlatt->dvert) + free_dverts(lt->editlatt->dvert, lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw); + + MEM_freeN(lt->editlatt); + lt->editlatt= NULL; + } +} + +void make_editLatt(Object *obedit) +{ + Lattice *lt= obedit->data; + KeyBlock *actkey; + + free_editLatt(obedit); + + lt= obedit->data; + + actkey= ob_get_keyblock(obedit); + if(actkey) + key_to_latt(actkey, lt); + + lt->editlatt= MEM_dupallocN(lt); + lt->editlatt->def= MEM_dupallocN(lt->def); + + if(lt->dvert) { + int tot= lt->pntsu*lt->pntsv*lt->pntsw; + lt->editlatt->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert"); + copy_dverts(lt->editlatt->dvert, lt->dvert, tot); + } +} + +void load_editLatt(Object *obedit) +{ + Lattice *lt; + KeyBlock *actkey; + BPoint *bp; + float *fp; + int tot; + + lt= obedit->data; + + actkey= ob_get_keyblock(obedit); + + if(actkey) { + /* active key: vertices */ + tot= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; + + if(actkey->data) MEM_freeN(actkey->data); + + fp=actkey->data= MEM_callocN(lt->key->elemsize*tot, "actkey->data"); + actkey->totelem= tot; + + bp= lt->editlatt->def; + while(tot--) { + VECCOPY(fp, bp->vec); + fp+= 3; + bp++; + } + } + else { + MEM_freeN(lt->def); + + lt->def= MEM_dupallocN(lt->editlatt->def); + + lt->flag= lt->editlatt->flag; + + lt->pntsu= lt->editlatt->pntsu; + lt->pntsv= lt->editlatt->pntsv; + lt->pntsw= lt->editlatt->pntsw; + + lt->typeu= lt->editlatt->typeu; + lt->typev= lt->editlatt->typev; + lt->typew= lt->editlatt->typew; + } + + if(lt->dvert) { + free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw); + lt->dvert= NULL; + } + + if(lt->editlatt->dvert) { + int tot= lt->pntsu*lt->pntsv*lt->pntsw; + + lt->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert"); + copy_dverts(lt->dvert, lt->editlatt->dvert, tot); + } +} + +/************************** Operators *************************/ + +static void setflagsLatt(Object *obedit, int flag) +{ + Lattice *lt= obedit->data; + BPoint *bp; + int a; + + bp= lt->editlatt->def; + + a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; + + while(a--) { + if(bp->hide==0) { + bp->f1= flag; + } + bp++; + } +} + +int de_select_all_exec(bContext *C, wmOperator *op) +{ + Object *obedit= CTX_data_edit_object(C); + Lattice *lt= obedit->data; + BPoint *bp; + int a, deselect= 0; + + bp= lt->editlatt->def; + a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; + + while(a--) { + if(bp->hide==0) { + if(bp->f1) { + deselect= 1; + break; + } + } + bp++; + } + + if(deselect) + setflagsLatt(obedit, 0); + else + setflagsLatt(obedit, 1); + + WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data); + + return OPERATOR_FINISHED; +} + +void LATTICE_OT_select_all_toggle(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Select or Deselect All"; + ot->idname= "LATTICE_OT_select_all_toggle"; + + /* api callbacks */ + ot->exec= de_select_all_exec; + ot->poll= ED_operator_editlattice; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +int make_regular_poll(bContext *C) +{ + Object *ob; + + if(ED_operator_editlattice(C)) return 1; + + ob= CTX_data_active_object(C); + return (ob && ob->type==OB_LATTICE); +} + +int make_regular_exec(bContext *C, wmOperator *op) +{ + Object *ob= CTX_data_edit_object(C); + Lattice *lt; + + if(ob) { + lt= ob->data; + resizelattice(lt->editlatt, lt->pntsu, lt->pntsv, lt->pntsw, NULL); + } + else { + ob= CTX_data_active_object(C); + lt= ob->data; + resizelattice(lt, lt->pntsu, lt->pntsv, lt->pntsw, NULL); + } + + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data); + + return OPERATOR_FINISHED; +} + +void LATTICE_OT_make_regular(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Make Regular"; + ot->idname= "LATTICE_OT_make_regular"; + + /* api callbacks */ + ot->exec= make_regular_exec; + ot->poll= make_regular_poll; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/****************************** Mouse Selection *************************/ + +static void findnearestLattvert__doClosest(void *userData, BPoint *bp, int x, int y) +{ + struct { BPoint *bp; short dist, select, mval[2]; } *data = userData; + float temp = abs(data->mval[0]-x) + abs(data->mval[1]-y); + + if((bp->f1 & SELECT)==data->select) + temp += 5; + + if(tempdist) { + data->dist = temp; + + data->bp = bp; + } +} + +static BPoint *findnearestLattvert(ViewContext *vc, short mval[2], int sel) +{ + /* sel==1: selected gets a disadvantage */ + /* in nurb and bezt or bp the nearest is written */ + /* return 0 1 2: handlepunt */ + struct { BPoint *bp; short dist, select, mval[2]; } data = {0}; + + data.dist = 100; + data.select = sel; + data.mval[0]= mval[0]; + data.mval[1]= mval[1]; + + lattice_foreachScreenVert(vc, findnearestLattvert__doClosest, &data); + + return data.bp; +} + +void mouse_lattice(bContext *C, short mval[2], int extend) +{ + ViewContext vc; + BPoint *bp= NULL; + + view3d_set_viewcontext(C, &vc); + bp= findnearestLattvert(&vc, mval, 1); + + if(bp) { + if(extend==0) { + setflagsLatt(vc.obedit, 0); + bp->f1 |= SELECT; + } + else + bp->f1 ^= SELECT; /* swap */ + + WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc.obedit->data); + } +} + +/******************************** Undo *************************/ + +typedef struct UndoLattice { + BPoint *def; + int pntsu, pntsv, pntsw; +} UndoLattice; + +static void undoLatt_to_editLatt(void *data, void *edata) +{ + UndoLattice *ult= (UndoLattice*)data; + Lattice *editlatt= (Lattice *)edata; + int a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw; + + memcpy(editlatt->def, ult->def, a*sizeof(BPoint)); +} + +static void *editLatt_to_undoLatt(void *edata) +{ + UndoLattice *ult= MEM_callocN(sizeof(UndoLattice), "UndoLattice"); + Lattice *editlatt= (Lattice *)edata; + + ult->def= MEM_dupallocN(editlatt->def); + ult->pntsu= editlatt->pntsu; + ult->pntsv= editlatt->pntsv; + ult->pntsw= editlatt->pntsw; + + return ult; +} + +static void free_undoLatt(void *data) +{ + UndoLattice *ult= (UndoLattice*)data; + + if(ult->def) MEM_freeN(ult->def); + MEM_freeN(ult); +} + +static int validate_undoLatt(void *data, void *edata) +{ + UndoLattice *ult= (UndoLattice*)data; + Lattice *editlatt= (Lattice *)edata; + + return (ult->pntsu == editlatt->pntsu && + ult->pntsv == editlatt->pntsv && + ult->pntsw == editlatt->pntsw); +} + +static void *get_editlatt(bContext *C) +{ + Object *obedit= CTX_data_edit_object(C); + + if(obedit && obedit->type==OB_LATTICE) { + Lattice *lt= obedit->data; + return lt->editlatt; + } + + return NULL; +} + +/* and this is all the undo system needs to know */ +void undo_push_lattice(bContext *C, char *name) +{ + undo_editmode_push(C, name, get_editlatt, free_undoLatt, undoLatt_to_editLatt, editLatt_to_undoLatt, validate_undoLatt); +} + diff --git a/source/blender/editors/object/object_modifier.c b/source/blender/editors/object/object_modifier.c index aecb778db06..cc8cc420bf7 100644 --- a/source/blender/editors/object/object_modifier.c +++ b/source/blender/editors/object/object_modifier.c @@ -809,58 +809,6 @@ void OBJECT_OT_meshdeform_bind(wmOperatorType *ot) ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; } -#if 0 -typedef struct MenuEntry { - char *name; - int ID; -} MenuEntry; - -static int menuEntry_compare_names(const void *entry1, const void *entry2) -{ - return strcmp(((MenuEntry *)entry1)->name, ((MenuEntry *)entry2)->name); -} - -static uiBlock *modifiers_add_menu(void *ob_v) -{ - Object *ob = ob_v; - uiBlock *block; - int i, yco=0; - int numEntries = 0; - MenuEntry entries[NUM_MODIFIER_TYPES]; - - block= uiNewBlock(&curarea->uiblocks, "modifier_add_menu", - UI_EMBOSSP, UI_HELV, curarea->win); - uiBlockSetButmFunc(block, modifiers_add, ob); - - for (i=eModifierType_None+1; iflags&eModifierTypeFlag_AcceptsCVs) || - (ob->type==OB_MESH && (mti->flags&eModifierTypeFlag_AcceptsMesh))) { - entries[numEntries].name = mti->name; - entries[numEntries].ID = i; - - ++numEntries; - } - } - - qsort(entries, numEntries, sizeof(*entries), menuEntry_compare_names); - - - for(i = 0; i < numEntries; ++i) - uiDefBut(block, BUTM, B_MODIFIER_RECALC, entries[i].name, - 0, yco -= 20, 160, 19, NULL, 0, 0, 1, entries[i].ID, ""); - - uiTextBoundsBlock(block, 50); - uiBlockSetDirection(block, UI_DOWN); - - return block; -} -#endif - /******************** hook operators ************************/ static int hook_poll(bContext *C) diff --git a/source/blender/editors/object/object_ops.c b/source/blender/editors/object/object_ops.c index 09a27e9e613..bb8c5dc292e 100644 --- a/source/blender/editors/object/object_ops.c +++ b/source/blender/editors/object/object_ops.c @@ -64,14 +64,36 @@ void ED_operatortypes_object(void) { wmOperatorType *ot; + + WM_operatortype_append(OBJECT_OT_location_clear); + WM_operatortype_append(OBJECT_OT_rotation_clear); + WM_operatortype_append(OBJECT_OT_scale_clear); + WM_operatortype_append(OBJECT_OT_origin_clear); + WM_operatortype_append(OBJECT_OT_visual_transform_apply); + WM_operatortype_append(OBJECT_OT_location_apply); + WM_operatortype_append(OBJECT_OT_scale_apply); + WM_operatortype_append(OBJECT_OT_rotation_apply); + WM_operatortype_append(OBJECT_OT_center_set); WM_operatortype_append(OBJECT_OT_mode_set); WM_operatortype_append(OBJECT_OT_editmode_toggle); WM_operatortype_append(OBJECT_OT_posemode_toggle); + WM_operatortype_append(OBJECT_OT_proxy_make); + WM_operatortype_append(OBJECT_OT_restrictview_clear); + WM_operatortype_append(OBJECT_OT_restrictview_set); + WM_operatortype_append(OBJECT_OT_shade_smooth); + WM_operatortype_append(OBJECT_OT_shade_flat); + WM_operatortype_append(OBJECT_OT_parent_set); WM_operatortype_append(OBJECT_OT_parent_clear); + WM_operatortype_append(OBJECT_OT_vertex_parent_set); WM_operatortype_append(OBJECT_OT_track_set); WM_operatortype_append(OBJECT_OT_track_clear); + WM_operatortype_append(OBJECT_OT_slow_parent_set); + WM_operatortype_append(OBJECT_OT_slow_parent_clear); + WM_operatortype_append(OBJECT_OT_make_local); + WM_operatortype_append(OBJECT_OT_move_to_layer); + WM_operatortype_append(OBJECT_OT_select_inverse); WM_operatortype_append(OBJECT_OT_select_random); WM_operatortype_append(OBJECT_OT_select_all_toggle); @@ -80,21 +102,7 @@ void ED_operatortypes_object(void) WM_operatortype_append(OBJECT_OT_select_linked); WM_operatortype_append(OBJECT_OT_select_grouped); WM_operatortype_append(OBJECT_OT_select_mirror); - WM_operatortype_append(OBJECT_OT_location_clear); - WM_operatortype_append(OBJECT_OT_rotation_clear); - WM_operatortype_append(OBJECT_OT_scale_clear); - WM_operatortype_append(OBJECT_OT_origin_clear); - WM_operatortype_append(OBJECT_OT_restrictview_clear); - WM_operatortype_append(OBJECT_OT_restrictview_set); - WM_operatortype_append(OBJECT_OT_slowparent_set); - WM_operatortype_append(OBJECT_OT_slowparent_clear); - WM_operatortype_append(OBJECT_OT_center_set); - WM_operatortype_append(OBJECT_OT_duplicates_make_real); - WM_operatortype_append(OBJECT_OT_duplicate); - WM_operatortype_append(OBJECT_OT_join); - WM_operatortype_append(OBJECT_OT_proxy_make); - WM_operatortype_append(OBJECT_OT_shade_smooth); - WM_operatortype_append(OBJECT_OT_shade_flat); + WM_operatortype_append(GROUP_OT_group_create); WM_operatortype_append(GROUP_OT_objects_remove); WM_operatortype_append(GROUP_OT_objects_add_active); @@ -106,10 +114,14 @@ void ED_operatortypes_object(void) WM_operatortype_append(OBJECT_OT_text_add); WM_operatortype_append(OBJECT_OT_surface_add); WM_operatortype_append(OBJECT_OT_armature_add); - WM_operatortype_append(OBJECT_OT_object_add); + WM_operatortype_append(OBJECT_OT_add); WM_operatortype_append(OBJECT_OT_primitive_add); WM_operatortype_append(OBJECT_OT_mesh_add); WM_operatortype_append(OBJECT_OT_metaball_add); + WM_operatortype_append(OBJECT_OT_duplicates_make_real); + WM_operatortype_append(OBJECT_OT_duplicate); + WM_operatortype_append(OBJECT_OT_join); + WM_operatortype_append(OBJECT_OT_convert); WM_operatortype_append(OBJECT_OT_modifier_add); WM_operatortype_append(OBJECT_OT_modifier_remove); @@ -151,6 +163,8 @@ void ED_operatortypes_object(void) WM_operatortype_append(OBJECT_OT_vertex_group_deselect); WM_operatortype_append(OBJECT_OT_vertex_group_copy_to_linked); WM_operatortype_append(OBJECT_OT_vertex_group_copy); + WM_operatortype_append(OBJECT_OT_vertex_group_menu); + WM_operatortype_append(OBJECT_OT_vertex_group_set_active); WM_operatortype_append(OBJECT_OT_game_property_new); WM_operatortype_append(OBJECT_OT_game_property_remove); diff --git a/source/blender/editors/object/object_relations.c b/source/blender/editors/object/object_relations.c new file mode 100644 index 00000000000..6fe01cced74 --- /dev/null +++ b/source/blender/editors/object/object_relations.c @@ -0,0 +1,1766 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * Contributor(s): Blender Foundation, 2002-2008 full recode + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include +#include +#include + +#include "MEM_guardedalloc.h" + +#include "DNA_anim_types.h" +#include "DNA_armature_types.h" +#include "DNA_camera_types.h" +#include "DNA_constraint_types.h" +#include "DNA_curve_types.h" +#include "DNA_group_types.h" +#include "DNA_lamp_types.h" +#include "DNA_lattice_types.h" +#include "DNA_material_types.h" +#include "DNA_mesh_types.h" +#include "DNA_meta_types.h" +#include "DNA_object_types.h" +#include "DNA_particle_types.h" +#include "DNA_scene_types.h" +#include "DNA_view3d_types.h" +#include "DNA_world_types.h" + +#include "BLI_arithb.h" +#include "BLI_editVert.h" +#include "BLI_listbase.h" +#include "BLI_string.h" + +#include "BKE_action.h" +#include "BKE_animsys.h" +#include "BKE_armature.h" +#include "BKE_context.h" +#include "BKE_constraint.h" +#include "BKE_curve.h" +#include "BKE_depsgraph.h" +#include "BKE_displist.h" +#include "BKE_global.h" +#include "BKE_lattice.h" +#include "BKE_library.h" +#include "BKE_main.h" +#include "BKE_material.h" +#include "BKE_mball.h" +#include "BKE_mesh.h" +#include "BKE_modifier.h" +#include "BKE_object.h" +#include "BKE_report.h" +#include "BKE_sca.h" +#include "BKE_texture.h" +#include "BKE_utildefines.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "UI_interface.h" +#include "UI_resources.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "ED_anim_api.h" +#include "ED_armature.h" +#include "ED_curve.h" +#include "ED_object.h" +#include "ED_screen.h" + +#include "object_intern.h" + +/* ************* XXX **************** */ +static int pupmenu(const char *msg) {return 0;} +static int pupmenu_col(const char *msg, int val) {return 0;} + +/*********************** Make Vertex Parent Operator ************************/ + +static int vertex_parent_set_poll(bContext *C) +{ + return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C); +} + +static int vertex_parent_set_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Object *obedit= CTX_data_edit_object(C); + EditVert *eve; + Curve *cu; + Nurb *nu; + BezTriple *bezt; + BPoint *bp; + Object *par; + int a, v1=0, v2=0, v3=0, v4=0, nr=1; + + /* we need 1 to 3 selected vertices */ + + if(obedit->type==OB_MESH) { + Mesh *me= obedit->data; + EditMesh *em = BKE_mesh_get_editmesh(me); + + eve= em->verts.first; + while(eve) { + if(eve->f & 1) { + if(v1==0) v1= nr; + else if(v2==0) v2= nr; + else if(v3==0) v3= nr; + else if(v4==0) v4= nr; + else break; + } + nr++; + eve= eve->next; + } + + BKE_mesh_end_editmesh(me, em); + } + else if(ELEM(obedit->type, OB_SURF, OB_CURVE)) { + ListBase *editnurb= curve_get_editcurve(obedit); + + cu= obedit->data; + + nu= editnurb->first; + while(nu) { + if(nu->type == CU_BEZIER) { + bezt= nu->bezt; + a= nu->pntsu; + while(a--) { + if(BEZSELECTED_HIDDENHANDLES(cu, bezt)) { + if(v1==0) v1= nr; + else if(v2==0) v2= nr; + else if(v3==0) v3= nr; + else if(v4==0) v4= nr; + else break; + } + nr++; + bezt++; + } + } + else { + bp= nu->bp; + a= nu->pntsu*nu->pntsv; + while(a--) { + if(bp->f1 & SELECT) { + if(v1==0) v1= nr; + else if(v2==0) v2= nr; + else if(v3==0) v3= nr; + else if(v4==0) v4= nr; + else break; + } + nr++; + bp++; + } + } + nu= nu->next; + } + } + else if(obedit->type==OB_LATTICE) { + Lattice *lt= obedit->data; + + a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw; + bp= lt->editlatt->def; + while(a--) { + if(bp->f1 & SELECT) { + if(v1==0) v1= nr; + else if(v2==0) v2= nr; + else if(v3==0) v3= nr; + else if(v4==0) v4= nr; + else break; + } + nr++; + bp++; + } + } + + if(v4 || !((v1 && v2==0 && v3==0) || (v1 && v2 && v3)) ) { + BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to"); + return OPERATOR_CANCELLED; + } + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(ob != obedit) { + ob->recalc |= OB_RECALC; + par= obedit->parent; + + while(par) { + if(par==ob) break; + par= par->parent; + } + if(par) { + BKE_report(op->reports, RPT_ERROR, "Loop in parents"); + } + else { + Object workob; + + ob->parent= BASACT->object; + if(v3) { + ob->partype= PARVERT3; + ob->par1= v1-1; + ob->par2= v2-1; + ob->par3= v3-1; + + /* inverse parent matrix */ + what_does_parent(scene, ob, &workob); + Mat4Invert(ob->parentinv, workob.obmat); + } + else { + ob->partype= PARVERT1; + ob->par1= v1-1; + + /* inverse parent matrix */ + what_does_parent(scene, ob, &workob); + Mat4Invert(ob->parentinv, workob.obmat); + } + } + } + } + CTX_DATA_END; + + DAG_scene_sort(scene); + + WM_event_add_notifier(C, NC_OBJECT, NULL); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_vertex_parent_set(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Make Vertex Parent"; + ot->description = "Parent selected objects to the selected vertices."; + ot->idname= "OBJECT_OT_vertex_parent_set"; + + /* api callbacks */ + ot->poll= vertex_parent_set_poll; + ot->exec= vertex_parent_set_exec; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/********************** Make Proxy Operator *************************/ + +/* present menu listing the possible objects within the group to proxify */ +static void proxy_group_objects_menu (bContext *C, wmOperator *op, Object *ob, Group *group) +{ + uiPopupMenu *pup; + uiLayout *layout; + GroupObject *go; + int len=0; + + /* check if there are any objects within the group to assign for */ + for (go= group->gobject.first; go; go= go->next) { + if (go->ob) len++; + } + if (len==0) return; + + /* now create the menu to draw */ + pup= uiPupMenuBegin(C, "Make Proxy For:", 0); + layout= uiPupMenuLayout(pup); + + for (go= group->gobject.first; go; go= go->next) { + if (go->ob) { + PointerRNA props_ptr; + + /* create operator menu item with relevant properties filled in */ + props_ptr= uiItemFullO(layout, go->ob->id.name+2, 0, op->idname, NULL, WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS); + RNA_string_set(&props_ptr, "object", go->ob->id.name+2); + RNA_string_set(&props_ptr, "group_object", go->ob->id.name+2); + } + } + + /* display the menu, and be done */ + uiPupMenuEnd(C, pup); +} + +/* set the object to proxify */ +static int make_proxy_invoke (bContext *C, wmOperator *op, wmEvent *evt) +{ + Scene *scene= CTX_data_scene(C); + Object *ob= CTX_data_active_object(C); + + /* sanity checks */ + if (!scene || scene->id.lib || !ob) + return OPERATOR_CANCELLED; + + /* Get object to work on - use a menu if we need to... */ + if (ob->dup_group && ob->dup_group->id.lib) { + /* gives menu with list of objects in group */ + proxy_group_objects_menu(C, op, ob, ob->dup_group); + } + else if (ob->id.lib) { + uiPopupMenu *pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION); + uiLayout *layout= uiPupMenuLayout(pup); + PointerRNA props_ptr; + + /* create operator menu item with relevant properties filled in */ + props_ptr= uiItemFullO(layout, op->type->name, 0, op->idname, NULL, WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS); + RNA_string_set(&props_ptr, "object", ob->id.name+2); + + /* present the menu and be done... */ + uiPupMenuEnd(C, pup); + } + else { + /* error.. cannot continue */ + BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group"); + } + + /* this invoke just calls another instance of this operator... */ + return OPERATOR_CANCELLED; +} + +static int make_proxy_exec (bContext *C, wmOperator *op) +{ + Object *ob=NULL, *gob=NULL; + Scene *scene= CTX_data_scene(C); + char ob_name[21], gob_name[21]; + + /* get object and group object + * - firstly names + * - then pointers from context + */ + RNA_string_get(op->ptr, "object", ob_name); + RNA_string_get(op->ptr, "group_object", gob_name); + + if (gob_name[0]) { + Group *group; + GroupObject *go; + + /* active object is group object... */ + // FIXME: we should get the nominated name instead + gob= CTX_data_active_object(C); + group= gob->dup_group; + + /* find the object to affect */ + for (go= group->gobject.first; go; go= go->next) { + if ((go->ob) && strcmp(go->ob->id.name+2, gob_name)==0) { + ob= go->ob; + break; + } + } + } + else { + /* just use the active object for now */ + // FIXME: we should get the nominated name instead + ob= CTX_data_active_object(C); + } + + if (ob) { + Object *newob; + Base *newbase, *oldbase= BASACT; + char name[32]; + + /* Add new object for the proxy */ + newob= add_object(scene, OB_EMPTY); + if (gob) + strcpy(name, gob->id.name+2); + else + strcpy(name, ob->id.name+2); + strcat(name, "_proxy"); + rename_id(&newob->id, name); + + /* set layers OK */ + newbase= BASACT; /* add_object sets active... */ + newbase->lay= oldbase->lay; + newob->lay= newbase->lay; + + /* remove base, leave user count of object, it gets linked in object_make_proxy */ + if (gob==NULL) { + BLI_remlink(&scene->base, oldbase); + MEM_freeN(oldbase); + } + + object_make_proxy(newob, ob, gob); + + /* depsgraph flushes are needed for the new data */ + DAG_scene_sort(scene); + DAG_id_flush_update(&newob->id, OB_RECALC); + + WM_event_add_notifier(C, NC_OBJECT, NULL); + } + else { + BKE_report(op->reports, RPT_ERROR, "No object to make proxy for"); + return OPERATOR_CANCELLED; + } + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_proxy_make (wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Make Proxy"; + ot->idname= "OBJECT_OT_proxy_make"; + ot->description= "Add empty object to become local replacement data of a library-linked object"; + + /* callbacks */ + ot->invoke= make_proxy_invoke; + ot->exec= make_proxy_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_string(ot->srna, "object", "", 19, "Proxy Object", "Name of lib-linked/grouped object to make a proxy for."); + RNA_def_string(ot->srna, "group_object", "", 19, "Group Object", "Name of group instancer (if applicable)."); +} + +/********************** Clear Parent Operator ******************* */ + +static EnumPropertyItem prop_clear_parent_types[] = { + {0, "CLEAR", 0, "Clear Parent", ""}, + {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""}, + {2, "CLEAR_INVERSE", 0, "Clear Parent Inverse", ""}, + {0, NULL, 0, NULL, NULL} +}; + +/* note, poll should check for editable scene */ +static int parent_clear_exec(bContext *C, wmOperator *op) +{ + int type= RNA_enum_get(op->ptr, "type"); + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + + if(type == 0) { + ob->parent= NULL; + } + else if(type == 1) { + ob->parent= NULL; + ob->track= NULL; + ED_object_apply_obmat(ob); + } + else if(type == 2) + Mat4One(ob->parentinv); + + ob->recalc |= OB_RECALC; + } + CTX_DATA_END; + + DAG_scene_sort(CTX_data_scene(C)); + ED_anim_dag_flush_update(C); + WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_parent_clear(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Clear Parent"; + ot->description = "Clear the object's parenting."; + ot->idname= "OBJECT_OT_parent_clear"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= parent_clear_exec; + + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", ""); +} + +/* ******************** Make Parent Operator *********************** */ + +#define PAR_OBJECT 0 +#define PAR_ARMATURE 1 +#define PAR_ARMATURE_NAME 2 +#define PAR_ARMATURE_ENVELOPE 3 +#define PAR_ARMATURE_AUTO 4 +#define PAR_BONE 5 +#define PAR_CURVE 6 +#define PAR_FOLLOW 7 +#define PAR_PATH_CONST 8 +#define PAR_LATTICE 9 +#define PAR_VERTEX 10 +#define PAR_TRIA 11 + +static EnumPropertyItem prop_make_parent_types[] = { + {PAR_OBJECT, "OBJECT", 0, "Object", ""}, + {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""}, + {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, " With Empty Groups", ""}, + {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, " With Automatic Weights", ""}, + {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, " With Envelope Weights", ""}, + {PAR_BONE, "BONE", 0, "Bone", ""}, + {PAR_CURVE, "CURVE", 0, "Curve Deform", ""}, + {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""}, + {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""}, + {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""}, + {PAR_VERTEX, "VERTEX", 0, "Vertex", ""}, + {PAR_TRIA, "TRIA", 0, "Triangle", ""}, + {0, NULL, 0, NULL, NULL} +}; + +static int test_parent_loop(Object *par, Object *ob) +{ + /* test if 'ob' is a parent somewhere in par's parents */ + + if(par == NULL) return 0; + if(ob == par) return 1; + + return test_parent_loop(par->parent, ob); +} + +void ED_object_parent(Object *ob, Object *par, int type, const char *substr) +{ + if(!par || test_parent_loop(par, ob)) { + ob->parent= NULL; + ob->partype= PAROBJECT; + ob->parsubstr[0]= 0; + return; + } + + /* this could use some more checks */ + + ob->parent= par; + ob->partype &= ~PARTYPE; + ob->partype |= type; + BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr)); +} + +static int parent_set_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Object *par= CTX_data_active_object(C); + bPoseChannel *pchan= NULL; + int partype= RNA_enum_get(op->ptr, "type"); + int pararm= ELEM4(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO); + + par->recalc |= OB_RECALC_OB; + + /* preconditions */ + if(partype==PAR_FOLLOW || partype==PAR_PATH_CONST) { + if(par->type!=OB_CURVE) + return OPERATOR_CANCELLED; + else { + Curve *cu= par->data; + + if((cu->flag & CU_PATH)==0) { + cu->flag |= CU_PATH|CU_FOLLOW; + makeDispListCurveTypes(scene, par, 0); /* force creation of path data */ + } + else cu->flag |= CU_FOLLOW; + + /* fall back on regular parenting now */ + partype= PAR_OBJECT; + } + } + else if(partype==PAR_BONE) { + pchan= get_active_posechannel(par); + + if(pchan==NULL) { + BKE_report(op->reports, RPT_ERROR, "No active Bone"); + return OPERATOR_CANCELLED; + } + } + + /* context itterator */ + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + + if(ob!=par) { + + if( test_parent_loop(par, ob) ) { + BKE_report(op->reports, RPT_ERROR, "Loop in parents"); + } + else { + Object workob; + + /* apply transformation of previous parenting */ + ED_object_apply_obmat(ob); + + ob->parent= par; + + /* handle types */ + if (pchan) + strcpy (ob->parsubstr, pchan->name); + else + ob->parsubstr[0]= 0; + + /* constraint */ + if(partype==PAR_PATH_CONST) { + bConstraint *con; + bFollowPathConstraint *data; + float cmat[4][4], vec[3]; + + con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH); + strcpy (con->name, "AutoPath"); + + data = con->data; + data->tar = par; + + add_constraint_to_object(con, ob); + + get_constraint_target_matrix(con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra - give_timeoffset(ob)); + VecSubf(vec, ob->obmat[3], cmat[3]); + + ob->loc[0] = vec[0]; + ob->loc[1] = vec[1]; + } + else if(pararm && ob->type==OB_MESH && par->type == OB_ARMATURE) { + if(partype == PAR_ARMATURE_NAME) + create_vgroups_from_armature(scene, ob, par, ARM_GROUPS_NAME); + else if(partype == PAR_ARMATURE_ENVELOPE) + create_vgroups_from_armature(scene, ob, par, ARM_GROUPS_ENVELOPE); + else if(partype == PAR_ARMATURE_AUTO) + create_vgroups_from_armature(scene, ob, par, ARM_GROUPS_AUTO); + + /* get corrected inverse */ + ob->partype= PAROBJECT; + what_does_parent(scene, ob, &workob); + + ob->partype= PARSKEL; + + Mat4Invert(ob->parentinv, workob.obmat); + } + else { + /* calculate inverse parent matrix */ + what_does_parent(scene, ob, &workob); + Mat4Invert(ob->parentinv, workob.obmat); + } + + ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA; + + if( ELEM(partype, PAR_CURVE, PAR_LATTICE) || pararm ) + ob->partype= PARSKEL; /* note, dna define, not operator property */ + else + ob->partype= PAROBJECT; /* note, dna define, not operator property */ + } + } + } + CTX_DATA_END; + + DAG_scene_sort(CTX_data_scene(C)); + ED_anim_dag_flush_update(C); + WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); + + return OPERATOR_FINISHED; +} + +static int parent_set_invoke(bContext *C, wmOperator *op, wmEvent *event) +{ + Object *ob= CTX_data_active_object(C); + uiPopupMenu *pup= uiPupMenuBegin(C, "Set Parent To", 0); + uiLayout *layout= uiPupMenuLayout(pup); + + uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT); + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_OBJECT); + + /* ob becomes parent, make the associated menus */ + if(ob->type==OB_ARMATURE) { + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE); + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE_NAME); + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE_ENVELOPE); + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE_AUTO); + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_BONE); + } + else if(ob->type==OB_CURVE) { + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_CURVE); + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_FOLLOW); + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_PATH_CONST); + } + else if(ob->type == OB_LATTICE) { + uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_LATTICE); + } + + uiPupMenuEnd(C, pup); + + return OPERATOR_CANCELLED; +} + + +void OBJECT_OT_parent_set(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Make Parent"; + ot->description = "Set the object's parenting."; + ot->idname= "OBJECT_OT_parent_set"; + + /* api callbacks */ + ot->invoke= parent_set_invoke; + ot->exec= parent_set_exec; + + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", ""); +} + +/************************ Clear Slow Parent Operator *********************/ + +static int object_slow_parent_clear_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(ob->parent) { + if(ob->partype & PARSLOW) { + ob->partype -= PARSLOW; + where_is_object(scene, ob); + ob->partype |= PARSLOW; + ob->recalc |= OB_RECALC_OB; + } + } + } + CTX_DATA_END; + + ED_anim_dag_flush_update(C); + WM_event_add_notifier(C, NC_SCENE, scene); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_slow_parent_clear(wmOperatorType *ot) +{ + + /* identifiers */ + ot->name= "Clear Slow Parent"; + ot->description = "Clear the object's slow parent."; + ot->idname= "OBJECT_OT_slow_parent_clear"; + + /* api callbacks */ + ot->invoke= WM_operator_confirm; + ot->exec= object_slow_parent_clear_exec; + ot->poll= ED_operator_view3d_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/********************** Make Slow Parent Operator *********************/ + +static int object_slow_parent_set_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(ob->parent) + ob->partype |= PARSLOW; + + ob->recalc |= OB_RECALC_OB; + + } + CTX_DATA_END; + + ED_anim_dag_flush_update(C); + WM_event_add_notifier(C, NC_SCENE, scene); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_slow_parent_set(wmOperatorType *ot) +{ + + /* identifiers */ + ot->name= "Set Slow Parent"; + ot->description = "Set the object's slow parent."; + ot->idname= "OBJECT_OT_slow_parent_set"; + + /* api callbacks */ + ot->invoke= WM_operator_confirm; + ot->exec= object_slow_parent_set_exec; + ot->poll= ED_operator_view3d_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/* ******************** Clear Track Operator ******************* */ + +static EnumPropertyItem prop_clear_track_types[] = { + {0, "CLEAR", 0, "Clear Track", ""}, + {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""}, + {0, NULL, 0, NULL, NULL} +}; + +/* note, poll should check for editable scene */ +static int object_track_clear_exec(bContext *C, wmOperator *op) +{ + int type= RNA_enum_get(op->ptr, "type"); + + if(CTX_data_edit_object(C)) { + BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode"); + return OPERATOR_CANCELLED; + } + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + ob->track= NULL; + ob->recalc |= OB_RECALC; + + if(type == 1) + ED_object_apply_obmat(ob); + } + CTX_DATA_END; + + DAG_scene_sort(CTX_data_scene(C)); + ED_anim_dag_flush_update(C); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_track_clear(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Clear track"; + ot->description = "Clear tracking constraint or flag from object."; + ot->idname= "OBJECT_OT_track_clear"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_track_clear_exec; + + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", ""); +} + +/************************** Make Track Operator *****************************/ + +static EnumPropertyItem prop_make_track_types[] = { + {1, "TRACKTO", 0, "TrackTo Constraint", ""}, + {2, "LOCKTRACK", 0, "LockTrack Constraint", ""}, + {3, "OLDTRACK", 0, "Old Track", ""}, + {0, NULL, 0, NULL, NULL} +}; + +static int track_set_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + int type= RNA_enum_get(op->ptr, "type"); + + if(type == 1) { + bConstraint *con; + bTrackToConstraint *data; + + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + if(base!=BASACT) { + con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO); + strcpy (con->name, "AutoTrack"); + + data = con->data; + data->tar = BASACT->object; + base->object->recalc |= OB_RECALC; + + /* Lamp and Camera track differently by default */ + if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) { + data->reserved1 = TRACK_nZ; + data->reserved2 = UP_Y; + } + + add_constraint_to_object(con, base->object); + } + } + CTX_DATA_END; + } + else if(type == 2) { + bConstraint *con; + bLockTrackConstraint *data; + + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + if(base!=BASACT) { + con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK); + strcpy (con->name, "AutoTrack"); + + data = con->data; + data->tar = BASACT->object; + base->object->recalc |= OB_RECALC; + + /* Lamp and Camera track differently by default */ + if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) { + data->trackflag = TRACK_nZ; + data->lockflag = LOCK_Y; + } + + add_constraint_to_object(con, base->object); + } + } + CTX_DATA_END; + } + else { + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + if(base!=BASACT) { + base->object->track= BASACT->object; + base->object->recalc |= OB_RECALC; + } + } + CTX_DATA_END; + } + DAG_scene_sort(CTX_data_scene(C)); + ED_anim_dag_flush_update(C); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_track_set(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Make Track"; + ot->description = "Make the object track another object, either by constraint or old way or locked track."; + ot->idname= "OBJECT_OT_track_set"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= track_set_exec; + + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", ""); +} + +/************************** Move to Layer Operator *****************************/ + +static unsigned int move_to_layer_init(bContext *C, wmOperator *op) +{ + int values[20], a; + unsigned int lay= 0; + + if(!RNA_property_is_set(op->ptr, "layer")) { + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + lay |= base->lay; + } + CTX_DATA_END; + + for(a=0; a<20; a++) + values[a]= (lay & (1<ptr, "layer", values); + } + else { + RNA_boolean_get_array(op->ptr, "layer", values); + + for(a=0; a<20; a++) + if(values[a]) + lay |= (1 << a); + } + + return lay; +} + +static int move_to_layer_invoke(bContext *C, wmOperator *op, wmEvent *event) +{ + move_to_layer_init(C, op); + return WM_operator_props_popup(C, op, event); +} + +static int move_to_layer_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + View3D *v3d= CTX_wm_view3d(C); + unsigned int lay, local; + int islamp= 0; + + lay= move_to_layer_init(C, op); + lay &= 0xFFFFFF; + + if(lay==0) return OPERATOR_CANCELLED; + + if(v3d && v3d->localview) { + /* now we can move out of localview. */ + // XXX if (!okee("Move from localview")) return; + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + lay= base->lay & ~v3d->lay; + base->lay= lay; + base->object->lay= lay; + base->object->flag &= ~SELECT; + base->flag &= ~SELECT; + if(base->object->type==OB_LAMP) islamp= 1; + } + CTX_DATA_END; + } + else { + /* normal non localview operation */ + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + /* upper byte is used for local view */ + local= base->lay & 0xFF000000; + base->lay= lay + local; + base->object->lay= lay; + if(base->object->type==OB_LAMP) islamp= 1; + } + CTX_DATA_END; + } + + if(islamp) reshadeall_displist(scene); /* only frees */ + + /* warning, active object may be hidden now */ + + WM_event_add_notifier(C, NC_SCENE, scene); + DAG_scene_sort(scene); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_move_to_layer(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Move to Layer"; + ot->description = "Move the object to different layers."; + ot->idname= "OBJECT_OT_move_to_layer"; + + /* api callbacks */ + ot->invoke= move_to_layer_invoke; + ot->exec= move_to_layer_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_boolean_array(ot->srna, "layer", 20, NULL, "Layer", ""); + /* XXX boolean layer subtype, behavior */ +} + +/************************** Link to Scene Operator *****************************/ + +void link_to_scene(unsigned short nr) +{ +#if 0 + Scene *sce= (Scene*) BLI_findlink(&G.main->scene, G.curscreen->scenenr-1); + Base *base, *nbase; + + if(sce==0) return; + if(sce->id.lib) return; + + for(base= FIRSTBASE; base; base= base->next) { + if(TESTBASE(v3d, base)) { + + nbase= MEM_mallocN( sizeof(Base), "newbase"); + *nbase= *base; + BLI_addhead( &(sce->base), nbase); + id_us_plus((ID *)base->object); + } + } +#endif +} + + +void make_links(bContext *C, wmOperator *op, Scene *scene, View3D *v3d, short event) +{ + Object *ob, *obt; + Base *base, *nbase, *sbase; + Scene *sce = NULL; + ID *id; + int a; + short nr=0; + char *strp; + + if(!(ob=OBACT)) return; + + if(event==1) { + IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->scene), 0, &nr); + + if(nr == -2) { + MEM_freeN(strp); + +// XXX activate_databrowse((ID *)scene, ID_SCE, 0, B_INFOSCE, &(G.curscreen->scenenr), link_to_scene ); + + return; + } + else { + event= pupmenu_col(strp, 20); + MEM_freeN(strp); + + if(event<= 0) return; + + nr= 1; + sce= G.main->scene.first; + while(sce) { + if(nr==event) break; + nr++; + sce= sce->id.next; + } + if(sce==scene) { + BKE_report(op->reports, RPT_ERROR, "This is the current scene"); + return; + } + if(sce==0 || sce->id.lib) return; + + /* remember: is needed below */ + event= 1; + } + } + + /* All non group linking */ + for(base= FIRSTBASE; base; base= base->next) { + if(event==1 || base != BASACT) { + + obt= base->object; + + if(TESTBASE(v3d, base)) { + + if(event==1) { /* to scene */ + + /* test if already linked */ + sbase= sce->base.first; + while(sbase) { + if(sbase->object==base->object) break; + sbase= sbase->next; + } + if(sbase) { /* remove */ + continue; + } + + nbase= MEM_mallocN( sizeof(Base), "newbase"); + *nbase= *base; + BLI_addhead( &(sce->base), nbase); + id_us_plus((ID *)base->object); + } + } + if(TESTBASELIB(v3d, base)) { + if(event==2 || event==5) { /* obdata */ + if(ob->type==obt->type) { + + id= obt->data; + id->us--; + + id= ob->data; + id_us_plus(id); + obt->data= id; + + /* if amount of material indices changed: */ + test_object_materials(obt->data); + + obt->recalc |= OB_RECALC_DATA; + } + } + else if(event==4) { /* ob ipo */ +#if 0 // XXX old animation system + if(obt->ipo) obt->ipo->id.us--; + obt->ipo= ob->ipo; + if(obt->ipo) { + id_us_plus((ID *)obt->ipo); + do_ob_ipo(scene, obt); + } +#endif // XXX old animation system + } + else if(event==6) { + if(ob->dup_group) ob->dup_group->id.us--; + obt->dup_group= ob->dup_group; + if(obt->dup_group) { + id_us_plus((ID *)obt->dup_group); + obt->transflag |= OB_DUPLIGROUP; + } + } + else if(event==3) { /* materials */ + + /* new approach, using functions from kernel */ + for(a=0; atotcol; a++) { + Material *ma= give_current_material(ob, a+1); + assign_material(obt, ma, a+1); /* also works with ma==NULL */ + } + } + } + } + } + + ED_anim_dag_flush_update(C); + +} + +void make_links_menu(bContext *C, Scene *scene, View3D *v3d) +{ + Object *ob; + short event=0; + char str[140]; + + if(!(ob=OBACT)) return; + + strcpy(str, "Make Links %t|To Scene...%x1|%l|Object Ipo%x4"); + + if(ob->type==OB_MESH) + strcat(str, "|Mesh Data%x2|Materials%x3"); + else if(ob->type==OB_CURVE) + strcat(str, "|Curve Data%x2|Materials%x3"); + else if(ob->type==OB_FONT) + strcat(str, "|Text Data%x2|Materials%x3"); + else if(ob->type==OB_SURF) + strcat(str, "|Surface Data%x2|Materials%x3"); + else if(ob->type==OB_MBALL) + strcat(str, "|Materials%x3"); + else if(ob->type==OB_CAMERA) + strcat(str, "|Camera Data%x2"); + else if(ob->type==OB_LAMP) + strcat(str, "|Lamp Data%x2"); + else if(ob->type==OB_LATTICE) + strcat(str, "|Lattice Data%x2"); + else if(ob->type==OB_ARMATURE) + strcat(str, "|Armature Data%x2"); + + event= pupmenu(str); + + if(event<= 0) return; + + make_links(C, NULL, scene, v3d, event); +} + +/**************************** Make Single User ********************************/ + +static void single_object_users__forwardModifierLinks(void *userData, Object *ob, Object **obpoin) +{ + ID_NEW(*obpoin); +} + +void single_object_users(Scene *scene, View3D *v3d, int flag) +{ + Base *base; + Object *ob, *obn; + + clear_sca_new_poins(); /* sensor/contr/act */ + + /* duplicate (must set newid) */ + for(base= FIRSTBASE; base; base= base->next) { + ob= base->object; + + if( (base->flag & flag)==flag ) { + if(ob->id.lib==NULL && ob->id.us>1) { + /* base gets copy of object */ + obn= copy_object(ob); + base->object= obn; + ob->id.us--; + } + } + } + + ID_NEW(scene->camera); + if(v3d) ID_NEW(v3d->camera); + + /* object pointers */ + for(base= FIRSTBASE; base; base= base->next) { + ob= base->object; + if(ob->id.lib==NULL) { + relink_constraints(&base->object->constraints); + if (base->object->pose){ + bPoseChannel *chan; + for (chan = base->object->pose->chanbase.first; chan; chan=chan->next){ + relink_constraints(&chan->constraints); + } + } + modifiers_foreachObjectLink(base->object, single_object_users__forwardModifierLinks, NULL); + + ID_NEW(ob->parent); + ID_NEW(ob->track); + } + } + + set_sca_new_poins(); +} + +void new_id_matar(Material **matar, int totcol) +{ + ID *id; + int a; + + for(a=0; alib==0) { + if(id->newid) { + matar[a]= (Material *)id->newid; + id_us_plus(id->newid); + id->us--; + } + else if(id->us>1) { + matar[a]= copy_material(matar[a]); + id->us--; + id->newid= (ID *)matar[a]; + } + } + } +} + +void single_obdata_users(Scene *scene, int flag) +{ + Object *ob; + Lamp *la; + Curve *cu; + //Camera *cam; + Base *base; + Mesh *me; + ID *id; + int a; + + for(base= FIRSTBASE; base; base= base->next) { + ob= base->object; + if(ob->id.lib==NULL && (base->flag & flag)==flag ) { + id= ob->data; + + if(id && id->us>1 && id->lib==0) { + ob->recalc= OB_RECALC_DATA; + + switch(ob->type) { + case OB_LAMP: + if(id && id->us>1 && id->lib==NULL) { + ob->data= la= copy_lamp(ob->data); + for(a=0; amtex[a]) { + ID_NEW(la->mtex[a]->object); + } + } + } + break; + case OB_CAMERA: + ob->data= copy_camera(ob->data); + break; + case OB_MESH: + me= ob->data= copy_mesh(ob->data); + //if(me && me->key) + // ipo_idnew(me->key->ipo); /* drivers */ + break; + case OB_MBALL: + ob->data= copy_mball(ob->data); + break; + case OB_CURVE: + case OB_SURF: + case OB_FONT: + ob->data= cu= copy_curve(ob->data); + ID_NEW(cu->bevobj); + ID_NEW(cu->taperobj); + break; + case OB_LATTICE: + ob->data= copy_lattice(ob->data); + break; + case OB_ARMATURE: + ob->recalc |= OB_RECALC_DATA; + ob->data= copy_armature(ob->data); + armature_rebuild_pose(ob, ob->data); + break; + default: + printf("ERROR single_obdata_users: can't copy %s\n", id->name); + return; + } + + id->us--; + id->newid= ob->data; + + } + +#if 0 // XXX old animation system + id= (ID *)ob->action; + if (id && id->us>1 && id->lib==NULL){ + if(id->newid){ + ob->action= (bAction *)id->newid; + id_us_plus(id->newid); + } + else { + ob->action= copy_action(ob->action); + id->us--; + id->newid=(ID *)ob->action; + } + } + id= (ID *)ob->ipo; + if(id && id->us>1 && id->lib==NULL) { + if(id->newid) { + ob->ipo= (Ipo *)id->newid; + id_us_plus(id->newid); + } + else { + ob->ipo= copy_ipo(ob->ipo); + id->us--; + id->newid= (ID *)ob->ipo; + } + ipo_idnew(ob->ipo); /* drivers */ + } + /* other ipos */ + switch(ob->type) { + case OB_LAMP: + la= ob->data; + if(la->ipo && la->ipo->id.us>1) { + la->ipo->id.us--; + la->ipo= copy_ipo(la->ipo); + ipo_idnew(la->ipo); /* drivers */ + } + break; + case OB_CAMERA: + cam= ob->data; + if(cam->ipo && cam->ipo->id.us>1) { + cam->ipo->id.us--; + cam->ipo= copy_ipo(cam->ipo); + ipo_idnew(cam->ipo); /* drivers */ + } + break; + } +#endif // XXX old animation system + } + } + + me= G.main->mesh.first; + while(me) { + ID_NEW(me->texcomesh); + me= me->id.next; + } +} + +void single_ipo_users(Scene *scene, int flag) +{ +#if 0 // XXX old animation system + Object *ob; + Base *base; + ID *id; + + for(base= FIRSTBASE; base; base= base->next) { + ob= base->object; + if(ob->id.lib==NULL && (flag==0 || (base->flag & SELECT)) ) { + ob->recalc= OB_RECALC_DATA; + + id= (ID *)ob->ipo; + if(id && id->us>1 && id->lib==NULL) { + ob->ipo= copy_ipo(ob->ipo); + id->us--; + ipo_idnew(ob->ipo); /* drivers */ + } + } + } +#endif // XXX old animation system +} + +void single_mat_users(Scene *scene, int flag) +{ + Object *ob; + Base *base; + Material *ma, *man; + Tex *tex; + int a, b; + + for(base= FIRSTBASE; base; base= base->next) { + ob= base->object; + if(ob->id.lib==NULL && (flag==0 || (base->flag & SELECT)) ) { + + for(a=1; a<=ob->totcol; a++) { + ma= give_current_material(ob, a); + if(ma) { + /* do not test for LIB_NEW: this functions guaranteed delivers single_users! */ + + if(ma->id.us>1) { + man= copy_material(ma); + + man->id.us= 0; + assign_material(ob, man, a); + +#if 0 // XXX old animation system + if(ma->ipo) { + man->ipo= copy_ipo(ma->ipo); + ma->ipo->id.us--; + ipo_idnew(ma->ipo); /* drivers */ + } +#endif // XXX old animation system + + for(b=0; bmtex[b] && ma->mtex[b]->tex) { + tex= ma->mtex[b]->tex; + if(tex->id.us>1) { + ma->mtex[b]->tex= copy_texture(tex); + tex->id.us--; + } + } + } + + } + } + } + } + } +} + +void do_single_tex_user(Tex **from) +{ + Tex *tex, *texn; + + tex= *from; + if(tex==0) return; + + if(tex->id.newid) { + *from= (Tex *)tex->id.newid; + id_us_plus(tex->id.newid); + tex->id.us--; + } + else if(tex->id.us>1) { + texn= copy_texture(tex); + tex->id.newid= (ID *)texn; + tex->id.us--; + *from= texn; + } +} + +void single_tex_users_expand() +{ + /* only when 'parent' blocks are LIB_NEW */ + Main *bmain= G.main; + Material *ma; + Lamp *la; + World *wo; + int b; + + for(ma= bmain->mat.first; ma; ma=ma->id.next) { + if(ma->id.flag & LIB_NEW) { + for(b=0; bmtex[b] && ma->mtex[b]->tex) { + do_single_tex_user( &(ma->mtex[b]->tex) ); + } + } + } + } + + for(la= bmain->lamp.first; la; la=la->id.next) { + if(la->id.flag & LIB_NEW) { + for(b=0; bmtex[b] && la->mtex[b]->tex) { + do_single_tex_user( &(la->mtex[b]->tex) ); + } + } + } + } + + for(wo= bmain->world.first; wo; wo=wo->id.next) { + if(wo->id.flag & LIB_NEW) { + for(b=0; bmtex[b] && wo->mtex[b]->tex) { + do_single_tex_user( &(wo->mtex[b]->tex) ); + } + } + } + } +} + +static void single_mat_users_expand(void) +{ + /* only when 'parent' blocks are LIB_NEW */ + Main *bmain= G.main; + Object *ob; + Mesh *me; + Curve *cu; + MetaBall *mb; + Material *ma; + int a; + + for(ob=bmain->object.first; ob; ob=ob->id.next) + if(ob->id.flag & LIB_NEW) + new_id_matar(ob->mat, ob->totcol); + + for(me=bmain->mesh.first; me; me=me->id.next) + if(me->id.flag & LIB_NEW) + new_id_matar(me->mat, me->totcol); + + for(cu=bmain->curve.first; cu; cu=cu->id.next) + if(cu->id.flag & LIB_NEW) + new_id_matar(cu->mat, cu->totcol); + + for(mb=bmain->mball.first; mb; mb=mb->id.next) + if(mb->id.flag & LIB_NEW) + new_id_matar(mb->mat, mb->totcol); + + /* material imats */ + for(ma=bmain->mat.first; ma; ma=ma->id.next) + if(ma->id.flag & LIB_NEW) + for(a=0; amtex[a]) + ID_NEW(ma->mtex[a]->object); +} + +void single_user(Scene *scene, View3D *v3d) +{ + int nr; + + if(scene->id.lib) return; + + clear_id_newpoins(); + + nr= pupmenu("Make Single User%t|Object|Object & ObData|Object & ObData & Materials+Tex|Materials+Tex|Ipos"); + if(nr>0) { + + if(nr==1) single_object_users(scene, v3d, 1); + + else if(nr==2) { + single_object_users(scene, v3d, 1); + single_obdata_users(scene, 1); + } + else if(nr==3) { + single_object_users(scene, v3d, 1); + single_obdata_users(scene, 1); + single_mat_users(scene, 1); /* also tex */ + + } + else if(nr==4) { + single_mat_users(scene, 1); + } + else if(nr==5) { + single_ipo_users(scene, 1); + } + + + clear_id_newpoins(); + + } +} + +/* used for copying scenes */ +void ED_object_single_users(Scene *scene, int full) +{ + single_object_users(scene, NULL, 0); + + if(full) { + single_obdata_users(scene, 0); + single_mat_users_expand(); + single_tex_users_expand(); + } + + clear_id_newpoins(); +} + +/******************************* Make Local ***********************************/ + +/* helper for below, ma was checked to be not NULL */ +static void make_local_makelocalmaterial(Material *ma) +{ + AnimData *adt; + int b; + + id_make_local(&ma->id, 0); + + for(b=0; bmtex[b] && ma->mtex[b]->tex) + id_make_local(&ma->mtex[b]->tex->id, 0); + + adt= BKE_animdata_from_id(&ma->id); + if(adt) BKE_animdata_make_local(adt); + + /* nodetree? XXX */ +} + +static int make_local_exec(bContext *C, wmOperator *op) +{ + AnimData *adt; + ParticleSystem *psys; + Material *ma, ***matarar; + Lamp *la; + ID *id; + int a, b, mode= RNA_boolean_get(op->ptr, "type"); + + if(mode==3) { + all_local(NULL, 0); /* NULL is all libs */ + WM_event_add_notifier(C, NC_WINDOW, NULL); + return OPERATOR_FINISHED; + } + + clear_id_newpoins(); + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(ob->id.lib) + id_make_local(&ob->id, 0); + } + CTX_DATA_END; + + /* maybe object pointers */ + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(ob->id.lib==NULL) { + ID_NEW(ob->parent); + ID_NEW(ob->track); + } + } + CTX_DATA_END; + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + id= ob->data; + + if(id && mode>1) { + id_make_local(id, 0); + adt= BKE_animdata_from_id(id); + if(adt) BKE_animdata_make_local(adt); + } + + for(psys=ob->particlesystem.first; psys; psys=psys->next) + id_make_local(&psys->part->id, 0); + + adt= BKE_animdata_from_id(&ob->id); + if(adt) BKE_animdata_make_local(adt); + } + CTX_DATA_END; + + if(mode>1) { + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(ob->type==OB_LAMP) { + la= ob->data; + + for(b=0; bmtex[b] && la->mtex[b]->tex) + id_make_local(&la->mtex[b]->tex->id, 0); + } + else { + for(a=0; atotcol; a++) { + ma= ob->mat[a]; + if(ma) + make_local_makelocalmaterial(ma); + } + + matarar= (Material ***)give_matarar(ob); + if(matarar) { + for(a=0; atotcol; a++) { + ma= (*matarar)[a]; + if(ma) + make_local_makelocalmaterial(ma); + } + } + } + } + CTX_DATA_END; + } + + WM_event_add_notifier(C, NC_WINDOW, NULL); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_make_local(wmOperatorType *ot) +{ + static EnumPropertyItem type_items[]= { + {1, "SELECTED_OBJECTS", 0, "Selected Objects", ""}, + {2, "SELECTED_OBJECTS_DATA", 0, "Selected Objects and Data", ""}, + {3, "ALL", 0, "All", ""}, + {0, NULL, 0, NULL, NULL}}; + + /* identifiers */ + ot->name= "Make Local"; + ot->description = "Make library linked datablocks local to this file."; + ot->idname= "OBJECT_OT_make_local"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= make_local_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_enum(ot->srna, "type", type_items, 0, "Type", ""); +} + diff --git a/source/blender/editors/object/object_select.c b/source/blender/editors/object/object_select.c new file mode 100644 index 00000000000..50ba4ab2934 --- /dev/null +++ b/source/blender/editors/object/object_select.c @@ -0,0 +1,974 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * Contributor(s): Blender Foundation, 2002-2008 full recode + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include +#include +#include +#include + +#include "DNA_group_types.h" +#include "DNA_material_types.h" +#include "DNA_modifier_types.h" +#include "DNA_object_types.h" +#include "DNA_property_types.h" +#include "DNA_scene_types.h" +#include "DNA_texture_types.h" + +#include "BLI_arithb.h" +#include "BLI_listbase.h" +#include "BLI_rand.h" +#include "BLI_string.h" + +#include "BKE_context.h" +#include "BKE_depsgraph.h" +#include "BKE_global.h" +#include "BKE_group.h" +#include "BKE_main.h" +#include "BKE_material.h" +#include "BKE_particle.h" +#include "BKE_property.h" +#include "BKE_report.h" +#include "BKE_scene.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_object.h" +#include "ED_screen.h" + +#include "RNA_access.h" +#include "RNA_define.h" +#include "RNA_enum_types.h" + +#include "object_intern.h" + +/************************ Exported **************************/ + +/* simple API for object selection, rather than just using the flag + * this takes into account the 'restrict selection in 3d view' flag. + * deselect works always, the restriction just prevents selection */ + +/* Note: send a NC_SCENE|ND_OB_SELECT notifier yourself! */ + +void ED_base_object_select(Base *base, short mode) +{ + if (base) { + if (mode==BA_SELECT) { + if (!(base->object->restrictflag & OB_RESTRICT_SELECT)) + if (mode==BA_SELECT) base->flag |= SELECT; + } + else if (mode==BA_DESELECT) { + base->flag &= ~SELECT; + } + base->object->flag= base->flag; + } +} + +/* also to set active NULL */ +void ED_base_object_activate(bContext *C, Base *base) +{ + Scene *scene= CTX_data_scene(C); + Base *tbase; + + /* sets scene->basact */ + BASACT= base; + + if(base) { + + /* XXX old signals, remember to handle notifiers now! */ + // select_actionchannel_by_name(base->object->action, "Object", 1); + + /* disable temporal locks */ + for(tbase=FIRSTBASE; tbase; tbase= tbase->next) { + if(base!=tbase && (tbase->object->shapeflag & OB_SHAPE_TEMPLOCK)) { + tbase->object->shapeflag &= ~OB_SHAPE_TEMPLOCK; + DAG_id_flush_update(&tbase->object->id, OB_RECALC_DATA); + } + } + WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene); + } + else + WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, NULL); +} + +/********************** Selection Operators **********************/ + +static EnumPropertyItem prop_select_types[] = { + {0, "EXCLUSIVE", 0, "Exclusive", ""}, + {1, "EXTEND", 0, "Extend", ""}, + {0, NULL, 0, NULL, NULL} +}; + +/************************ Select by Type *************************/ + +static int object_select_by_type_exec(bContext *C, wmOperator *op) +{ + short obtype, seltype; + + obtype = RNA_enum_get(op->ptr, "type"); + seltype = RNA_enum_get(op->ptr, "seltype"); + + if (seltype == 0) { + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + ED_base_object_select(base, BA_DESELECT); + } + CTX_DATA_END; + } + + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + if(base->object->type==obtype) { + ED_base_object_select(base, BA_SELECT); + } + } + CTX_DATA_END; + + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_select_by_type(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Select By Type"; + ot->description = "Select all visible objects that are of a type."; + ot->idname= "OBJECT_OT_select_by_type"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_select_by_type_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "seltype", prop_select_types, 0, "Selection", "Extend selection or clear selection then select"); + RNA_def_enum(ot->srna, "type", object_type_items, 1, "Type", ""); + +} + +/*********************** Selection by Links *********************/ + +static EnumPropertyItem prop_select_linked_types[] = { + {1, "IPO", 0, "Object IPO", ""}, // XXX depreceated animation system stuff... + {2, "OBDATA", 0, "Ob Data", ""}, + {3, "MATERIAL", 0, "Material", ""}, + {4, "TEXTURE", 0, "Texture", ""}, + {5, "DUPGROUP", 0, "Dupligroup", ""}, + {6, "PARTICLE", 0, "Particle System", ""}, + {0, NULL, 0, NULL, NULL} +}; + +static int object_select_linked_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Object *ob; + void *obdata = NULL; + Material *mat = NULL, *mat1; + Tex *tex=0; + int a, b; + int nr = RNA_enum_get(op->ptr, "type"); + short changed = 0, seltype; + /* events (nr): + * Object Ipo: 1 + * ObData: 2 + * Current Material: 3 + * Current Texture: 4 + * DupliGroup: 5 + * PSys: 6 + */ + + seltype = RNA_enum_get(op->ptr, "seltype"); + + if (seltype == 0) { + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + ED_base_object_select(base, BA_DESELECT); + } + CTX_DATA_END; + } + + ob= OBACT; + if(ob==0){ + BKE_report(op->reports, RPT_ERROR, "No Active Object"); + return OPERATOR_CANCELLED; + } + + if(nr==1) { + // XXX old animation system + //ipo= ob->ipo; + //if(ipo==0) return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; + } + else if(nr==2) { + if(ob->data==0) return OPERATOR_CANCELLED; + obdata= ob->data; + } + else if(nr==3 || nr==4) { + mat= give_current_material(ob, ob->actcol); + if(mat==0) return OPERATOR_CANCELLED; + if(nr==4) { + if(mat->mtex[ (int)mat->texact ]) tex= mat->mtex[ (int)mat->texact ]->tex; + if(tex==0) return OPERATOR_CANCELLED; + } + } + else if(nr==5) { + if(ob->dup_group==NULL) return OPERATOR_CANCELLED; + } + else if(nr==6) { + if(ob->particlesystem.first==NULL) return OPERATOR_CANCELLED; + } + else return OPERATOR_CANCELLED; + + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + if(nr==1) { + // XXX old animation system + //if(base->object->ipo==ipo) base->flag |= SELECT; + //changed = 1; + } + else if(nr==2) { + if(base->object->data==obdata) base->flag |= SELECT; + changed = 1; + } + else if(nr==3 || nr==4) { + ob= base->object; + + for(a=1; a<=ob->totcol; a++) { + mat1= give_current_material(ob, a); + if(nr==3) { + if(mat1==mat) base->flag |= SELECT; + changed = 1; + } + else if(mat1 && nr==4) { + for(b=0; bmtex[b]) { + if(tex==mat1->mtex[b]->tex) { + base->flag |= SELECT; + changed = 1; + break; + } + } + } + } + } + } + else if(nr==5) { + if(base->object->dup_group==ob->dup_group) { + base->flag |= SELECT; + changed = 1; + } + } + else if(nr==6) { + /* loop through other, then actives particles*/ + ParticleSystem *psys; + ParticleSystem *psys_act; + + for(psys=base->object->particlesystem.first; psys; psys=psys->next) { + for(psys_act=ob->particlesystem.first; psys_act; psys_act=psys_act->next) { + if (psys->part == psys_act->part) { + base->flag |= SELECT; + changed = 1; + break; + } + } + + if (base->flag & SELECT) { + break; + } + } + } + base->object->flag= base->flag; + } + CTX_DATA_END; + + if (changed) { + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); + return OPERATOR_FINISHED; + } + + return OPERATOR_CANCELLED; +} + +void OBJECT_OT_select_linked(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Select Linked"; + ot->description = "Select all visible objects that are linked."; + ot->idname= "OBJECT_OT_select_linked"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_select_linked_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", prop_select_linked_types, 0, "Type", ""); + RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); + +} + +/*********************** Selected Grouped ********************/ + +static EnumPropertyItem prop_select_grouped_types[] = { + {1, "CHILDREN_RECURSIVE", 0, "Children", ""}, + {2, "CHILDREN", 0, "Immediate Children", ""}, + {3, "PARENT", 0, "Parent", ""}, + {4, "SIBLINGS", 0, "Siblings", "Shared Parent"}, + {5, "TYPE", 0, "Type", "Shared object type"}, + {6, "LAYER", 0, "Layer", "Shared layers"}, + {7, "GROUP", 0, "Group", "Shared group"}, + {8, "HOOK", 0, "Hook", ""}, + {9, "PASS", 0, "Pass", "Render pass Index"}, + {10, "COLOR", 0, "Color", "Object Color"}, + {11, "PROPERTIES", 0, "Properties", "Game Properties"}, + {0, NULL, 0, NULL, NULL} +}; + +static short select_grouped_children(bContext *C, Object *ob, int recursive) +{ + short changed = 0; + + CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { + if (ob == base->object->parent) { + if (!(base->flag & SELECT)) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + + if (recursive) + changed |= select_grouped_children(C, base->object, 1); + } + } + CTX_DATA_END; + return changed; +} + +static short select_grouped_parent(bContext *C) /* Makes parent active and de-selected OBACT */ +{ + Scene *scene= CTX_data_scene(C); + View3D *v3d= CTX_wm_view3d(C); + + short changed = 0; + Base *baspar, *basact= CTX_data_active_base(C); + + if (!basact || !(basact->object->parent)) return 0; /* we know OBACT is valid */ + + baspar= object_in_scene(basact->object->parent, scene); + + /* can be NULL if parent in other scene */ + if(baspar && BASE_SELECTABLE(v3d, baspar)) { + ED_base_object_select(basact, BA_DESELECT); + ED_base_object_select(baspar, BA_SELECT); + ED_base_object_activate(C, baspar); + changed = 1; + } + return changed; +} + + +#define GROUP_MENU_MAX 24 +static short select_grouped_group(bContext *C, Object *ob) /* Select objects in the same group as the active */ +{ + short changed = 0; + Group *group, *ob_groups[GROUP_MENU_MAX]; + //char str[10 + (24*GROUP_MENU_MAX)]; + //char *p = str; + int group_count=0; //, menu, i; + + for ( group=G.main->group.first; + group && group_count < GROUP_MENU_MAX; + group=group->id.next + ) { + if (object_in_group (ob, group)) { + ob_groups[group_count] = group; + group_count++; + } + } + + if (!group_count) + return 0; + + else if (group_count == 1) { + group = ob_groups[0]; + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + if (!(base->flag & SELECT) && object_in_group(base->object, group)) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + } + CTX_DATA_END; + return changed; + } +#if 0 // XXX hows this work in 2.5? + /* build the menu. */ + p += sprintf(str, "Groups%%t"); + for (i=0; iid.name+2, i); + } + + menu = pupmenu (str); + if (menu == -1) + return 0; + + group = ob_groups[menu]; + for (base= FIRSTBASE; base; base= base->next) { + if (!(base->flag & SELECT) && object_in_group(base->object, group)) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + } +#endif + return changed; +} + +static short select_grouped_object_hooks(bContext *C, Object *ob) +{ + Scene *scene= CTX_data_scene(C); + View3D *v3d= CTX_wm_view3d(C); + + short changed = 0; + Base *base; + ModifierData *md; + HookModifierData *hmd; + + for (md = ob->modifiers.first; md; md=md->next) { + if (md->type==eModifierType_Hook) { + hmd= (HookModifierData*) md; + if (hmd->object && !(hmd->object->flag & SELECT)) { + base= object_in_scene(hmd->object, scene); + if (base && (BASE_SELECTABLE(v3d, base))) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + } + } + } + return changed; +} + +/* Select objects woth the same parent as the active (siblings), + * parent can be NULL also */ +static short select_grouped_siblings(bContext *C, Object *ob) +{ + short changed = 0; + + CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { + if ((base->object->parent==ob->parent) && !(base->flag & SELECT)) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + } + CTX_DATA_END; + return changed; +} + +static short select_grouped_type(bContext *C, Object *ob) +{ + short changed = 0; + + CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { + if ((base->object->type == ob->type) && !(base->flag & SELECT)) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + } + CTX_DATA_END; + return changed; +} + +static short select_grouped_layer(bContext *C, Object *ob) +{ + char changed = 0; + + CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { + if ((base->lay & ob->lay) && !(base->flag & SELECT)) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + } + CTX_DATA_END; + return changed; +} + +static short select_grouped_index_object(bContext *C, Object *ob) +{ + char changed = 0; + + CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { + if ((base->object->index == ob->index) && !(base->flag & SELECT)) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + } + CTX_DATA_END; + return changed; +} + +static short select_grouped_color(bContext *C, Object *ob) +{ + char changed = 0; + + CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { + if (!(base->flag & SELECT) && (FloatCompare(base->object->col, ob->col, 0.005f))) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + } + CTX_DATA_END; + return changed; +} + +static short objects_share_gameprop(Object *a, Object *b) +{ + bProperty *prop; + /*make a copy of all its properties*/ + + for( prop= a->prop.first; prop; prop = prop->next ) { + if ( get_ob_property(b, prop->name) ) + return 1; + } + return 0; +} + +static short select_grouped_gameprops(bContext *C, Object *ob) +{ + char changed = 0; + + CTX_DATA_BEGIN(C, Base*, base, selectable_bases) { + if (!(base->flag & SELECT) && (objects_share_gameprop(base->object, ob))) { + ED_base_object_select(base, BA_SELECT); + changed = 1; + } + } + CTX_DATA_END; + return changed; +} + +static int object_select_grouped_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Object *ob; + int nr = RNA_enum_get(op->ptr, "type"); + short changed = 0, seltype; + + seltype = RNA_enum_get(op->ptr, "seltype"); + + if (seltype == 0) { + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + ED_base_object_select(base, BA_DESELECT); + } + CTX_DATA_END; + } + + ob= OBACT; + if(ob==0){ + BKE_report(op->reports, RPT_ERROR, "No Active Object"); + return OPERATOR_CANCELLED; + } + + if(nr==1) changed = select_grouped_children(C, ob, 1); + else if(nr==2) changed = select_grouped_children(C, ob, 0); + else if(nr==3) changed = select_grouped_parent(C); + else if(nr==4) changed = select_grouped_siblings(C, ob); + else if(nr==5) changed = select_grouped_type(C, ob); + else if(nr==6) changed = select_grouped_layer(C, ob); + else if(nr==7) changed = select_grouped_group(C, ob); + else if(nr==8) changed = select_grouped_object_hooks(C, ob); + else if(nr==9) changed = select_grouped_index_object(C, ob); + else if(nr==10) changed = select_grouped_color(C, ob); + else if(nr==11) changed = select_grouped_gameprops(C, ob); + + if (changed) { + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); + return OPERATOR_FINISHED; + } + + return OPERATOR_CANCELLED; +} + +void OBJECT_OT_select_grouped(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Select Grouped"; + ot->description = "Select all visible objects grouped by various properties."; + ot->idname= "OBJECT_OT_select_grouped"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_select_grouped_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_enum(ot->srna, "type", prop_select_grouped_types, 0, "Type", ""); + RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); +} + +/************************* Select by Layer **********************/ + +static int object_select_by_layer_exec(bContext *C, wmOperator *op) +{ + unsigned int layernum; + short seltype; + + seltype = RNA_enum_get(op->ptr, "seltype"); + layernum = RNA_int_get(op->ptr, "layer"); + + if (seltype == 0) { + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + ED_base_object_select(base, BA_DESELECT); + } + CTX_DATA_END; + } + + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + if(base->lay == (1<< (layernum -1))) + ED_base_object_select(base, BA_SELECT); + } + CTX_DATA_END; + + /* undo? */ + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_select_by_layer(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "select by layer"; + ot->description = "Select all visible objects on a layer."; + ot->idname= "OBJECT_OT_select_by_layer"; + + /* api callbacks */ + /*ot->invoke = XXX - need a int grid popup*/ + ot->exec= object_select_by_layer_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_int(ot->srna, "layer", 1, 1, 20, "Layer", "", 1, 20); + RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); +} + +/************************** Select Inverse *************************/ + +static int object_select_inverse_exec(bContext *C, wmOperator *op) +{ + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + if (base->flag & SELECT) + ED_base_object_select(base, BA_DESELECT); + else + ED_base_object_select(base, BA_SELECT); + } + CTX_DATA_END; + + /* undo? */ + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_select_inverse(wmOperatorType *ot) +{ + + /* identifiers */ + ot->name= "Select Inverse"; + ot->description = "Invert selection of all visible objects."; + ot->idname= "OBJECT_OT_select_inverse"; + + /* api callbacks */ + ot->exec= object_select_inverse_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + +} + +/**************************** (De)select All ****************************/ + +static int object_select_de_select_all_exec(bContext *C, wmOperator *op) +{ + + int a=0, ok=0; + + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + if (base->flag & SELECT) { + ok= a= 1; + break; + } + else ok=1; + } + CTX_DATA_END; + + if (!ok) return OPERATOR_PASS_THROUGH; + + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + if (a) ED_base_object_select(base, BA_DESELECT); + else ED_base_object_select(base, BA_SELECT); + } + CTX_DATA_END; + + /* undo? */ + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_select_all_toggle(wmOperatorType *ot) +{ + + /* identifiers */ + ot->name= "deselect all"; + ot->description = "(de)select all visible objects in scene."; + ot->idname= "OBJECT_OT_select_all_toggle"; + + /* api callbacks */ + ot->exec= object_select_de_select_all_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + +} + +/**************************** Select Mirror ****************************/ + +/* finds the best possible flipped name. For renaming; check for unique names afterwards */ +/* if strip_number: removes number extensions */ +void object_flip_name (char *name) +{ + int len; + char prefix[128]={""}; /* The part before the facing */ + char suffix[128]={""}; /* The part after the facing */ + char replace[128]={""}; /* The replacement string */ + char number[128]={""}; /* The number extension string */ + char *index=NULL; + + len= strlen(name); + if(len<3) return; // we don't do names like .R or .L + + /* We first check the case with a .### extension, let's find the last period */ + if(isdigit(name[len-1])) { + index= strrchr(name, '.'); // last occurrance + if (index && isdigit(index[1]) ) { // doesnt handle case bone.1abc2 correct..., whatever! + strcpy(number, index); + *index= 0; + len= strlen(name); + } + } + + strcpy (prefix, name); + +#define IS_SEPARATOR(a) ((a)=='.' || (a)==' ' || (a)=='-' || (a)=='_') + + /* first case; separator . - _ with extensions r R l L */ + if( IS_SEPARATOR(name[len-2]) ) { + switch(name[len-1]) { + case 'l': + prefix[len-1]= 0; + strcpy(replace, "r"); + break; + case 'r': + prefix[len-1]= 0; + strcpy(replace, "l"); + break; + case 'L': + prefix[len-1]= 0; + strcpy(replace, "R"); + break; + case 'R': + prefix[len-1]= 0; + strcpy(replace, "L"); + break; + } + } + /* case; beginning with r R l L , with separator after it */ + else if( IS_SEPARATOR(name[1]) ) { + switch(name[0]) { + case 'l': + strcpy(replace, "r"); + strcpy(suffix, name+1); + prefix[0]= 0; + break; + case 'r': + strcpy(replace, "l"); + strcpy(suffix, name+1); + prefix[0]= 0; + break; + case 'L': + strcpy(replace, "R"); + strcpy(suffix, name+1); + prefix[0]= 0; + break; + case 'R': + strcpy(replace, "L"); + strcpy(suffix, name+1); + prefix[0]= 0; + break; + } + } + else if(len > 5) { + /* hrms, why test for a separator? lets do the rule 'ultimate left or right' */ + index = BLI_strcasestr(prefix, "right"); + if (index==prefix || index==prefix+len-5) { + if(index[0]=='r') + strcpy (replace, "left"); + else { + if(index[1]=='I') + strcpy (replace, "LEFT"); + else + strcpy (replace, "Left"); + } + *index= 0; + strcpy (suffix, index+5); + } + else { + index = BLI_strcasestr(prefix, "left"); + if (index==prefix || index==prefix+len-4) { + if(index[0]=='l') + strcpy (replace, "right"); + else { + if(index[1]=='E') + strcpy (replace, "RIGHT"); + else + strcpy (replace, "Right"); + } + *index= 0; + strcpy (suffix, index+4); + } + } + } + +#undef IS_SEPARATOR + + sprintf (name, "%s%s%s%s", prefix, replace, suffix, number); +} + +static int object_select_mirror_exec(bContext *C, wmOperator *op) +{ + char tmpname[32]; + short seltype; + + seltype = RNA_enum_get(op->ptr, "seltype"); + + CTX_DATA_BEGIN(C, Base*, primbase, selected_bases) { + + strcpy(tmpname, primbase->object->id.name+2); + object_flip_name(tmpname); + + CTX_DATA_BEGIN(C, Base*, secbase, visible_bases) { + if(!strcmp(secbase->object->id.name+2, tmpname)) { + ED_base_object_select(secbase, BA_SELECT); + } + } + CTX_DATA_END; + + if (seltype == 0) ED_base_object_select(primbase, BA_DESELECT); + + } + CTX_DATA_END; + + /* undo? */ + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_select_mirror(wmOperatorType *ot) +{ + + /* identifiers */ + ot->name= "Select Mirror"; + ot->description = "Select the Mirror objects of the selected object eg. L.sword -> R.sword"; + ot->idname= "OBJECT_OT_select_mirror"; + + /* api callbacks */ + ot->exec= object_select_mirror_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); +} + +/**************************** Select Random ****************************/ + +static int object_select_random_exec(bContext *C, wmOperator *op) +{ + float percent; + short seltype; + + seltype = RNA_enum_get(op->ptr, "seltype"); + + if (seltype == 0) { + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + ED_base_object_select(base, BA_DESELECT); + } + CTX_DATA_END; + } + percent = RNA_float_get(op->ptr, "percent"); + + CTX_DATA_BEGIN(C, Base*, base, visible_bases) { + if (BLI_frand() < percent) { + ED_base_object_select(base, BA_SELECT); + } + } + CTX_DATA_END; + + WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C)); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_select_random(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Random select"; + ot->description = "Set select on random visible objects."; + ot->idname= "OBJECT_OT_select_random"; + + /* api callbacks */ + /*ot->invoke= object_select_random_invoke XXX - need a number popup ;*/ + ot->exec = object_select_random_exec; + ot->poll= ED_operator_scene_editable; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_float_percentage(ot->srna, "percent", 0.5f, 0.0f, 1.0f, "Percent", "percentage of objects to randomly select", 0.0001f, 1.0f); + RNA_def_enum(ot->srna, "seltype", prop_select_types, 1, "Selection", "Extend selection or clear selection then select"); +} + + diff --git a/source/blender/editors/object/object_shapekey.c b/source/blender/editors/object/object_shapekey.c new file mode 100644 index 00000000000..2ec3edd846a --- /dev/null +++ b/source/blender/editors/object/object_shapekey.c @@ -0,0 +1,539 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * Contributor(s): Blender Foundation, shapekey support + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include +#include + +#ifndef WIN32 +#include +#else +#include +#endif + +#include "MEM_guardedalloc.h" + +#include "BLI_blenlib.h" +#include "BLI_arithb.h" + +#include "DNA_action_types.h" +#include "DNA_curve_types.h" +#include "DNA_ipo_types.h" +#include "DNA_key_types.h" +#include "DNA_lattice_types.h" +#include "DNA_mesh_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" +#include "DNA_screen_types.h" +#include "DNA_space_types.h" +#include "DNA_userdef_types.h" +#include "DNA_view2d_types.h" + +#include "BKE_action.h" +#include "BKE_anim.h" +#include "BKE_context.h" +#include "BKE_curve.h" +#include "BKE_depsgraph.h" +#include "BKE_global.h" +#include "BKE_ipo.h" +#include "BKE_key.h" +#include "BKE_library.h" +#include "BKE_main.h" +#include "BKE_mesh.h" +#include "BKE_object.h" +#include "BKE_utildefines.h" + +#include "BLO_sys_types.h" // for intptr_t support + +#include "ED_object.h" + +#include "RNA_access.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "object_intern.h" + +#if 0 // XXX old animation system +static void default_key_ipo(Scene *scene, Key *key) +{ + IpoCurve *icu; + BezTriple *bezt; + + key->ipo= add_ipo(scene, "KeyIpo", ID_KE); + + icu= MEM_callocN(sizeof(IpoCurve), "ipocurve"); + + icu->blocktype= ID_KE; + icu->adrcode= KEY_SPEED; + icu->flag= IPO_VISIBLE|IPO_SELECT|IPO_AUTO_HORIZ; + set_icu_vars(icu); + + BLI_addtail( &(key->ipo->curve), icu); + + icu->bezt= bezt= MEM_callocN(2*sizeof(BezTriple), "defaultipo"); + icu->totvert= 2; + + bezt->hide= IPO_BEZ; + bezt->f1=bezt->f2= bezt->f3= SELECT; + bezt->h1= bezt->h2= HD_AUTO; + bezt++; + bezt->vec[1][0]= 100.0; + bezt->vec[1][1]= 1.0; + bezt->hide= IPO_BEZ; + bezt->f1=bezt->f2= bezt->f3= SELECT; + bezt->h1= bezt->h2= HD_AUTO; + + calchandles_ipocurve(icu); +} +#endif // XXX old animation system + + +/************************* Mesh ************************/ + +void mesh_to_key(Mesh *me, KeyBlock *kb) +{ + MVert *mvert; + float *fp; + int a; + + if(me->totvert==0) return; + + if(kb->data) MEM_freeN(kb->data); + + kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data"); + kb->totelem= me->totvert; + + mvert= me->mvert; + fp= kb->data; + for(a=0; atotelem; a++, fp+=3, mvert++) { + VECCOPY(fp, mvert->co); + + } +} + +void key_to_mesh(KeyBlock *kb, Mesh *me) +{ + MVert *mvert; + float *fp; + int a, tot; + + mvert= me->mvert; + fp= kb->data; + + tot= MIN2(kb->totelem, me->totvert); + + for(a=0; aco, fp); + } +} + +static KeyBlock *add_keyblock(Scene *scene, Key *key) +{ + KeyBlock *kb; + float curpos= -0.1; + int tot; + + kb= key->block.last; + if(kb) curpos= kb->pos; + + kb= MEM_callocN(sizeof(KeyBlock), "Keyblock"); + BLI_addtail(&key->block, kb); + kb->type= KEY_CARDINAL; + + tot= BLI_countlist(&key->block); + if(tot==1) strcpy(kb->name, "Basis"); + else sprintf(kb->name, "Key %d", tot-1); + + // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey) + kb->adrcode= tot-1; + + key->totkey++; + if(key->totkey==1) key->refkey= kb; + + kb->slidermin= 0.0f; + kb->slidermax= 1.0f; + + // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor... + if(key->type == KEY_RELATIVE) + kb->pos= curpos+0.1; + else { +#if 0 // XXX old animation system + curpos= bsystem_time(scene, 0, (float)CFRA, 0.0); + if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) { + curpos /= 100.0; + } + kb->pos= curpos; + + sort_keys(key); +#endif // XXX old animation system + } + return kb; +} + +void insert_meshkey(Scene *scene, Mesh *me, short rel) +{ + Key *key; + KeyBlock *kb; + + if(me->key==NULL) { + me->key= add_key( (ID *)me); + + if(rel) + me->key->type = KEY_RELATIVE; +// else +// default_key_ipo(scene, me->key); // XXX old animation system + } + key= me->key; + + kb= add_keyblock(scene, key); + + mesh_to_key(me, kb); +} + +/************************* Lattice ************************/ + +void latt_to_key(Lattice *lt, KeyBlock *kb) +{ + BPoint *bp; + float *fp; + int a, tot; + + tot= lt->pntsu*lt->pntsv*lt->pntsw; + if(tot==0) return; + + if(kb->data) MEM_freeN(kb->data); + + kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data"); + kb->totelem= tot; + + bp= lt->def; + fp= kb->data; + for(a=0; atotelem; a++, fp+=3, bp++) { + VECCOPY(fp, bp->vec); + } +} + +void key_to_latt(KeyBlock *kb, Lattice *lt) +{ + BPoint *bp; + float *fp; + int a, tot; + + bp= lt->def; + fp= kb->data; + + tot= lt->pntsu*lt->pntsv*lt->pntsw; + tot= MIN2(kb->totelem, tot); + + for(a=0; avec, fp); + } + +} + +/* exported to python... hrms, should not, use object levels! (ton) */ +void insert_lattkey(Scene *scene, Lattice *lt, short rel) +{ + Key *key; + KeyBlock *kb; + + if(lt->key==NULL) { + lt->key= add_key( (ID *)lt); +// default_key_ipo(scene, lt->key); // XXX old animation system + } + key= lt->key; + + kb= add_keyblock(scene, key); + + latt_to_key(lt, kb); +} + +/************************* Curve ************************/ + +void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb) +{ + Nurb *nu; + BezTriple *bezt; + BPoint *bp; + float *fp; + int a, tot; + + /* count */ + tot= count_curveverts(nurb); + if(tot==0) return; + + if(kb->data) MEM_freeN(kb->data); + + kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data"); + kb->totelem= tot; + + nu= nurb->first; + fp= kb->data; + while(nu) { + + if(nu->bezt) { + bezt= nu->bezt; + a= nu->pntsu; + while(a--) { + VECCOPY(fp, bezt->vec[0]); + fp+= 3; + VECCOPY(fp, bezt->vec[1]); + fp+= 3; + VECCOPY(fp, bezt->vec[2]); + fp+= 3; + fp[0]= bezt->alfa; + fp+= 3; /* alphas */ + bezt++; + } + } + else { + bp= nu->bp; + a= nu->pntsu*nu->pntsv; + while(a--) { + VECCOPY(fp, bp->vec); + fp[3]= bp->alfa; + + fp+= 4; + bp++; + } + } + nu= nu->next; + } +} + +void key_to_curve(KeyBlock *kb, Curve *cu, ListBase *nurb) +{ + Nurb *nu; + BezTriple *bezt; + BPoint *bp; + float *fp; + int a, tot; + + nu= nurb->first; + fp= kb->data; + + tot= count_curveverts(nurb); + + tot= MIN2(kb->totelem, tot); + + while(nu && tot>0) { + + if(nu->bezt) { + bezt= nu->bezt; + a= nu->pntsu; + while(a-- && tot>0) { + VECCOPY(bezt->vec[0], fp); + fp+= 3; + VECCOPY(bezt->vec[1], fp); + fp+= 3; + VECCOPY(bezt->vec[2], fp); + fp+= 3; + bezt->alfa= fp[0]; + fp+= 3; /* alphas */ + + tot-= 3; + bezt++; + } + } + else { + bp= nu->bp; + a= nu->pntsu*nu->pntsv; + while(a-- && tot>0) { + VECCOPY(bp->vec, fp); + bp->alfa= fp[3]; + + fp+= 4; + tot--; + bp++; + } + } + nu= nu->next; + } +} + + +void insert_curvekey(Scene *scene, Curve *cu, short rel) +{ + Key *key; + KeyBlock *kb; + + if(cu->key==NULL) { + cu->key= add_key( (ID *)cu); + + if(rel) + cu->key->type = KEY_RELATIVE; +// else +// default_key_ipo(scene, cu->key); // XXX old animation system + } + key= cu->key; + + kb= add_keyblock(scene, key); + + if(cu->editnurb->first) curve_to_key(cu, kb, cu->editnurb); + else curve_to_key(cu, kb, &cu->nurb); +} + +/*********************** add shape key ***********************/ + +void ED_object_shape_key_add(bContext *C, Scene *scene, Object *ob) +{ + Key *key; + + if(ob->type==OB_MESH) insert_meshkey(scene, ob->data, 1); + else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob->data, 1); + else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob->data, 1); + + key= ob_get_key(ob); + ob->shapenr= BLI_countlist(&key->block); + + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob); +} + +/*********************** remove shape key ***********************/ + +int ED_object_shape_key_remove(bContext *C, Scene *scene, Object *ob) +{ + Main *bmain= CTX_data_main(C); + KeyBlock *kb, *rkb; + Key *key; + //IpoCurve *icu; + + key= ob_get_key(ob); + if(key==NULL) + return 0; + + kb= BLI_findlink(&key->block, ob->shapenr-1); + + if(kb) { + for(rkb= key->block.first; rkb; rkb= rkb->next) + if(rkb->relative == ob->shapenr-1) + rkb->relative= 0; + + BLI_remlink(&key->block, kb); + key->totkey--; + if(key->refkey== kb) + key->refkey= key->block.first; + + if(kb->data) MEM_freeN(kb->data); + MEM_freeN(kb); + + for(kb= key->block.first; kb; kb= kb->next) + if(kb->adrcode>=ob->shapenr) + kb->adrcode--; + +#if 0 // XXX old animation system + if(key->ipo) { + + for(icu= key->ipo->curve.first; icu; icu= icu->next) { + if(icu->adrcode==ob->shapenr-1) { + BLI_remlink(&key->ipo->curve, icu); + free_ipo_curve(icu); + break; + } + } + for(icu= key->ipo->curve.first; icu; icu= icu->next) + if(icu->adrcode>=ob->shapenr) + icu->adrcode--; + } +#endif // XXX old animation system + + if(ob->shapenr>1) ob->shapenr--; + } + + if(key->totkey==0) { + if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL; + else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL; + else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL; + + free_libblock_us(&(bmain->key), key); + } + + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob); + + return 1; +} + +/********************** shape key operators *********************/ + +static int shape_key_poll(bContext *C) +{ + Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; + ID *data= (ob)? ob->data: NULL; + return (ob && !ob->id.lib && data && !data->lib); +} + +static int shape_key_add_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; + + ED_object_shape_key_add(C, scene, ob); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_shape_key_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Add Shape Key"; + ot->idname= "OBJECT_OT_shape_key_add"; + + /* api callbacks */ + ot->poll= shape_key_poll; + ot->exec= shape_key_add_exec; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int shape_key_remove_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; + + if(!ED_object_shape_key_remove(C, scene, ob)) + return OPERATOR_CANCELLED; + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_shape_key_remove(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Remove Shape Key"; + ot->idname= "OBJECT_OT_shape_key_remove"; + + /* api callbacks */ + ot->poll= shape_key_poll; + ot->exec= shape_key_remove_exec; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + diff --git a/source/blender/editors/object/object_transform.c b/source/blender/editors/object/object_transform.c new file mode 100644 index 00000000000..2b207f2f27c --- /dev/null +++ b/source/blender/editors/object/object_transform.c @@ -0,0 +1,926 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * Contributor(s): Blender Foundation, 2002-2008 full recode + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include + +#include "DNA_armature_types.h" +#include "DNA_curve_types.h" +#include "DNA_key_types.h" +#include "DNA_mesh_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" +#include "DNA_screen_types.h" +#include "DNA_view3d_types.h" + +#include "BLI_arithb.h" +#include "BLI_editVert.h" +#include "BLI_listbase.h" + +#include "BKE_context.h" +#include "BKE_curve.h" +#include "BKE_depsgraph.h" +#include "BKE_global.h" +#include "BKE_main.h" +#include "BKE_mesh.h" +#include "BKE_object.h" +#include "BKE_report.h" +#include "BKE_utildefines.h" + +#include "RNA_define.h" +#include "RNA_access.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_anim_api.h" +#include "ED_armature.h" +#include "ED_curve.h" +#include "ED_mesh.h" +#include "ED_object.h" +#include "ED_screen.h" +#include "ED_view3d.h" + +#include "object_intern.h" + +/*************************** Clear Transformation ****************************/ + +static int object_location_clear_exec(bContext *C, wmOperator *op) +{ + int armature_clear= 0; + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) { + if((ob->protectflag & OB_LOCK_LOCX)==0) + ob->loc[0]= ob->dloc[0]= 0.0f; + if((ob->protectflag & OB_LOCK_LOCY)==0) + ob->loc[1]= ob->dloc[1]= 0.0f; + if((ob->protectflag & OB_LOCK_LOCZ)==0) + ob->loc[2]= ob->dloc[2]= 0.0f; + } + ob->recalc |= OB_RECALC_OB; + } + CTX_DATA_END; + + if(armature_clear==0) /* in this case flush was done */ + ED_anim_dag_flush_update(C); + + WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_location_clear(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Clear Location"; + ot->description = "Clear the object's location."; + ot->idname= "OBJECT_OT_location_clear"; + + /* api callbacks */ + ot->exec= object_location_clear_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int object_rotation_clear_exec(bContext *C, wmOperator *op) +{ + int armature_clear= 0; + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) { + /* eulers can only get cleared if they are not protected */ + if((ob->protectflag & OB_LOCK_ROTX)==0) + ob->rot[0]= ob->drot[0]= 0.0f; + if((ob->protectflag & OB_LOCK_ROTY)==0) + ob->rot[1]= ob->drot[1]= 0.0f; + if((ob->protectflag & OB_LOCK_ROTZ)==0) + ob->rot[2]= ob->drot[2]= 0.0f; + } + ob->recalc |= OB_RECALC_OB; + } + CTX_DATA_END; + + if(armature_clear==0) /* in this case flush was done */ + ED_anim_dag_flush_update(C); + + WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_rotation_clear(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Clear Rotation"; + ot->description = "Clear the object's rotation."; + ot->idname= "OBJECT_OT_rotation_clear"; + + /* api callbacks */ + ot->exec= object_rotation_clear_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int object_scale_clear_exec(bContext *C, wmOperator *op) +{ + int armature_clear= 0; + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) { + if((ob->protectflag & OB_LOCK_SCALEX)==0) { + ob->dsize[0]= 0.0f; + ob->size[0]= 1.0f; + } + if((ob->protectflag & OB_LOCK_SCALEY)==0) { + ob->dsize[1]= 0.0f; + ob->size[1]= 1.0f; + } + if((ob->protectflag & OB_LOCK_SCALEZ)==0) { + ob->dsize[2]= 0.0f; + ob->size[2]= 1.0f; + } + } + ob->recalc |= OB_RECALC_OB; + } + CTX_DATA_END; + + if(armature_clear==0) /* in this case flush was done */ + ED_anim_dag_flush_update(C); + + WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_scale_clear(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Clear Scale"; + ot->description = "Clear the object's scale."; + ot->idname= "OBJECT_OT_scale_clear"; + + /* api callbacks */ + ot->exec= object_scale_clear_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int object_origin_clear_exec(bContext *C, wmOperator *op) +{ + float *v1, *v3, mat[3][3]; + int armature_clear= 0; + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + if(ob->parent) { + v1= ob->loc; + v3= ob->parentinv[3]; + + Mat3CpyMat4(mat, ob->parentinv); + VECCOPY(v3, v1); + v3[0]= -v3[0]; + v3[1]= -v3[1]; + v3[2]= -v3[2]; + Mat3MulVecfl(mat, v3); + } + ob->recalc |= OB_RECALC_OB; + } + CTX_DATA_END; + + if(armature_clear==0) /* in this case flush was done */ + ED_anim_dag_flush_update(C); + + WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_origin_clear(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Clear Origin"; + ot->description = "Clear the object's origin."; + ot->idname= "OBJECT_OT_origin_clear"; + + /* api callbacks */ + ot->exec= object_origin_clear_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/*************************** Apply Transformation ****************************/ + +/* use this when the loc/size/rot of the parent has changed but the children + * should stay in the same place, e.g. for apply-size-rot or object center */ +static void ignore_parent_tx(Main *bmain, Scene *scene, Object *ob ) +{ + Object workob; + Object *ob_child; + + /* a change was made, adjust the children to compensate */ + for(ob_child=bmain->object.first; ob_child; ob_child=ob_child->id.next) { + if(ob_child->parent == ob) { + ED_object_apply_obmat(ob_child); + what_does_parent(scene, ob_child, &workob); + Mat4Invert(ob_child->parentinv, workob.obmat); + } + } +} + +static int apply_objects_internal(bContext *C, ReportList *reports, int apply_loc, int apply_scale, int apply_rot) +{ + Main *bmain= CTX_data_main(C); + Scene *scene= CTX_data_scene(C); + Object *ob; + bArmature *arm; + Mesh *me; + Curve *cu; + Nurb *nu; + BPoint *bp; + BezTriple *bezt; + MVert *mvert; + float rsmat[3][3], tmat[3][3], obmat[3][3], iobmat[3][3], mat[4][4], scale; + int a, change = 0; + + /* first check if we can execute */ + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + ob= base->object; + + if(ob->type==OB_MESH) { + me= ob->data; + + if(me->id.us>1) { + BKE_report(reports, RPT_ERROR, "Can't apply to a multi user mesh, doing nothing."); + return OPERATOR_CANCELLED; + } + } + else if(ob->type==OB_ARMATURE) { + arm= ob->data; + + if(arm->id.us>1) { + BKE_report(reports, RPT_ERROR, "Can't apply to a multi user armature, doing nothing."); + return OPERATOR_CANCELLED; + } + } + else if(ELEM(ob->type, OB_CURVE, OB_SURF)) { + cu= ob->data; + + if(cu->id.us>1) { + BKE_report(reports, RPT_ERROR, "Can't apply to a multi user curve, doing nothing."); + return OPERATOR_CANCELLED; + } + if(cu->key) { + BKE_report(reports, RPT_ERROR, "Can't apply to a curve with vertex keys, doing nothing."); + return OPERATOR_CANCELLED; + } + } + } + CTX_DATA_END; + + /* now execute */ + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + ob= base->object; + + /* calculate rotation/scale matrix */ + if(apply_scale && apply_rot) + object_to_mat3(ob, rsmat); + else if(apply_scale) + object_scale_to_mat3(ob, rsmat); + else if(apply_rot) + object_rot_to_mat3(ob, rsmat); + else + Mat3One(rsmat); + + Mat4CpyMat3(mat, rsmat); + + /* calculate translation */ + if(apply_loc) { + VecCopyf(mat[3], ob->loc); + + if(!(apply_scale && apply_rot)) { + /* correct for scale and rotation that is still applied */ + object_to_mat3(ob, obmat); + Mat3Inv(iobmat, obmat); + Mat3MulMat3(tmat, rsmat, iobmat); + Mat3MulVecfl(tmat, mat[3]); + } + } + + /* apply to object data */ + if(ob->type==OB_MESH) { + me= ob->data; + + /* adjust data */ + mvert= me->mvert; + for(a=0; atotvert; a++, mvert++) + Mat4MulVecfl(mat, mvert->co); + + if(me->key) { + KeyBlock *kb; + + for(kb=me->key->block.first; kb; kb=kb->next) { + float *fp= kb->data; + + for(a=0; atotelem; a++, fp+=3) + Mat4MulVecfl(mat, fp); + } + } + + /* update normals */ + mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL); + } + else if (ob->type==OB_ARMATURE) { + ED_armature_apply_transform(ob, mat); + } + else if(ELEM(ob->type, OB_CURVE, OB_SURF)) { + cu= ob->data; + + scale = Mat3ToScalef(rsmat); + + for(nu=cu->nurb.first; nu; nu=nu->next) { + if(nu->type == CU_BEZIER) { + a= nu->pntsu; + for(bezt= nu->bezt; a--; bezt++) { + Mat4MulVecfl(mat, bezt->vec[0]); + Mat4MulVecfl(mat, bezt->vec[1]); + Mat4MulVecfl(mat, bezt->vec[2]); + bezt->radius *= scale; + bezt++; + } + } + else { + a= nu->pntsu*nu->pntsv; + for(bp= nu->bp; a--; bp++) + Mat4MulVecfl(mat, bp->vec); + } + } + } + else + continue; + + if(apply_loc) + ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0f; + if(apply_scale) + ob->size[0]= ob->size[1]= ob->size[2]= 1.0f; + if(apply_rot) + ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0f; + + where_is_object(scene, ob); + ignore_parent_tx(bmain, scene, ob); + + DAG_id_flush_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA); + + change = 1; + } + CTX_DATA_END; + + if(!change) + return OPERATOR_CANCELLED; + + WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); + return OPERATOR_FINISHED; +} + +static int visual_transform_apply_exec(bContext *C, wmOperator *op) +{ + Scene *scene= CTX_data_scene(C); + int change = 0; + + CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) { + where_is_object(scene, ob); + + VECCOPY(ob->loc, ob->obmat[3]); + Mat4ToSize(ob->obmat, ob->size); + Mat4ToEul(ob->obmat, ob->rot); + + where_is_object(scene, ob); + + change = 1; + } + CTX_DATA_END; + + if(!change) + return OPERATOR_CANCELLED; + + WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL); + return OPERATOR_FINISHED; +} + +void OBJECT_OT_visual_transform_apply(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Apply Visual Transform"; + ot->description = "Apply the object's visual transformation to its data."; + ot->idname= "OBJECT_OT_visual_transform_apply"; + + /* api callbacks */ + ot->exec= visual_transform_apply_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int location_apply_exec(bContext *C, wmOperator *op) +{ + return apply_objects_internal(C, op->reports, 1, 0, 0); +} + +void OBJECT_OT_location_apply(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Apply Location"; + ot->description = "Apply the object's location to its data."; + ot->idname= "OBJECT_OT_location_apply"; + + /* api callbacks */ + ot->exec= location_apply_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int scale_apply_exec(bContext *C, wmOperator *op) +{ + return apply_objects_internal(C, op->reports, 0, 1, 0); +} + +void OBJECT_OT_scale_apply(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Apply Scale"; + ot->description = "Apply the object's scale to its data."; + ot->idname= "OBJECT_OT_scale_apply"; + + /* api callbacks */ + ot->exec= scale_apply_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +static int rotation_apply_exec(bContext *C, wmOperator *op) +{ + return apply_objects_internal(C, op->reports, 0, 0, 1); +} + +void OBJECT_OT_rotation_apply(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Apply Rotation"; + ot->description = "Apply the object's rotation to its data."; + ot->idname= "OBJECT_OT_rotation_apply"; + + /* api callbacks */ + ot->exec= rotation_apply_exec; + ot->poll= ED_operator_object_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; +} + +/************************ Texture Space Transform ****************************/ + +void texspace_edit(Scene *scene, View3D *v3d) +{ + Base *base; + int nr=0; + + /* first test if from visible and selected objects + * texspacedraw is set: + */ + + if(scene->obedit) return; // XXX get from context + + for(base= FIRSTBASE; base; base= base->next) { + if(TESTBASELIB(v3d, base)) { + break; + } + } + + if(base==0) { + return; + } + + nr= 0; // XXX pupmenu("Texture Space %t|Grab/Move%x1|Size%x2"); + if(nr<1) return; + + for(base= FIRSTBASE; base; base= base->next) { + if(TESTBASELIB(v3d, base)) { + base->object->dtx |= OB_TEXSPACE; + } + } + + + if(nr==1) { +// XXX initTransform(TFM_TRANSLATION, CTX_TEXTURE); +// XXX Transform(); + } + else if(nr==2) { +// XXX initTransform(TFM_RESIZE, CTX_TEXTURE); +// XXX Transform(); + } + else if(nr==3) { +// XXX initTransform(TFM_ROTATION, CTX_TEXTURE); +// XXX Transform(); + } +} + +/************************ Mirror Menu ****************************/ + +void mirrormenu(void) +{ +// XXX initTransform(TFM_MIRROR, CTX_NO_PET); +// XXX Transform(); +} + +/********************* Set Object Center ************************/ + +static EnumPropertyItem prop_set_center_types[] = { + {0, "CENTER", 0, "ObData to Center", "Move object data around Object center"}, + {1, "CENTERNEW", 0, "Center New", "Move Object center to center of object data"}, + {2, "CENTERCURSOR", 0, "Center Cursor", "Move Object Center to position of the 3d cursor"}, + {0, NULL, 0, NULL, NULL} +}; + +/* 0 == do center, 1 == center new, 2 == center cursor */ +static int object_center_set_exec(bContext *C, wmOperator *op) +{ + Main *bmain= CTX_data_main(C); + Scene *scene= CTX_data_scene(C); + ScrArea *sa= CTX_wm_area(C); + View3D *v3d= sa->spacedata.first; + Object *obedit= CTX_data_edit_object(C); + Object *ob; + Mesh *me, *tme; + Curve *cu; +/* BezTriple *bezt; + BPoint *bp; */ + Nurb *nu, *nu1; + EditVert *eve; + float cent[3], centn[3], min[3], max[3], omat[3][3]; + int a, total= 0; + int centermode = RNA_enum_get(op->ptr, "type"); + + /* keep track of what is changed */ + int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0; + MVert *mvert; + + if(scene->id.lib || v3d==NULL){ + BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed on Lib data"); + return OPERATOR_CANCELLED; + } + if (obedit && centermode > 0) { + BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode"); + return OPERATOR_CANCELLED; + } + cent[0]= cent[1]= cent[2]= 0.0; + + if(obedit) { + + INIT_MINMAX(min, max); + + if(obedit->type==OB_MESH) { + Mesh *me= obedit->data; + EditMesh *em = BKE_mesh_get_editmesh(me); + + for(eve= em->verts.first; eve; eve= eve->next) { + if(v3d->around==V3D_CENTROID) { + total++; + VECADD(cent, cent, eve->co); + } + else { + DO_MINMAX(eve->co, min, max); + } + } + + if(v3d->around==V3D_CENTROID) { + VecMulf(cent, 1.0f/(float)total); + } + else { + cent[0]= (min[0]+max[0])/2.0f; + cent[1]= (min[1]+max[1])/2.0f; + cent[2]= (min[2]+max[2])/2.0f; + } + + for(eve= em->verts.first; eve; eve= eve->next) { + VecSubf(eve->co, eve->co, cent); + } + + recalc_editnormals(em); + tot_change++; + DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + BKE_mesh_end_editmesh(me, em); + } + } + + /* reset flags */ + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + base->object->flag &= ~OB_DONE; + } + CTX_DATA_END; + + for (me= G.main->mesh.first; me; me= me->id.next) { + me->flag &= ~ME_ISDONE; + } + + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + if((base->object->flag & OB_DONE)==0) { + base->object->flag |= OB_DONE; + + if(obedit==NULL && (me=get_mesh(base->object)) ) { + if (me->id.lib) { + tot_lib_error++; + } else { + if(centermode==2) { + VECCOPY(cent, give_cursor(scene, v3d)); + Mat4Invert(base->object->imat, base->object->obmat); + Mat4MulVecfl(base->object->imat, cent); + } else { + INIT_MINMAX(min, max); + mvert= me->mvert; + for(a=0; atotvert; a++, mvert++) { + DO_MINMAX(mvert->co, min, max); + } + + cent[0]= (min[0]+max[0])/2.0f; + cent[1]= (min[1]+max[1])/2.0f; + cent[2]= (min[2]+max[2])/2.0f; + } + + mvert= me->mvert; + for(a=0; atotvert; a++, mvert++) { + VecSubf(mvert->co, mvert->co, cent); + } + + if (me->key) { + KeyBlock *kb; + for (kb=me->key->block.first; kb; kb=kb->next) { + float *fp= kb->data; + + for (a=0; atotelem; a++, fp+=3) { + VecSubf(fp, fp, cent); + } + } + } + + me->flag |= ME_ISDONE; + + if(centermode) { + Mat3CpyMat4(omat, base->object->obmat); + + VECCOPY(centn, cent); + Mat3MulVecfl(omat, centn); + base->object->loc[0]+= centn[0]; + base->object->loc[1]+= centn[1]; + base->object->loc[2]+= centn[2]; + + where_is_object(scene, base->object); + ignore_parent_tx(bmain, scene, base->object); + + /* other users? */ + CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) { + ob = base->object; + if((ob->flag & OB_DONE)==0) { + tme= get_mesh(ob); + + if(tme==me) { + + ob->flag |= OB_DONE; + ob->recalc= OB_RECALC_OB|OB_RECALC_DATA; + + Mat3CpyMat4(omat, ob->obmat); + VECCOPY(centn, cent); + Mat3MulVecfl(omat, centn); + ob->loc[0]+= centn[0]; + ob->loc[1]+= centn[1]; + ob->loc[2]+= centn[2]; + + where_is_object(scene, ob); + ignore_parent_tx(bmain, scene, ob); + + if(tme && (tme->flag & ME_ISDONE)==0) { + mvert= tme->mvert; + for(a=0; atotvert; a++, mvert++) { + VecSubf(mvert->co, mvert->co, cent); + } + + if (tme->key) { + KeyBlock *kb; + for (kb=tme->key->block.first; kb; kb=kb->next) { + float *fp= kb->data; + + for (a=0; atotelem; a++, fp+=3) { + VecSubf(fp, fp, cent); + } + } + } + + tme->flag |= ME_ISDONE; + } + } + } + + ob= ob->id.next; + } + CTX_DATA_END; + } + tot_change++; + } + } + else if (ELEM(base->object->type, OB_CURVE, OB_SURF)) { + + /* weak code here... (ton) */ + if(obedit==base->object) { + ListBase *editnurb= curve_get_editcurve(obedit); + + nu1= editnurb->first; + cu= obedit->data; + } + else { + cu= base->object->data; + nu1= cu->nurb.first; + } + + if (cu->id.lib) { + tot_lib_error++; + } else { + if(centermode==2) { + VECCOPY(cent, give_cursor(scene, v3d)); + Mat4Invert(base->object->imat, base->object->obmat); + Mat4MulVecfl(base->object->imat, cent); + + /* don't allow Z change if curve is 2D */ + if( !( cu->flag & CU_3D ) ) + cent[2] = 0.0; + } + else { + INIT_MINMAX(min, max); + + nu= nu1; + while(nu) { + minmaxNurb(nu, min, max); + nu= nu->next; + } + + cent[0]= (min[0]+max[0])/2.0f; + cent[1]= (min[1]+max[1])/2.0f; + cent[2]= (min[2]+max[2])/2.0f; + } + + nu= nu1; + while(nu) { + if(nu->type == CU_BEZIER) { + a= nu->pntsu; + while (a--) { + VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent); + VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent); + VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent); + } + } + else { + a= nu->pntsu*nu->pntsv; + while (a--) + VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent); + } + nu= nu->next; + } + + if(centermode && obedit==0) { + Mat3CpyMat4(omat, base->object->obmat); + + Mat3MulVecfl(omat, cent); + base->object->loc[0]+= cent[0]; + base->object->loc[1]+= cent[1]; + base->object->loc[2]+= cent[2]; + + where_is_object(scene, base->object); + ignore_parent_tx(bmain, scene, base->object); + } + + tot_change++; + if(obedit) { + if (centermode==0) { + DAG_id_flush_update(&obedit->id, OB_RECALC_DATA); + } + break; + } + } + } + else if(base->object->type==OB_FONT) { + /* get from bb */ + + cu= base->object->data; + + if(cu->bb==0) { + /* do nothing*/ + } else if (cu->id.lib) { + tot_lib_error++; + } else { + cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]); + cu->yof= -0.5f -0.5f*( cu->bb->vec[0][1] - cu->bb->vec[2][1]); /* extra 0.5 is the height o above line */ + + /* not really ok, do this better once! */ + cu->xof /= cu->fsize; + cu->yof /= cu->fsize; + + tot_change++; + } + } + else if(base->object->type==OB_ARMATURE) { + bArmature *arm = base->object->data; + + if (arm->id.lib) { + tot_lib_error++; + } else if(arm->id.us>1) { + /*BKE_report(op->reports, RPT_ERROR, "Can't apply to a multi user armature"); + return;*/ + tot_multiuser_arm_error++; + } else { + /* Function to recenter armatures in editarmature.c + * Bone + object locations are handled there. + */ + docenter_armature(scene, v3d, base->object, centermode); + tot_change++; + + where_is_object(scene, base->object); + ignore_parent_tx(bmain, scene, base->object); + + if(obedit) + break; + } + } + base->object->recalc= OB_RECALC_OB|OB_RECALC_DATA; + } + } + CTX_DATA_END; + + if (tot_change) { + ED_anim_dag_flush_update(C); + } + + /* Warn if any errors occured */ + if (tot_lib_error+tot_multiuser_arm_error) { + BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change); + if (tot_lib_error) + BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects",tot_lib_error); + if (tot_multiuser_arm_error) + BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)",tot_multiuser_arm_error); + } + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_center_set(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Set Center"; + ot->description = "Set the object's center, by either moving the data, or set to center of data, or use 3d cursor"; + ot->idname= "OBJECT_OT_center_set"; + + /* api callbacks */ + ot->invoke= WM_menu_invoke; + ot->exec= object_center_set_exec; + + ot->poll= ED_operator_view3d_active; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", ""); +} + diff --git a/source/blender/editors/object/object_vgroup.c b/source/blender/editors/object/object_vgroup.c index 1660160b56c..6808b10b49d 100644 --- a/source/blender/editors/object/object_vgroup.c +++ b/source/blender/editors/object/object_vgroup.c @@ -25,8 +25,6 @@ * Contributor(s): none yet. * * ***** END GPL LICENSE BLOCK ***** - * Creator-specific support for vertex deformation groups - * Added: apply deform function (ton) */ #include @@ -61,6 +59,7 @@ #include "BKE_utildefines.h" #include "RNA_access.h" +#include "RNA_define.h" #include "WM_api.h" #include "WM_types.h" @@ -68,127 +67,55 @@ #include "ED_mesh.h" #include "ED_view3d.h" +#include "UI_interface.h" + #include "object_intern.h" -/* XXX */ -static void BIF_undo_push() {} -static void error() {} +/************************ Exported Functions **********************/ -static Lattice *def_get_lattice(Object *ob) +static Lattice *vgroup_edit_lattice(Object *ob) { if(ob->type==OB_LATTICE) { Lattice *lt= ob->data; - if(lt->editlatt) - return lt->editlatt; - return lt; - } - return NULL; -} - -/* only in editmode */ -void sel_verts_defgroup (Object *obedit, int select) -{ - EditVert *eve; - Object *ob; - int i; - MDeformVert *dvert; - - ob= obedit; - - if (!ob) - return; - - switch (ob->type){ - case OB_MESH: - { - Mesh *me= ob->data; - EditMesh *em = BKE_mesh_get_editmesh(me); - - for (eve=em->verts.first; eve; eve=eve->next){ - dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT); - - if (dvert && dvert->totweight){ - for (i=0; itotweight; i++){ - if (dvert->dw[i].def_nr == (ob->actdef-1)){ - if (select) eve->f |= SELECT; - else eve->f &= ~SELECT; - - break; - } - } - } - } - /* this has to be called, because this function operates on vertices only */ - if(select) EM_select_flush(em); // vertices to edges/faces - else EM_deselect_flush(em); - - BKE_mesh_end_editmesh(me, em); + return (lt->editlatt)? lt->editlatt: lt; } - break; - case OB_LATTICE: - { - Lattice *lt= def_get_lattice(ob); - - if(lt->dvert) { - BPoint *bp; - int a, tot; - - dvert= lt->dvert; - tot= lt->pntsu*lt->pntsv*lt->pntsw; - for(a=0, bp= lt->def; atotweight; i++){ - if (dvert->dw[i].def_nr == (ob->actdef-1)) { - if(select) bp->f1 |= SELECT; - else bp->f1 &= ~SELECT; - - break; - } - } - } - } - } - break; - - default: - break; - } + return NULL; } /* check if deform vertex has defgroup index */ -MDeformWeight *get_defweight (MDeformVert *dv, int defgroup) +MDeformWeight *ED_vgroup_weight_get(MDeformVert *dv, int defgroup) { int i; - if (!dv || defgroup<0) + if(!dv || defgroup<0) return NULL; - for (i=0; itotweight; i++){ - if (dv->dw[i].def_nr == defgroup) + for(i=0; itotweight; i++) + if(dv->dw[i].def_nr == defgroup) return dv->dw+i; - } + return NULL; } -/* Ensures that mv has a deform weight entry for - the specified defweight group */ +/* Ensures that mv has a deform weight entry for the specified defweight group */ /* Note this function is mirrored in editmesh_tools.c, for use for editvertices */ -MDeformWeight *verify_defweight (MDeformVert *dv, int defgroup) +MDeformWeight *ED_vgroup_weight_verify(MDeformVert *dv, int defgroup) { MDeformWeight *newdw; /* do this check always, this function is used to check for it */ - if (!dv || defgroup<0) + if(!dv || defgroup<0) return NULL; - newdw = get_defweight (dv, defgroup); - if (newdw) + newdw = ED_vgroup_weight_get(dv, defgroup); + if(newdw) return newdw; - newdw = MEM_callocN (sizeof(MDeformWeight)*(dv->totweight+1), "deformWeight"); - if (dv->dw){ - memcpy (newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight); - MEM_freeN (dv->dw); + newdw = MEM_callocN(sizeof(MDeformWeight)*(dv->totweight+1), "deformWeight"); + if(dv->dw) { + memcpy(newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight); + MEM_freeN(dv->dw); } dv->dw=newdw; @@ -201,16 +128,16 @@ MDeformWeight *verify_defweight (MDeformVert *dv, int defgroup) return dv->dw+(dv->totweight-1); } -bDeformGroup *add_defgroup_name (Object *ob, char *name) +bDeformGroup *ED_vgroup_add_name(Object *ob, char *name) { - bDeformGroup *defgroup; + bDeformGroup *defgroup; - if (!ob) + if(!ob) return NULL; - defgroup = MEM_callocN (sizeof(bDeformGroup), "add deformGroup"); + defgroup = MEM_callocN(sizeof(bDeformGroup), "add deformGroup"); - BLI_strncpy (defgroup->name, name, 32); + BLI_strncpy(defgroup->name, name, 32); BLI_addtail(&ob->defbase, defgroup); unique_vertexgroup_name(defgroup, ob); @@ -220,444 +147,157 @@ bDeformGroup *add_defgroup_name (Object *ob, char *name) return defgroup; } -void add_defgroup (Object *ob) +bDeformGroup *ED_vgroup_add(Object *ob) { - add_defgroup_name (ob, "Group"); + return ED_vgroup_add_name(ob, "Group"); } - -void duplicate_defgroup ( Object *ob ) +void ED_vgroup_data_create(ID *id) { - bDeformGroup *dg, *cdg; - char name[32], s[32]; - MDeformWeight *org, *cpy; - MDeformVert *dvert, *dvert_array=NULL; - int i, idg, icdg, dvert_tot=0; - - if (ob->type != OB_MESH && ob->type != OB_LATTICE) - return; - - dg = BLI_findlink (&ob->defbase, (ob->actdef-1)); - if (!dg) - return; - - if (strstr(dg->name, "_copy")) { - BLI_strncpy (name, dg->name, 32); /* will be renamed _copy.001... etc */ - } else { - BLI_snprintf (name, 32, "%s_copy", dg->name); - while (get_named_vertexgroup (ob, name)) { - if ((strlen (name) + 6) > 32) { - error ("Error: the name for the new group is > 32 characters"); - return; - } - strcpy (s, name); - BLI_snprintf (name, 32, "%s_copy", s); - } - } + /* create deform verts */ - cdg = copy_defgroup (dg); - strcpy (cdg->name, name); - unique_vertexgroup_name(cdg, ob); - - BLI_addtail (&ob->defbase, cdg); - - idg = (ob->actdef-1); - ob->actdef = BLI_countlist (&ob->defbase); - icdg = (ob->actdef-1); - - if(ob->type == OB_MESH) { - Mesh *me = get_mesh (ob); - dvert_array= me->dvert; - dvert_tot= me->totvert; - } - else if (ob->type == OB_LATTICE) { - Lattice *lt= (Lattice *)ob->data; - dvert_array= lt->dvert; - dvert_tot= lt->pntsu*lt->pntsv*lt->pntsw; + if(GS(id->name)==ID_ME) { + Mesh *me= (Mesh *)id; + me->dvert= CustomData_add_layer(&me->vdata, CD_MDEFORMVERT, CD_CALLOC, NULL, me->totvert); } - - if (!dvert_array) - return; - - for (i = 0; i < dvert_tot; i++) { - dvert = dvert_array+i; - org = get_defweight (dvert, idg); - if (org) { - float weight = org->weight; - /* verify_defweight re-allocs org so need to store the weight first */ - cpy = verify_defweight (dvert, icdg); - cpy->weight = weight; - } + else if(GS(id->name)==ID_LT) { + Lattice *lt= (Lattice *)id; + lt->dvert= MEM_callocN(sizeof(MDeformVert)*lt->pntsu*lt->pntsv*lt->pntsw, "lattice deformVert"); } } -static void del_defgroup_update_users(Object *ob, int id) +/* for mesh in object mode + lattice can be in editmode */ +void ED_vgroup_nr_vert_remove(Object *ob, int def_nr, int vertnum) { - ExplodeModifierData *emd; - ModifierData *md; - ParticleSystem *psys; - ClothModifierData *clmd; - ClothSimSettings *clsim; - int a; + /* This routine removes the vertex from the deform + * group with number def_nr. + * + * This routine is meant to be fast, so it is the + * responsibility of the calling routine to: + * a) test whether ob is non-NULL + * b) test whether ob is a mesh + * c) calculate def_nr + */ - /* these cases don't use names to refer to vertex groups, so when - * they get deleted the numbers get out of sync, this corrects that */ + MDeformWeight *newdw; + MDeformVert *dvert= NULL; + int i; - if(ob->soft) { - if(ob->soft->vertgroup == id) - ob->soft->vertgroup= 0; - else if(ob->soft->vertgroup > id) - ob->soft->vertgroup--; + /* get the deform vertices corresponding to the + * vertnum + */ + if(ob->type==OB_MESH) { + if(((Mesh*)ob->data)->dvert) + dvert = ((Mesh*)ob->data)->dvert + vertnum; } + else if(ob->type==OB_LATTICE) { + Lattice *lt= vgroup_edit_lattice(ob); + + if(lt->dvert) + dvert = lt->dvert + vertnum; + } + + if(dvert==NULL) + return; + + /* for all of the deform weights in the + * deform vert + */ + for(i=dvert->totweight - 1 ; i>=0 ; i--){ - for(md=ob->modifiers.first; md; md=md->next) { - if(md->type == eModifierType_Explode) { - emd= (ExplodeModifierData*)md; - - if(emd->vgroup == id) - emd->vgroup= 0; - else if(emd->vgroup > id) - emd->vgroup--; - } - else if(md->type == eModifierType_Cloth) { - clmd= (ClothModifierData*)md; - clsim= clmd->sim_parms; - - if(clsim) { - if(clsim->vgroup_mass == id) - clsim->vgroup_mass= 0; - else if(clsim->vgroup_mass > id) - clsim->vgroup_mass--; - - if(clsim->vgroup_bend == id) - clsim->vgroup_bend= 0; - else if(clsim->vgroup_bend > id) - clsim->vgroup_bend--; - - if(clsim->vgroup_struct == id) - clsim->vgroup_struct= 0; - else if(clsim->vgroup_struct > id) - clsim->vgroup_struct--; + /* if the def_nr is the same as the one + * for our weight group then remove it + * from this deform vert. + */ + if(dvert->dw[i].def_nr == def_nr) { + dvert->totweight--; + + /* if there are still other deform weights + * attached to this vert then remove this + * deform weight, and reshuffle the others + */ + if(dvert->totweight) { + newdw = MEM_mallocN(sizeof(MDeformWeight)*(dvert->totweight), + "deformWeight"); + if(dvert->dw){ + memcpy(newdw, dvert->dw, sizeof(MDeformWeight)*i); + memcpy(newdw+i, dvert->dw+i+1, + sizeof(MDeformWeight)*(dvert->totweight-i)); + MEM_freeN(dvert->dw); + } + dvert->dw=newdw; + } + /* if there are no other deform weights + * left then just remove the deform weight + */ + else { + MEM_freeN(dvert->dw); + dvert->dw = NULL; + break; } } } - for(psys=ob->particlesystem.first; psys; psys=psys->next) { - for(a=0; avgroup[a] == id) - psys->vgroup[a]= 0; - else if(psys->vgroup[a] > id) - psys->vgroup[a]--; - } } -void del_defgroup_in_object_mode ( Object *ob ) +/* for Mesh in Object mode */ +/* allows editmode for Lattice */ +void ED_vgroup_nr_vert_add(Object *ob, int def_nr, int vertnum, float weight, int assignmode) { - bDeformGroup *dg; - MDeformVert *dvert, *dvert_array=NULL; - int i, e, dvert_tot=0; - - if ((!ob) || (ob->type != OB_MESH && ob->type != OB_LATTICE)) - return; + /* add the vert to the deform group with the + * specified number + */ + MDeformVert *dv= NULL; + MDeformWeight *newdw; + int i; - if(ob->type == OB_MESH) { - Mesh *me = get_mesh (ob); - dvert_array= me->dvert; - dvert_tot= me->totvert; + /* get the vert */ + if(ob->type==OB_MESH) { + if(((Mesh*)ob->data)->dvert) + dv = ((Mesh*)ob->data)->dvert + vertnum; } - else if (ob->type == OB_LATTICE) { - Lattice *lt= (Lattice *)ob->data; - dvert_array= lt->dvert; - dvert_tot= lt->pntsu*lt->pntsv*lt->pntsw; + else if(ob->type==OB_LATTICE) { + Lattice *lt= vgroup_edit_lattice(ob); + + if(lt->dvert) + dv = lt->dvert + vertnum; } - dg = BLI_findlink (&ob->defbase, (ob->actdef-1)); - if (!dg) + if(dv==NULL) return; - if (dvert_array) { - for (i = 0; i < dvert_tot; i++) { - dvert = dvert_array + i; - if (dvert) { - if (get_defweight (dvert, (ob->actdef-1))) - remove_vert_defgroup (ob, dg, i); - } - } - - for (i = 0; i < dvert_tot; i++) { - dvert = dvert_array+i; - if (dvert) { - for (e = 0; e < dvert->totweight; e++) { - if (dvert->dw[e].def_nr > (ob->actdef-1)) - dvert->dw[e].def_nr--; - } + /* Lets first check to see if this vert is + * already in the weight group -- if so + * lets update it + */ + for(i=0; itotweight; i++){ + + /* if this weight cooresponds to the + * deform group, then add it using + * the assign mode provided + */ + if(dv->dw[i].def_nr == def_nr){ + + switch(assignmode) { + case WEIGHT_REPLACE: + dv->dw[i].weight=weight; + break; + case WEIGHT_ADD: + dv->dw[i].weight+=weight; + if(dv->dw[i].weight >= 1.0) + dv->dw[i].weight = 1.0; + break; + case WEIGHT_SUBTRACT: + dv->dw[i].weight-=weight; + /* if the weight is zero or less then + * remove the vert from the deform group + */ + if(dv->dw[i].weight <= 0.0) + ED_vgroup_nr_vert_remove(ob, def_nr, vertnum); + break; } - } - } - - del_defgroup_update_users(ob, ob->actdef); - - /* Update the active deform index if necessary */ - if (ob->actdef == BLI_countlist(&ob->defbase)) - ob->actdef--; - - /* Remove the group */ - BLI_freelinkN (&ob->defbase, dg); -} - -void del_defgroup (Object *ob) -{ - bDeformGroup *defgroup; - int i; - - if (!ob) - return; - - if (!ob->actdef) - return; - - defgroup = BLI_findlink(&ob->defbase, ob->actdef-1); - if (!defgroup) - return; - - /* Make sure that no verts are using this group */ - remove_verts_defgroup(ob, 1); - - /* Make sure that any verts with higher indices are adjusted accordingly */ - if(ob->type==OB_MESH) { - Mesh *me= ob->data; - EditMesh *em = BKE_mesh_get_editmesh(me); - EditVert *eve; - MDeformVert *dvert; - - for (eve=em->verts.first; eve; eve=eve->next){ - dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT); - - if (dvert) - for (i=0; itotweight; i++) - if (dvert->dw[i].def_nr > (ob->actdef-1)) - dvert->dw[i].def_nr--; - } - BKE_mesh_end_editmesh(me, em); - } - else if(ob->type==OB_LATTICE) { - Lattice *lt= def_get_lattice(ob); - BPoint *bp; - MDeformVert *dvert= lt->dvert; - int a, tot; - - if (dvert) { - tot= lt->pntsu*lt->pntsv*lt->pntsw; - for(a=0, bp= lt->def; atotweight; i++){ - if (dvert->dw[i].def_nr > (ob->actdef-1)) - dvert->dw[i].def_nr--; - } - } - } - } - - del_defgroup_update_users(ob, ob->actdef); - - /* Update the active deform index if necessary */ - if (ob->actdef==BLI_countlist(&ob->defbase)) - ob->actdef--; - - /* Remove the group */ - BLI_freelinkN (&ob->defbase, defgroup); - - /* remove all dverts */ - if(ob->actdef==0) { - if(ob->type==OB_MESH) { - Mesh *me= ob->data; - CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert); - me->dvert= NULL; - } - else if(ob->type==OB_LATTICE) { - Lattice *lt= def_get_lattice(ob); - if (lt->dvert) { - MEM_freeN(lt->dvert); - lt->dvert= NULL; - } - } - } -} - -void del_all_defgroups (Object *ob) -{ - /* Sanity check */ - if (ob == NULL) - return; - - /* Remove all DVerts */ - if (ob->type==OB_MESH) { - Mesh *me= ob->data; - CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert); - me->dvert= NULL; - } - else if(ob->type==OB_LATTICE) { - Lattice *lt= def_get_lattice(ob); - if (lt->dvert) { - MEM_freeN(lt->dvert); - lt->dvert= NULL; - } - } - - /* Remove all DefGroups */ - BLI_freelistN(&ob->defbase); - - /* Fix counters/indices */ - ob->actdef= 0; -} - -void create_dverts(ID *id) -{ - /* create deform verts - */ - - if( GS(id->name)==ID_ME) { - Mesh *me= (Mesh *)id; - me->dvert= CustomData_add_layer(&me->vdata, CD_MDEFORMVERT, CD_CALLOC, NULL, me->totvert); - } - else if( GS(id->name)==ID_LT) { - Lattice *lt= (Lattice *)id; - lt->dvert= MEM_callocN(sizeof(MDeformVert)*lt->pntsu*lt->pntsv*lt->pntsw, "lattice deformVert"); - } -} - -/* for mesh in object mode - lattice can be in editmode */ -void remove_vert_def_nr (Object *ob, int def_nr, int vertnum) -{ - /* This routine removes the vertex from the deform - * group with number def_nr. - * - * This routine is meant to be fast, so it is the - * responsibility of the calling routine to: - * a) test whether ob is non-NULL - * b) test whether ob is a mesh - * c) calculate def_nr - */ - - MDeformWeight *newdw; - MDeformVert *dvert= NULL; - int i; - - /* get the deform vertices corresponding to the - * vertnum - */ - if(ob->type==OB_MESH) { - if( ((Mesh*)ob->data)->dvert ) - dvert = ((Mesh*)ob->data)->dvert + vertnum; - } - else if(ob->type==OB_LATTICE) { - Lattice *lt= def_get_lattice(ob); - - if(lt->dvert) - dvert = lt->dvert + vertnum; - } - - if(dvert==NULL) - return; - - /* for all of the deform weights in the - * deform vert - */ - for (i=dvert->totweight - 1 ; i>=0 ; i--){ - - /* if the def_nr is the same as the one - * for our weight group then remove it - * from this deform vert. - */ - if (dvert->dw[i].def_nr == def_nr) { - dvert->totweight--; - - /* if there are still other deform weights - * attached to this vert then remove this - * deform weight, and reshuffle the others - */ - if (dvert->totweight) { - newdw = MEM_mallocN (sizeof(MDeformWeight)*(dvert->totweight), - "deformWeight"); - if (dvert->dw){ - memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*i); - memcpy (newdw+i, dvert->dw+i+1, - sizeof(MDeformWeight)*(dvert->totweight-i)); - MEM_freeN (dvert->dw); - } - dvert->dw=newdw; - } - /* if there are no other deform weights - * left then just remove the deform weight - */ - else { - MEM_freeN (dvert->dw); - dvert->dw = NULL; - break; - } - } - } - -} - -/* for Mesh in Object mode */ -/* allows editmode for Lattice */ -void add_vert_defnr (Object *ob, int def_nr, int vertnum, - float weight, int assignmode) -{ - /* add the vert to the deform group with the - * specified number - */ - MDeformVert *dv= NULL; - MDeformWeight *newdw; - int i; - - /* get the vert */ - if(ob->type==OB_MESH) { - if(((Mesh*)ob->data)->dvert) - dv = ((Mesh*)ob->data)->dvert + vertnum; - } - else if(ob->type==OB_LATTICE) { - Lattice *lt= def_get_lattice(ob); - - if(lt->dvert) - dv = lt->dvert + vertnum; - } - - if(dv==NULL) - return; - - /* Lets first check to see if this vert is - * already in the weight group -- if so - * lets update it - */ - for (i=0; itotweight; i++){ - - /* if this weight cooresponds to the - * deform group, then add it using - * the assign mode provided - */ - if (dv->dw[i].def_nr == def_nr){ - - switch (assignmode) { - case WEIGHT_REPLACE: - dv->dw[i].weight=weight; - break; - case WEIGHT_ADD: - dv->dw[i].weight+=weight; - if (dv->dw[i].weight >= 1.0) - dv->dw[i].weight = 1.0; - break; - case WEIGHT_SUBTRACT: - dv->dw[i].weight-=weight; - /* if the weight is zero or less then - * remove the vert from the deform group - */ - if (dv->dw[i].weight <= 0.0) - remove_vert_def_nr(ob, def_nr, vertnum); - break; - } - return; + return; } } @@ -665,7 +305,7 @@ void add_vert_defnr (Object *ob, int def_nr, int vertnum, * we must take a different form of action ... */ - switch (assignmode) { + switch(assignmode) { case WEIGHT_SUBTRACT: /* if we are subtracting then we don't * need to do anything @@ -677,11 +317,11 @@ void add_vert_defnr (Object *ob, int def_nr, int vertnum, /* if we are doing an additive assignment, then * we need to create the deform weight */ - newdw = MEM_callocN (sizeof(MDeformWeight)*(dv->totweight+1), + newdw = MEM_callocN(sizeof(MDeformWeight)*(dv->totweight+1), "deformWeight"); - if (dv->dw){ - memcpy (newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight); - MEM_freeN (dv->dw); + if(dv->dw){ + memcpy(newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight); + MEM_freeN(dv->dw); } dv->dw=newdw; @@ -694,8 +334,7 @@ void add_vert_defnr (Object *ob, int def_nr, int vertnum, } /* called while not in editmode */ -void add_vert_to_defgroup (Object *ob, bDeformGroup *dg, int vertnum, - float weight, int assignmode) +void ED_vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode) { /* add the vert to the deform group with the * specified assign mode @@ -706,115 +345,27 @@ void add_vert_to_defgroup (Object *ob, bDeformGroup *dg, int vertnum, * it can't be found */ def_nr = get_defgroup_num(ob, dg); - if (def_nr < 0) return; + if(def_nr < 0) return; /* if there's no deform verts then * create some */ if(ob->type==OB_MESH) { - if (!((Mesh*)ob->data)->dvert) - create_dverts(ob->data); + if(!((Mesh*)ob->data)->dvert) + ED_vgroup_data_create(ob->data); } else if(ob->type==OB_LATTICE) { - if (!((Lattice*)ob->data)->dvert) - create_dverts(ob->data); + if(!((Lattice*)ob->data)->dvert) + ED_vgroup_data_create(ob->data); } /* call another function to do the work */ - add_vert_defnr (ob, def_nr, vertnum, weight, assignmode); -} - -/* Only available in editmode */ -void assign_verts_defgroup (Object *ob, float weight) -{ - EditVert *eve; - bDeformGroup *dg, *eg; - MDeformWeight *newdw; - MDeformVert *dvert; - int i, done; - - if (!ob) - return; - - dg=BLI_findlink(&ob->defbase, ob->actdef-1); - if (!dg){ - error ("No vertex group is active"); - return; - } - - switch (ob->type){ - case OB_MESH: - { - Mesh *me= ob->data; - EditMesh *em = BKE_mesh_get_editmesh(me); - - if (!CustomData_has_layer(&em->vdata, CD_MDEFORMVERT)) - EM_add_data_layer(em, &em->vdata, CD_MDEFORMVERT); - - /* Go through the list of editverts and assign them */ - for (eve=em->verts.first; eve; eve=eve->next){ - dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT); - - if (dvert && (eve->f & 1)){ - done=0; - /* See if this vert already has a reference to this group */ - /* If so: Change its weight */ - done=0; - for (i=0; itotweight; i++){ - eg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr); - /* Find the actual group */ - if (eg==dg){ - dvert->dw[i].weight= weight; - done=1; - break; - } - } - /* If not: Add the group and set its weight */ - if (!done){ - newdw = MEM_callocN (sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight"); - if (dvert->dw){ - memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*dvert->totweight); - MEM_freeN (dvert->dw); - } - dvert->dw=newdw; - - dvert->dw[dvert->totweight].weight= weight; - dvert->dw[dvert->totweight].def_nr= ob->actdef-1; - - dvert->totweight++; - - } - } - } - BKE_mesh_end_editmesh(me, em); - } - break; - case OB_LATTICE: - { - Lattice *lt= def_get_lattice(ob); - BPoint *bp; - int a, tot; - - if(lt->dvert==NULL) - create_dverts(<->id); - - tot= lt->pntsu*lt->pntsv*lt->pntsw; - for(a=0, bp= lt->def; af1 & SELECT) - add_vert_defnr (ob, ob->actdef-1, a, weight, WEIGHT_REPLACE); - } - } - break; - default: - printf ("Assigning deformation groups to unknown object type\n"); - break; - } - + ED_vgroup_nr_vert_add(ob, def_nr, vertnum, weight, assignmode); } /* mesh object mode, lattice can be in editmode */ -void remove_vert_defgroup (Object *ob, bDeformGroup *dg, int vertnum) +void ED_vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum) { /* This routine removes the vertex from the specified * deform group. @@ -824,7 +375,7 @@ void remove_vert_defgroup (Object *ob, bDeformGroup *dg, int vertnum) /* if the object is NULL abort */ - if (!ob) + if(!ob) return; /* get the deform number that cooresponds @@ -832,28 +383,34 @@ void remove_vert_defgroup (Object *ob, bDeformGroup *dg, int vertnum) * can not be found. */ def_nr = get_defgroup_num(ob, dg); - if (def_nr < 0) return; + if(def_nr < 0) return; /* call another routine to do the work */ - remove_vert_def_nr (ob, def_nr, vertnum); + ED_vgroup_nr_vert_remove(ob, def_nr, vertnum); } -/* for mesh in object mode lattice can be in editmode */ -static float get_vert_def_nr (Object *ob, int def_nr, int vertnum) +static float get_vert_def_nr(Object *ob, int def_nr, int vertnum) { MDeformVert *dvert= NULL; + EditVert *eve; + Mesh *me; int i; - /* get the deform vertices corresponding to the - * vertnum - */ + /* get the deform vertices corresponding to the vertnum */ if(ob->type==OB_MESH) { - if( ((Mesh*)ob->data)->dvert ) - dvert = ((Mesh*)ob->data)->dvert + vertnum; + me= ob->data; + + if(me->edit_mesh) { + eve= BLI_findlink(&me->edit_mesh->verts, vertnum); + if(!eve) return 0.0f; + dvert= CustomData_em_get(&me->edit_mesh->vdata, eve->data, CD_MDEFORMVERT); + } + else + dvert = me->dvert + vertnum; } else if(ob->type==OB_LATTICE) { - Lattice *lt= def_get_lattice(ob); + Lattice *lt= vgroup_edit_lattice(ob); if(lt->dvert) dvert = lt->dvert + vertnum; @@ -869,23 +426,267 @@ static float get_vert_def_nr (Object *ob, int def_nr, int vertnum) return 0.0f; } -/* mesh object mode, lattice can be in editmode */ -float get_vert_defgroup (Object *ob, bDeformGroup *dg, int vertnum) +float ED_vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum) { int def_nr; - if(!ob) - return 0.0f; + if(!ob) return 0.0f; def_nr = get_defgroup_num(ob, dg); if(def_nr < 0) return 0.0f; - return get_vert_def_nr (ob, def_nr, vertnum); + return get_vert_def_nr(ob, def_nr, vertnum); +} + +void ED_vgroup_select_by_name(Object *ob, char *name) +{ + bDeformGroup *curdef; + int actdef= 1; + + for(curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++){ + if(!strcmp(curdef->name, name)) { + ob->actdef= actdef; + return; + } + } + + ob->actdef=0; // this signals on painting to create a new one, if a bone in posemode is selected */ +} + +/********************** Operator Implementations *********************/ + +/* only in editmode */ +static void vgroup_select_verts(Object *ob, int select) +{ + EditVert *eve; + MDeformVert *dvert; + int i; + + if(ob->type == OB_MESH) { + Mesh *me= ob->data; + EditMesh *em = BKE_mesh_get_editmesh(me); + + for(eve=em->verts.first; eve; eve=eve->next){ + dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT); + + if(dvert && dvert->totweight){ + for(i=0; itotweight; i++){ + if(dvert->dw[i].def_nr == (ob->actdef-1)){ + if(select) eve->f |= SELECT; + else eve->f &= ~SELECT; + + break; + } + } + } + } + /* this has to be called, because this function operates on vertices only */ + if(select) EM_select_flush(em); // vertices to edges/faces + else EM_deselect_flush(em); + + BKE_mesh_end_editmesh(me, em); + } + else if(ob->type == OB_LATTICE) { + Lattice *lt= vgroup_edit_lattice(ob); + + if(lt->dvert) { + BPoint *bp; + int a, tot; + + dvert= lt->dvert; + + tot= lt->pntsu*lt->pntsv*lt->pntsw; + for(a=0, bp= lt->def; atotweight; i++){ + if(dvert->dw[i].def_nr == (ob->actdef-1)) { + if(select) bp->f1 |= SELECT; + else bp->f1 &= ~SELECT; + + break; + } + } + } + } + } +} + +static void vgroup_duplicate(Object *ob) +{ + bDeformGroup *dg, *cdg; + char name[32], s[32]; + MDeformWeight *org, *cpy; + MDeformVert *dvert, *dvert_array=NULL; + int i, idg, icdg, dvert_tot=0; + + dg = BLI_findlink(&ob->defbase, (ob->actdef-1)); + if(!dg) + return; + + if(strstr(dg->name, "_copy")) { + BLI_strncpy(name, dg->name, 32); /* will be renamed _copy.001... etc */ + } + else { + BLI_snprintf(name, 32, "%s_copy", dg->name); + while(get_named_vertexgroup(ob, name)) { + if((strlen(name) + 6) > 32) { + printf("Internal error: the name for the new vertex group is > 32 characters"); + return; + } + strcpy(s, name); + BLI_snprintf(name, 32, "%s_copy", s); + } + } + + cdg = copy_defgroup(dg); + strcpy(cdg->name, name); + unique_vertexgroup_name(cdg, ob); + + BLI_addtail(&ob->defbase, cdg); + + idg = (ob->actdef-1); + ob->actdef = BLI_countlist(&ob->defbase); + icdg = (ob->actdef-1); + + if(ob->type == OB_MESH) { + Mesh *me = get_mesh(ob); + dvert_array= me->dvert; + dvert_tot= me->totvert; + } + else if(ob->type == OB_LATTICE) { + Lattice *lt= (Lattice *)ob->data; + dvert_array= lt->dvert; + dvert_tot= lt->pntsu*lt->pntsv*lt->pntsw; + } + + if(!dvert_array) + return; + + for(i = 0; i < dvert_tot; i++) { + dvert = dvert_array+i; + org = ED_vgroup_weight_get(dvert, idg); + if(org) { + float weight = org->weight; + /* ED_vgroup_weight_verify re-allocs org so need to store the weight first */ + cpy = ED_vgroup_weight_verify(dvert, icdg); + cpy->weight = weight; + } + } +} + +static void vgroup_delete_update_users(Object *ob, int id) +{ + ExplodeModifierData *emd; + ModifierData *md; + ParticleSystem *psys; + ClothModifierData *clmd; + ClothSimSettings *clsim; + int a; + + /* these cases don't use names to refer to vertex groups, so when + * they get deleted the numbers get out of sync, this corrects that */ + + if(ob->soft) { + if(ob->soft->vertgroup == id) + ob->soft->vertgroup= 0; + else if(ob->soft->vertgroup > id) + ob->soft->vertgroup--; + } + + for(md=ob->modifiers.first; md; md=md->next) { + if(md->type == eModifierType_Explode) { + emd= (ExplodeModifierData*)md; + + if(emd->vgroup == id) + emd->vgroup= 0; + else if(emd->vgroup > id) + emd->vgroup--; + } + else if(md->type == eModifierType_Cloth) { + clmd= (ClothModifierData*)md; + clsim= clmd->sim_parms; + + if(clsim) { + if(clsim->vgroup_mass == id) + clsim->vgroup_mass= 0; + else if(clsim->vgroup_mass > id) + clsim->vgroup_mass--; + + if(clsim->vgroup_bend == id) + clsim->vgroup_bend= 0; + else if(clsim->vgroup_bend > id) + clsim->vgroup_bend--; + + if(clsim->vgroup_struct == id) + clsim->vgroup_struct= 0; + else if(clsim->vgroup_struct > id) + clsim->vgroup_struct--; + } + } + } + + for(psys=ob->particlesystem.first; psys; psys=psys->next) { + for(a=0; avgroup[a] == id) + psys->vgroup[a]= 0; + else if(psys->vgroup[a] > id) + psys->vgroup[a]--; + } +} + +static void vgroup_delete_object_mode(Object *ob) +{ + bDeformGroup *dg; + MDeformVert *dvert, *dvert_array=NULL; + int i, e, dvert_tot=0; + + if(ob->type == OB_MESH) { + Mesh *me = get_mesh(ob); + dvert_array= me->dvert; + dvert_tot= me->totvert; + } + else if(ob->type == OB_LATTICE) { + Lattice *lt= (Lattice *)ob->data; + dvert_array= lt->dvert; + dvert_tot= lt->pntsu*lt->pntsv*lt->pntsw; + } + + dg = BLI_findlink(&ob->defbase, (ob->actdef-1)); + if(!dg) + return; + + if(dvert_array) { + for(i = 0; i < dvert_tot; i++) { + dvert = dvert_array + i; + if(dvert) { + if(ED_vgroup_weight_get(dvert, (ob->actdef-1))) + ED_vgroup_vert_remove(ob, dg, i); + } + } + + for(i = 0; i < dvert_tot; i++) { + dvert = dvert_array+i; + if(dvert) { + for(e = 0; e < dvert->totweight; e++) { + if(dvert->dw[e].def_nr > (ob->actdef-1)) + dvert->dw[e].def_nr--; + } + } + } + } + + vgroup_delete_update_users(ob, ob->actdef); + + /* Update the active deform index if necessary */ + if(ob->actdef == BLI_countlist(&ob->defbase)) + ob->actdef--; + + /* Remove the group */ + BLI_freelinkN(&ob->defbase, dg); } -/* Only available in editmode */ +/* only in editmode */ /* removes from active defgroup, if allverts==0 only selected vertices */ -void remove_verts_defgroup (Object *ob, int allverts) +static void vgroup_active_remove_verts(Object *ob, int allverts) { EditVert *eve; MDeformVert *dvert; @@ -893,42 +694,35 @@ void remove_verts_defgroup (Object *ob, int allverts) bDeformGroup *dg, *eg; int i; - if (!ob) - return; - dg=BLI_findlink(&ob->defbase, ob->actdef-1); - if (!dg){ - error ("No vertex group is active"); + if(!dg) return; - } - switch (ob->type){ - case OB_MESH: - { + if(ob->type == OB_MESH) { Mesh *me= ob->data; EditMesh *em = BKE_mesh_get_editmesh(me); - for (eve=em->verts.first; eve; eve=eve->next){ + for(eve=em->verts.first; eve; eve=eve->next){ dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT); - if (dvert && dvert->dw && ((eve->f & 1) || allverts)){ - for (i=0; itotweight; i++){ + if(dvert && dvert->dw && ((eve->f & 1) || allverts)){ + for(i=0; itotweight; i++){ /* Find group */ - eg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr); - if (eg == dg){ + eg = BLI_findlink(&ob->defbase, dvert->dw[i].def_nr); + if(eg == dg){ dvert->totweight--; - if (dvert->totweight){ - newdw = MEM_mallocN (sizeof(MDeformWeight)*(dvert->totweight), "deformWeight"); + if(dvert->totweight){ + newdw = MEM_mallocN(sizeof(MDeformWeight)*(dvert->totweight), "deformWeight"); - if (dvert->dw){ - memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*i); - memcpy (newdw+i, dvert->dw+i+1, sizeof(MDeformWeight)*(dvert->totweight-i)); - MEM_freeN (dvert->dw); + if(dvert->dw){ + memcpy(newdw, dvert->dw, sizeof(MDeformWeight)*i); + memcpy(newdw+i, dvert->dw+i+1, sizeof(MDeformWeight)*(dvert->totweight-i)); + MEM_freeN(dvert->dw); } dvert->dw=newdw; } else{ - MEM_freeN (dvert->dw); + MEM_freeN(dvert->dw); dvert->dw=NULL; break; } @@ -938,10 +732,8 @@ void remove_verts_defgroup (Object *ob, int allverts) } BKE_mesh_end_editmesh(me, em); } - break; - case OB_LATTICE: - { - Lattice *lt= def_get_lattice(ob); + else if(ob->type == OB_LATTICE) { + Lattice *lt= vgroup_edit_lattice(ob); if(lt->dvert) { BPoint *bp; @@ -949,155 +741,219 @@ void remove_verts_defgroup (Object *ob, int allverts) for(a=0, bp= lt->def; af1 & SELECT)) - remove_vert_defgroup (ob, dg, a); + ED_vgroup_vert_remove(ob, dg, a); } } } - break; - - default: - printf ("Removing deformation groups from unknown object type\n"); - break; - } } -/* Only available in editmode */ -/* removes from all defgroup, if allverts==0 only selected vertices */ -void remove_verts_defgroups(Object *ob, int allverts) +static void vgroup_delete_edit_mode(Object *ob) { - int actdef, defCount; + bDeformGroup *defgroup; + int i; - if (ob == NULL) return; - - actdef= ob->actdef; - defCount= BLI_countlist(&ob->defbase); - - if (defCount == 0) { - error("Object has no vertex groups"); + if(!ob->actdef) return; + + defgroup = BLI_findlink(&ob->defbase, ob->actdef-1); + if(!defgroup) + return; + + /* Make sure that no verts are using this group */ + vgroup_active_remove_verts(ob, 1); + + /* Make sure that any verts with higher indices are adjusted accordingly */ + if(ob->type==OB_MESH) { + Mesh *me= ob->data; + EditMesh *em = BKE_mesh_get_editmesh(me); + EditVert *eve; + MDeformVert *dvert; + + for(eve=em->verts.first; eve; eve=eve->next){ + dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT); + + if(dvert) + for(i=0; itotweight; i++) + if(dvert->dw[i].def_nr > (ob->actdef-1)) + dvert->dw[i].def_nr--; + } + BKE_mesh_end_editmesh(me, em); } - - /* To prevent code redundancy, we just use remove_verts_defgroup, but that - * only operates on the active vgroup. So we iterate through all groups, by changing - * active group index - */ - for (ob->actdef= 1; ob->actdef <= defCount; ob->actdef++) - remove_verts_defgroup(ob, allverts); + else if(ob->type==OB_LATTICE) { + Lattice *lt= vgroup_edit_lattice(ob); + BPoint *bp; + MDeformVert *dvert= lt->dvert; + int a, tot; - ob->actdef= actdef; -} + if(dvert) { + tot= lt->pntsu*lt->pntsv*lt->pntsw; + for(a=0, bp= lt->def; atotweight; i++){ + if(dvert->dw[i].def_nr > (ob->actdef-1)) + dvert->dw[i].def_nr--; + } + } + } + } -void vertexgroup_select_by_name(Object *ob, char *name) -{ - bDeformGroup *curdef; - int actdef= 1; + vgroup_delete_update_users(ob, ob->actdef); + + /* Update the active deform index if necessary */ + if(ob->actdef==BLI_countlist(&ob->defbase)) + ob->actdef--; - if(ob==NULL) return; + /* Remove the group */ + BLI_freelinkN (&ob->defbase, defgroup); - for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++){ - if (!strcmp(curdef->name, name)) { - ob->actdef= actdef; - return; + /* remove all dverts */ + if(ob->actdef==0) { + if(ob->type==OB_MESH) { + Mesh *me= ob->data; + CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert); + me->dvert= NULL; + } + else if(ob->type==OB_LATTICE) { + Lattice *lt= vgroup_edit_lattice(ob); + if(lt->dvert) { + MEM_freeN(lt->dvert); + lt->dvert= NULL; + } } } - ob->actdef=0; // this signals on painting to create a new one, if a bone in posemode is selected */ } -/* This function provides a shortcut for adding/removing verts from - * vertex groups. It is called by the Ctrl-G hotkey in EditMode for Meshes - * and Lattices. (currently only restricted to those two) - * It is only responsible for - */ -void vgroup_assign_with_menu(Scene *scene, Object *ob) +static int vgroup_object_in_edit_mode(Object *ob) { - VPaint *wp= scene->toolsettings->wpaint; - int defCount; - int mode= 0; + if(ob->type == OB_MESH) + return (((Mesh*)ob->data)->edit_mesh != NULL); + else if(ob->type == OB_LATTICE) + return (((Lattice*)ob->data)->editlatt != NULL); - /* prevent crashes */ - if (wp==NULL || ob==NULL) return; - - defCount= BLI_countlist(&ob->defbase); + return 0; +} + +static void vgroup_delete(Object *ob) +{ + if(vgroup_object_in_edit_mode(ob)) + vgroup_delete_edit_mode(ob); + else + vgroup_delete_object_mode(ob); +} + +static void vgroup_delete_all(Object *ob) +{ + /* Remove all DVerts */ + if(ob->type==OB_MESH) { + Mesh *me= ob->data; + CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert); + me->dvert= NULL; + } + else if(ob->type==OB_LATTICE) { + Lattice *lt= vgroup_edit_lattice(ob); + if(lt->dvert) { + MEM_freeN(lt->dvert); + lt->dvert= NULL; + } + } - /* give user choices of adding to current/new or removing from current */ -// XXX if (defCount && ob->actdef) -// mode = pupmenu("Vertex Groups %t|Add Selected to New Group %x1|Add Selected to Active Group %x2|Remove Selected from Active Group %x3|Remove Selected from All Groups %x4"); -// else -// mode= pupmenu("Vertex Groups %t|Add Selected to New Group %x1"); + /* Remove all DefGroups */ + BLI_freelistN(&ob->defbase); - /* handle choices */ - switch (mode) { - case 1: /* add to new group */ - add_defgroup(ob); - assign_verts_defgroup(ob, paint_brush(&wp->paint)->alpha); - BIF_undo_push("Assign to vertex group"); - break; - case 2: /* add to current group */ - assign_verts_defgroup(ob, paint_brush(&wp->paint)->alpha); - BIF_undo_push("Assign to vertex group"); - break; - case 3: /* remove from current group */ - remove_verts_defgroup(ob, 0); - BIF_undo_push("Remove from vertex group"); - break; - case 4: /* remove from all groups */ - remove_verts_defgroups(ob, 0); - BIF_undo_push("Remove from all vertex groups"); - break; + /* Fix counters/indices */ + ob->actdef= 0; +} + +/* only in editmode */ +static void vgroup_assign_verts(Object *ob, float weight) +{ + EditVert *eve; + bDeformGroup *dg, *eg; + MDeformWeight *newdw; + MDeformVert *dvert; + int i, done; + + dg=BLI_findlink(&ob->defbase, ob->actdef-1); + + if(ob->type == OB_MESH) { + Mesh *me= ob->data; + EditMesh *em = BKE_mesh_get_editmesh(me); + + if(!CustomData_has_layer(&em->vdata, CD_MDEFORMVERT)) + EM_add_data_layer(em, &em->vdata, CD_MDEFORMVERT); + + /* Go through the list of editverts and assign them */ + for(eve=em->verts.first; eve; eve=eve->next){ + dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT); + + if(dvert && (eve->f & 1)){ + done=0; + /* See if this vert already has a reference to this group */ + /* If so: Change its weight */ + done=0; + for(i=0; itotweight; i++){ + eg = BLI_findlink(&ob->defbase, dvert->dw[i].def_nr); + /* Find the actual group */ + if(eg==dg){ + dvert->dw[i].weight= weight; + done=1; + break; + } + } + /* If not: Add the group and set its weight */ + if(!done){ + newdw = MEM_callocN(sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight"); + if(dvert->dw){ + memcpy(newdw, dvert->dw, sizeof(MDeformWeight)*dvert->totweight); + MEM_freeN(dvert->dw); + } + dvert->dw=newdw; + + dvert->dw[dvert->totweight].weight= weight; + dvert->dw[dvert->totweight].def_nr= ob->actdef-1; + + dvert->totweight++; + + } + } + } + BKE_mesh_end_editmesh(me, em); + } + else if(ob->type == OB_LATTICE) { + Lattice *lt= vgroup_edit_lattice(ob); + BPoint *bp; + int a, tot; + + if(lt->dvert==NULL) + ED_vgroup_data_create(<->id); + + tot= lt->pntsu*lt->pntsv*lt->pntsw; + for(a=0, bp= lt->def; af1 & SELECT) + ED_vgroup_nr_vert_add(ob, ob->actdef-1, a, weight, WEIGHT_REPLACE); + } } } -/* This function provides a shortcut for commonly used vertex group - * functions - change weight (not implemented), change active group, delete active group, - * when Ctrl-Shift-G is used in EditMode, for Meshes and Lattices (only for now). - */ -void vgroup_operation_with_menu(Object *ob) +/* only in editmode */ +/* removes from all defgroup, if allverts==0 only selected vertices */ +static void vgroup_remove_verts(Object *ob, int allverts) { - int defCount; - int mode= 0; - - /* prevent crashes and useless cases */ - if (ob==NULL) return; + int actdef, defCount; + actdef= ob->actdef; defCount= BLI_countlist(&ob->defbase); - if (defCount == 0) return; - /* give user choices of adding to current/new or removing from current */ -// XXX if (ob->actdef) -// mode = pupmenu("Vertex Groups %t|Change Active Group%x1|Delete Active Group%x2|Delete All Groups%x3"); -// else -// mode= pupmenu("Vertex Groups %t|Change Active Group%x1|Delete All Groups%x3"); + if(defCount == 0) + return; - /* handle choices */ - switch (mode) { - case 1: /* change active group*/ - { - char *menustr= NULL; // XXX get_vertexgroup_menustr(ob); - short nr; - - if (menustr) { - nr= 1; // pupmenu(menustr); // XXX - - if ((nr >= 1) && (nr <= defCount)) - ob->actdef= nr; - - MEM_freeN(menustr); - } - } - break; - case 2: /* delete active group */ - { - del_defgroup(ob); - BIF_undo_push("Delete vertex group"); - } - break; - case 3: /* delete all groups */ - { - del_all_defgroups(ob); - BIF_undo_push("Delete all vertex groups"); - } - break; - } + /* To prevent code redundancy, we just use vgroup_active_remove_verts, but that + * only operates on the active vgroup. So we iterate through all groups, by changing + * active group index + */ + for(ob->actdef= 1; ob->actdef <= defCount; ob->actdef++) + vgroup_active_remove_verts(ob, allverts); + + ob->actdef= actdef; } /********************** vertex group operators *********************/ @@ -1106,14 +962,25 @@ static int vertex_group_poll(bContext *C) { Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; ID *data= (ob)? ob->data: NULL; - return (ob && !ob->id.lib && data && !data->lib); + return (ob && !ob->id.lib && ELEM(ob->type, OB_MESH, OB_LATTICE) && data && !data->lib); +} + +static int vertex_group_poll_edit(bContext *C) +{ + Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; + ID *data= (ob)? ob->data: NULL; + + if(!(ob && !ob->id.lib && data && !data->lib)) + return 0; + + return vgroup_object_in_edit_mode(ob); } static int vertex_group_add_exec(bContext *C, wmOperator *op) { Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; - add_defgroup(ob); + ED_vgroup_add(ob); DAG_id_flush_update(&ob->id, OB_RECALC_DATA); WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data); WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob); @@ -1138,16 +1005,13 @@ void OBJECT_OT_vertex_group_add(wmOperatorType *ot) static int vertex_group_remove_exec(bContext *C, wmOperator *op) { Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; - Scene *scene= CTX_data_scene(C); - if(scene->obedit == ob) { - del_defgroup(ob); - } - else { - del_defgroup_in_object_mode(ob); - DAG_id_flush_update(&ob->id, OB_RECALC_DATA); - } + if(RNA_boolean_get(op->ptr, "all")) + vgroup_delete_all(ob); + else + vgroup_delete(ob); + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data); WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob); @@ -1166,6 +1030,9 @@ void OBJECT_OT_vertex_group_remove(wmOperatorType *ot) /* flags */ ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_boolean(ot->srna, "all", 0, "All", "Remove from all vertex groups."); } static int vertex_group_assign_exec(bContext *C, wmOperator *op) @@ -1173,7 +1040,10 @@ static int vertex_group_assign_exec(bContext *C, wmOperator *op) ToolSettings *ts= CTX_data_tool_settings(C); Object *ob= CTX_data_edit_object(C); - assign_verts_defgroup(ob, ts->vgroup_weight); + if(RNA_boolean_get(op->ptr, "new")) + ED_vgroup_add(ob); + + vgroup_assign_verts(ob, ts->vgroup_weight); DAG_id_flush_update(&ob->id, OB_RECALC_DATA); WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data); @@ -1187,21 +1057,24 @@ void OBJECT_OT_vertex_group_assign(wmOperatorType *ot) ot->idname= "OBJECT_OT_vertex_group_assign"; /* api callbacks */ - ot->poll= vertex_group_poll; + ot->poll= vertex_group_poll_edit; ot->exec= vertex_group_assign_exec; /* flags */ ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_boolean(ot->srna, "new", 0, "New", "Assign vertex to new vertex group."); } static int vertex_group_remove_from_exec(bContext *C, wmOperator *op) { Object *ob= CTX_data_edit_object(C); - remove_verts_defgroup(ob, 0); + vgroup_remove_verts(ob, 0); DAG_id_flush_update(&ob->id, OB_RECALC_DATA); WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data); - + return OPERATOR_FINISHED; } @@ -1210,13 +1083,16 @@ void OBJECT_OT_vertex_group_remove_from(wmOperatorType *ot) /* identifiers */ ot->name= "Remove from Vertex Group"; ot->idname= "OBJECT_OT_vertex_group_remove_from"; - + /* api callbacks */ - ot->poll= vertex_group_poll; + ot->poll= vertex_group_poll_edit; ot->exec= vertex_group_remove_from_exec; /* flags */ ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + RNA_def_boolean(ot->srna, "all", 0, "All", "Remove from all vertex groups."); } static int vertex_group_select_exec(bContext *C, wmOperator *op) @@ -1226,7 +1102,7 @@ static int vertex_group_select_exec(bContext *C, wmOperator *op) if(!ob || ob->id.lib) return OPERATOR_CANCELLED; - sel_verts_defgroup(ob, 1); + vgroup_select_verts(ob, 1); WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data); return OPERATOR_FINISHED; @@ -1237,9 +1113,9 @@ void OBJECT_OT_vertex_group_select(wmOperatorType *ot) /* identifiers */ ot->name= "Select Vertex Group"; ot->idname= "OBJECT_OT_vertex_group_select"; - + /* api callbacks */ - ot->poll= vertex_group_poll; + ot->poll= vertex_group_poll_edit; ot->exec= vertex_group_select_exec; /* flags */ @@ -1250,7 +1126,7 @@ static int vertex_group_deselect_exec(bContext *C, wmOperator *op) { Object *ob= CTX_data_edit_object(C); - sel_verts_defgroup(ob, 0); + vgroup_select_verts(ob, 0); WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data); return OPERATOR_FINISHED; @@ -1261,9 +1137,9 @@ void OBJECT_OT_vertex_group_deselect(wmOperatorType *ot) /* identifiers */ ot->name= "Deselect Vertex Group"; ot->idname= "OBJECT_OT_vertex_group_deselect"; - + /* api callbacks */ - ot->poll= vertex_group_poll; + ot->poll= vertex_group_poll_edit; ot->exec= vertex_group_deselect_exec; /* flags */ @@ -1274,11 +1150,11 @@ static int vertex_group_copy_exec(bContext *C, wmOperator *op) { Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; - duplicate_defgroup(ob); + vgroup_duplicate(ob); DAG_id_flush_update(&ob->id, OB_RECALC_DATA); WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob); WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data); - + return OPERATOR_FINISHED; } @@ -1287,7 +1163,7 @@ void OBJECT_OT_vertex_group_copy(wmOperatorType *ot) /* identifiers */ ot->name= "Copy Vertex Group"; ot->idname= "OBJECT_OT_vertex_group_copy"; - + /* api callbacks */ ot->poll= vertex_group_poll; ot->exec= vertex_group_copy_exec; @@ -1300,25 +1176,25 @@ static int vertex_group_copy_to_linked_exec(bContext *C, wmOperator *op) { Scene *scene= CTX_data_scene(C); Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; - Base *base; + Base *base; int retval= OPERATOR_CANCELLED; - for(base=scene->base.first; base; base= base->next) { - if(base->object->type==ob->type) { - if(base->object!=ob && base->object->data==ob->data) { - BLI_freelistN(&base->object->defbase); - BLI_duplicatelist(&base->object->defbase, &ob->defbase); - base->object->actdef= ob->actdef; + for(base=scene->base.first; base; base= base->next) { + if(base->object->type==ob->type) { + if(base->object!=ob && base->object->data==ob->data) { + BLI_freelistN(&base->object->defbase); + BLI_duplicatelist(&base->object->defbase, &ob->defbase); + base->object->actdef= ob->actdef; - DAG_id_flush_update(&base->object->id, OB_RECALC_DATA); + DAG_id_flush_update(&base->object->id, OB_RECALC_DATA); WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, base->object); WM_event_add_notifier(C, NC_GEOM|ND_DATA, base->object->data); retval = OPERATOR_FINISHED; - } - } - } - + } + } + } + return retval; } @@ -1327,7 +1203,7 @@ void OBJECT_OT_vertex_group_copy_to_linked(wmOperatorType *ot) /* identifiers */ ot->name= "Copy Vertex Group to Linked"; ot->idname= "OBJECT_OT_vertex_group_copy_to_linked"; - + /* api callbacks */ ot->poll= vertex_group_poll; ot->exec= vertex_group_copy_to_linked_exec; @@ -1336,3 +1212,110 @@ void OBJECT_OT_vertex_group_copy_to_linked(wmOperatorType *ot) ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; } +static EnumPropertyItem vgroup_items[]= { + {0, NULL, 0, NULL, NULL}}; + +static int set_active_group_exec(bContext *C, wmOperator *op) +{ + Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; + int nr= RNA_enum_get(op->ptr, "group"); + + ob->actdef= nr+1; + + DAG_id_flush_update(&ob->id, OB_RECALC_DATA); + WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob); + + return OPERATOR_FINISHED; +} + +static EnumPropertyItem *vgroup_itemf(bContext *C, PointerRNA *ptr, int *free) +{ + Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; + EnumPropertyItem tmp = {0, "", 0, "", ""}; + EnumPropertyItem *item= NULL; + bDeformGroup *def; + int a, totitem= 0; + + if(!C) /* needed for docs */ + return vgroup_items; + + for(a=0, def=ob->defbase.first; def; def=def->next, a++) { + tmp.value= a; + tmp.identifier= def->name; + tmp.name= def->name; + RNA_enum_item_add(&item, &totitem, &tmp); + } + + RNA_enum_item_end(&item, &totitem); + + *free= 1; + + return item; +} + +void OBJECT_OT_vertex_group_set_active(wmOperatorType *ot) +{ + PropertyRNA *prop; + + /* identifiers */ + ot->name= "Set Active Vertex Group"; + ot->idname= "OBJECT_OT_vertex_group_set_active"; + + /* api callbacks */ + ot->poll= vertex_group_poll; + ot->exec= set_active_group_exec; + ot->invoke= WM_menu_invoke; + + /* flags */ + ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; + + /* properties */ + prop= RNA_def_enum(ot->srna, "group", vgroup_items, 0, "Group", "Vertex group to set as active."); + RNA_def_enum_funcs(prop, vgroup_itemf); +} + +static int vertex_group_menu_exec(bContext *C, wmOperator *op) +{ + Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data; + uiPopupMenu *pup; + uiLayout *layout; + + pup= uiPupMenuBegin(C, "Vertex Groups", 0); + layout= uiPupMenuLayout(pup); + uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_REGION_WIN); + + if(vgroup_object_in_edit_mode(ob)) { + uiItemBooleanO(layout, "Assign to New Group", 0, "OBJECT_OT_vertex_group_assign", "new", 1); + + if(BLI_countlist(&ob->defbase) && ob->actdef) { + uiItemO(layout, "Assign to Group", 0, "OBJECT_OT_vertex_group_assign"); + uiItemO(layout, "Remove from Group", 0, "OBJECT_OT_vertex_group_remove_from"); + uiItemBooleanO(layout, "Remove from All", 0, "OBJECT_OT_vertex_group_remove_from", "all", 1); + } + } + + if(BLI_countlist(&ob->defbase) && ob->actdef) { + if(vgroup_object_in_edit_mode(ob)) + uiItemS(layout); + + uiItemO(layout, "Set Active Group", 0, "OBJECT_OT_vertex_group_set_active"); + uiItemO(layout, "Remove Group", 0, "OBJECT_OT_vertex_group_remove"); + uiItemBooleanO(layout, "Remove All Groups", 0, "OBJECT_OT_vertex_group_remove", "all", 1); + } + + uiPupMenuEnd(C, pup); + + return OPERATOR_FINISHED; +} + +void OBJECT_OT_vertex_group_menu(wmOperatorType *ot) +{ + /* identifiers */ + ot->name= "Vertex Group Menu"; + ot->idname= "OBJECT_OT_vertex_group_menu"; + + /* api callbacks */ + ot->poll= vertex_group_poll; + ot->exec= vertex_group_menu_exec; +} + diff --git a/source/blender/editors/screen/screen_context.c b/source/blender/editors/screen/screen_context.c index 1e36a32b9e1..17c51a7b7d3 100644 --- a/source/blender/editors/screen/screen_context.c +++ b/source/blender/editors/screen/screen_context.c @@ -108,6 +108,12 @@ int ed_screen_context(const bContext *C, const char *member, bContextDataResult return 1; } + else if(CTX_data_equals(member, "object")) { + if(scene->basact) + CTX_data_id_pointer_set(result, &scene->basact->object->id); + + return 1; + } else if(CTX_data_equals(member, "edit_object")) { /* convenience for now, 1 object per scene in editmode */ if(scene->obedit) diff --git a/source/blender/editors/sculpt_paint/paint_vertex.c b/source/blender/editors/sculpt_paint/paint_vertex.c index 73589371c4f..25ff57ca87f 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.c +++ b/source/blender/editors/sculpt_paint/paint_vertex.c @@ -408,8 +408,8 @@ void clear_wpaint_selectedfaces(Scene *scene) if (!strcmp(curdef->name, name)) break; if(curdef==NULL) { - int olddef= ob->actdef; /* tsk, add_defgroup sets the active defgroup */ - curdef= add_defgroup_name (ob, name); + int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */ + curdef= ED_vgroup_add_name (ob, name); ob->actdef= olddef; } @@ -431,9 +431,9 @@ void clear_wpaint_selectedfaces(Scene *scene) faceverts[3]= mface->v4; for (i=0; i<3 || faceverts[i]; i++) { if(!((me->dvert+faceverts[i])->flag)) { - dw= verify_defweight(me->dvert+faceverts[i], vgroup); + dw= ED_vgroup_weight_verify(me->dvert+faceverts[i], vgroup); if(dw) { - uw= verify_defweight(wp->wpaint_prev+faceverts[i], vgroup); + uw= ED_vgroup_weight_verify(wp->wpaint_prev+faceverts[i], vgroup); uw->weight= dw->weight; /* set the undo weight */ dw->weight= paintweight; @@ -442,11 +442,11 @@ void clear_wpaint_selectedfaces(Scene *scene) if(j>=0) { /* copy, not paint again */ if(vgroup_mirror != -1) { - dw= verify_defweight(me->dvert+j, vgroup_mirror); - uw= verify_defweight(wp->wpaint_prev+j, vgroup_mirror); + dw= ED_vgroup_weight_verify(me->dvert+j, vgroup_mirror); + uw= ED_vgroup_weight_verify(wp->wpaint_prev+j, vgroup_mirror); } else { - dw= verify_defweight(me->dvert+j, vgroup); - uw= verify_defweight(wp->wpaint_prev+j, vgroup); + dw= ED_vgroup_weight_verify(me->dvert+j, vgroup); + uw= ED_vgroup_weight_verify(wp->wpaint_prev+j, vgroup); } uw->weight= dw->weight; /* set the undo weight */ dw->weight= paintweight; @@ -963,20 +963,20 @@ void sample_wpaint(Scene *scene, ARegion *ar, View3D *v3d, int mode) fac= MIN4(w1, w2, w3, w4); if(w1==fac) { - dw= get_defweight(me->dvert+mface->v1, ob->actdef-1); + dw= ED_vgroup_weight_get(me->dvert+mface->v1, ob->actdef-1); if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f; } else if(w2==fac) { - dw= get_defweight(me->dvert+mface->v2, ob->actdef-1); + dw= ED_vgroup_weight_get(me->dvert+mface->v2, ob->actdef-1); if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f; } else if(w3==fac) { - dw= get_defweight(me->dvert+mface->v3, ob->actdef-1); + dw= ED_vgroup_weight_get(me->dvert+mface->v3, ob->actdef-1); if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f; } else if(w4==fac) { if(mface->v4) { - dw= get_defweight(me->dvert+mface->v4, ob->actdef-1); + dw= ED_vgroup_weight_get(me->dvert+mface->v4, ob->actdef-1); if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f; } } @@ -995,12 +995,12 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, int alpha, int vgroup= ob->actdef-1; if(wp->flag & VP_ONLYVGROUP) { - dw= get_defweight(me->dvert+index, vgroup); - uw= get_defweight(wp->wpaint_prev+index, vgroup); + dw= ED_vgroup_weight_get(me->dvert+index, vgroup); + uw= ED_vgroup_weight_get(wp->wpaint_prev+index, vgroup); } else { - dw= verify_defweight(me->dvert+index, vgroup); - uw= verify_defweight(wp->wpaint_prev+index, vgroup); + dw= ED_vgroup_weight_verify(me->dvert+index, vgroup); + uw= ED_vgroup_weight_verify(wp->wpaint_prev+index, vgroup); } if(dw==NULL || uw==NULL) return; @@ -1012,9 +1012,9 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, int alpha, if(j>=0) { /* copy, not paint again */ if(vgroup_mirror != -1) - uw= verify_defweight(me->dvert+j, vgroup_mirror); + uw= ED_vgroup_weight_verify(me->dvert+j, vgroup_mirror); else - uw= verify_defweight(me->dvert+j, vgroup); + uw= ED_vgroup_weight_verify(me->dvert+j, vgroup); uw->weight= dw->weight; } @@ -1070,7 +1070,7 @@ static int set_wpaint(bContext *C, wmOperator *op) /* toggle */ if(pchan->bone->flag & BONE_ACTIVE) break; if(pchan) - vertexgroup_select_by_name(ob, pchan->name); + ED_vgroup_select_by_name(ob, pchan->name); } } else { @@ -1222,7 +1222,7 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *event) /* if nothing was added yet, we make dverts and a vertex deform group */ if (!me->dvert) - create_dverts(&me->id); + ED_vgroup_data_create(&me->id); /* make mode data storage */ wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData"); @@ -1256,14 +1256,14 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *event) if(pchan) { bDeformGroup *dg= get_named_vertexgroup(ob, pchan->name); if(dg==NULL) - dg= add_defgroup_name(ob, pchan->name); /* sets actdef */ + dg= ED_vgroup_add_name(ob, pchan->name); /* sets actdef */ else ob->actdef= get_defgroup_num(ob, dg); } } } if(ob->defbase.first==NULL) { - add_defgroup(ob); + ED_vgroup_add(ob); } // if(ob->lay & v3d->lay); else error("Active object is not in this layer"); @@ -1288,8 +1288,8 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *event) if (!strcmp(curdef->name, name)) break; if(curdef==NULL) { - int olddef= ob->actdef; /* tsk, add_defgroup sets the active defgroup */ - curdef= add_defgroup_name (ob, name); + int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */ + curdef= ED_vgroup_add_name (ob, name); ob->actdef= olddef; } @@ -1381,10 +1381,10 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P if(mface->v4) (me->dvert+mface->v4)->flag= 1; if(wp->mode==VP_BLUR) { - MDeformWeight *dw, *(*dw_func)(MDeformVert *, int) = verify_defweight; + MDeformWeight *dw, *(*dw_func)(MDeformVert *, int) = ED_vgroup_weight_verify; if(wp->flag & VP_ONLYVGROUP) - dw_func= get_defweight; + dw_func= ED_vgroup_weight_get; dw= dw_func(me->dvert+mface->v1, ob->actdef-1); if(dw) {paintweight+= dw->weight; totw++;} diff --git a/source/blender/editors/space_buttons/buttons_ops.c b/source/blender/editors/space_buttons/buttons_ops.c index 1567f393d87..0df6f6250ff 100644 --- a/source/blender/editors/space_buttons/buttons_ops.c +++ b/source/blender/editors/space_buttons/buttons_ops.c @@ -85,133 +85,6 @@ #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) diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c index 001021e8d4b..20cab3b8aeb 100644 --- a/source/blender/editors/space_view3d/drawobject.c +++ b/source/blender/editors/space_view3d/drawobject.c @@ -1198,7 +1198,7 @@ static void drawlattice__point(Lattice *lt, DispList *dl, int u, int v, int w, i if(use_wcol) { float col[3]; - MDeformWeight *mdw= get_defweight (lt->dvert+index, use_wcol-1); + MDeformWeight *mdw= ED_vgroup_weight_get (lt->dvert+index, use_wcol-1); weight_to_rgb(mdw?mdw->weight:0.0f, col, col+1, col+2); glColor3fv(col); diff --git a/source/blender/editors/space_view3d/view3d_buttons.c b/source/blender/editors/space_view3d/view3d_buttons.c index 2bef37e43e6..06320f871da 100644 --- a/source/blender/editors/space_view3d/view3d_buttons.c +++ b/source/blender/editors/space_view3d/view3d_buttons.c @@ -899,7 +899,7 @@ static void do_view3d_region_buttons(bContext *C, void *arg, int event) Mesh *me= ob->data; int a; for(a=0; atotvert; a++) - remove_vert_defgroup (ob, defGroup, a); + ED_vgroup_vert_remove (ob, defGroup, a); DAG_id_flush_update(&ob->id, OB_RECALC_DATA); } } diff --git a/source/blender/editors/util/undo.c b/source/blender/editors/util/undo.c index 41159397634..d26f7a7a484 100644 --- a/source/blender/editors/util/undo.c +++ b/source/blender/editors/util/undo.c @@ -58,6 +58,7 @@ #include "ED_armature.h" #include "ED_particle.h" #include "ED_curve.h" +#include "ED_mball.h" #include "ED_mesh.h" #include "ED_object.h" #include "ED_screen.h" diff --git a/source/blender/makesrna/RNA_enum_types.h b/source/blender/makesrna/RNA_enum_types.h index a59fc8716ec..ccf4b7a2db3 100644 --- a/source/blender/makesrna/RNA_enum_types.h +++ b/source/blender/makesrna/RNA_enum_types.h @@ -56,6 +56,8 @@ extern EnumPropertyItem brush_sculpt_tool_items[]; extern EnumPropertyItem unpack_method_items[]; +extern EnumPropertyItem object_type_items[]; + #endif /* RNA_ENUM_TYPES */ diff --git a/source/blender/makesrna/intern/rna_object.c b/source/blender/makesrna/intern/rna_object.c index 50ccbc86719..f81b314de6f 100644 --- a/source/blender/makesrna/intern/rna_object.c +++ b/source/blender/makesrna/intern/rna_object.c @@ -64,6 +64,22 @@ static EnumPropertyItem parent_type_items[] = { {PARBONE, "BONE", 0, "Bone", ""}, {0, NULL, 0, NULL, NULL}}; +EnumPropertyItem object_type_items[] = { + {OB_MESH, "MESH", 0, "Mesh", ""}, + {OB_CURVE, "CURVE", 0, "Curve", ""}, + {OB_SURF, "SURFACE", 0, "Surface", ""}, + {OB_MBALL, "META", 0, "Meta", ""}, + {OB_FONT, "TEXT", 0, "Text", ""}, + {0, "", 0, NULL, NULL}, + {OB_ARMATURE, "ARMATURE", 0, "Armature", ""}, + {OB_LATTICE, "LATTICE", 0, "Lattice", ""}, + {OB_EMPTY, "EMPTY", 0, "Empty", ""}, + {0, "", 0, NULL, NULL}, + {OB_CAMERA, "CAMERA", 0, "Camera", ""}, + {OB_LAMP, "LAMP", 0, "Lamp", ""}, + {0, NULL, 0, NULL, NULL} +}; + #ifdef RNA_RUNTIME #include "DNA_key_types.h" diff --git a/source/blender/windowmanager/intern/wm_event_system.c b/source/blender/windowmanager/intern/wm_event_system.c index c2c90f055b0..3c03d24ca93 100644 --- a/source/blender/windowmanager/intern/wm_event_system.c +++ b/source/blender/windowmanager/intern/wm_event_system.c @@ -177,7 +177,7 @@ void wm_event_do_notifiers(bContext *C) do_anim= 1; } } - if(ELEM3(note->category, NC_SCENE, NC_OBJECT, NC_GEOM)) { + if(ELEM4(note->category, NC_SCENE, NC_OBJECT, NC_GEOM, NC_SCENE)) { ED_info_stats_clear(CTX_data_scene(C)); WM_event_add_notifier(C, NC_SPACE|ND_SPACE_INFO, NULL); } -- cgit v1.2.3