4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2009 Blender Foundation.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation
25 * ***** END GPL LICENSE BLOCK *****
31 #include "MEM_guardedalloc.h"
33 #include "DNA_curve_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_material_types.h"
36 #include "DNA_texture_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_world_types.h"
40 #include "BKE_context.h"
41 #include "BKE_depsgraph.h"
43 #include "BKE_library.h"
44 #include "BKE_material.h"
45 #include "BKE_particle.h"
46 #include "BKE_texture.h"
47 #include "BKE_utildefines.h"
48 #include "BKE_world.h"
50 #include "BLI_editVert.h"
52 #include "RNA_access.h"
60 #include "buttons_intern.h" // own include
62 /********************** material slot operators *********************/
64 static int material_slot_add_exec(bContext *C, wmOperator *op)
66 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
69 return OPERATOR_CANCELLED;
71 object_add_material_slot(ob);
72 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
74 return OPERATOR_FINISHED;
77 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
80 ot->name= "Add Material Slot";
81 ot->idname= "OBJECT_OT_material_slot_add";
84 ot->exec= material_slot_add_exec;
87 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
90 static int material_slot_remove_exec(bContext *C, wmOperator *op)
92 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
95 return OPERATOR_CANCELLED;
97 object_remove_material_slot(ob);
98 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
100 return OPERATOR_FINISHED;
103 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
106 ot->name= "Remove Material Slot";
107 ot->idname= "OBJECT_OT_material_slot_remove";
110 ot->exec= material_slot_remove_exec;
113 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
116 static int material_slot_assign_exec(bContext *C, wmOperator *op)
118 Scene *scene= CTX_data_scene(C);
119 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
122 return OPERATOR_CANCELLED;
124 if(ob && ob->actcol>0) {
125 if(ob->type == OB_MESH) {
126 EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
130 for(efa= em->faces.first; efa; efa=efa->next)
132 efa->mat_nr= ob->actcol-1;
135 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
136 ListBase *editnurb= ((Curve*)ob->data)->editnurb;
140 for(nu= editnurb->first; nu; nu= nu->next)
142 nu->mat_nr= nu->charidx= ob->actcol-1;
145 else if(ob->type == OB_FONT) {
146 EditFont *ef= ((Curve*)ob->data)->editfont;
147 int i, selstart, selend;
149 if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
150 for(i=selstart; i<=selend; i++)
151 ef->textbufinfo[i].mat_nr = ob->actcol-1;
156 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
157 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, ob);
159 return OPERATOR_FINISHED;
162 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
165 ot->name= "Assign Material Slot";
166 ot->idname= "OBJECT_OT_material_slot_assign";
169 ot->exec= material_slot_assign_exec;
172 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
175 static int material_slot_de_select(bContext *C, int select)
177 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
180 return OPERATOR_CANCELLED;
182 if(ob->type == OB_MESH) {
183 EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
187 EM_select_by_material(em, ob->actcol-1);
189 EM_deselect_by_material(em, ob->actcol-1);
192 else if ELEM(ob->type, OB_CURVE, OB_SURF) {
193 ListBase *editnurb= ((Curve*)ob->data)->editnurb;
199 for(nu= editnurb->first; nu; nu=nu->next) {
200 if(nu->mat_nr==ob->actcol-1) {
221 a= nu->pntsu*nu->pntsv;
225 if(select) bp->f1 |= SELECT;
226 else bp->f1 &= ~SELECT;
235 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, ob);
237 return OPERATOR_FINISHED;
240 static int material_slot_select_exec(bContext *C, wmOperator *op)
242 return material_slot_de_select(C, 1);
245 void OBJECT_OT_material_slot_select(wmOperatorType *ot)
248 ot->name= "Select Material Slot";
249 ot->idname= "OBJECT_OT_material_slot_select";
252 ot->exec= material_slot_select_exec;
255 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
258 static int material_slot_deselect_exec(bContext *C, wmOperator *op)
260 return material_slot_de_select(C, 0);
263 void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
266 ot->name= "Deselect Material Slot";
267 ot->idname= "OBJECT_OT_material_slot_deselect";
270 ot->exec= material_slot_deselect_exec;
273 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
276 /********************** new material operator *********************/
278 static int new_material_exec(bContext *C, wmOperator *op)
280 Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
285 /* add or copy material */
287 ma= copy_material(ma);
289 ma= add_material("Material");
291 ma->id.us--; /* compensating for us++ in assign_material */
293 /* attempt to assign to material slot */
294 ptr= CTX_data_pointer_get_type(C, "material_slot", &RNA_MaterialSlot);
298 index= (Material**)ptr.data - ob->mat;
300 assign_material(ob, ma, index+1);
302 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
305 WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
307 return OPERATOR_FINISHED;
310 void MATERIAL_OT_new(wmOperatorType *ot)
313 ot->name= "New Material";
314 ot->idname= "MATERIAL_OT_new";
317 ot->exec= new_material_exec;
320 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
323 /********************** new texture operator *********************/
325 static int new_texture_exec(bContext *C, wmOperator *op)
327 Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
332 /* add or copy texture */
334 tex= copy_texture(tex);
336 tex= add_texture("Texture");
340 /* attempt to assign to texture slot */
341 ptr= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot);
349 id_us_min(&mtex->tex->id);
351 id_us_plus(&tex->id);
354 /* XXX nodes, notifier .. */
357 WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
359 return OPERATOR_FINISHED;
362 void TEXTURE_OT_new(wmOperatorType *ot)
365 ot->name= "New Texture";
366 ot->idname= "TEXTURE_OT_new";
369 ot->exec= new_texture_exec;
372 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
375 /********************** new world operator *********************/
377 static int new_world_exec(bContext *C, wmOperator *op)
379 Scene *scene= CTX_data_scene(C);
380 World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
382 /* add or copy world */
386 wo= add_world("World");
388 /* assign to scene */
390 id_us_min(&scene->world->id);
393 WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
395 return OPERATOR_FINISHED;
398 void WORLD_OT_new(wmOperatorType *ot)
401 ot->name= "New World";
402 ot->idname= "WORLD_OT_new";
405 ot->exec= new_world_exec;
408 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
413 /********************** particle system slot operators *********************/
415 static int particle_system_slot_add_exec(bContext *C, wmOperator *op)
417 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
418 Scene *scene = CTX_data_scene(C);
421 return OPERATOR_CANCELLED;
423 object_add_particle_system_slot(scene, ob);
424 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
426 return OPERATOR_FINISHED;
429 void OBJECT_OT_particle_system_slot_add(wmOperatorType *ot)
432 ot->name= "Add Particle System Slot";
433 ot->idname= "OBJECT_OT_particle_system_slot_add";
436 ot->exec= particle_system_slot_add_exec;
439 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
442 static int particle_system_slot_remove_exec(bContext *C, wmOperator *op)
444 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
445 Scene *scene = CTX_data_scene(C);
448 return OPERATOR_CANCELLED;
450 object_remove_particle_system_slot(scene, ob);
451 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
453 return OPERATOR_FINISHED;
456 void OBJECT_OT_particle_system_slot_remove(wmOperatorType *ot)
459 ot->name= "Remove Particle System Slot";
460 ot->idname= "OBJECT_OT_particle_system_slot_remove";
463 ot->exec= particle_system_slot_remove_exec;
466 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
469 /********************** new particle settings operator *********************/
471 static int new_particle_settings_exec(bContext *C, wmOperator *op)
473 Scene *scene = CTX_data_scene(C);
474 ParticleSettings *part= CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings).data;
478 /* add or copy particle setting */
480 part= psys_copy_settings(part);
482 part= psys_new_settings("PSys", NULL);
484 /* attempt to assign to material slot */
485 ptr= CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem);
488 ParticleSystem *psys = (ParticleSystem*)ptr.data;
496 DAG_scene_sort(scene);
497 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
499 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
502 return OPERATOR_FINISHED;
505 void PARTICLE_OT_new(wmOperatorType *ot)
508 ot->name= "New Particle Settings";
509 ot->idname= "PARTICLE_OT_new";
512 ot->exec= new_particle_settings_exec;
515 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;