49# include "potracelib.h"
76 bool *stop, *do_update;
93 bool use_current_frame;
98 TurnPolicy turnpolicy;
107 void ensure_output_object();
110void TraceJob::ensure_output_object()
115 if (this->ob_grease_pencil ==
nullptr) {
116 const ushort local_view_bits = (this->v3d && this->v3d->localvd) ? this->v3d->local_view_uid :
123 this->ob_active->loc,
124 this->ob_active->rot,
127 copy_v3_v3(this->ob_grease_pencil->scale, this->ob_active->scale);
128 this->was_ob_created =
true;
134 if (this->
layer ==
nullptr) {
137 this->
layer = &new_layer;
141static float4x4 pixel_to_object_transform(
const Object &image_object,
145 const float3 pixel_center_3d =
float3(pixel_center.x, pixel_center.y, 0);
147 const float3 image_offset_3d =
float3(image_object.ima_ofs[0], image_object.ima_ofs[1], 0);
148 const float max_image_scale = image_object.empty_drawsize;
149 const float3 image_aspect_3d = (ibuf.
x > ibuf.
y ?
float3(1,
float(ibuf.
y) /
float(ibuf.
x), 1) :
156 return to_normalized;
159static int ensure_material(Main *bmain, Object *ob,
const StringRefNull name)
170static bke::CurvesGeometry grease_pencil_trace_image(TraceJob &trace_job,
const ImBuf &ibuf)
179 image_trace::TraceParams params;
180 params.size_threshold = 0;
181 params.turn_policy = trace_job.turnpolicy;
186 const float4x4 transform = pixel_to_object_transform(*trace_job.ob_active, ibuf);
193 "ensure_material must be called on the main thread");
196 material_indices.
span.fill(trace_job.material_index);
197 material_indices.
finish();
209static void trace_start_job(
void *customdata, wmJobWorkerStatus *worker_status)
211 TraceJob &trace_job = *
static_cast<TraceJob *
>(customdata);
213 trace_job.stop = &worker_status->
stop;
214 trace_job.do_update = &worker_status->
do_update;
215 trace_job.progress = &worker_status->
progress;
216 trace_job.was_canceled =
false;
217 const int init_frame = std::max((trace_job.use_current_frame) ? trace_job.frame_target : 0, 0);
223 ImageUser &iuser = *trace_job.ob_active->iuser;
224 trace_job.traced_curves.reinitialize(1);
226 iuser.
framenr = ((trace_job.frame_number == 0) || (trace_job.frame_number > iuser.
frames)) ?
228 trace_job.frame_number;
232 trace_job.traced_curves.first() = grease_pencil_trace_image(trace_job, *ibuf);
234 *(trace_job.progress) = 1.0f;
242 ImageUser &iuser = *trace_job.ob_active->iuser;
243 const int num_frames = iuser.
frames - init_frame + 1;
244 trace_job.traced_curves.reinitialize(num_frames);
247 trace_job.was_canceled =
true;
251 const int frame_number = init_frame + i;
252 *(trace_job.progress) =
float(frame_number) /
float(iuser.
frames);
260 trace_job.traced_curves[i] = grease_pencil_trace_image(trace_job, *ibuf);
266 trace_job.success = !trace_job.was_canceled;
268 worker_status->
stop =
false;
271static void trace_end_job(
void *customdata)
273 TraceJob &trace_job = *
static_cast<TraceJob *
>(customdata);
274 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(trace_job.ob_grease_pencil->data);
276 auto ensure_drawing_at_frame = [&](
const int frame_number) {
277 const std::optional<int> start_frame = trace_job.layer->start_frame_at(frame_number);
278 if (start_frame && *start_frame == frame_number) {
279 return grease_pencil.get_editable_drawing_at(*trace_job.layer, frame_number);
281 return grease_pencil.insert_frame(*trace_job.layer, frame_number);
286 switch (trace_job.mode) {
288 BLI_assert(trace_job.traced_curves.size() == 1);
289 bke::greasepencil::Drawing *drawing = ensure_drawing_at_frame(trace_job.frame_target);
296 const ImageUser &iuser = *trace_job.ob_active->iuser;
297 const int init_frame = std::max((trace_job.use_current_frame) ? trace_job.frame_target : 0,
299 const int num_frames = iuser.
frames - init_frame + 1;
300 BLI_assert(trace_job.traced_curves.size() == num_frames);
302 const int frame_number = init_frame + i;
303 bke::greasepencil::Drawing *drawing = ensure_drawing_at_frame(frame_number);
313 if ((trace_job.was_canceled) && (trace_job.was_ob_created) && (trace_job.ob_grease_pencil)) {
314 BKE_id_delete(trace_job.bmain, &trace_job.ob_grease_pencil->id);
318 if (trace_job.success) {
329static void trace_free_job(
void *customdata)
331 TraceJob *tj =
static_cast<TraceJob *
>(customdata);
336static bool grease_pencil_trace_image_poll(bContext *
C)
339 if ((ob ==
nullptr) || (ob->
type !=
OB_EMPTY) || (ob->data ==
nullptr)) {
353static wmOperatorStatus grease_pencil_trace_image_exec(bContext *
C, wmOperator *op)
355 TraceJob *job = MEM_new<TraceJob>(
"TraceJob");
364 job->ob_active = job->base_active->object;
365 job->image = id_cast<Image *>(job->ob_active->data);
366 job->frame_target = scene->r.cfra;
376 if (job->ob_grease_pencil !=
nullptr) {
379 job->ob_grease_pencil =
nullptr;
383 job->ob_grease_pencil =
nullptr;
387 job->was_ob_created =
false;
394 job->ensure_output_object();
400 job->material_index = ensure_material(job->bmain, job->ob_grease_pencil,
"Material");
402 if ((job->image->source ==
IMA_SRC_FILE) || (job->frame_number > 0)) {
403 wmJobWorkerStatus worker_status = {};
404 trace_start_job(job, &worker_status);
435static void GREASE_PENCIL_OT_trace_image(wmOperatorType *ot)
439 static const EnumPropertyItem turnpolicy_type[] = {
440 {int(TurnPolicy::Foreground),
444 "Prefers to connect foreground components"},
445 {int(TurnPolicy::Background),
449 "Prefers to connect background components"},
450 {int(TurnPolicy::Left),
"LEFT", 0,
"Left",
"Always take a left turn"},
451 {int(TurnPolicy::Right),
"RIGHT", 0,
"Right",
"Always take a right turn"},
452 {int(TurnPolicy::Minority),
456 "Prefers to connect the color that occurs least frequently in the local "
457 "neighborhood of the current position"},
458 {int(TurnPolicy::Majority),
462 "Prefers to connect the color that occurs most frequently in the local "
463 "neighborhood of the current position"},
464 {int(TurnPolicy::Random),
"RANDOM", 0,
"Random",
"Choose pseudo-randomly"},
465 {0,
nullptr, 0,
nullptr,
nullptr},
468 static const EnumPropertyItem trace_modes[] = {
469 {int(
TraceMode::Single),
"SINGLE", 0,
"Single",
"Trace the current frame of the image"},
471 {0,
nullptr, 0,
nullptr,
nullptr},
474 static const EnumPropertyItem target_object_modes[] = {
477 {0,
nullptr, 0,
nullptr,
nullptr},
481 ot->
name =
"Trace Image to Grease Pencil";
482 ot->
idname =
"GREASE_PENCIL_OT_trace_image";
483 ot->
description =
"Extract Grease Pencil strokes from image";
486 ot->
invoke = grease_pencil_trace_image_invoke;
487 ot->
exec = grease_pencil_trace_image_exec;
488 ot->
poll = grease_pencil_trace_image_poll;
499 "Target Grease Pencil");
508 "Determine the lightness threshold above which strokes are generated",
514 int(TurnPolicy::Minority),
516 "Determines how to resolve ambiguities during decomposition of bitmaps into paths");
522 "Determines if trace simple image or full sequence");
526 "Start At Current Frame",
527 "Trace Image starting in current image frame");
535 "Used to trace only one frame of the image sequence, set to zero to trace all",
Low-level operations for curves.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
constexpr const char * c_str() const
MutableAttributeAccessor attributes_for_write()
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
bke::CurvesGeometry & strokes_for_write()
void tag_topology_changed()
#define image(slot, qualifiers, format)
void free_bitmap(Bitmap *bm)
bke::CurvesGeometry trace_to_curves(const Trace &trace, const float4x4 &transform)
Bitmap * image_to_bitmap(const ImBuf &ibuf, ThresholdFn fn)
void free_trace(Trace *trace)
Trace * trace_bitmap(const TraceParams ¶ms, Bitmap &bm)
Object * add_type(bContext *C, int type, const char *name, const float loc[3], const float rot[3], bool enter_editmode, unsigned short local_view_bits) ATTR_NONNULL(1) ATTR_RETURNS_NONNULL
void base_activate(bContext *C, Base *base)
MatBase< T, NumCol, NumRow > scale(const MatBase< T, NumCol, NumRow > &mat, const VectorT &scale)
T average(const VecBase< T, Size > &a)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
MatBase< T, NumCol, NumRow > translate(const MatBase< T, NumCol, NumRow > &mat, const VectorT &translation)
void WM_main_add_notifier(uint type, void *reference)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
int RNA_enum_get(PointerRNA *ptr, const char *name)
void BKE_id_delete(Main *bmain, void *idv, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
MINLINE void copy_v3_v3(float r[3], const float a[3])
Object * BKE_view_layer_non_active_selected_object(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
@ WM_JOB_TYPE_TRACE_IMAGE
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
int RNA_int_get(PointerRNA *ptr, const char *name)
wmWindow * CTX_wm_window(const bContext *C)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
MatBase< float, 4, 4 > float4x4
Base * CTX_data_active_base(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
VecBase< float, 4 > float4
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)
Material * BKE_grease_pencil_object_material_new(Main *bmain, Object *ob, const char *name, int *r_index)
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
VecBase< float, 2 > float2
int BKE_grease_pencil_object_material_index_get_by_name(Object *ob, const char *name)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
wmWindowManager * CTX_wm_manager(const bContext *C)
wmOperatorStatus WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message, const bool show_icon)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
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)
Main * CTX_data_main(const bContext *C)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
MINLINE void copy_v4_v4(float r[4], const float a[4])
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
Object * CTX_data_active_object(const bContext *C)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
float RNA_float_get(PointerRNA *ptr, const char *name)
View3D * CTX_wm_view3d(const bContext *C)
bool BKE_object_obdata_is_libdata(const Object *ob)
void ED_operatortypes_grease_pencil_trace()
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
void trace(Resources &srt, const LightprobeRenderData &lightprobes, const Uniform &uni, const draw::View &views, const Sampling &sampling, const gbuffer::Reader &reader, const uint3 group_id, const uint3 local_id)
float4 transform(float4 hs_position)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
MutableVArraySpan< T > span
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
struct ReportList * reports