Blender V4.3
object_random.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "MEM_guardedalloc.h"
10
11#include "DNA_layer_types.h"
12#include "DNA_object_types.h"
13
14#include "BLI_math_vector.h"
15#include "BLI_rand.h"
16
17#include "BKE_context.hh"
18#include "BKE_layer.hh"
19
20#include "RNA_access.hh"
21#include "RNA_define.hh"
22
23#include "WM_api.hh"
24#include "WM_types.hh"
25
26#include "ED_object.hh"
27#include "ED_transverts.hh"
28
29#include "object_intern.hh"
30
31namespace blender::ed::object {
32
38 const float offset,
39 const float uniform,
40 const float normal_factor,
41 const uint seed)
42{
43 bool use_normal = (normal_factor != 0.0f);
44 RNG *rng;
45 TransVert *tv;
46 int a;
47
48 if (!tvs || !(tvs->transverts)) {
49 return false;
50 }
51
52 rng = BLI_rng_new(seed);
53
54 tv = tvs->transverts;
55 for (a = 0; a < tvs->transverts_tot; a++, tv++) {
56 const float t = max_ff(0.0f, uniform + ((1.0f - uniform) * BLI_rng_get_float(rng)));
57 float vec[3];
59
60 if (use_normal && (tv->flag & TX_VERT_USE_NORMAL)) {
61 float no[3];
62
63 /* avoid >90d rotation to align with normal */
64 if (dot_v3v3(vec, tv->normal) < 0.0f) {
65 negate_v3_v3(no, tv->normal);
66 }
67 else {
68 copy_v3_v3(no, tv->normal);
69 }
70
71 interp_v3_v3v3_slerp_safe(vec, vec, no, normal_factor);
72 }
73
74 madd_v3_v3fl(tv->loc, vec, offset * t);
75 }
76
77 BLI_rng_free(rng);
78
79 return true;
80}
81
83{
84 const Scene *scene = CTX_data_scene(C);
85 ViewLayer *view_layer = CTX_data_view_layer(C);
86 Object *ob_active = CTX_data_edit_object(C);
87 const int ob_mode = ob_active->mode;
88
89 const float offset = RNA_float_get(op->ptr, "offset");
90 const float uniform = RNA_float_get(op->ptr, "uniform");
91 const float normal_factor = RNA_float_get(op->ptr, "normal");
92 const uint seed = RNA_int_get(op->ptr, "seed");
93
94 bool changed_multi = false;
96 scene, view_layer, CTX_wm_view3d(C), eObjectMode(ob_mode));
97 for (const int ob_index : objects.index_range()) {
98 Object *ob_iter = objects[ob_index];
99
100 TransVertStore tvs = {nullptr};
101
102 if (ob_iter) {
103 int mode = TM_ALL_JOINTS;
104
105 if (normal_factor != 0.0f) {
106 mode |= TX_VERT_USE_NORMAL;
107 }
108
109 if (shape_key_report_if_locked(ob_iter, op->reports)) {
110 continue;
111 }
112
113 ED_transverts_create_from_obedit(&tvs, ob_iter, mode);
114 if (tvs.transverts_tot == 0) {
115 continue;
116 }
117
118 int seed_iter = seed;
119 /* This gives a consistent result regardless of object order. */
120 if (ob_index) {
121 seed_iter += BLI_ghashutil_strhash_p(ob_iter->id.name);
122 }
123
124 object_rand_transverts(&tvs, offset, uniform, normal_factor, seed_iter);
125
126 ED_transverts_update_obedit(&tvs, ob_iter);
127 ED_transverts_free(&tvs);
128
130 changed_multi = true;
131 }
132 }
133
134 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
135}
136
138{
139 /* identifiers */
140 ot->name = "Randomize";
141 ot->description = "Randomize vertices";
142 ot->idname = "TRANSFORM_OT_vertex_random";
143
144 /* api callbacks */
147
148 /* flags */
150
151 /* props */
153 ot->srna, "offset", 0.0f, -FLT_MAX, FLT_MAX, "Amount", "Distance to offset", -10.0f, 10.0f);
155 "uniform",
156 0.0f,
157 0.0f,
158 1.0f,
159 "Uniform",
160 "Increase for uniform offset distance",
161 0.0f,
162 1.0f);
164 "normal",
165 0.0f,
166 0.0f,
167 1.0f,
168 "Normal",
169 "Align offset direction to normals",
170 0.0f,
171 1.0f);
173 ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
174
175 /* Set generic modal callbacks. */
177}
178
179} // namespace blender::ed::object
Scene * CTX_data_scene(const bContext *C)
Object * CTX_data_edit_object(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
blender::Vector< Object * > BKE_view_layer_array_from_objects_in_mode_unique_data(const Scene *scene, ViewLayer *view_layer, const View3D *v3d, eObjectMode mode)
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
MINLINE float max_ff(float a, float b)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
void interp_v3_v3v3_slerp_safe(float target[3], const float a[3], const float b[3], float t)
Definition math_vector.c:99
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
Random number functions.
struct RNG * BLI_rng_new(unsigned int seed)
Definition rand.cc:39
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition rand.cc:58
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:93
void void BLI_rng_get_float_unit_v3(struct RNG *rng, float v[3]) ATTR_NONNULL(1
unsigned int uint
eObjectMode
Object is a sort of wrapper for general info.
bool ED_transverts_poll(bContext *C)
void ED_transverts_create_from_obedit(TransVertStore *tvs, const Object *obedit, int mode)
void ED_transverts_free(TransVertStore *tvs)
void ED_transverts_update_obedit(TransVertStore *tvs, Object *obedit)
@ TX_VERT_USE_NORMAL
@ TM_ALL_JOINTS
Read Guarded memory(de)allocation.
@ OPTYPE_UNDO
Definition WM_types.hh:162
@ OPTYPE_REGISTER
Definition WM_types.hh:160
#define ND_DRAW
Definition WM_types.hh:428
#define NC_OBJECT
Definition WM_types.hh:346
static unsigned long seed
Definition btSoftBody.h:39
bool shape_key_report_if_locked(const Object *obedit, ReportList *reports)
static int object_rand_verts_exec(bContext *C, wmOperator *op)
static bool object_rand_transverts(TransVertStore *tvs, const float offset, const float uniform, const float normal_factor, const uint seed)
void TRANSFORM_OT_vertex_random(wmOperatorType *ot)
int RNA_int_get(PointerRNA *ptr, const char *name)
float RNA_float_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
PropertyRNA * RNA_def_float_distance(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
#define FLT_MAX
Definition stdcycles.h:14
char name[66]
Definition DNA_ID.h:425
Definition rand.cc:33
TransVert * transverts
float * loc
float normal[3]
const char * name
Definition WM_types.hh:990
bool(* poll)(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1042
const char * idname
Definition WM_types.hh:992
int(* exec)(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1006
const char * description
Definition WM_types.hh:996
PropertyRNA * prop
Definition WM_types.hh:1092
StructRNA * srna
Definition WM_types.hh:1080
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4125
void WM_operator_type_modal_from_exec_for_object_edit_coords(wmOperatorType *ot)