This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/editors/render/render_shading.c

1054 lines
25 KiB
C
Raw Normal View History

/*
* ***** 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.
*
* 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,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2009 Blender Foundation.
* All rights reserved.
*
* Contributor(s): Blender Foundation
*
* ***** END GPL LICENSE BLOCK *****
*/
2011-02-27 20:29:51 +00:00
/** \file blender/editors/render/render_shading.c
* \ingroup edrend
*/
#include <stdlib.h>
#include <string.h>
#include "MEM_guardedalloc.h"
#include "DNA_mesh_types.h"
#include "DNA_curve_types.h"
#include "DNA_lamp_types.h"
#include "DNA_material_types.h"
#include "DNA_node_types.h"
#include "DNA_object_types.h"
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_space_types.h"
#include "DNA_world_types.h"
#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "BLF_translation.h"
#include "BKE_animsys.h"
#include "BKE_context.h"
#include "BKE_curve.h"
#include "BKE_depsgraph.h"
#include "BKE_font.h"
#include "BKE_global.h"
#include "BKE_icons.h"
#include "BKE_image.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_material.h"
#include "BKE_node.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "BKE_texture.h"
#include "BKE_world.h"
#include "BKE_tessmesh.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "GPU_material.h"
#include "RNA_access.h"
#include "WM_api.h"
#include "WM_types.h"
#include "ED_object.h"
#include "ED_curve.h"
#include "ED_mesh.h"
#include "ED_node.h"
#include "ED_render.h"
#include "ED_screen.h"
#include "RNA_define.h"
#include "UI_interface.h"
#include "RE_pipeline.h"
2012-03-29 22:42:32 +00:00
#include "render_intern.h" // own include
/********************** material slot operators *********************/
static int material_slot_add_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Object *ob = ED_object_context(C);
if (!ob)
return OPERATOR_CANCELLED;
object_add_material_slot(ob);
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_material_slot_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Material Slot";
ot->idname = "OBJECT_OT_material_slot_add";
ot->description = "Add a new material slot";
/* api callbacks */
ot->exec = material_slot_add_exec;
ot->poll = ED_operator_object_active_editable;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int material_slot_remove_exec(bContext *C, wmOperator *op)
{
2012-03-29 22:42:32 +00:00
Object *ob = ED_object_context(C);
if (!ob)
return OPERATOR_CANCELLED;
/* Removing material slots in edit mode screws things up, see bug #21822.*/
if (ob == CTX_data_edit_object(C)) {
BKE_report(op->reports, RPT_ERROR, "Unable to remove material slot in edit mode");
return OPERATOR_CANCELLED;
}
object_remove_material_slot(ob);
DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Remove Material Slot";
ot->idname = "OBJECT_OT_material_slot_remove";
ot->description = "Remove the selected material slot";
/* api callbacks */
ot->exec = material_slot_remove_exec;
ot->poll = ED_operator_object_active_editable;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int material_slot_assign_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Object *ob = ED_object_context(C);
if (!ob)
return OPERATOR_CANCELLED;
2012-03-29 22:42:32 +00:00
if (ob && ob->actcol > 0) {
if (ob->type == OB_MESH) {
BMEditMesh *em = BMEdit_FromObject(ob);
BMFace *efa;
BMIter iter;
if (em) {
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(efa, BM_ELEM_SELECT))
2012-03-29 22:42:32 +00:00
efa->mat_nr = ob->actcol - 1;
}
}
}
else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
Nurb *nu;
ListBase *nurbs = BKE_curve_editNurbs_get((Curve *)ob->data);
if (nurbs) {
2012-03-29 22:42:32 +00:00
for (nu = nurbs->first; nu; nu = nu->next)
if (isNurbsel(nu))
2012-03-29 22:42:32 +00:00
nu->mat_nr = nu->charidx = ob->actcol - 1;
}
}
else if (ob->type == OB_FONT) {
2012-03-29 22:42:32 +00:00
EditFont *ef = ((Curve *)ob->data)->editfont;
int i, selstart, selend;
if (ef && BKE_vfont_select_get(ob, &selstart, &selend)) {
2012-03-29 22:42:32 +00:00
for (i = selstart; i <= selend; i++)
ef->textbufinfo[i].mat_nr = ob->actcol;
}
}
}
DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
return OPERATOR_FINISHED;
}
void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Assign Material Slot";
ot->idname = "OBJECT_OT_material_slot_assign";
ot->description = "Assign active material slot to selection";
/* api callbacks */
ot->exec = material_slot_assign_exec;
ot->poll = ED_operator_object_active_editable;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int material_slot_de_select(bContext *C, int select)
{
Object *ob = ED_object_context(C);
if (!ob)
return OPERATOR_CANCELLED;
if (ob->type == OB_MESH) {
BMEditMesh *em = BMEdit_FromObject(ob);
if (em) {
2012-03-29 22:42:32 +00:00
EDBM_deselect_by_material(em, ob->actcol - 1, select);
}
}
else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
ListBase *nurbs = BKE_curve_editNurbs_get((Curve *)ob->data);
Nurb *nu;
BPoint *bp;
BezTriple *bezt;
int a;
if (nurbs) {
2012-03-29 22:42:32 +00:00
for (nu = nurbs->first; nu; nu = nu->next) {
if (nu->mat_nr == ob->actcol - 1) {
if (nu->bezt) {
2012-03-29 22:42:32 +00:00
a = nu->pntsu;
bezt = nu->bezt;
while (a--) {
2012-03-29 22:42:32 +00:00
if (bezt->hide == 0) {
if (select) {
bezt->f1 |= SELECT;
bezt->f2 |= SELECT;
bezt->f3 |= SELECT;
}
else {
bezt->f1 &= ~SELECT;
bezt->f2 &= ~SELECT;
bezt->f3 &= ~SELECT;
}
}
bezt++;
}
}
else if (nu->bp) {
2012-03-29 22:42:32 +00:00
a = nu->pntsu * nu->pntsv;
bp = nu->bp;
while (a--) {
2012-03-29 22:42:32 +00:00
if (bp->hide == 0) {
if (select) bp->f1 |= SELECT;
else bp->f1 &= ~SELECT;
}
bp++;
}
}
}
}
}
}
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
return OPERATOR_FINISHED;
}
static int material_slot_select_exec(bContext *C, wmOperator *UNUSED(op))
{
return material_slot_de_select(C, 1);
}
void OBJECT_OT_material_slot_select(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Material Slot";
ot->idname = "OBJECT_OT_material_slot_select";
ot->description = "Select by active material slot";
/* api callbacks */
ot->exec = material_slot_select_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int material_slot_deselect_exec(bContext *C, wmOperator *UNUSED(op))
{
return material_slot_de_select(C, 0);
}
void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Deselect Material Slot";
ot->idname = "OBJECT_OT_material_slot_deselect";
ot->description = "Deselect by active material slot";
/* api callbacks */
ot->exec = material_slot_deselect_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int material_slot_copy_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Object *ob = ED_object_context(C);
Material ***matar;
2012-03-29 22:42:32 +00:00
if (!ob || !(matar = give_matarar(ob)))
return OPERATOR_CANCELLED;
CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
{
if (ob != ob_iter && give_matarar(ob_iter)) {
if (ob->data != ob_iter->data)
assign_matarar(ob_iter, matar, ob->totcol);
2012-03-29 22:42:32 +00:00
if (ob_iter->totcol == ob->totcol) {
ob_iter->actcol = ob->actcol;
WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob_iter);
}
}
}
CTX_DATA_END;
return OPERATOR_FINISHED;
}
void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Copy Material to Others";
ot->idname = "OBJECT_OT_material_slot_copy";
ot->description = "Copies materials to other selected objects";
/* api callbacks */
ot->exec = material_slot_copy_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/********************** new material operator *********************/
static int new_material_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Scene *scene = CTX_data_scene(C);
Material *ma = CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
Main *bmain = CTX_data_main(C);
PointerRNA ptr, idptr;
PropertyRNA *prop;
/* add or copy material */
if (ma) {
ma = BKE_material_copy(ma);
}
else {
ma = BKE_material_add(bmain, DATA_("Material"));
if (BKE_scene_use_new_shading_nodes(scene)) {
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
ED_node_shader_default(C, &ma->id);
ma->use_nodes = TRUE;
}
}
/* hook into UI */
uiIDContextProperty(C, &ptr, &prop);
if (prop) {
/* when creating new ID blocks, use is already 1, but RNA
* pointer se also increases user, so this compensates it */
ma->id.us--;
RNA_id_pointer_create(&ma->id, &idptr);
RNA_property_pointer_set(&ptr, prop, idptr);
RNA_property_update(C, &ptr, prop);
}
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_MATERIAL | NA_ADDED, ma);
return OPERATOR_FINISHED;
}
void MATERIAL_OT_new(wmOperatorType *ot)
{
/* identifiers */
ot->name = "New Material";
ot->idname = "MATERIAL_OT_new";
ot->description = "Add a new material";
/* api callbacks */
ot->exec = new_material_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/********************** new texture operator *********************/
static int new_texture_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
Main *bmain = CTX_data_main(C);
PointerRNA ptr, idptr;
PropertyRNA *prop;
/* add or copy texture */
if (tex)
tex = BKE_texture_copy(tex);
else
tex = add_texture(bmain, DATA_("Texture"));
/* hook into UI */
uiIDContextProperty(C, &ptr, &prop);
if (prop) {
/* when creating new ID blocks, use is already 1, but RNA
* pointer se also increases user, so this compensates it */
tex->id.us--;
RNA_id_pointer_create(&tex->id, &idptr);
RNA_property_pointer_set(&ptr, prop, idptr);
RNA_property_update(C, &ptr, prop);
}
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_TEXTURE | NA_ADDED, tex);
return OPERATOR_FINISHED;
}
void TEXTURE_OT_new(wmOperatorType *ot)
{
/* identifiers */
ot->name = "New Texture";
ot->idname = "TEXTURE_OT_new";
ot->description = "Add a new texture";
/* api callbacks */
ot->exec = new_texture_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/********************** new world operator *********************/
static int new_world_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Scene *scene = CTX_data_scene(C);
World *wo = CTX_data_pointer_get_type(C, "world", &RNA_World).data;
Main *bmain = CTX_data_main(C);
PointerRNA ptr, idptr;
PropertyRNA *prop;
/* add or copy world */
if (wo) {
wo = BKE_world_copy(wo);
}
else {
wo = add_world(bmain, "World");
if (BKE_scene_use_new_shading_nodes(scene)) {
Merge of the PyNodes branch (aka "custom nodes") into trunk. PyNodes opens up the node system in Blender to scripters and adds a number of UI-level improvements. === Dynamic node type registration === Node types can now be added at runtime, using the RNA registration mechanism from python. This enables addons such as render engines to create a complete user interface with nodes. Examples of how such nodes can be defined can be found in my personal wiki docs atm [1] and as a script template in release/scripts/templates_py/custom_nodes.py [2]. === Node group improvements === Each node editor now has a tree history of edited node groups, which allows opening and editing nested node groups. The node editor also supports pinning now, so that different spaces can be used to edit different node groups simultaneously. For more ramblings and rationale see (really old) blog post on code.blender.org [3]. The interface of node groups has been overhauled. Sockets of a node group are no longer displayed in columns on either side, but instead special input/output nodes are used to mirror group sockets inside a node tree. This solves the problem of long node lines in groups and allows more adaptable node layout. Internal sockets can be exposed from a group by either connecting to the extension sockets in input/output nodes (shown as empty circle) or by adding sockets from the node property bar in the "Interface" panel. Further details such as the socket name can also be changed there. [1] http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes [2] http://projects.blender.org/scm/viewvc.php/trunk/blender/release/scripts/templates_py/custom_nodes.py?view=markup&root=bf-blender [3] http://code.blender.org/index.php/2012/01/improving-node-group-interface-editing/
2013-03-18 16:34:57 +00:00
ED_node_shader_default(C, &wo->id);
wo->use_nodes = TRUE;
}
}
/* hook into UI */
uiIDContextProperty(C, &ptr, &prop);
if (prop) {
/* when creating new ID blocks, use is already 1, but RNA
* pointer se also increases user, so this compensates it */
wo->id.us--;
RNA_id_pointer_create(&wo->id, &idptr);
RNA_property_pointer_set(&ptr, prop, idptr);
RNA_property_update(C, &ptr, prop);
}
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_WORLD | NA_ADDED, wo);
return OPERATOR_FINISHED;
}
void WORLD_OT_new(wmOperatorType *ot)
{
/* identifiers */
ot->name = "New World";
ot->idname = "WORLD_OT_new";
ot->description = "Add a new world";
/* api callbacks */
ot->exec = new_world_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/********************** render layer operators *********************/
static int render_layer_add_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Scene *scene = CTX_data_scene(C);
BKE_scene_add_render_layer(scene, NULL);
2012-03-29 22:42:32 +00:00
scene->r.actlay = BLI_countlist(&scene->r.layers) - 1;
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);
return OPERATOR_FINISHED;
}
void SCENE_OT_render_layer_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Render Layer";
ot->idname = "SCENE_OT_render_layer_add";
ot->description = "Add a render layer";
/* api callbacks */
ot->exec = render_layer_add_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int render_layer_remove_exec(bContext *C, wmOperator *UNUSED(op))
{
Scene *scene = CTX_data_scene(C);
SceneRenderLayer *rl = BLI_findlink(&scene->r.layers, scene->r.actlay);
if (!BKE_scene_remove_render_layer(CTX_data_main(C), scene, rl))
return OPERATOR_CANCELLED;
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);
return OPERATOR_FINISHED;
}
void SCENE_OT_render_layer_remove(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Remove Render Layer";
ot->idname = "SCENE_OT_render_layer_remove";
ot->description = "Remove the selected render layer";
/* api callbacks */
ot->exec = render_layer_remove_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int texture_slot_move(bContext *C, wmOperator *op)
{
2012-03-29 22:42:32 +00:00
ID *id = CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
if (id) {
MTex **mtex_ar, *mtexswap;
short act;
2012-03-29 22:42:32 +00:00
int type = RNA_enum_get(op->ptr, "type");
struct AnimData *adt = BKE_animdata_from_id(id);
give_active_mtex(id, &mtex_ar, &act);
if (type == -1) { /* Up */
if (act > 0) {
mtexswap = mtex_ar[act];
2012-03-29 22:42:32 +00:00
mtex_ar[act] = mtex_ar[act - 1];
mtex_ar[act - 1] = mtexswap;
BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, act - 1, -1, 0);
BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, act, act - 1, 0);
BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, -1, act, 0);
2012-03-29 22:42:32 +00:00
if (GS(id->name) == ID_MA) {
Material *ma = (Material *)id;
int mtexuse = ma->septex & (1 << act);
ma->septex &= ~(1 << act);
ma->septex |= (ma->septex & (1 << (act - 1))) << 1;
ma->septex &= ~(1 << (act - 1));
ma->septex |= mtexuse >> 1;
}
2012-03-29 22:42:32 +00:00
set_active_mtex(id, act - 1);
}
}
else { /* Down */
2012-03-29 22:42:32 +00:00
if (act < MAX_MTEX - 1) {
mtexswap = mtex_ar[act];
2012-03-29 22:42:32 +00:00
mtex_ar[act] = mtex_ar[act + 1];
mtex_ar[act + 1] = mtexswap;
BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, act + 1, -1, 0);
BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, act, act + 1, 0);
BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, -1, act, 0);
2012-03-29 22:42:32 +00:00
if (GS(id->name) == ID_MA) {
Material *ma = (Material *)id;
int mtexuse = ma->septex & (1 << act);
ma->septex &= ~(1 << act);
ma->septex |= (ma->septex & (1 << (act + 1))) >> 1;
ma->septex &= ~(1 << (act + 1));
ma->septex |= mtexuse << 1;
}
2012-03-29 22:42:32 +00:00
set_active_mtex(id, act + 1);
}
}
DAG_id_tag_update(id, 0);
WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
}
return OPERATOR_FINISHED;
}
void TEXTURE_OT_slot_move(wmOperatorType *ot)
{
static EnumPropertyItem slot_move[] = {
{-1, "UP", 0, "Up", ""},
{1, "DOWN", 0, "Down", ""},
{0, NULL, 0, NULL, NULL}
};
/* identifiers */
ot->name = "Move Texture Slot";
ot->idname = "TEXTURE_OT_slot_move";
ot->description = "Move texture slots up and down";
/* api callbacks */
ot->exec = texture_slot_move;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
}
/********************** environment map operators *********************/
static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *path, const char imtype)
{
float layout[12];
2012-03-29 22:42:32 +00:00
if (RNA_struct_find_property(op->ptr, "layout") )
RNA_float_get_array(op->ptr, "layout", layout);
else
memcpy(layout, default_envmap_layout, sizeof(layout));
if (RE_WriteEnvmapResult(op->reports, scene, env, path, imtype, layout)) {
return OPERATOR_FINISHED;
}
else {
return OPERATOR_CANCELLED;
}
}
static int envmap_save_exec(bContext *C, wmOperator *op)
{
2012-03-29 22:42:32 +00:00
Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
Scene *scene = CTX_data_scene(C);
//int imtype = RNA_enum_get(op->ptr, "file_type");
char imtype = scene->r.im_format.imtype;
char path[FILE_MAX];
RNA_string_get(op->ptr, "filepath", path);
if (scene->r.scemode & R_EXTENSION) {
BKE_add_image_extension(path, &scene->r.im_format);
}
WM_cursor_wait(1);
save_envmap(op, scene, tex->env, path, imtype);
WM_cursor_wait(0);
WM_event_add_notifier(C, NC_TEXTURE, tex);
return OPERATOR_FINISHED;
}
static int envmap_save_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
//Scene *scene= CTX_data_scene(C);
if (RNA_struct_property_is_set(op->ptr, "filepath"))
return envmap_save_exec(C, op);
//RNA_enum_set(op->ptr, "file_type", scene->r.im_format.imtype);
RNA_string_set(op->ptr, "filepath", G.main->name);
WM_event_add_fileselect(C, op);
return OPERATOR_RUNNING_MODAL;
}
static int envmap_save_poll(bContext *C)
{
2012-03-29 22:42:32 +00:00
Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
if (!tex)
return 0;
if (!tex->env || !tex->env->ok)
return 0;
2012-03-29 22:42:32 +00:00
if (tex->env->cube[1] == NULL)
return 0;
return 1;
}
void TEXTURE_OT_envmap_save(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Save Environment Map";
ot->idname = "TEXTURE_OT_envmap_save";
ot->description = "Save the current generated Environment map to an image file";
/* api callbacks */
ot->exec = envmap_save_exec;
ot->invoke = envmap_save_invoke;
ot->poll = envmap_save_poll;
/* flags */
ot->flag = OPTYPE_REGISTER; /* no undo since this doesnt modify the env-map */
/* properties */
prop = RNA_def_float_array(ot->srna, "layout", 12, default_envmap_layout, 0.0f, 0.0f,
"File layout",
"Flat array describing the X,Y position of each cube face in the output image, "
"where 1 is the size of a face - order is [+Z -Z +Y -X -Y +X] "
"(use -1 to skip a face)", 0.0f, 0.0f);
RNA_def_property_flag(prop, PROP_HIDDEN);
2012-08-04 12:54:27 +00:00
WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE | MOVIEFILE, FILE_SPECIAL, FILE_SAVE,
WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
}
static int envmap_clear_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
BKE_free_envmapdata(tex->env);
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_TEXTURE | NA_EDITED, tex);
return OPERATOR_FINISHED;
}
static int envmap_clear_poll(bContext *C)
{
2012-03-29 22:42:32 +00:00
Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
if (!tex)
return 0;
if (!tex->env || !tex->env->ok)
return 0;
2012-03-29 22:42:32 +00:00
if (tex->env->cube[1] == NULL)
return 0;
return 1;
}
void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Clear Environment Map";
ot->idname = "TEXTURE_OT_envmap_clear";
ot->description = "Discard the environment map and free it from memory";
/* api callbacks */
ot->exec = envmap_clear_exec;
ot->poll = envmap_clear_poll;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int envmap_clear_all_exec(bContext *C, wmOperator *UNUSED(op))
{
Main *bmain = CTX_data_main(C);
Tex *tex;
2012-03-29 22:42:32 +00:00
for (tex = bmain->tex.first; tex; tex = tex->id.next)
if (tex->env)
BKE_free_envmapdata(tex->env);
2012-03-29 22:42:32 +00:00
WM_event_add_notifier(C, NC_TEXTURE | NA_EDITED, tex);
return OPERATOR_FINISHED;
}
void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Clear All Environment Maps";
ot->idname = "TEXTURE_OT_envmap_clear_all";
ot->description = "Discard all environment maps in the .blend file and free them from memory";
/* api callbacks */
ot->exec = envmap_clear_all_exec;
ot->poll = envmap_clear_poll;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/********************** material operators *********************/
/* material copy/paste */
static int copy_material_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Material *ma = CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
2012-03-29 22:42:32 +00:00
if (ma == NULL)
return OPERATOR_CANCELLED;
copy_matcopybuf(ma);
return OPERATOR_FINISHED;
}
void MATERIAL_OT_copy(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Copy Material";
ot->idname = "MATERIAL_OT_copy";
ot->description = "Copy the material settings and nodes";
/* api callbacks */
ot->exec = copy_material_exec;
/* flags */
ot->flag = OPTYPE_REGISTER; /* no undo needed since no changes are made to the material */
}
static int paste_material_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
Material *ma = CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
2012-03-29 22:42:32 +00:00
if (ma == NULL)
2010-01-28 17:50:50 +00:00
return OPERATOR_CANCELLED;
paste_matcopybuf(ma);
WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
return OPERATOR_FINISHED;
}
void MATERIAL_OT_paste(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Paste Material";
ot->idname = "MATERIAL_OT_paste";
ot->description = "Paste the material settings and nodes";
/* api callbacks */
ot->exec = paste_material_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
2012-03-29 22:42:32 +00:00
static short mtexcopied = 0; /* must be reset on file load */
static MTex mtexcopybuf;
void ED_render_clear_mtex_copybuf(void)
2012-03-29 22:42:32 +00:00
{ /* use for file reload */
mtexcopied = 0;
}
static void copy_mtex_copybuf(ID *id)
{
2012-03-29 22:42:32 +00:00
MTex **mtex = NULL;
2012-03-29 22:42:32 +00:00
switch (GS(id->name)) {
case ID_MA:
2012-03-29 22:42:32 +00:00
mtex = &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
break;
case ID_LA:
2012-03-29 22:42:32 +00:00
mtex = &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
// la->mtex[(int)la->texact] // TODO
break;
case ID_WO:
2012-03-29 22:42:32 +00:00
mtex = &(((World *)id)->mtex[(int)((World *)id)->texact]);
// mtex= wrld->mtex[(int)wrld->texact]; // TODO
break;
case ID_PA:
2012-03-29 22:42:32 +00:00
mtex = &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
break;
}
if (mtex && *mtex) {
memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
2012-03-29 22:42:32 +00:00
mtexcopied = 1;
}
else {
2012-03-29 22:42:32 +00:00
mtexcopied = 0;
}
}
static void paste_mtex_copybuf(ID *id)
{
2012-03-29 22:42:32 +00:00
MTex **mtex = NULL;
2012-03-29 22:42:32 +00:00
if (mtexcopied == 0 || mtexcopybuf.tex == NULL)
return;
2012-03-29 22:42:32 +00:00
switch (GS(id->name)) {
case ID_MA:
2012-03-29 22:42:32 +00:00
mtex = &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
break;
case ID_LA:
2012-03-29 22:42:32 +00:00
mtex = &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
// la->mtex[(int)la->texact] // TODO
break;
case ID_WO:
2012-03-29 22:42:32 +00:00
mtex = &(((World *)id)->mtex[(int)((World *)id)->texact]);
// mtex= wrld->mtex[(int)wrld->texact]; // TODO
break;
case ID_PA:
2012-03-29 22:42:32 +00:00
mtex = &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
break;
default:
BLI_assert("invalid id type");
return;
}
if (mtex) {
2012-03-29 22:42:32 +00:00
if (*mtex == NULL) {
*mtex = MEM_mallocN(sizeof(MTex), "mtex copy");
}
else if ((*mtex)->tex) {
(*mtex)->tex->id.us--;
}
memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
id_us_plus((ID *)mtexcopybuf.tex);
}
}
static int copy_mtex_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
ID *id = CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
2012-03-29 22:42:32 +00:00
if (id == NULL) {
/* copying empty slot */
ED_render_clear_mtex_copybuf();
return OPERATOR_CANCELLED;
}
copy_mtex_copybuf(id);
return OPERATOR_FINISHED;
}
static int copy_mtex_poll(bContext *C)
{
2012-03-29 22:42:32 +00:00
ID *id = CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
return (id != NULL);
}
void TEXTURE_OT_slot_copy(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Copy Texture Slot Settings";
ot->idname = "TEXTURE_OT_slot_copy";
ot->description = "Copy the material texture settings and nodes";
/* api callbacks */
ot->exec = copy_mtex_exec;
ot->poll = copy_mtex_poll;
/* flags */
ot->flag = OPTYPE_REGISTER; /* no undo needed since no changes are made to the mtex */
}
static int paste_mtex_exec(bContext *C, wmOperator *UNUSED(op))
{
2012-03-29 22:42:32 +00:00
ID *id = CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
2012-03-29 22:42:32 +00:00
if (id == NULL) {
Material *ma = CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
Lamp *la = CTX_data_pointer_get_type(C, "lamp", &RNA_Lamp).data;
World *wo = CTX_data_pointer_get_type(C, "world", &RNA_World).data;
ParticleSystem *psys = CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem).data;
if (ma)
id = &ma->id;
else if (la)
id = &la->id;
else if (wo)
id = &wo->id;
else if (psys)
id = &psys->part->id;
2012-03-29 22:42:32 +00:00
if (id == NULL)
return OPERATOR_CANCELLED;
}
paste_mtex_copybuf(id);
WM_event_add_notifier(C, NC_TEXTURE | ND_SHADING_LINKS, NULL);
return OPERATOR_FINISHED;
}
void TEXTURE_OT_slot_paste(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Paste Texture Slot Settings";
ot->idname = "TEXTURE_OT_slot_paste";
ot->description = "Copy the texture settings and nodes";
/* api callbacks */
ot->exec = paste_mtex_exec;
/* flags */
2012-03-29 22:42:32 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}