Fix #32219: Inconsistent influence of Units Scale on new objects

Made it so meshes, curves, surfaces and metaballs are scaling to a grid cell size,
which makes them behave consistently now.

There're still issues to be resolved still:

- Lattice is not scaled to grid cell size yet, it uses slightly different add
  function which makes scaling a bit tricky and hacky. Would prefer to do a
  bit bigger refactor here, so it's a TODO for now.

- Cameras, speakers and other helpers are not scaling. They don't have data
  on which scale could be applied and perhaps it should be some kind of draw
  scale. Also would consider it's a TODO for now.
This commit is contained in:
Sergey Sharybin 2012-10-19 12:53:03 +00:00
parent 3a41ab73ff
commit a75f11d036
9 changed files with 70 additions and 28 deletions

@ -187,3 +187,16 @@ class AddObjectHelper:
name="Rotation",
subtype='EULER',
)
def object_add_grid_scale(context):
"""
Return scale which should be applied on object data to align it to grid scale
"""
space_data = context.space_data
if space_data and space_data.type == 'VIEW_3D':
return space_data.grid_scale_unit
return 1.0

@ -133,13 +133,15 @@ class AddTorus(Operator, object_utils.AddObjectHelper):
)
def execute(self, context):
grid_scale = object_utils.object_add_grid_scale(context)
if self.use_abso is True:
extra_helper = (self.abso_major_rad - self.abso_minor_rad) * 0.5
self.major_radius = self.abso_minor_rad + extra_helper
self.minor_radius = extra_helper
verts_loc, faces = add_torus(self.major_radius,
self.minor_radius,
verts_loc, faces = add_torus(self.major_radius * grid_scale,
self.minor_radius * grid_scale,
self.major_segments,
self.minor_segments)

@ -6486,12 +6486,7 @@ Nurb *add_nurbs_primitive(bContext *C, Object *obedit, float mat[4][4], int type
vec[0] = vec[1] = 0.0;
vec[2] = -grid;
if (newob && (U.flag & USER_ADD_VIEWALIGNED) == 0) {
/* pass */
}
else {
mul_mat3_m4_v3(mat, vec);
}
mul_mat3_m4_v3(mat, vec);
translateflagNurb(editnurb, 1, vec);
extrudeflagNurb(cu->editnurb, 1);
@ -6609,6 +6604,7 @@ static int curvesurf_prim_add(bContext *C, wmOperator *op, int type, int isSurf)
unsigned int layer;
float loc[3], rot[3];
float mat[4][4];
float dia;
if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, &is_aligned))
return OPERATOR_CANCELLED;
@ -6652,7 +6648,7 @@ static int curvesurf_prim_add(bContext *C, wmOperator *op, int type, int isSurf)
if (newob && enter_editmode)
ED_undo_push(C, "Enter Editmode");
ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
dia = ED_object_new_primitive_matrix(C, obedit, loc, rot, mat, TRUE);
nu = add_nurbs_primitive(C, obedit, mat, type, newob);
editnurb = object_editcurve_get(obedit);

@ -38,7 +38,7 @@ struct wmKeyConfig;
void ED_operatortypes_metaball(void);
void ED_keymap_metaball(struct wmKeyConfig *keyconf);
struct MetaElem *add_metaball_primitive(struct bContext *C, struct Object *obedit, float mat[4][4], int type, int newname);
struct MetaElem *add_metaball_primitive(struct bContext *C, struct Object *obedit, float mat[4][4], float dia, int type, int newname);
int mouse_mball(struct bContext *C, const int mval[2], int extend, int deselect, int toggle);

@ -134,7 +134,8 @@ void ED_object_location_from_view(struct bContext *C, float loc[3]);
void ED_object_rotation_from_view(struct bContext *C, float rot[3]);
void ED_object_base_init_transform(struct bContext *C, struct Base *base, const float loc[3], const float rot[3]);
float ED_object_new_primitive_matrix(struct bContext *C, struct Object *editob,
const float loc[3], const float rot[3], float primmat[][4]);
const float loc[3], const float rot[3], float primmat[][4],
int apply_diameter);
void ED_object_add_generic_props(struct wmOperatorType *ot, int do_editmode);
int ED_object_add_generic_get_opts(struct bContext *C, struct wmOperator *op, float loc[3], float rot[3],

@ -73,7 +73,7 @@ static Object *make_prim_init(bContext *C, const char *idname,
*state = 1;
}
*dia = ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
*dia = ED_object_new_primitive_matrix(C, obedit, loc, rot, mat, FALSE);
return obedit;
}
@ -200,7 +200,7 @@ static int add_primitive_circle_exec(bContext *C, wmOperator *op)
if (!EDBM_op_call_and_selectf(em, op, "vertout",
"create_circle segments=%i diameter=%f cap_ends=%b cap_tris=%b mat=%m4",
RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius"),
RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius") * dia,
cap_end, cap_tri, mat))
{
return OPERATOR_CANCELLED;
@ -256,10 +256,10 @@ static int add_primitive_cylinder_exec(bContext *C, wmOperator *op)
em, op, "vertout",
"create_cone segments=%i diameter1=%f diameter2=%f cap_ends=%b cap_tris=%b depth=%f mat=%m4",
RNA_int_get(op->ptr, "vertices"),
RNA_float_get(op->ptr, "radius"),
RNA_float_get(op->ptr, "radius"),
RNA_float_get(op->ptr, "radius") * dia,
RNA_float_get(op->ptr, "radius") * dia,
cap_end, cap_tri,
RNA_float_get(op->ptr, "depth"), mat))
RNA_float_get(op->ptr, "depth") * dia, mat))
{
return OPERATOR_CANCELLED;
}
@ -315,8 +315,8 @@ static int add_primitive_cone_exec(bContext *C, wmOperator *op)
if (!EDBM_op_call_and_selectf(
em, op, "vertout",
"create_cone segments=%i diameter1=%f diameter2=%f cap_ends=%b cap_tris=%b depth=%f mat=%m4",
RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius1"),
RNA_float_get(op->ptr, "radius2"), cap_end, cap_tri, RNA_float_get(op->ptr, "depth"), mat))
RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius1") * dia,
RNA_float_get(op->ptr, "radius2") * dia, cap_end, cap_tri, RNA_float_get(op->ptr, "depth") * dia, mat))
{
return OPERATOR_CANCELLED;
}
@ -421,6 +421,10 @@ static int add_primitive_monkey_exec(bContext *C, wmOperator *op)
rot[0] += (float)M_PI / 2.0f;
obedit = make_prim_init(C, "Monkey", &dia, mat, &state, loc, rot, layer);
mat[0][0] *= dia;
mat[1][1] *= dia;
mat[2][2] *= dia;
em = BMEdit_FromObject(obedit);
if (!EDBM_op_call_and_selectf(em, op, "vertout", "create_monkey mat=%m4", mat)) {
@ -465,7 +469,7 @@ static int add_primitive_uvsphere_exec(bContext *C, wmOperator *op)
if (!EDBM_op_call_and_selectf(em, op, "vertout",
"create_uvsphere segments=%i revolutions=%i diameter=%f mat=%m4",
RNA_int_get(op->ptr, "segments"), RNA_int_get(op->ptr, "ring_count"),
RNA_float_get(op->ptr, "size"), mat))
RNA_float_get(op->ptr, "size") * dia, mat))
{
return OPERATOR_CANCELLED;
}
@ -517,7 +521,7 @@ static int add_primitive_icosphere_exec(bContext *C, wmOperator *op)
em, op, "vertout",
"create_icosphere subdivisions=%i diameter=%f mat=%m4",
RNA_int_get(op->ptr, "subdivisions"),
RNA_float_get(op->ptr, "size"), mat))
RNA_float_get(op->ptr, "size") * dia, mat))
{
return OPERATOR_CANCELLED;
}

@ -98,7 +98,7 @@ void load_editMball(Object *UNUSED(obedit))
}
/* Add metaelem primitive to metaball object (which is in edit mode) */
MetaElem *add_metaball_primitive(bContext *UNUSED(C), Object *obedit, float mat[4][4], int type, int UNUSED(newname))
MetaElem *add_metaball_primitive(bContext *UNUSED(C), Object *obedit, float mat[4][4], float dia, int type, int UNUSED(newname))
{
MetaBall *mball = (MetaBall *)obedit->data;
MetaElem *ml;
@ -111,6 +111,7 @@ MetaElem *add_metaball_primitive(bContext *UNUSED(C), Object *obedit, float mat[
}
ml = BKE_mball_element_add(mball, type);
ml->rad *= dia;
copy_v3_v3(&ml->x, mat[3]);
ml->flag |= SELECT;

@ -186,7 +186,8 @@ void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3],
/* Uses context to figure out transform for primitive.
* Returns standard diameter. */
float ED_object_new_primitive_matrix(bContext *C, Object *obedit,
const float loc[3], const float rot[3], float primmat[][4])
const float loc[3], const float rot[3], float primmat[][4],
int apply_diameter)
{
Scene *scene = CTX_data_scene(C);
View3D *v3d = CTX_wm_view3d(C);
@ -209,8 +210,17 @@ float ED_object_new_primitive_matrix(bContext *C, Object *obedit,
invert_m3_m3(imat, mat);
mul_m3_v3(imat, primmat[3]);
if (v3d)
return ED_view3d_grid_scale(scene, v3d, NULL);
if (v3d) {
float dia = ED_view3d_grid_scale(scene, v3d, NULL);
if (apply_diameter) {
primmat[0][0] *= dia;
primmat[1][1] *= dia;
primmat[2][2] *= dia;
}
return dia;
}
return 1.0f;
}
@ -442,7 +452,7 @@ static int effector_add_exec(bContext *C, wmOperator *op)
rename_id(&ob->id, "CurveGuide");
((Curve *)ob->data)->flag |= CU_PATH | CU_3D;
ED_object_enter_editmode(C, 0);
ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
ED_object_new_primitive_matrix(C, ob, loc, rot, mat, FALSE);
BLI_addtail(object_editcurve_get(ob), add_nurbs_primitive(C, ob, mat, CU_NURBS | CU_PRIM_PATH, 1));
if (!enter_editmode)
ED_object_exit_editmode(C, EM_FREEDATA);
@ -547,6 +557,7 @@ static int object_metaball_add_exec(bContext *C, wmOperator *op)
unsigned int layer;
float loc[3], rot[3];
float mat[4][4];
float dia;
if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
return OPERATOR_CANCELLED;
@ -558,9 +569,9 @@ static int object_metaball_add_exec(bContext *C, wmOperator *op)
else
DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
dia = ED_object_new_primitive_matrix(C, obedit, loc, rot, mat, FALSE);
add_metaball_primitive(C, obedit, mat, RNA_enum_get(op->ptr, "type"), newob);
add_metaball_primitive(C, obedit, mat, dia, RNA_enum_get(op->ptr, "type"), newob);
/* userdef */
if (newob && !enter_editmode) {

@ -336,6 +336,15 @@ static void rna_View3D_CursorLocation_set(PointerRNA *ptr, const float *values)
copy_v3_v3(cursor, values);
}
static float rna_View3D_GridScaleUnit_get(PointerRNA *ptr)
{
View3D *v3d = (View3D *)(ptr->data);
bScreen *sc = (bScreen *)ptr->id.data;
Scene *scene = (Scene *)sc->scene;
return ED_view3d_grid_scale(scene, v3d, NULL);
}
static void rna_SpaceView3D_layer_set(PointerRNA *ptr, const int *values)
{
View3D *v3d = (View3D *)(ptr->data);
@ -1603,7 +1612,12 @@ static void rna_def_space_view3d(BlenderRNA *brna)
RNA_def_property_range(prop, 1, 1024);
RNA_def_property_int_default(prop, 10);
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
prop = RNA_def_property(srna, "grid_scale_unit", PROP_FLOAT, PROP_NONE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_float_funcs(prop, "rna_View3D_GridScaleUnit_get", NULL, NULL);
RNA_def_property_ui_text(prop, "Grid Scale Unit", "Grid cell size scaled by scene unit system settings");
prop = RNA_def_property(srna, "show_floor", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "gridflag", V3D_SHOW_FLOOR);
RNA_def_property_ui_text(prop, "Display Grid Floor", "Show the ground plane grid in perspective view");