2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/makesrna/intern/rna_object_api.c
37 #include "RNA_define.h"
39 #include "DNA_object_types.h"
40 #include "DNA_modifier_types.h"
42 // #include "BLO_sys_types.h" /* needed for intptr_t used in ED_mesh.h */
44 // #include "ED_mesh.h"
51 #include "BKE_global.h"
52 #include "BKE_context.h"
53 #include "BKE_report.h"
54 #include "BKE_object.h"
56 #include "BKE_DerivedMesh.h"
57 #include "BKE_bvhutils.h"
59 #include "BKE_customdata.h"
61 #include "BKE_depsgraph.h"
62 #include "BKE_displist.h"
64 #include "BKE_mball.h"
65 #include "BKE_modifier.h"
67 #include "DNA_mesh_types.h"
68 #include "DNA_scene_types.h"
69 #include "DNA_meshdata_types.h"
70 #include "DNA_curve_types.h"
71 #include "DNA_modifier_types.h"
72 #include "DNA_constraint_types.h"
73 #include "DNA_view3d_types.h"
75 #include "MEM_guardedalloc.h"
77 /* copied from Mesh_getFromObject and adapted to RNA interface */
78 /* settings: 0 - preview, 1 - render */
79 Mesh *rna_Object_to_mesh(Object *ob, ReportList *reports, Scene *sce, int apply_modifiers, int settings)
83 Object *tmpobj = NULL;
84 int render = settings == eModifierMode_Render, i;
85 int cage = !apply_modifiers;
87 /* perform the mesh extraction based on type */
93 /* copies object and modifiers (but not the data) */
94 tmpobj= copy_object(ob);
95 tmpcu = (Curve *)tmpobj->data;
98 /* if getting the original caged mesh, delete object modifiers */
100 object_free_modifiers(tmpobj);
102 /* copies the data */
103 tmpobj->data = copy_curve( (Curve *) ob->data );
106 /* copy_curve() sets disp.first null, so currently not need */
109 cu = (Curve *)tmpobj->data;
111 MEM_freeN( cu->disp.first );
112 cu->disp.first = NULL;
117 /* get updated display list, and convert to a mesh */
118 makeDispListCurveTypes( sce, tmpobj, 0 );
119 nurbs_to_mesh( tmpobj );
121 /* nurbs_to_mesh changes the type to a mesh, check it worked */
122 if (tmpobj->type != OB_MESH) {
123 free_libblock_us( &(G.main->object), tmpobj );
124 BKE_report(reports, RPT_ERROR, "cant convert curve to mesh. Does the curve have any segments?");
127 tmpmesh = tmpobj->data;
128 free_libblock_us( &G.main->object, tmpobj );
132 /* metaballs don't have modifiers, so just convert to mesh */
133 ob = find_basis_mball( sce, ob );
134 /* todo, re-generatre for render-res */
135 /* metaball_polygonize(scene, ob) */
137 tmpmesh = add_mesh("Mesh");
138 mball_to_mesh( &ob->disp, tmpmesh );
142 /* copies object and modifiers (but not the data) */
144 /* copies the data */
145 tmpmesh = copy_mesh( ob->data );
146 /* if not getting the original caged mesh, get final derived mesh */
148 /* Make a dummy mesh, saves copying */
150 /* CustomDataMask mask = CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL; */
151 CustomDataMask mask = CD_MASK_MESH; /* this seems more suitable, exporter,
152 for example, needs CD_MASK_MDEFORMVERT */
154 /* Write the display mesh into the dummy mesh */
156 dm = mesh_create_derived_render( sce, ob, mask );
158 dm = mesh_create_derived_view( sce, ob, mask );
160 tmpmesh = add_mesh( "Mesh" );
161 DM_to_mesh( dm, tmpmesh, ob );
167 BKE_report(reports, RPT_ERROR, "Object does not have geometry data");
171 /* Copy materials to new mesh */
176 tmpmesh->totcol = tmpcu->totcol;
178 /* free old material list (if it exists) and adjust user counts */
180 for( i = tmpcu->totcol; i-- > 0; ) {
181 /* are we an object material or data based? */
182 if (ob->colbits & 1<<i)
183 tmpmesh->mat[i] = ob->mat[i];
185 tmpmesh->mat[i] = tmpcu->mat[i];
188 tmpmesh->mat[i]->id.us++;
194 /* Crashes when assigning the new material, not sure why */
196 tmpmb = (MetaBall *)ob->data;
197 tmpmesh->totcol = tmpmb->totcol;
199 /* free old material list (if it exists) and adjust user counts */
201 for( i = tmpmb->totcol; i-- > 0; ) {
202 tmpmesh->mat[i] = tmpmb->mat[i]; /* CRASH HERE ??? */
203 if (tmpmesh->mat[i]) {
204 tmpmb->mat[i]->id.us++;
213 Mesh *origmesh= ob->data;
214 tmpmesh->flag= origmesh->flag;
215 tmpmesh->mat = MEM_dupallocN(origmesh->mat);
216 tmpmesh->totcol = origmesh->totcol;
217 tmpmesh->smoothresh= origmesh->smoothresh;
218 if( origmesh->mat ) {
219 for( i = origmesh->totcol; i-- > 0; ) {
220 /* are we an object material or data based? */
221 if (ob->colbits & 1<<i)
222 tmpmesh->mat[i] = ob->mat[i];
224 tmpmesh->mat[i] = origmesh->mat[i];
226 tmpmesh->mat[i]->id.us++;
231 } /* end copy materials */
233 /* we don't assign it to anything */
236 /* make sure materials get updated in objects */
237 test_object_materials( ( ID * ) tmpmesh );
242 /* mostly a copy from convertblender.c */
243 static void dupli_render_particle_set(Scene *scene, Object *ob, int level, int enable)
245 /* ugly function, but we need to set particle systems to their render
246 * settings before calling object_duplilist, to get render level duplis */
249 ParticleSystem *psys;
255 if(level >= MAX_DUPLI_RECUR)
258 if(ob->transflag & OB_DUPLIPARTS) {
259 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
260 if(ELEM(psys->part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
262 psys_render_set(ob, psys, mat, mat, 1, 1, 0.f);
264 psys_render_restore(ob, psys);
268 if(level == 0 && enable) {
269 /* this is to make sure we get render level duplis in groups:
270 * the derivedmesh must be created before init_render_mesh,
271 * since object_duplilist does dupliparticles before that */
272 dm = mesh_create_derived_render(scene, ob, CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
275 for(psys=ob->particlesystem.first; psys; psys=psys->next)
276 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
280 if(ob->dup_group==NULL) return;
281 group= ob->dup_group;
283 for(go= group->gobject.first; go; go= go->next)
284 dupli_render_particle_set(scene, go->ob, level+1, enable);
286 /* When no longer needed, duplilist should be freed with Object.free_duplilist */
287 void rna_Object_create_duplilist(Object *ob, ReportList *reports, Scene *sce)
289 if (!(ob->transflag & OB_DUPLI)) {
290 BKE_report(reports, RPT_ERROR, "Object does not have duplis");
294 /* free duplilist if a user forgets to */
296 BKE_reportf(reports, RPT_WARNING, "Object.dupli_list has not been freed");
298 free_object_duplilist(ob->duplilist);
302 dupli_render_particle_set(sce, ob, 0, 1);
303 ob->duplilist= object_duplilist(sce, ob);
305 dupli_render_particle_set(sce, ob, 0, 0);
306 /* ob->duplilist should now be freed with Object.free_duplilist */
309 void rna_Object_free_duplilist(Object *ob)
312 free_object_duplilist(ob->duplilist);
317 static PointerRNA rna_Object_shape_key_add(Object *ob, bContext *C, ReportList *reports, const char *name, int from_mix)
319 Scene *scene= CTX_data_scene(C);
322 if((kb=object_insert_shape_key(scene, ob, name, from_mix))) {
325 RNA_pointer_create((ID *)ob->data, &RNA_ShapeKey, kb, &keyptr);
326 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
331 BKE_reportf(reports, RPT_ERROR, "Object \"%s\"does not support shapes", ob->id.name+2);
332 return PointerRNA_NULL;
336 int rna_Object_is_visible(Object *ob, Scene *sce)
338 return !(ob->restrictflag & OB_RESTRICT_VIEW) && (ob->lay & sce->lay);
342 static void rna_Mesh_assign_verts_to_group(Object *ob, bDeformGroup *group, int *indices, int totindex, float weight, int assignmode)
344 if (ob->type != OB_MESH) {
345 BKE_report(reports, RPT_ERROR, "Object should be of MESH type");
349 Mesh *me = (Mesh*)ob->data;
350 int group_index = defgroup_find_index(ob, group);
351 if (group_index == -1) {
352 BKE_report(reports, RPT_ERROR, "No deform groups assigned to mesh");
356 if (assignmode != WEIGHT_REPLACE && assignmode != WEIGHT_ADD && assignmode != WEIGHT_SUBTRACT) {
357 BKE_report(reports, RPT_ERROR, "Bad assignment mode" );
361 // makes a set of dVerts corresponding to the mVerts
363 create_dverts(&me->id);
365 // loop list adding verts to group
366 for (i= 0; i < totindex; i++) {
367 if(i < 0 || i >= me->totvert) {
368 BKE_report(reports, RPT_ERROR, "Bad vertex index in list");
372 add_vert_defnr(ob, group_index, i, weight, assignmode);
377 void rna_Object_ray_cast(Object *ob, ReportList *reports, float ray_start[3], float ray_end[3], float r_location[3], float r_normal[3], int *index)
379 BVHTreeFromMesh treeData= {NULL};
381 if(ob->derivedFinal==NULL) {
382 BKE_reportf(reports, RPT_ERROR, "object \"%s\" has no mesh data to be used for ray casting", ob->id.name+2);
386 /* no need to managing allocation or freeing of the BVH data. this is generated and freed as needed */
387 bvhtree_from_mesh_faces(&treeData, ob->derivedFinal, 0.0f, 4, 6);
389 if(treeData.tree==NULL) {
390 BKE_reportf(reports, RPT_ERROR, "object \"%s\" could not create internal data for ray casting", ob->id.name+2);
395 float ray_nor[3], dist;
396 sub_v3_v3v3(ray_nor, ray_end, ray_start);
398 dist= hit.dist = normalize_v3(ray_nor);
401 if(BLI_bvhtree_ray_cast(treeData.tree, ray_start, ray_nor, 0.0f, &hit, treeData.raycast_callback, &treeData) != -1) {
403 copy_v3_v3(r_location, hit.co);
404 copy_v3_v3(r_normal, hit.no);
416 void rna_Object_closest_point_on_mesh(Object *ob, ReportList *reports, float point_co[3], float max_dist, float n_location[3], float n_normal[3], int *index)
418 BVHTreeFromMesh treeData= {NULL};
420 if(ob->derivedFinal==NULL) {
421 BKE_reportf(reports, RPT_ERROR, "object \"%s\" has no mesh data to be used for finding nearest point", ob->id.name+2);
425 /* no need to managing allocation or freeing of the BVH data. this is generated and freed as needed */
426 bvhtree_from_mesh_faces(&treeData, ob->derivedFinal, 0.0f, 4, 6);
428 if(treeData.tree==NULL) {
429 BKE_reportf(reports, RPT_ERROR, "object \"%s\" could not create internal data for finding nearest point", ob->id.name+2);
433 BVHTreeNearest nearest;
436 nearest.dist = max_dist * max_dist;
438 if(BLI_bvhtree_find_nearest(treeData.tree, point_co, &nearest, treeData.nearest_callback, &treeData) != -1) {
439 copy_v3_v3(n_location, nearest.co);
440 copy_v3_v3(n_normal, nearest.no);
441 *index= nearest.index;
453 void rna_ObjectBase_layers_from_view(Base *base, View3D *v3d)
455 base->lay= base->object->lay= v3d->lay;
458 int rna_Object_is_modified(Object *ob, Scene *scene, int settings)
460 return object_is_modified(scene, ob) & settings;
465 void RNA_api_object(StructRNA *srna)
470 static EnumPropertyItem mesh_type_items[] = {
471 {eModifierMode_Realtime, "PREVIEW", 0, "Preview", "Apply modifier preview settings"},
472 {eModifierMode_Render, "RENDER", 0, "Render", "Apply modifier render settings"},
473 {0, NULL, 0, NULL, NULL}
477 func= RNA_def_function(srna, "to_mesh", "rna_Object_to_mesh");
478 RNA_def_function_ui_description(func, "Create a Mesh datablock with modifiers applied");
479 RNA_def_function_flag(func, FUNC_USE_REPORTS);
480 parm= RNA_def_pointer(func, "scene", "Scene", "", "Scene within which to evaluate modifiers");
481 RNA_def_property_flag(parm, PROP_REQUIRED|PROP_NEVER_NULL);
482 parm= RNA_def_boolean(func, "apply_modifiers", 0, "", "Apply modifiers");
483 RNA_def_property_flag(parm, PROP_REQUIRED);
484 parm= RNA_def_enum(func, "settings", mesh_type_items, 0, "", "Modifier settings to apply");
485 RNA_def_property_flag(parm, PROP_REQUIRED);
486 parm= RNA_def_pointer(func, "mesh", "Mesh", "", "Mesh created from object, remove it if it is only used for export");
487 RNA_def_function_return(func, parm);
490 func= RNA_def_function(srna, "dupli_list_create", "rna_Object_create_duplilist");
491 RNA_def_function_ui_description(func, "Create a list of dupli objects for this object, needs to "
492 "be freed manually with free_dupli_list to restore the "
493 "objects real matrix and layers");
494 parm= RNA_def_pointer(func, "scene", "Scene", "", "Scene within which to evaluate duplis");
495 RNA_def_property_flag(parm, PROP_REQUIRED|PROP_NEVER_NULL);
496 RNA_def_function_flag(func, FUNC_USE_REPORTS);
498 func= RNA_def_function(srna, "dupli_list_clear", "rna_Object_free_duplilist");
499 RNA_def_function_ui_description(func, "Free the list of dupli objects");
502 func= RNA_def_function(srna, "find_armature", "modifiers_isDeformedByArmature");
503 RNA_def_function_ui_description(func, "Find armature influencing this object as a parent or via a modifier");
504 parm= RNA_def_pointer(func, "ob_arm", "Object", "", "Armature object influencing this object or NULL");
505 RNA_def_function_return(func, parm);
508 func= RNA_def_function(srna, "shape_key_add", "rna_Object_shape_key_add");
509 RNA_def_function_ui_description(func, "Add shape key to an object");
510 RNA_def_function_flag(func, FUNC_USE_CONTEXT|FUNC_USE_REPORTS);
511 RNA_def_string(func, "name", "Key", 0, "", "Unique name for the new keylock"); /* optional */
512 RNA_def_boolean(func, "from_mix", 1, "", "Create new shape from existing mix of shapes");
513 parm= RNA_def_pointer(func, "key", "ShapeKey", "", "New shape keyblock");
514 RNA_def_property_flag(parm, PROP_RNAPTR);
515 RNA_def_function_return(func, parm);
518 func= RNA_def_function(srna, "ray_cast", "rna_Object_ray_cast");
519 RNA_def_function_ui_description(func, "Cast a ray onto in object space");
520 RNA_def_function_flag(func, FUNC_USE_REPORTS);
522 /* ray start and end */
523 parm= RNA_def_float_vector(func, "start", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
524 RNA_def_property_flag(parm, PROP_REQUIRED);
525 parm= RNA_def_float_vector(func, "end", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
526 RNA_def_property_flag(parm, PROP_REQUIRED);
528 /* return location and normal */
529 parm= RNA_def_float_vector(func, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location", "The hit location of this ray cast", -1e4, 1e4);
530 RNA_def_property_flag(parm, PROP_THICK_WRAP);
531 RNA_def_function_output(func, parm);
532 parm= RNA_def_float_vector(func, "normal", 3, NULL, -FLT_MAX, FLT_MAX, "Normal", "The face normal at the ray cast hit location", -1e4, 1e4);
533 RNA_def_property_flag(parm, PROP_THICK_WRAP);
534 RNA_def_function_output(func, parm);
536 parm= RNA_def_int(func, "index", 0, 0, 0, "", "The face index, -1 when no intersection is found", 0, 0);
537 RNA_def_function_output(func, parm);
540 func= RNA_def_function(srna, "closest_point_on_mesh", "rna_Object_closest_point_on_mesh");
541 RNA_def_function_ui_description(func, "Find the nearest point on the object");
542 RNA_def_function_flag(func, FUNC_USE_REPORTS);
544 /* location of point for test and max distance */
545 parm= RNA_def_float_vector(func, "point", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
546 RNA_def_property_flag(parm, PROP_REQUIRED);
547 /* default is sqrt(FLT_MAX) */
548 RNA_def_float(func, "max_dist", 1.844674352395373e+19, 0.0, FLT_MAX, "", "", 0.0, FLT_MAX);
550 /* return location and normal */
551 parm= RNA_def_float_vector(func, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location", "The location on the object closest to the point", -1e4, 1e4);
552 RNA_def_property_flag(parm, PROP_THICK_WRAP);
553 RNA_def_function_output(func, parm);
554 parm= RNA_def_float_vector(func, "normal", 3, NULL, -FLT_MAX, FLT_MAX, "Normal", "The face normal at the closest point", -1e4, 1e4);
555 RNA_def_property_flag(parm, PROP_THICK_WRAP);
556 RNA_def_function_output(func, parm);
558 parm= RNA_def_int(func, "index", 0, 0, 0, "", "The face index, -1 when no closest point is found", 0, 0);
559 RNA_def_function_output(func, parm);
562 func= RNA_def_function(srna, "is_visible", "rna_Object_is_visible");
563 RNA_def_function_ui_description(func, "Determine if object is visible in a given scene");
564 parm= RNA_def_pointer(func, "scene", "Scene", "", "");
565 RNA_def_property_flag(parm, PROP_REQUIRED|PROP_NEVER_NULL);
566 parm= RNA_def_boolean(func, "result", 0, "", "Object visibility");
567 RNA_def_function_return(func, parm);
569 /* utility function for checking if the object is modified */
570 func= RNA_def_function(srna, "is_modified", "rna_Object_is_modified");
571 RNA_def_function_ui_description(func, "Determine if this object is modified from the base mesh data");
572 parm= RNA_def_pointer(func, "scene", "Scene", "", "");
573 RNA_def_property_flag(parm, PROP_REQUIRED|PROP_NEVER_NULL);
574 parm= RNA_def_enum(func, "settings", mesh_type_items, 0, "", "Modifier settings to apply");
575 RNA_def_property_flag(parm, PROP_REQUIRED);
576 parm= RNA_def_boolean(func, "result", 0, "", "Object visibility");
577 RNA_def_function_return(func, parm);
581 void RNA_api_object_base(StructRNA *srna)
586 func= RNA_def_function(srna, "layers_from_view", "rna_ObjectBase_layers_from_view");
587 RNA_def_function_ui_description(func, "Sets the object layers from a 3D View (use when adding an object in local view)");
588 parm= RNA_def_pointer(func, "view", "SpaceView3D", "", "");
589 RNA_def_property_flag(parm, PROP_REQUIRED|PROP_NEVER_NULL);