2.8: Python API: bpy.context.object not available in Application Timers #62051

Closed
opened 2019-02-28 16:03:53 +01:00 by Marc · 15 comments

System Information
Operating system: Windows 7, 64bit
Graphics card: N/A

Blender Version
Broken:
2.80, dd9cedddae, win64

Short description of error

The property bpy.context.object is not available in Application Timers. It raises an AttributeError: 'Context' object has no attribute 'object'.

Exact steps for others to reproduce the error

Just run this minimal example:


import bpy


def add_cylinder():
    bpy.ops.mesh.primitive_cylinder_add(
        radius=1, depth=1,
        location=(1.5, 0.0, 1),
    )

    obj = bpy.context.object

    print(obj)
    
    
# add_cylinder()   # this works
bpy.app.timers.register(add_cylinder)
**System Information** Operating system: Windows 7, 64bit Graphics card: N/A **Blender Version** Broken: 2.80, dd9cedddae69, win64 **Short description of error** The property `bpy.context.object` is not available in Application Timers. It raises an `AttributeError: 'Context' object has no attribute 'object'`. **Exact steps for others to reproduce the error** Just run this minimal example: ``` import bpy def add_cylinder(): bpy.ops.mesh.primitive_cylinder_add( radius=1, depth=1, location=(1.5, 0.0, 1), ) obj = bpy.context.object print(obj) # add_cylinder() # this works bpy.app.timers.register(add_cylinder) ```
Author

Added subscriber: @schlamar

Added subscriber: @schlamar
Member

Added subscriber: @JacquesLucke

Added subscriber: @JacquesLucke
Member

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Jacques Lucke self-assigned this 2019-02-28 16:06:01 +01:00
Member

Unfortunately the context is not really available within timers.
This is because timers are not bound to a window/scene/...

Unfortunately the context is not really available within timers. This is because timers are not bound to a window/scene/...
Author

In 2.7 I used bpy.app.handlers.scene_update_pre to emulate such an application timer and there I could access bpy.context.object. Looks like scene_update_pre is gone in 2.8 (without any mention in the release notes AFAIS).

So what would be the alternative in 2.8? Or is there an alternative way to get the created object after bpy.ops.mesh.primitive_cylinder_add?

In 2.7 I used `bpy.app.handlers.scene_update_pre` to emulate such an application timer and there I could access `bpy.context.object`. Looks like `scene_update_pre` is gone in 2.8 (without any mention in the release notes AFAIS). So what would be the alternative in 2.8? Or is there an alternative way to get the created object after `bpy.ops.mesh.primitive_cylinder_add`?
Member

Read this: https://devtalk.blender.org/t/porting-my-addons-to-2-8-missing-scene-update-post-handler/2465

Not sure what you are doing exactly, but adding stuff like that can really mess with the undo system..

You can also use the more low level api to create new meshes, although then you probably don't have ready made code that creates a cylinder.

Read this: https://devtalk.blender.org/t/porting-my-addons-to-2-8-missing-scene-update-post-handler/2465 Not sure what you are doing exactly, but adding stuff like that can really mess with the undo system.. You can also use the more low level api to create new meshes, although then you probably don't have ready made code that creates a cylinder.
Author

I have a rather special use case. I'm using Blender as a 3D live view controlled from an external application.

What I'm mainly doing:

  • creating objects
  • moving objects around (by setting its location)
  • joining objects
  • setting and unsetting parents (so I have a kinematics chain)

So I'm doing other high level stuff which is too much to refactor to use low level APIs.

Sadly, I couldn't find anything helpful in the linked discussion.

I have a rather special use case. I'm using Blender as a 3D live view controlled from an external application. What I'm mainly doing: - creating objects - moving objects around (by setting its location) - joining objects - setting and unsetting parents (so I have a kinematics chain) So I'm doing other high level stuff which is too much to refactor to use low level APIs. Sadly, I couldn't find anything helpful in the linked discussion.

Added subscriber: @brecht

Added subscriber: @brecht

bpy.context.object gives you the active object, but that can be different depending on the window and view layer, so it's not accessible by something global like a timer.

With a controlled environment like you have, using scene.view_layers- [x].objects.active will probably work.

`bpy.context.object` gives you the active object, but that can be different depending on the window and view layer, so it's not accessible by something global like a timer. With a controlled environment like you have, using `scene.view_layers- [x].objects.active` will probably work.
Author

Yes, bpy.context.scene.view_layers- [x].objects.active works for the code above. But just hitting the next issue:

def join_objects(*objects):
    bpy.ops.object.select_all(action='DESELECT')
    for o in objects:
        o.select_set(True)
    bpy.context.scene.view_layers[0].objects.active = objects[0]
    bpy.ops.object.join()
    bpy.ops.object.select_all(action='DESELECT')

Fails with: RuntimeError: Operator bpy.ops.object.join.poll() failed, context is incorrect.

Yes, `bpy.context.scene.view_layers- [x].objects.active` works for the code above. But just hitting the next issue: ``` def join_objects(*objects): bpy.ops.object.select_all(action='DESELECT') for o in objects: o.select_set(True) bpy.context.scene.view_layers[0].objects.active = objects[0] bpy.ops.object.join() bpy.ops.object.select_all(action='DESELECT') ``` Fails with: `RuntimeError: Operator bpy.ops.object.join.poll() failed, context is incorrect`.
Author

This works:

def get_default_context():
    window = bpy.context.window_manager.windows[0]
    return {'window': window, 'screen': window.screen}

def join_objects(*objects):
    ctx = get_default_context()
    ctx['object'] = objects[0]
    ctx['active_object'] = objects[0]
    ctx['selected_objects'] = objects
    ctx['selected_editable_objects'] = objects
    bpy.ops.object.join(ctx)

Using get_default_context seems to be working for other bpy.ops operators, too.

This works: ``` def get_default_context(): window = bpy.context.window_manager.windows[0] return {'window': window, 'screen': window.screen} def join_objects(*objects): ctx = get_default_context() ctx['object'] = objects[0] ctx['active_object'] = objects[0] ctx['selected_objects'] = objects ctx['selected_editable_objects'] = objects bpy.ops.object.join(ctx) ``` Using `get_default_context` seems to be working for other `bpy.ops` operators, too.

Added subscriber: @dodododorian

Added subscriber: @dodododorian

anyone here know how to override inside of a timer for drawing something in the bpy.context.window_manager ? i can't get my hand on a code working like you did with bpy.context.object
i think that def draw(self, context): pose also a problem
this is driving me crazy all i want is poping up a message, why does it need to be so complicated

import bpy 

def ShowMessageBox(message = "", title = "Message Box", icon = 'INFO'): #Message function
    def draw(self, context):
        self.layout.label(text=message)
    bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)

def every_X_seconds_word():
    ShowMessageBox("Hello World", "Many Crash" ,"BLENDER")
    return 5.0
bpy.app.timers.register(every_X_seconds_word)
anyone here know how to override inside of a timer for drawing something in the bpy.context.window_manager ? i can't get my hand on a code working like you did with bpy.context.object i think that def `draw(self, context):` pose also a problem this is driving me crazy all i want is poping up a message, why does it need to be so complicated ``` import bpy def ShowMessageBox(message = "", title = "Message Box", icon = 'INFO'): #Message function def draw(self, context): self.layout.label(text=message) bpy.context.window_manager.popup_menu(draw, title = title, icon = icon) def every_X_seconds_word(): ShowMessageBox("Hello World", "Many Crash" ,"BLENDER") return 5.0 bpy.app.timers.register(every_X_seconds_word) ```

Added subscriber: @JoseConseco

Added subscriber: @JoseConseco
Member

Added subscriber: @DanielGrauer

Added subscriber: @DanielGrauer
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
6 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#62051
No description provided.