0
$\begingroup$

I made a small modal operator that prints the name of the object I select. however each selection made is one object behind.

example:

select object 1 then select object 2. when object 2 is selected it will print the name of object 1.

code:

# Import
import bpy

# Properties
class Blank(bpy.types.Operator):
    bl_label = "Blank"
    bl_idname = "blank.example"

    # TESTING ##################################################################
    def modal(self, context, event):
        if (event.type in 'LEFTMOUSE' and event.value == 'RELEASE'):
            # name = bpy.context.active_object.name
            name = bpy.context.object.name
            print(name)
        if event.type == 'ESC':
            return{'CANCELLED'}
        return{'PASS_THROUGH'}

    def invoke(self, context, event):
        context.window_manager.modal_handler_add(self)
        return{'RUNNING_MODAL'}
    ############################################################################


# Menu on the Sidebar
class Blank_PT_Panel(bpy.types.Panel):
    bl_label = "Panel"
    bl_idname = "OBJECT_PT_Blank"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Blank"
    bl_context = "objectmode"

    def draw(self, context):
        layout = self.layout
        # Button
        row = layout.row(align=True)
        row.operator("blank.example", text="Button", icon="QUESTION")

# Registration
classes = (
    Blank_PT_Panel,
    Blank,
    )
def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)
def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
if __name__ == "__main__":
    register()

How do you make the selection display the name of the object without any delay.

P.S. Code edited to run from the Blender Text Editor as requested. it creates a tab called blank on the Sidebar(N-menu) and a single button inside. Press it to run the code in question.

$\endgroup$
4
  • 1
    $\begingroup$ I think you're using the same click function as the selection. The change in active object is not registered until the mouse has been released, so my best guess is: while Blender is still changing the active_object, your script already prints it, it meaning the last active object. $\endgroup$ Commented Aug 7, 2020 at 11:38
  • 2
    $\begingroup$ Am of the opinion if you add the 10 or so lines required to make a working example that can be copy > paste > run in blender, will increase the chances of an answer. $\endgroup$
    – batFINGER
    Commented Aug 7, 2020 at 13:09
  • 1
    $\begingroup$ I have no delay when calling your Operator, it prints exactly currently selected object's name. Blender 2.83.3, Windows 10. $\endgroup$ Commented Aug 8, 2020 at 8:27
  • $\begingroup$ FrederikSteinmetz I choose the button release to try and fight it but has no effect, is there another click I can choose? batFINGER edited the code to do that as requested. AndreySokolov it prints the name of what you selected? but that is not what I get ... I always get the previous confusion I have also windows 10 and 2.83.1 I will try and update. $\endgroup$ Commented Aug 8, 2020 at 22:35

1 Answer 1

0
$\begingroup$

I was poking around and I think I found a solution:

# Import
import bpy
from bpy.props import StringProperty

# Properties
class Blank(bpy.types.Operator):
    bl_label = "Blank"
    bl_idname = "blank.example"

    current: StringProperty()
    last: StringProperty()

    # TESTING ##################################################################
    def modal(self, context, event):
        current = bpy.context.active_object.name
        if event.type == 'MOUSEMOVE' and current != self.last:
            name = bpy.context.active_object.name
            print(name)
        if event.type == 'ESC':
            print("CANCELLED")
            return{'CANCELLED'}
        self.last = bpy.context.active_object.name
        return{'PASS_THROUGH'}
    def invoke(self, context, event):
        context.window_manager.modal_handler_add(self)
        self.last = bpy.context.active_object.name
        return{'RUNNING_MODAL'}
    ############################################################################


# Menu on the Sidebar
class Blank_PT_Panel(bpy.types.Panel):
    bl_label = "Panel"
    bl_idname = "OBJECT_PT_Blank"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Blank"
    bl_context = "objectmode"

    def draw(self, context):
        layout = self.layout
        # Button
        row = layout.row(align=True)
        row.operator("blank.example", text="Button", icon="QUESTION")

# Registration
classes = (
    Blank_PT_Panel,
    Blank,
    )
def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)
def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .