Skip to content

Commit

Permalink
refactor: Update warning message related to sequence_wrapper_blender
Browse files Browse the repository at this point in the history
- Changed method name from `xf_runner.Sequence.new` to `xf_runner.sequence` for consistency with the updated API.
- Introduced a new helper function `_get_caller_frame` in `sequence_wrapper.py` to improve warning messages by providing the actual caller's frame information.
- Updated warning handling in `SequenceWrapperBlender` to utilize the new caller frame information, enhancing the clarity of deprecation warnings.
  • Loading branch information
HaiyiMei committed Dec 23, 2024
1 parent 394af2a commit 599c51e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion xrfeitoria/cmd/blender/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def main(
camera_name = 'camera'

with xf.init_blender(exec_path=blender_exec, background=background) as xf_runner:
with xf_runner.Sequence.new(seq_name=seq_name) as seq:
with xf_runner.sequence(seq_name=seq_name) as seq:
actor = seq.import_actor(actor_name='actor', file_path=mesh_path, stencil_value=255)
actor.set_origin_to_center()
actor.location = (0, 0, 0)
Expand Down
36 changes: 33 additions & 3 deletions xrfeitoria/sequence/sequence_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Sequence wrapper functions."""

import inspect
import warnings
from contextlib import contextmanager
from typing import ContextManager, List, Optional, Tuple, Union
from typing import ContextManager, Optional, Union

from typing_extensions import deprecated

Expand All @@ -15,6 +16,21 @@
__all__ = ['sequence_wrapper_blender', 'sequence_wrapper_unreal']


def _get_caller_frame():
"""Get the actual caller's frame by traversing the frame stack."""
frame = inspect.currentframe()
try:
while frame:
frame_info = inspect.getframeinfo(frame)
# Skip frames from this module and contextlib
if frame_info.filename.endswith(('sequence_wrapper.py', 'contextlib.py')):
frame = frame.f_back
continue
return frame
finally:
del frame


@deprecated('Use `xf_runner.sequence` function instead.', category=DeprecationWarning)
class SequenceWrapperBlender:
"""Sequence utils class."""
Expand Down Expand Up @@ -47,7 +63,14 @@ def new(
Yields:
SequenceBase: Sequence object.
"""
warnings.showwarning(cls._warn_msg, DeprecationWarning, __file__, 0)
caller_frame = _get_caller_frame()
if caller_frame:
warnings.showwarning(
cls._warn_msg, DeprecationWarning, caller_frame.f_code.co_filename, caller_frame.f_lineno
)
else:
warnings.warn(cls._warn_msg, DeprecationWarning, stacklevel=3)

cls._seq._new(
seq_name=seq_name,
level=level,
Expand All @@ -69,7 +92,14 @@ def open(cls, seq_name: str) -> ContextManager[SequenceBase]:
Yields:
SequenceBase: Sequence object.
"""
warnings.showwarning(cls._warn_msg, DeprecationWarning, __file__, 0)
caller_frame = _get_caller_frame()
if caller_frame:
warnings.showwarning(
cls._warn_msg, DeprecationWarning, caller_frame.f_code.co_filename, caller_frame.f_lineno
)
else:
warnings.warn(cls._warn_msg, DeprecationWarning, stacklevel=3)

cls._seq._open(seq_name=seq_name)
yield cls._seq
cls._seq.close()
Expand Down

0 comments on commit 599c51e

Please sign in to comment.