GPencil: Add operator to create new grease pencil object #108462

Merged
Falk David merged 7 commits from filedescriptor/blender:gp-add-stroke into main 2023-05-31 18:00:32 +02:00
5 changed files with 130 additions and 4 deletions
Showing only changes of commit fe2eaa97fa - Show all commits

View File

@ -2303,6 +2303,16 @@ class VIEW3D_MT_volume_add(Menu):
icon='OUTLINER_DATA_VOLUME')
class VIEW3D_MT_grease_pencil_add(Menu):
bl_idname = "VIEW3D_MT_grease_pencil_add"
bl_label = "Grease Pencil"
def draw(self, _context):
layout = self.layout
layout.operator("object.grease_pencil_add", text="Empty", icon='EMPTY_AXIS').type = 'EMPTY'
layout.operator("object.grease_pencil_add", text="Stroke", icon='STROKE').type = 'STROKE'
class VIEW3D_MT_add(Menu):
bl_label = "Add"
bl_translation_context = i18n_contexts.operator_default
@ -2328,7 +2338,14 @@ class VIEW3D_MT_add(Menu):
if context.preferences.experimental.use_new_point_cloud_type:
layout.operator("object.pointcloud_add", text="Point Cloud", icon='OUTLINER_OB_POINTCLOUD')
layout.menu("VIEW3D_MT_volume_add", text="Volume", text_ctxt=i18n_contexts.id_id, icon='OUTLINER_OB_VOLUME')
layout.operator_menu_enum("object.gpencil_add", "type", text="Grease Pencil", icon='OUTLINER_OB_GREASEPENCIL')
if context.preferences.experimental.use_grease_pencil_version3:
layout.menu("VIEW3D_MT_grease_pencil_add", text="Grease Pencil", icon='OUTLINER_OB_GREASEPENCIL')
else:
layout.operator_menu_enum(
"object.gpencil_add",
"type",
text="Grease Pencil",
icon='OUTLINER_OB_GREASEPENCIL')
layout.separator()
@ -8188,6 +8205,7 @@ classes = (
VIEW3D_MT_lightprobe_add,
VIEW3D_MT_camera_add,
VIEW3D_MT_volume_add,
VIEW3D_MT_grease_pencil_add,
VIEW3D_MT_add,
VIEW3D_MT_image_add,
VIEW3D_MT_object,

View File

@ -68,6 +68,7 @@ set(LIB
bf_blenkernel
bf_blenlib
bf_editor_mesh
bf_editor_grease_pencil
bf_render
bf_windowmanager
)

View File

@ -104,6 +104,7 @@
#include "ED_curve.h"
#include "ED_curves.h"
#include "ED_gpencil_legacy.h"
#include "ED_grease_pencil.h"
#include "ED_mball.h"
#include "ED_mesh.h"
#include "ED_node.h"
@ -1305,7 +1306,7 @@ void OBJECT_OT_drop_named_image(wmOperatorType *ot)
/** \} */
/* -------------------------------------------------------------------- */
/** \name Add Gpencil Operator
/** \name Add Gpencil (legacy) Operator
* \{ */
static bool object_gpencil_add_poll(bContext *C)
@ -1528,8 +1529,8 @@ static EnumPropertyItem rna_enum_gpencil_add_stroke_depth_order_items[] = {
void OBJECT_OT_gpencil_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Grease Pencil";
ot->description = "Add a Grease Pencil object to the scene";
ot->name = "Add Grease Pencil (legacy)";
ot->description = "Add a Grease Pencil (legacy) object to the scene";
ot->idname = "OBJECT_OT_gpencil_add";
/* api callbacks */
@ -1575,6 +1576,110 @@ void OBJECT_OT_gpencil_add(wmOperatorType *ot)
/** \} */
/* -------------------------------------------------------------------- */
/** \name Add Grease Pencil Operator
* \{ */
static int object_grease_pencil_add_exec(bContext *C, wmOperator *op)
{
using namespace blender::ed;
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
/* TODO: For now, only support adding the 'Stroke' type. */
const int type = RNA_enum_get(op->ptr, "type");
ushort local_view_bits;
float loc[3], rot[3];
/* NOTE: We use 'Y' here (not 'Z'), as. */
WM_operator_view3d_unit_defaults(C, op);
if (!ED_object_add_generic_get_opts(
C, op, 'Y', loc, rot, nullptr, nullptr, &local_view_bits, nullptr))
{
return OPERATOR_CANCELLED;
}
const char *ob_name = nullptr;
switch (type) {
case GP_EMPTY: {
ob_name = CTX_DATA_(BLT_I18NCONTEXT_ID_GPENCIL, "GPencil");
break;
}
case GP_STROKE: {
ob_name = CTX_DATA_(BLT_I18NCONTEXT_ID_GPENCIL, "Stroke");
break;
}
case GP_MONKEY: {
ob_name = CTX_DATA_(BLT_I18NCONTEXT_ID_GPENCIL, "Suzanne");
break;
}
case GP_LRT_OBJECT:
case GP_LRT_SCENE:
case GP_LRT_COLLECTION: {
ob_name = CTX_DATA_(BLT_I18NCONTEXT_ID_GPENCIL, "LineArt");
break;
}
default: {
break;
}
}
Object *object = ED_object_add_type(
C, OB_GREASE_PENCIL, ob_name, loc, rot, false, local_view_bits);
GreasePencil &grease_pencil_id = *static_cast<GreasePencil *>(object->data);
switch (type) {
case GP_EMPTY: {
greasepencil::create_blank(*bmain, *object, scene->r.cfra);
break;
}
case GP_STROKE: {
float radius = RNA_float_get(op->ptr, "radius");
float scale[3];
copy_v3_fl(scale, radius);
filedescriptor marked this conversation as resolved Outdated

const float3 scale(radius);

`const float3 scale(radius);`
float mat[4][4];
ED_object_new_primitive_matrix(C, object, loc, rot, scale, mat);
filedescriptor marked this conversation as resolved
Review

This should work here:

float4x4 mat;
ED_object_new_primitive_matrix(C, object, loc, rot, scale, mat.ptr());
This should work here: ``` float4x4 mat; ED_object_new_primitive_matrix(C, object, loc, rot, scale, mat.ptr()); ```
greasepencil::create_stroke(*bmain, *object, float4x4(mat), scene->r.cfra);
break;
}
case GP_MONKEY:
case GP_LRT_OBJECT:
case GP_LRT_SCENE:
case GP_LRT_COLLECTION: {
/* TODO. */
break;
}
}
DEG_id_tag_update(&grease_pencil_id.id, ID_RECALC_GEOMETRY);
WM_main_add_notifier(NC_GEOM | ND_DATA, &grease_pencil_id.id);
return OPERATOR_FINISHED;
}
void OBJECT_OT_grease_pencil_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Grease Pencil";
ot->description = "Add a Grease Pencil object to the scene";
ot->idname = "OBJECT_OT_grease_pencil_add";
/* api callbacks */
ot->exec = object_grease_pencil_add_exec;
ot->poll = ED_operator_objectmode;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_object_gpencil_type_items, 0, "Type", "");
ED_object_add_unit_props_radius(ot);
ED_object_add_generic_props(ot, false);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Add Light Operator
* \{ */

View File

@ -118,6 +118,7 @@ void OBJECT_OT_empty_add(struct wmOperatorType *ot);
void OBJECT_OT_lightprobe_add(struct wmOperatorType *ot);
void OBJECT_OT_drop_named_image(struct wmOperatorType *ot);
void OBJECT_OT_gpencil_add(struct wmOperatorType *ot);
void OBJECT_OT_grease_pencil_add(struct wmOperatorType *ot);
void OBJECT_OT_light_add(struct wmOperatorType *ot);
void OBJECT_OT_effector_add(struct wmOperatorType *ot);
void OBJECT_OT_camera_add(struct wmOperatorType *ot);

View File

@ -89,6 +89,7 @@ void ED_operatortypes_object(void)
WM_operatortype_append(OBJECT_OT_lightprobe_add);
WM_operatortype_append(OBJECT_OT_drop_named_image);
WM_operatortype_append(OBJECT_OT_gpencil_add);
WM_operatortype_append(OBJECT_OT_grease_pencil_add);
WM_operatortype_append(OBJECT_OT_light_add);
WM_operatortype_append(OBJECT_OT_camera_add);
WM_operatortype_append(OBJECT_OT_speaker_add);