Blender  V2.93
MOD_util.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <string.h>
25 
26 #include "BLI_utildefines.h"
27 
28 #include "BLI_bitmap.h"
29 #include "BLI_math_matrix.h"
30 #include "BLI_math_vector.h"
31 
32 #include "DNA_image_types.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_modifier_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38 
39 #include "BKE_action.h" /* BKE_pose_channel_find_name */
40 #include "BKE_deform.h"
41 #include "BKE_editmesh.h"
42 #include "BKE_image.h"
43 #include "BKE_lattice.h"
44 #include "BKE_lib_id.h"
45 #include "BKE_mesh.h"
46 #include "BKE_mesh_wrapper.h"
47 #include "BKE_object.h"
48 
49 #include "BKE_modifier.h"
50 
51 #include "DEG_depsgraph.h"
52 #include "DEG_depsgraph_query.h"
53 
54 #include "MOD_modifiertypes.h"
55 #include "MOD_util.h"
56 
57 #include "MEM_guardedalloc.h"
58 
59 #include "bmesh.h"
60 
62 {
63  Tex *tex = dmd->texture;
64 
65  if (tex == NULL) {
66  return;
67  }
68 
69  if (tex->ima && BKE_image_is_animated(tex->ima)) {
71  }
72 }
73 
74 /* TODO to be renamed to get_texture_coords once we are done with moving modifiers to Mesh. */
77  const ModifierEvalContext *UNUSED(ctx),
78  Object *ob,
79  Mesh *mesh,
80  float (*cos)[3],
81  float (*r_texco)[3])
82 {
83  const int numVerts = mesh->totvert;
84  int i;
85  int texmapping = dmd->texmapping;
86  float mapref_imat[4][4];
87 
88  if (texmapping == MOD_DISP_MAP_OBJECT) {
89  if (dmd->map_object != NULL) {
90  Object *map_object = dmd->map_object;
91  if (dmd->map_bone[0] != '\0') {
92  bPoseChannel *pchan = BKE_pose_channel_find_name(map_object->pose, dmd->map_bone);
93  if (pchan) {
94  float mat_bone_world[4][4];
95  mul_m4_m4m4(mat_bone_world, map_object->obmat, pchan->pose_mat);
96  invert_m4_m4(mapref_imat, mat_bone_world);
97  }
98  else {
99  invert_m4_m4(mapref_imat, map_object->obmat);
100  }
101  }
102  else {
103  invert_m4_m4(mapref_imat, map_object->obmat);
104  }
105  }
106  else { /* if there is no map object, default to local */
107  texmapping = MOD_DISP_MAP_LOCAL;
108  }
109  }
110 
111  /* UVs need special handling, since they come from faces */
112  if (texmapping == MOD_DISP_MAP_UV) {
114  MPoly *mpoly = mesh->mpoly;
115  MPoly *mp;
116  MLoop *mloop = mesh->mloop;
117  BLI_bitmap *done = BLI_BITMAP_NEW(numVerts, __func__);
118  const int numPolys = mesh->totpoly;
119  char uvname[MAX_CUSTOMDATA_LAYER_NAME];
120  MLoopUV *mloop_uv;
121 
123  mloop_uv = CustomData_get_layer_named(&mesh->ldata, CD_MLOOPUV, uvname);
124 
125  /* verts are given the UV from the first face that uses them */
126  for (i = 0, mp = mpoly; i < numPolys; i++, mp++) {
127  uint fidx = mp->totloop - 1;
128 
129  do {
130  uint lidx = mp->loopstart + fidx;
131  uint vidx = mloop[lidx].v;
132 
133  if (!BLI_BITMAP_TEST(done, vidx)) {
134  /* remap UVs from [0, 1] to [-1, 1] */
135  r_texco[vidx][0] = (mloop_uv[lidx].uv[0] * 2.0f) - 1.0f;
136  r_texco[vidx][1] = (mloop_uv[lidx].uv[1] * 2.0f) - 1.0f;
137  BLI_BITMAP_ENABLE(done, vidx);
138  }
139 
140  } while (fidx--);
141  }
142 
143  MEM_freeN(done);
144  return;
145  }
146 
147  /* if there are no UVs, default to local */
148  texmapping = MOD_DISP_MAP_LOCAL;
149  }
150 
151  MVert *mv = mesh->mvert;
152  for (i = 0; i < numVerts; i++, mv++, r_texco++) {
153  switch (texmapping) {
154  case MOD_DISP_MAP_LOCAL:
155  copy_v3_v3(*r_texco, cos != NULL ? *cos : mv->co);
156  break;
157  case MOD_DISP_MAP_GLOBAL:
158  mul_v3_m4v3(*r_texco, ob->obmat, cos != NULL ? *cos : mv->co);
159  break;
160  case MOD_DISP_MAP_OBJECT:
161  mul_v3_m4v3(*r_texco, ob->obmat, cos != NULL ? *cos : mv->co);
162  mul_m4_v3(mapref_imat, *r_texco);
163  break;
164  }
165  if (cos != NULL) {
166  cos++;
167  }
168  }
169 }
170 
171 void MOD_previous_vcos_store(ModifierData *md, const float (*vert_coords)[3])
172 {
173  while ((md = md->next) && md->type == eModifierType_Armature) {
175  if (amd->multi && amd->vert_coords_prev == NULL) {
176  amd->vert_coords_prev = MEM_dupallocN(vert_coords);
177  }
178  else {
179  break;
180  }
181  }
182  /* lattice/mesh modifier too */
183 }
184 
185 /* returns a mesh if mesh == NULL, for deforming modifiers that need it */
187  struct BMEditMesh *em,
188  Mesh *mesh,
189  const float (*vertexCos)[3],
190  const int num_verts,
191  const bool use_normals,
192  const bool use_orco)
193 {
194  if (mesh != NULL) {
195  /* pass */
196  }
197  else if (ob->type == OB_MESH) {
198  if (em) {
200  }
201  else {
202  /* TODO(sybren): after modifier conversion of DM to Mesh is done, check whether
203  * we really need a copy here. Maybe the CoW ob->data can be directly used. */
204  Mesh *mesh_prior_modifiers = BKE_object_get_pre_modified_mesh(ob);
206  &mesh_prior_modifiers->id,
207  NULL,
210  }
211 
212  if (em != NULL) {
213  /* pass */
214  }
215  /* TODO(sybren): after modifier conversion of DM to Mesh is done, check whether
216  * we really need vertexCos here. */
217  else if (vertexCos) {
218  BKE_mesh_vert_coords_apply(mesh, vertexCos);
220  }
221 
222  if (use_orco) {
225  }
226  }
227  else if (ELEM(ob->type, OB_FONT, OB_CURVE, OB_SURF)) {
228  /* TODO(sybren): get evaluated mesh from depsgraph once
229  * that's properly generated for curves. */
231 
232  /* Currently, that may not be the case every time
233  * (texts e.g. tend to give issues,
234  * also when deforming curve points instead of generated curve geometry... ). */
235  if (mesh != NULL && mesh->totvert != num_verts) {
237  mesh = NULL;
238  }
239  }
240 
241  if (use_normals) {
242  if (LIKELY(mesh)) {
244  }
245  }
246 
248  BLI_assert(mesh->totvert == num_verts);
249  }
250 
251  return mesh;
252 }
253 
255  Object *ob, struct Mesh *mesh, const char *name, MDeformVert **dvert, int *defgrp_index)
256 {
257  *defgrp_index = BKE_object_defgroup_name_index(ob, name);
258  *dvert = NULL;
259 
260  if (*defgrp_index != -1) {
261  if (ob->type == OB_LATTICE) {
262  *dvert = BKE_lattice_deform_verts_get(ob);
263  }
264  else if (mesh) {
265  *dvert = mesh->dvert;
266  }
267  }
268 }
269 
271  Object *object,
272  const char *bonename,
273  const char *description)
274 {
275  if (object == NULL) {
276  return;
277  }
278  if (bonename[0] != '\0' && object->type == OB_ARMATURE) {
279  DEG_add_object_relation(node, object, DEG_OB_COMP_EVAL_POSE, description);
280  }
281  else {
282  DEG_add_object_relation(node, object, DEG_OB_COMP_TRANSFORM, description);
283  }
284 }
285 
286 /* only called by BKE_modifier.h/modifier.c */
288 {
289 #define INIT_TYPE(typeName) (types[eModifierType_##typeName] = &modifierType_##typeName)
290  INIT_TYPE(None);
291  INIT_TYPE(Curve);
293  INIT_TYPE(Subsurf);
294  INIT_TYPE(Build);
295  INIT_TYPE(Array);
296  INIT_TYPE(Mirror);
298  INIT_TYPE(Bevel);
299  INIT_TYPE(Displace);
300  INIT_TYPE(UVProject);
301  INIT_TYPE(Decimate);
302  INIT_TYPE(Smooth);
303  INIT_TYPE(Cast);
304  INIT_TYPE(Wave);
305  INIT_TYPE(Armature);
306  INIT_TYPE(Hook);
307  INIT_TYPE(Softbody);
308  INIT_TYPE(Cloth);
309  INIT_TYPE(Collision);
310  INIT_TYPE(Boolean);
311  INIT_TYPE(MeshDeform);
312  INIT_TYPE(Ocean);
314  INIT_TYPE(ParticleInstance);
315  INIT_TYPE(Explode);
316  INIT_TYPE(Shrinkwrap);
317  INIT_TYPE(Mask);
318  INIT_TYPE(SimpleDeform);
319  INIT_TYPE(Multires);
321  INIT_TYPE(Fluid);
322  INIT_TYPE(ShapeKey);
323  INIT_TYPE(Solidify);
324  INIT_TYPE(Screw);
325  INIT_TYPE(Warp);
326  INIT_TYPE(WeightVGEdit);
327  INIT_TYPE(WeightVGMix);
328  INIT_TYPE(WeightVGProximity);
329  INIT_TYPE(DynamicPaint);
330  INIT_TYPE(Remesh);
331  INIT_TYPE(Skin);
332  INIT_TYPE(LaplacianSmooth);
333  INIT_TYPE(Triangulate);
334  INIT_TYPE(UVWarp);
335  INIT_TYPE(MeshCache);
336  INIT_TYPE(LaplacianDeform);
337  INIT_TYPE(Wireframe);
338  INIT_TYPE(Weld);
339  INIT_TYPE(DataTransfer);
340  INIT_TYPE(NormalEdit);
341  INIT_TYPE(CorrectiveSmooth);
342  INIT_TYPE(MeshSequenceCache);
343  INIT_TYPE(SurfaceDeform);
344  INIT_TYPE(WeightedNormal);
345  INIT_TYPE(MeshToVolume);
346  INIT_TYPE(VolumeDisplace);
347  INIT_TYPE(VolumeToMesh);
348  INIT_TYPE(Nodes);
349 #undef INIT_TYPE
350 }
Blender kernel action and pose functionality.
struct bPoseChannel * BKE_pose_channel_find_name(const struct bPose *pose, const char *name)
@ CD_ASSIGN
bool CustomData_has_layer(const struct CustomData *data, int type)
void * CustomData_get_layer_named(const struct CustomData *data, int type, const char *name)
Definition: customdata.c:3217
void * CustomData_add_layer(struct CustomData *data, int type, eCDAllocType alloctype, void *layer, int totelem)
Definition: customdata.c:2620
void CustomData_validate_layer_name(const struct CustomData *data, int type, const char *name, char *outname)
support for deformation groups and hooks.
int BKE_object_defgroup_name_index(const struct Object *ob, const char *name)
bool BKE_image_is_animated(struct Image *image)
Definition: image.c:5640
void BKE_image_user_frame_calc(struct Image *ima, struct ImageUser *iuser, int cfra)
Definition: image.c:5335
struct MDeformVert * BKE_lattice_deform_verts_get(const struct Object *oblatt)
Definition: lattice.c:599
@ LIB_ID_COPY_CD_REFERENCE
Definition: BKE_lib_id.h:122
@ LIB_ID_COPY_LOCALIZE
Definition: BKE_lib_id.h:145
struct ID * BKE_id_copy_ex(struct Main *bmain, const struct ID *id, struct ID **r_newid, const int flag)
void BKE_id_free(struct Main *bmain, void *idv)
float(* BKE_mesh_orco_verts_get(struct Object *ob))[3]
Definition: mesh.c:1162
void BKE_mesh_ensure_normals(struct Mesh *me)
void BKE_mesh_vert_coords_apply(struct Mesh *mesh, const float(*vert_coords)[3])
Definition: mesh.c:1755
struct Mesh * BKE_mesh_new_nomain_from_curve(struct Object *ob)
Definition: mesh_convert.c:572
struct Mesh * BKE_mesh_wrapper_from_editmesh_with_coords(struct BMEditMesh *em, const struct CustomData_MeshMasks *cd_mask_extra, const float(*vert_coords)[3], const struct Mesh *me_settings)
General operations, lookup, etc. for blender objects.
struct Mesh * BKE_object_get_pre_modified_mesh(struct Object *object)
Definition: object.c:4472
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition: BLI_bitmap.h:63
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:78
#define BLI_BITMAP_NEW(_tot, _alloc_string)
Definition: BLI_bitmap.h:50
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:32
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:732
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define ELEM(...)
#define LIKELY(x)
void DEG_add_object_relation(struct DepsNodeHandle *node_handle, struct Object *object, eDepsObjectComponentType component, const char *description)
@ DEG_OB_COMP_EVAL_POSE
@ DEG_OB_COMP_TRANSFORM
float DEG_get_ctime(const Depsgraph *graph)
#define MAX_CUSTOMDATA_LAYER_NAME
#define CD_MASK_NORMAL
@ CD_MLOOPUV
@ ME_WRAPPER_TYPE_MDATA
@ MOD_DISP_MAP_OBJECT
@ MOD_DISP_MAP_GLOBAL
@ MOD_DISP_MAP_LOCAL
@ MOD_DISP_MAP_UV
@ eModifierType_Armature
Object is a sort of wrapper for general info.
@ OB_LATTICE
@ OB_SURF
@ OB_FONT
@ OB_ARMATURE
@ OB_MESH
@ OB_CURVE
Read Guarded memory(de)allocation.
Mesh * MOD_deform_mesh_eval_get(Object *ob, struct BMEditMesh *em, Mesh *mesh, const float(*vertexCos)[3], const int num_verts, const bool use_normals, const bool use_orco)
Definition: MOD_util.c:186
void MOD_init_texture(MappingInfoModifierData *dmd, const ModifierEvalContext *ctx)
Definition: MOD_util.c:61
#define INIT_TYPE(typeName)
void MOD_depsgraph_update_object_bone_relation(struct DepsNodeHandle *node, Object *object, const char *bonename, const char *description)
Definition: MOD_util.c:270
void MOD_previous_vcos_store(ModifierData *md, const float(*vert_coords)[3])
Definition: MOD_util.c:171
void MOD_get_texture_coords(MappingInfoModifierData *dmd, const ModifierEvalContext *UNUSED(ctx), Object *ob, Mesh *mesh, float(*cos)[3], float(*r_texco)[3])
Definition: MOD_util.c:76
void MOD_get_vgroup(Object *ob, struct Mesh *mesh, const char *name, MDeformVert **dvert, int *defgrp_index)
Definition: MOD_util.c:254
void modifier_type_init(ModifierTypeInfo *types[])
Definition: MOD_util.c:287
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White RGB Map Separate Set Z Dilate Combine Combine Color Channel Split ID Combine Luminance Directional Alpha Distance Hue Movie Ellipse Bokeh View Corner Anti Mix RGB Hue Separate TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC Boolean Random Edge Subdivision Surface
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Bevel
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White RGB Map Separate Set Z Dilate Combine Combine Color Channel Split ID Combine Luminance Directional Alpha Distance Hue Movie Ellipse Bokeh View Corner Anti Mix RGB Hue Separate TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC Boolean Random EdgeSplit
OperationNode * node
static char ** types
Definition: makesdna.c:164
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
unsigned int v
int64_t cd_dirty_vert
struct CustomData pdata ldata
struct MVert * mvert
struct MDeformVert * dvert
int totvert
struct MLoop * mloop
Mesh_Runtime runtime
int totpoly
struct MPoly * mpoly
struct ModifierData * next
struct Depsgraph * depsgraph
Definition: BKE_modifier.h:153
struct bPose * pose
float obmat[4][4]
void * data
struct ImageUser iuser
struct Image * ima
float pose_mat[4][4]