Example #1
0
    def __init__(self, model, gl_context):
        """Initializes this MjrContext instance.

    Args:
      model: An `MjModel` instance.
      gl_context: A `render.ContextBase` instance.
    """
        ptr = ctypes.pointer(types.MJRCONTEXT())
        mjlib.mjr_defaultContext(ptr)

        with gl_context.make_current() as ctx:
            ctx.call(mjlib.mjr_makeContext, model.ptr, ptr, _FONT_SCALE)
            ctx.call(mjlib.mjr_setBuffer, enums.mjtFramebuffer.mjFB_OFFSCREEN,
                     ptr)
            gl_context.increment_refcount()

        # Free resources when the ctypes pointer is garbage collected.
        def finalize_mjr_context(ptr):
            with gl_context.make_current() as ctx:
                ctx.call(mjlib.mjr_freeContext, ptr)
                gl_context.decrement_refcount()

        _create_finalizer(ptr, finalize_mjr_context)

        super(MjrContext, self).__init__(ptr)
    def __init__(self,
                 model,
                 gl_context,
                 font_scale=enums.mjtFontScale.mjFONTSCALE_150):
        """Initializes this MjrContext instance.

    Args:
      model: An `MjModel` instance.
      gl_context: A `render.ContextBase` instance.
      font_scale: Integer controlling the font size for text. Must be a value
        in `mjbindings.enums.mjtFontScale`.

    Raises:
      ValueError: If `font_scale` is invalid.
    """
        if font_scale not in enums.mjtFontScale:
            raise ValueError(_INVALID_FONT_SCALE.format(font_scale))

        ptr = ctypes.pointer(types.MJRCONTEXT())
        mjlib.mjr_defaultContext(ptr)

        with gl_context.make_current() as ctx:
            ctx.call(mjlib.mjr_makeContext, model.ptr, ptr, font_scale)
            ctx.call(mjlib.mjr_setBuffer, enums.mjtFramebuffer.mjFB_OFFSCREEN,
                     ptr)
            gl_context.increment_refcount()

        # Free resources when the ctypes pointer is garbage collected.
        def finalize_mjr_context(ptr):
            if not gl_context.terminated:
                with gl_context.make_current() as ctx:
                    ctx.call(mjlib.mjr_freeContext, ptr)
                    gl_context.decrement_refcount()

        _create_finalizer(ptr, finalize_mjr_context)

        super(MjrContext, self).__init__(ptr)
Example #3
0
 def __init__(self):
   ptr = ctypes.pointer(types.MJRCONTEXT())
   mjlib.mjr_defaultContext(ptr)
   super(UnmanagedMjrContext, self).__init__(ptr)