Blender  V3.3
MOD_util.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
8 #include <string.h>
9 
10 #include "BLI_utildefines.h"
11 
12 #include "BLI_bitmap.h"
13 #include "BLI_math_matrix.h"
14 #include "BLI_math_vector.h"
15 
16 #include "DNA_image_types.h"
17 #include "DNA_mesh_types.h"
18 #include "DNA_meshdata_types.h"
19 #include "DNA_modifier_types.h"
20 #include "DNA_object_types.h"
21 #include "DNA_scene_types.h"
22 
23 #include "BKE_action.h" /* BKE_pose_channel_find_name */
24 #include "BKE_deform.h"
25 #include "BKE_editmesh.h"
26 #include "BKE_image.h"
27 #include "BKE_lattice.h"
28 #include "BKE_lib_id.h"
29 #include "BKE_mesh.h"
30 #include "BKE_mesh_wrapper.h"
31 #include "BKE_object.h"
32 
33 #include "BKE_modifier.h"
34 
35 #include "DEG_depsgraph.h"
36 #include "DEG_depsgraph_query.h"
37 
38 #include "MOD_modifiertypes.h"
39 #include "MOD_util.h"
40 
41 #include "MEM_guardedalloc.h"
42 
43 #include "bmesh.h"
44 
46 {
47  Tex *tex = dmd->texture;
48 
49  if (tex == NULL) {
50  return;
51  }
52 
53  if (tex->ima && BKE_image_is_animated(tex->ima)) {
55  }
56 }
57 
58 /* TODO: to be renamed to get_texture_coords once we are done with moving modifiers to Mesh. */
60  const ModifierEvalContext *UNUSED(ctx),
61  Object *ob,
62  Mesh *mesh,
63  float (*cos)[3],
64  float (*r_texco)[3])
65 {
66  const int verts_num = mesh->totvert;
67  int i;
68  int texmapping = dmd->texmapping;
69  float mapref_imat[4][4];
70 
71  if (texmapping == MOD_DISP_MAP_OBJECT) {
72  if (dmd->map_object != NULL) {
73  Object *map_object = dmd->map_object;
74  if (dmd->map_bone[0] != '\0') {
75  bPoseChannel *pchan = BKE_pose_channel_find_name(map_object->pose, dmd->map_bone);
76  if (pchan) {
77  float mat_bone_world[4][4];
78  mul_m4_m4m4(mat_bone_world, map_object->obmat, pchan->pose_mat);
79  invert_m4_m4(mapref_imat, mat_bone_world);
80  }
81  else {
82  invert_m4_m4(mapref_imat, map_object->obmat);
83  }
84  }
85  else {
86  invert_m4_m4(mapref_imat, map_object->obmat);
87  }
88  }
89  else { /* if there is no map object, default to local */
90  texmapping = MOD_DISP_MAP_LOCAL;
91  }
92  }
93 
94  /* UVs need special handling, since they come from faces */
95  if (texmapping == MOD_DISP_MAP_UV) {
97  MPoly *mpoly = mesh->mpoly;
98  MPoly *mp;
99  MLoop *mloop = mesh->mloop;
100  BLI_bitmap *done = BLI_BITMAP_NEW(verts_num, __func__);
101  const int polys_num = mesh->totpoly;
102  char uvname[MAX_CUSTOMDATA_LAYER_NAME];
103 
105  const MLoopUV *mloop_uv = CustomData_get_layer_named(&mesh->ldata, CD_MLOOPUV, uvname);
106 
107  /* verts are given the UV from the first face that uses them */
108  for (i = 0, mp = mpoly; i < polys_num; i++, mp++) {
109  uint fidx = mp->totloop - 1;
110 
111  do {
112  uint lidx = mp->loopstart + fidx;
113  uint vidx = mloop[lidx].v;
114 
115  if (!BLI_BITMAP_TEST(done, vidx)) {
116  /* remap UVs from [0, 1] to [-1, 1] */
117  r_texco[vidx][0] = (mloop_uv[lidx].uv[0] * 2.0f) - 1.0f;
118  r_texco[vidx][1] = (mloop_uv[lidx].uv[1] * 2.0f) - 1.0f;
119  BLI_BITMAP_ENABLE(done, vidx);
120  }
121 
122  } while (fidx--);
123  }
124 
125  MEM_freeN(done);
126  return;
127  }
128 
129  /* if there are no UVs, default to local */
130  texmapping = MOD_DISP_MAP_LOCAL;
131  }
132 
133  MVert *mv = mesh->mvert;
134  for (i = 0; i < verts_num; i++, mv++, r_texco++) {
135  switch (texmapping) {
136  case MOD_DISP_MAP_LOCAL:
137  copy_v3_v3(*r_texco, cos != NULL ? *cos : mv->co);
138  break;
139  case MOD_DISP_MAP_GLOBAL:
140  mul_v3_m4v3(*r_texco, ob->obmat, cos != NULL ? *cos : mv->co);
141  break;
142  case MOD_DISP_MAP_OBJECT:
143  mul_v3_m4v3(*r_texco, ob->obmat, cos != NULL ? *cos : mv->co);
144  mul_m4_v3(mapref_imat, *r_texco);
145  break;
146  }
147  if (cos != NULL) {
148  cos++;
149  }
150  }
151 }
152 
153 void MOD_previous_vcos_store(ModifierData *md, const float (*vert_coords)[3])
154 {
155  while ((md = md->next) && md->type == eModifierType_Armature) {
157  if (amd->multi && amd->vert_coords_prev == NULL) {
158  amd->vert_coords_prev = MEM_dupallocN(vert_coords);
159  }
160  else {
161  break;
162  }
163  }
164  /* lattice/mesh modifier too */
165 }
166 
168  struct BMEditMesh *em,
169  Mesh *mesh,
170  const float (*vertexCos)[3],
171  const int verts_num,
172  const bool use_normals,
173  const bool use_orco)
174 {
175  if (mesh != NULL) {
176  /* pass */
177  }
178  else if (ob->type == OB_MESH) {
179  if (em) {
181  }
182  else {
183  /* TODO(sybren): after modifier conversion of DM to Mesh is done, check whether
184  * we really need a copy here. Maybe the CoW ob->data can be directly used. */
185  Mesh *mesh_prior_modifiers = BKE_object_get_pre_modified_mesh(ob);
187  &mesh_prior_modifiers->id,
188  NULL,
191  }
192 
193  if (em != NULL) {
194  /* pass */
195  }
196  /* TODO(sybren): after modifier conversion of DM to Mesh is done, check whether
197  * we really need vertexCos here. */
198  else if (vertexCos) {
199  BKE_mesh_vert_coords_apply(mesh, vertexCos);
200  }
201 
202  if (use_orco) {
204  }
205  }
206  else if (ELEM(ob->type, OB_FONT, OB_CURVES_LEGACY, OB_SURF)) {
207  /* TODO(sybren): get evaluated mesh from depsgraph once
208  * that's properly generated for curves. */
210 
211  /* Currently, that may not be the case every time
212  * (texts e.g. tend to give issues,
213  * also when deforming curve points instead of generated curve geometry... ). */
214  if (mesh != NULL && mesh->totvert != verts_num) {
216  mesh = NULL;
217  }
218  }
219 
220  /* TODO: Remove this "use_normals" argument, since the caller should retrieve normals afterwards
221  * if necessary. */
222  if (use_normals) {
223  if (LIKELY(mesh)) {
225  }
226  }
227 
229  BLI_assert(mesh->totvert == verts_num);
230  }
231 
232  return mesh;
233 }
234 
236  Object *ob, struct Mesh *mesh, const char *name, MDeformVert **dvert, int *defgrp_index)
237 {
238  if (mesh) {
239  *defgrp_index = BKE_id_defgroup_name_index(&mesh->id, name);
240  if (*defgrp_index != -1) {
241  *dvert = mesh->dvert;
242  }
243  else {
244  *dvert = NULL;
245  }
246  }
247  else {
248  *defgrp_index = BKE_object_defgroup_name_index(ob, name);
249  if (*defgrp_index != -1 && ob->type == OB_LATTICE) {
250  *dvert = BKE_lattice_deform_verts_get(ob);
251  }
252  else {
253  *dvert = NULL;
254  }
255  }
256 }
257 
259  Object *object,
260  const char *bonename,
261  const char *description)
262 {
263  if (object == NULL) {
264  return;
265  }
266  if (bonename[0] != '\0' && object->type == OB_ARMATURE) {
267  DEG_add_object_relation(node, object, DEG_OB_COMP_EVAL_POSE, description);
268  }
269  else {
270  DEG_add_object_relation(node, object, DEG_OB_COMP_TRANSFORM, description);
271  }
272 }
273 
275 {
276 #define INIT_TYPE(typeName) (types[eModifierType_##typeName] = &modifierType_##typeName)
277  INIT_TYPE(None);
278  INIT_TYPE(Curve);
280  INIT_TYPE(Subsurf);
281  INIT_TYPE(Build);
282  INIT_TYPE(Array);
283  INIT_TYPE(Mirror);
284  INIT_TYPE(EdgeSplit);
285  INIT_TYPE(Bevel);
286  INIT_TYPE(Displace);
287  INIT_TYPE(UVProject);
288  INIT_TYPE(Decimate);
289  INIT_TYPE(Smooth);
290  INIT_TYPE(Cast);
291  INIT_TYPE(Wave);
292  INIT_TYPE(Armature);
293  INIT_TYPE(Hook);
294  INIT_TYPE(Softbody);
295  INIT_TYPE(Cloth);
296  INIT_TYPE(Collision);
297  INIT_TYPE(Boolean);
298  INIT_TYPE(MeshDeform);
299  INIT_TYPE(Ocean);
301  INIT_TYPE(ParticleInstance);
302  INIT_TYPE(Explode);
303  INIT_TYPE(Shrinkwrap);
304  INIT_TYPE(Mask);
305  INIT_TYPE(SimpleDeform);
306  INIT_TYPE(Multires);
307  INIT_TYPE(Surface);
308  INIT_TYPE(Fluid);
309  INIT_TYPE(ShapeKey);
310  INIT_TYPE(Solidify);
311  INIT_TYPE(Screw);
312  INIT_TYPE(Warp);
313  INIT_TYPE(WeightVGEdit);
314  INIT_TYPE(WeightVGMix);
315  INIT_TYPE(WeightVGProximity);
316  INIT_TYPE(DynamicPaint);
317  INIT_TYPE(Remesh);
318  INIT_TYPE(Skin);
319  INIT_TYPE(LaplacianSmooth);
320  INIT_TYPE(Triangulate);
321  INIT_TYPE(UVWarp);
322  INIT_TYPE(MeshCache);
323  INIT_TYPE(LaplacianDeform);
325  INIT_TYPE(Weld);
326  INIT_TYPE(DataTransfer);
327  INIT_TYPE(NormalEdit);
328  INIT_TYPE(CorrectiveSmooth);
329  INIT_TYPE(MeshSequenceCache);
330  INIT_TYPE(SurfaceDeform);
331  INIT_TYPE(WeightedNormal);
332  INIT_TYPE(MeshToVolume);
333  INIT_TYPE(VolumeDisplace);
334  INIT_TYPE(VolumeToMesh);
335  INIT_TYPE(Nodes);
336 #undef INIT_TYPE
337 }
Blender kernel action and pose functionality.
struct bPoseChannel * BKE_pose_channel_find_name(const struct bPose *pose, const char *name)
bool CustomData_has_layer(const struct CustomData *data, int type)
void * CustomData_get_layer_named(const struct CustomData *data, int type, const char *name)
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)
int BKE_id_defgroup_name_index(const struct ID *id, const char *name)
bool BKE_image_is_animated(struct Image *image)
void BKE_image_user_frame_calc(struct Image *ima, struct ImageUser *iuser, int cfra)
struct MDeformVert * BKE_lattice_deform_verts_get(const struct Object *oblatt)
Definition: lattice.c:590
@ LIB_ID_COPY_CD_REFERENCE
Definition: BKE_lib_id.h:156
@ LIB_ID_COPY_LOCALIZE
Definition: BKE_lib_id.h:187
struct ID * BKE_id_copy_ex(struct Main *bmain, const struct ID *id, struct ID **r_newid, int flag)
void BKE_id_free(struct Main *bmain, void *idv)
void BKE_mesh_orco_ensure(struct Object *ob, struct Mesh *mesh)
Definition: mesh.cc:1353
void BKE_mesh_vert_coords_apply(struct Mesh *mesh, const float(*vert_coords)[3])
Definition: mesh.cc:1834
const float(* BKE_mesh_vertex_normals_ensure(const struct Mesh *mesh))[3]
struct Mesh * BKE_mesh_new_nomain_from_curve(const struct Object *ob)
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(const struct Object *object)
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition: BLI_bitmap.h:40
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition: BLI_bitmap.h:64
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:81
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:259
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1287
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:729
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:739
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition: BLI_sys_types.h:67
#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
@ CD_MLOOPUV
@ ME_WRAPPER_TYPE_MDATA
@ eModifierType_Armature
@ MOD_DISP_MAP_OBJECT
@ MOD_DISP_MAP_GLOBAL
@ MOD_DISP_MAP_LOCAL
@ MOD_DISP_MAP_UV
Object is a sort of wrapper for general info.
@ OB_LATTICE
@ OB_SURF
@ OB_FONT
@ OB_ARMATURE
@ OB_MESH
@ OB_CURVES_LEGACY
Read Guarded memory(de)allocation.
Mesh * MOD_deform_mesh_eval_get(Object *ob, struct BMEditMesh *em, Mesh *mesh, const float(*vertexCos)[3], const int verts_num, const bool use_normals, const bool use_orco)
Definition: MOD_util.c:167
void MOD_init_texture(MappingInfoModifierData *dmd, const ModifierEvalContext *ctx)
Definition: MOD_util.c:45
#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:258
void MOD_previous_vcos_store(ModifierData *md, const float(*vert_coords)[3])
Definition: MOD_util.c:153
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:59
void MOD_get_vgroup(Object *ob, struct Mesh *mesh, const char *name, MDeformVert **dvert, int *defgrp_index)
Definition: MOD_util.c:235
void modifier_type_init(ModifierTypeInfo *types[])
Definition: MOD_util.c:274
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between and object coordinate space Combine Create a color from its and value channels Color Retrieve a color or the default fallback if none is specified Separate Split a vector into its and Z components Bevel
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Wireframe
OperationNode * node
static char ** types
Definition: makesdna.c:67
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:28
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
unsigned int v
struct MVert * mvert
struct MDeformVert * dvert
int totvert
struct MLoop * mloop
Mesh_Runtime runtime
int totpoly
struct MPoly * mpoly
CustomData ldata
struct ModifierData * next
struct Depsgraph * depsgraph
Definition: BKE_modifier.h:140
struct bPose * pose
float obmat[4][4]
void * data
struct ImageUser iuser
struct Image * ima
float pose_mat[4][4]