Cleanup: improve naming and comments of scene frame/ctime functions

Confusingly, BKE_scene_frame_get did not match the frame number as expected by
BKE_scene_frame_set. Instead it return the value after time remapping, which
is commonly named "ctime".

* Rename BKE_scene_frame_get to BKE_scene_ctime_get
* Add a new BKE_scene_frame_get that matches BKE_scene_frame_set
* Use int/float depending if fractional frame is expected
This commit is contained in:
Brecht Van Lommel 2021-07-12 16:15:03 +02:00
parent f709f12d93
commit 2ea565b0ec
23 changed files with 76 additions and 72 deletions

View File

@ -124,14 +124,16 @@ bool BKE_scene_camera_switch_update(struct Scene *scene);
const char *BKE_scene_find_marker_name(const struct Scene *scene, int frame);
const char *BKE_scene_find_last_marker_name(const struct Scene *scene, int frame);
int BKE_scene_frame_snap_by_seconds(struct Scene *scene, double interval_in_seconds, int cfra);
int BKE_scene_frame_snap_by_seconds(struct Scene *scene, double interval_in_seconds, int frame);
/* checks for cycle, returns 1 if it's all OK */
bool BKE_scene_validate_setscene(struct Main *bmain, struct Scene *sce);
float BKE_scene_ctime_get(const struct Scene *scene);
float BKE_scene_frame_to_ctime(const struct Scene *scene, const int frame);
float BKE_scene_frame_get(const struct Scene *scene);
float BKE_scene_frame_to_ctime(const struct Scene *scene, const float frame);
void BKE_scene_frame_set(struct Scene *scene, double cfra);
void BKE_scene_frame_set(struct Scene *scene, float frame);
struct TransformOrientationSlot *BKE_scene_orientation_slot_get_from_flag(struct Scene *scene,
int flag);

View File

@ -2843,7 +2843,7 @@ void BKE_pose_where_is(struct Depsgraph *depsgraph, Scene *scene, Object *ob)
* hopefully this is OK. */
BKE_pose_ensure(NULL, ob, arm, true);
ctime = BKE_scene_frame_get(scene); /* not accurate... */
ctime = BKE_scene_ctime_get(scene); /* not accurate... */
/* In edit-mode or rest-position we read the data from the bones. */
if (arm->edbo || (arm->flag & ARM_RESTPOS)) {

View File

@ -828,7 +828,7 @@ void BKE_pose_eval_init_ik(struct Depsgraph *depsgraph, Scene *scene, Object *ob
{
DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
BLI_assert(object->type == OB_ARMATURE);
const float ctime = BKE_scene_frame_get(scene); /* not accurate... */
const float ctime = BKE_scene_ctime_get(scene); /* not accurate... */
bArmature *armature = (bArmature *)object->data;
if (armature->flag & ARM_RESTPOS) {
return;
@ -869,7 +869,7 @@ void BKE_pose_eval_bone(struct Depsgraph *depsgraph, Scene *scene, Object *objec
else {
if ((pchan->flag & POSE_DONE) == 0) {
/* TODO(sergey): Use time source node for time. */
float ctime = BKE_scene_frame_get(scene); /* not accurate... */
float ctime = BKE_scene_ctime_get(scene); /* not accurate... */
BKE_pose_where_is_bone(depsgraph, scene, object, pchan, ctime, 1);
}
}
@ -897,7 +897,7 @@ void BKE_pose_constraints_evaluate(struct Depsgraph *depsgraph,
}
else {
if ((pchan->flag & POSE_DONE) == 0) {
float ctime = BKE_scene_frame_get(scene); /* not accurate... */
float ctime = BKE_scene_ctime_get(scene); /* not accurate... */
BKE_pose_where_is_bone(depsgraph, scene, object, pchan, ctime, 1);
}
}
@ -981,7 +981,7 @@ void BKE_pose_iktree_evaluate(struct Depsgraph *depsgraph,
DEG_debug_print_eval_subdata(
depsgraph, __func__, object->id.name, object, "rootchan", rootchan->name, rootchan);
BLI_assert(object->type == OB_ARMATURE);
const float ctime = BKE_scene_frame_get(scene); /* not accurate... */
const float ctime = BKE_scene_ctime_get(scene); /* not accurate... */
if (armature->flag & ARM_RESTPOS) {
return;
}
@ -1002,7 +1002,7 @@ void BKE_pose_splineik_evaluate(struct Depsgraph *depsgraph,
DEG_debug_print_eval_subdata(
depsgraph, __func__, object->id.name, object, "rootchan", rootchan->name, rootchan);
BLI_assert(object->type == OB_ARMATURE);
const float ctime = BKE_scene_frame_get(scene); /* not accurate... */
const float ctime = BKE_scene_ctime_get(scene); /* not accurate... */
if (armature->flag & ARM_RESTPOS) {
return;
}
@ -1031,7 +1031,7 @@ void BKE_pose_eval_cleanup(struct Depsgraph *depsgraph, Scene *scene, Object *ob
bPose *pose = object->pose;
BLI_assert(pose != NULL);
UNUSED_VARS_NDEBUG(pose);
const float ctime = BKE_scene_frame_get(scene); /* not accurate... */
const float ctime = BKE_scene_ctime_get(scene); /* not accurate... */
DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
BLI_assert(object->type == OB_ARMATURE);
/* Release the IK tree. */

View File

@ -314,7 +314,7 @@ bool BKE_cachefile_filepath_get(const Main *bmain,
if (cache_file->is_sequence && BLI_path_frame_get(r_filepath, &fframe, &frame_len)) {
Scene *scene = DEG_get_evaluated_scene(depsgraph);
const float ctime = BKE_scene_frame_get(scene);
const float ctime = BKE_scene_ctime_get(scene);
const float fps = (((double)scene->r.frs_sec) / (double)scene->r.frs_sec_base);
const float frame = BKE_cachefile_time_offset(cache_file, ctime, fps);

View File

@ -3818,7 +3818,7 @@ static void dynamicPaint_brushMeshCalculateVelocity(Depsgraph *depsgraph,
ob,
true,
SUBFRAME_RECURSION,
BKE_scene_frame_get(scene),
BKE_scene_ctime_get(scene),
eModifierType_DynamicPaint);
mesh_p = BKE_mesh_copy_for_eval(dynamicPaint_brush_mesh_get(brush), false);
numOfVerts_p = mesh_p->totvert;
@ -3834,7 +3834,7 @@ static void dynamicPaint_brushMeshCalculateVelocity(Depsgraph *depsgraph,
ob,
true,
SUBFRAME_RECURSION,
BKE_scene_frame_get(scene),
BKE_scene_ctime_get(scene),
eModifierType_DynamicPaint);
mesh_c = dynamicPaint_brush_mesh_get(brush);
numOfVerts_c = mesh_c->totvert;
@ -3894,7 +3894,7 @@ static void dynamicPaint_brushObjectCalculateVelocity(
ob,
false,
SUBFRAME_RECURSION,
BKE_scene_frame_get(scene),
BKE_scene_ctime_get(scene),
eModifierType_DynamicPaint);
copy_m4_m4(prev_obmat, ob->obmat);
@ -3906,7 +3906,7 @@ static void dynamicPaint_brushObjectCalculateVelocity(
ob,
false,
SUBFRAME_RECURSION,
BKE_scene_frame_get(scene),
BKE_scene_ctime_get(scene),
eModifierType_DynamicPaint);
/* calculate speed */
@ -6271,7 +6271,7 @@ static int dynamicPaint_doStep(Depsgraph *depsgraph,
brushObj,
true,
SUBFRAME_RECURSION,
BKE_scene_frame_get(scene),
BKE_scene_ctime_get(scene),
eModifierType_DynamicPaint);
}
@ -6312,7 +6312,7 @@ static int dynamicPaint_doStep(Depsgraph *depsgraph,
brushObj,
true,
SUBFRAME_RECURSION,
BKE_scene_frame_get(scene),
BKE_scene_ctime_get(scene),
eModifierType_DynamicPaint);
}

View File

@ -1283,10 +1283,10 @@ static void compute_obstaclesemission(Scene *scene,
# endif
/* Update frame time, this is considering current subframe fraction
* BLI_mutex_lock() called in manta_step(), so safe to update subframe here
* TODO(sebbas): Using BKE_scene_frame_get(scene) instead of new DEG_get_ctime(depsgraph)
* TODO(sebbas): Using BKE_scene_ctime_get(scene) instead of new DEG_get_ctime(depsgraph)
* as subframes don't work with the latter yet. */
BKE_object_modifier_update_subframe(
depsgraph, scene, effecobj, true, 5, BKE_scene_frame_get(scene), eModifierType_Fluid);
depsgraph, scene, effecobj, true, 5, BKE_scene_ctime_get(scene), eModifierType_Fluid);
if (subframes) {
obstacles_from_mesh(effecobj, fds, fes, &bb_temp, subframe_dt);
@ -1616,7 +1616,7 @@ static void emit_from_particles(Object *flow_ob,
}
/* `DEG_get_ctime(depsgraph)` does not give sub-frame time. */
state.time = BKE_scene_frame_get(scene);
state.time = BKE_scene_ctime_get(scene);
if (psys_get_particle_state(&sim, p, &state, 0) == 0) {
continue;
@ -2820,10 +2820,10 @@ static void compute_flowsemission(Scene *scene,
# endif
/* Update frame time, this is considering current subframe fraction
* BLI_mutex_lock() called in manta_step(), so safe to update subframe here
* TODO(sebbas): Using BKE_scene_frame_get(scene) instead of new DEG_get_ctime(depsgraph)
* TODO(sebbas): Using BKE_scene_ctime_get(scene) instead of new DEG_get_ctime(depsgraph)
* as subframes don't work with the latter yet. */
BKE_object_modifier_update_subframe(
depsgraph, scene, flowobj, true, 5, BKE_scene_frame_get(scene), eModifierType_Fluid);
depsgraph, scene, flowobj, true, 5, BKE_scene_ctime_get(scene), eModifierType_Fluid);
/* Emission from particles. */
if (ffs->source == FLUID_FLOW_SOURCE_PARTICLES) {

View File

@ -123,7 +123,7 @@ void BKE_object_eval_parent(Depsgraph *depsgraph, Object *ob)
void BKE_object_eval_constraints(Depsgraph *depsgraph, Scene *scene, Object *ob)
{
bConstraintOb *cob;
float ctime = BKE_scene_frame_get(scene);
float ctime = BKE_scene_ctime_get(scene);
DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);

View File

@ -3961,7 +3961,7 @@ static ModifierData *object_add_or_copy_particle_system(
psys->totpart = 0;
psys->flag = PSYS_CURRENT;
if (scene != NULL) {
psys->cfra = BKE_scene_frame_to_ctime(scene, CFRA + 1);
psys->cfra = BKE_scene_frame_to_ctime(scene, scene->r.cfra + 1);
}
DEG_relations_tag_update(bmain);

View File

@ -2807,8 +2807,8 @@ void BKE_ptcache_id_time(
cache = pid->cache;
if (timescale) {
time = BKE_scene_frame_get(scene);
nexttime = BKE_scene_frame_to_ctime(scene, CFRA + 1.0f);
time = BKE_scene_ctime_get(scene);
nexttime = BKE_scene_frame_to_ctime(scene, scene->r.cfra + 1);
*timescale = MAX2(nexttime - time, 0.0f);
}

View File

@ -2299,10 +2299,7 @@ Object *BKE_scene_camera_switch_find(Scene *scene)
return NULL;
}
const int cfra = ((scene->r.images == scene->r.framapto) ?
scene->r.cfra :
(int)(scene->r.cfra *
((float)scene->r.framapto / (float)scene->r.images)));
const int ctime = (int)BKE_scene_ctime_get(scene);
int frame = -(MAXFRAME + 1);
int min_frame = MAXFRAME + 1;
Object *camera = NULL;
@ -2310,11 +2307,11 @@ Object *BKE_scene_camera_switch_find(Scene *scene)
LISTBASE_FOREACH (TimeMarker *, m, &scene->markers) {
if (m->camera && (m->camera->restrictflag & OB_RESTRICT_RENDER) == 0) {
if ((m->frame <= cfra) && (m->frame > frame)) {
if ((m->frame <= ctime) && (m->frame > frame)) {
camera = m->camera;
frame = m->frame;
if (frame == cfra) {
if (frame == ctime) {
break;
}
}
@ -2396,13 +2393,13 @@ const char *BKE_scene_find_last_marker_name(const Scene *scene, int frame)
return best_marker ? best_marker->name : NULL;
}
int BKE_scene_frame_snap_by_seconds(Scene *scene, double interval_in_seconds, int cfra)
int BKE_scene_frame_snap_by_seconds(Scene *scene, double interval_in_seconds, int frame)
{
const int fps = round_db_to_int(FPS * interval_in_seconds);
const int second_prev = cfra - mod_i(cfra, fps);
const int second_prev = frame - mod_i(frame, fps);
const int second_next = second_prev + fps;
const int delta_prev = cfra - second_prev;
const int delta_next = second_next - cfra;
const int delta_prev = frame - second_prev;
const int delta_next = second_next - frame;
return (delta_prev < delta_next) ? second_prev : second_next;
}
@ -2444,16 +2441,17 @@ bool BKE_scene_validate_setscene(Main *bmain, Scene *sce)
return true;
}
/**
* This function is needed to cope with fractional frames, needed for motion blur & physics.
*/
float BKE_scene_frame_get(const Scene *scene)
/* Return fractional frame number taking into account subframes and time
* remapping. This the time value used by animation, modifiers and physics
* evaluation. */
float BKE_scene_ctime_get(const Scene *scene)
{
return BKE_scene_frame_to_ctime(scene, scene->r.cfra);
}
/* This function is used to obtain arbitrary fractional frames */
float BKE_scene_frame_to_ctime(const Scene *scene, const float frame)
/* Convert integer frame number to fractional frame number taking into account
* subframes and time remapping. */
float BKE_scene_frame_to_ctime(const Scene *scene, const int frame)
{
float ctime = frame;
ctime += scene->r.subframe;
@ -2461,13 +2459,18 @@ float BKE_scene_frame_to_ctime(const Scene *scene, const float frame)
return ctime;
}
/**
* Sets the frame int/float components.
*/
void BKE_scene_frame_set(struct Scene *scene, double cfra)
/* Get current fractional frame based on frame and subframe. */
float BKE_scene_frame_get(const Scene *scene)
{
return scene->r.cfra + scene->r.subframe;
}
/* Set current frame and subframe based on a fractional frame. */
void BKE_scene_frame_set(Scene *scene, float frame)
{
double intpart;
scene->r.subframe = modf(cfra, &intpart);
scene->r.subframe = modf((double)frame, &intpart);
scene->r.cfra = (int)intpart;
}
@ -2736,7 +2739,7 @@ void BKE_scene_graph_update_for_newframe_ex(Depsgraph *depsgraph, const bool cle
* edits from callback are properly taken into account. Doing a time update on those would
* lose any possible unkeyed changes made by the handler. */
if (pass == 0) {
const float ctime = BKE_scene_frame_get(scene);
const float ctime = BKE_scene_ctime_get(scene);
DEG_evaluate_on_framechange(depsgraph, ctime);
}
else {

View File

@ -68,7 +68,7 @@ Depsgraph::Depsgraph(Main *bmain, Scene *scene, ViewLayer *view_layer, eEvaluati
scene(scene),
view_layer(view_layer),
mode(mode),
ctime(BKE_scene_frame_get(scene)),
ctime(BKE_scene_ctime_get(scene)),
scene_cow(nullptr),
is_active(false),
is_evaluating(false),

View File

@ -63,7 +63,7 @@ void DEG_evaluate_on_refresh(Depsgraph *graph)
{
deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(graph);
const Scene *scene = DEG_get_input_scene(graph);
const float ctime = BKE_scene_frame_get(scene);
const float ctime = BKE_scene_ctime_get(scene);
if (ctime != deg_graph->ctime) {
deg_graph->tag_time_source();

View File

@ -109,7 +109,7 @@ static void draw_current_frame(const Scene *scene,
if (draw_line) {
/* Draw vertical line to from the bottom of the current frame box to the bottom of the screen.
*/
const float subframe_x = UI_view2d_view_to_region_x(v2d, BKE_scene_frame_get(scene));
const float subframe_x = UI_view2d_view_to_region_x(v2d, BKE_scene_ctime_get(scene));
GPUVertFormat *format = immVertexFormat();
uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);

View File

@ -468,7 +468,7 @@ bool fcurve_is_changed(struct PointerRNA ptr,
* Checks whether a keyframe exists for the given ID-block one the given frame.
* - It is recommended to call this method over the other keyframe-checkers directly,
* in case some detail of the implementation changes...
* - frame: the value of this is quite often result of #BKE_scene_frame_get()
* - frame: the value of this is quite often result of #BKE_scene_ctime_get()
*/
bool id_frame_has_keyframe(struct ID *id, float frame, short filter);

View File

@ -813,7 +813,7 @@ static int action_layer_next_exec(bContext *C, wmOperator *op)
NlaTrack *act_track;
Scene *scene = CTX_data_scene(C);
float ctime = BKE_scene_frame_get(scene);
float ctime = BKE_scene_ctime_get(scene);
/* Get active track */
act_track = BKE_nlatrack_find_tweaked(adt);
@ -925,7 +925,7 @@ static int action_layer_prev_exec(bContext *C, wmOperator *op)
NlaTrack *nlt;
Scene *scene = CTX_data_scene(C);
float ctime = BKE_scene_frame_get(scene);
float ctime = BKE_scene_ctime_get(scene);
/* Sanity Check */
if (adt == NULL) {

View File

@ -1404,7 +1404,7 @@ static void draw_selected_name(
/* color depends on whether there is a keyframe */
if (id_frame_has_keyframe(
(ID *)ob, /* BKE_scene_frame_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL)) {
(ID *)ob, /* BKE_scene_ctime_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL)) {
UI_FontThemeColor(font_id, TH_TIME_KEYFRAME);
}
else if (ED_gpencil_has_keyframe_v3d(scene, ob, cfra)) {

View File

@ -153,7 +153,7 @@ static void ObjectToTransData(TransInfo *t, TransData *td, Object *ob)
if (t->mode != TFM_DUMMY && ob->rigidbody_object) {
float rot[3][3], scale[3];
float ctime = BKE_scene_frame_get(scene);
float ctime = BKE_scene_ctime_get(scene);
/* only use rigid body transform if simulation is running,
* avoids problems with initial setup of rigid bodies */
@ -978,7 +978,7 @@ void special_aftertrans_update__object(bContext *C, TransInfo *t)
/* restore rigid body transform */
if (ob->rigidbody_object && canceled) {
float ctime = BKE_scene_frame_get(t->scene);
float ctime = BKE_scene_ctime_get(t->scene);
if (BKE_rigidbody_check_sim_running(t->scene->rigidbody_world, ctime)) {
BKE_rigidbody_aftertrans_update(ob,
td->ext->oloc,

View File

@ -101,7 +101,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct
range_vn_i(faceMap, numPoly_src, 0);
struct Scene *scene = DEG_get_input_scene(ctx->depsgraph);
frac = (BKE_scene_frame_get(scene) - bmd->start) / bmd->length;
frac = (BKE_scene_ctime_get(scene) - bmd->start) / bmd->length;
CLAMP(frac, 0.0f, 1.0f);
if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
frac = 1.0f - frac;

View File

@ -919,7 +919,7 @@ static Mesh *explodeMesh(ExplodeModifierData *emd,
EdgeHashIterator *ehi;
float *vertco = NULL, imat[4][4];
float rot[4];
float cfra;
float ctime;
/* float timestep; */
const int *facepa = emd->facepa;
int totdup = 0, totvert = 0, totface = 0, totpart = 0, delface = 0;
@ -940,7 +940,7 @@ static Mesh *explodeMesh(ExplodeModifierData *emd,
/* timestep = psys_get_timestep(&sim); */
cfra = BKE_scene_frame_get(scene);
ctime = BKE_scene_ctime_get(scene);
/* hash table for vertice <-> particle relations */
vertpahash = BLI_edgehash_new(__func__);
@ -962,7 +962,7 @@ static Mesh *explodeMesh(ExplodeModifierData *emd,
/* do mindex + totvert to ensure the vertex index to be the first
* with BLI_edgehashIterator_getKey */
if (pa == NULL || cfra < pa->time) {
if (pa == NULL || ctime < pa->time) {
mindex = totvert + totpart;
}
else {
@ -1022,7 +1022,7 @@ static Mesh *explodeMesh(ExplodeModifierData *emd,
psys_get_birth_coords(&sim, pa, &birth, 0, 0);
state.time = cfra;
state.time = ctime;
psys_get_particle_state(&sim, ed_v2, &state, 1);
vertco = explode->mvert[v].co;
@ -1076,7 +1076,7 @@ static Mesh *explodeMesh(ExplodeModifierData *emd,
orig_v4 = source.v4;
/* Same as above in the first loop over mesh's faces. */
if (pa == NULL || cfra < pa->time) {
if (pa == NULL || ctime < pa->time) {
mindex = totvert + totpart;
}
else {
@ -1096,7 +1096,7 @@ static Mesh *explodeMesh(ExplodeModifierData *emd,
/* override uv channel for particle age */
if (mtface) {
float age = (pa != NULL) ? (cfra - pa->time) / pa->lifetime : 0.0f;
float age = (pa != NULL) ? (ctime - pa->time) / pa->lifetime : 0.0f;
/* Clamp to this range to avoid flipping to the other side of the coordinates. */
CLAMP(age, 0.001f, 0.999f);

View File

@ -104,20 +104,20 @@ static void meshcache_do(MeshCacheModifierData *mcmd,
/* -------------------------------------------------------------------- */
/* Interpret Time (the reading functions also do some of this ) */
if (mcmd->play_mode == MOD_MESHCACHE_PLAY_CFEA) {
const float cfra = BKE_scene_frame_get(scene);
const float ctime = BKE_scene_ctime_get(scene);
switch (mcmd->time_mode) {
case MOD_MESHCACHE_TIME_FRAME: {
time = cfra;
time = ctime;
break;
}
case MOD_MESHCACHE_TIME_SECONDS: {
time = cfra / fps;
time = ctime / fps;
break;
}
case MOD_MESHCACHE_TIME_FACTOR:
default: {
time = cfra / fps;
time = ctime / fps;
break;
}
}

View File

@ -120,7 +120,6 @@ static void deformVerts(ModifierData *md,
Mesh *mesh_src = mesh;
ParticleSystemModifierData *psmd = (ParticleSystemModifierData *)md;
ParticleSystem *psys = NULL;
/* float cfra = BKE_scene_frame_get(md->scene); */ /* UNUSED */
if (ctx->object->particlesystem.first) {
psys = psmd->psys;

View File

@ -2410,7 +2410,7 @@ void RE_RenderAnim(Render *re,
* -sergey-
*/
{
float ctime = BKE_scene_frame_get(scene);
float ctime = BKE_scene_ctime_get(scene);
AnimData *adt = BKE_animdata_from_id(&scene->id);
const AnimationEvalContext anim_eval_context = BKE_animsys_eval_context_construct(
re->pipeline_depsgraph, ctime);

View File

@ -169,7 +169,7 @@ static void pointdensity_cache_psys(
ParticleCacheKey *cache;
ParticleSimulationData sim = {NULL};
ParticleData *pa = NULL;
float cfra = BKE_scene_frame_get(scene);
float cfra = BKE_scene_ctime_get(scene);
int i /*, Childexists*/ /* UNUSED */;
int total_particles;
int data_used;
@ -782,7 +782,7 @@ static void particle_system_minmax(Depsgraph *depsgraph,
float max[3])
{
const float size[3] = {radius, radius, radius};
const float cfra = BKE_scene_frame_get(scene);
const float cfra = BKE_scene_ctime_get(scene);
ParticleSettings *part = psys->part;
ParticleSimulationData sim = {NULL};
ParticleData *pa = NULL;