0
$\begingroup$

In operator class I define:

active_obj : bpy.props.StringProperty()

In panel class I define as below:

 obj = context.object
 col.operator("object.enable", text="",
                        icon=icn, emboss=False).active_obj=obj.name

But when clicking an object in the scene, it shows nothing without the active object name.

$\endgroup$

1 Answer 1

3
$\begingroup$

I have given you 2 ways of doing this. One with string property as you required and the second is the one I like in this situation.

import bpy

class PROPERTIRES(bpy.types.PropertyGroup):
    active_obj : bpy.props.StringProperty(name='Active Object is', default = bpy.context.object.name)

class NEWPANEL(bpy.types.Panel):
    bl_label = "New Panel"
    bl_idname = "NEW_PT_PANEL"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "New Panel"
    
    def draw (self, context):
        layout = self.layout
        row = layout.row()
#       First Way, Your way
        row.prop(bpy.context.scene.my_props, 'active_obj')
#       Second way, I liked with this situation
        layout.label(text= 'Active Object is ' + bpy.context.object.name)


def register():
    bpy.utils.register_class(NEWPANEL)
    bpy.utils.register_class(PROPERTIRES)
    bpy.types.Scene.my_props = bpy.props.PointerProperty(type = PROPERTIRES)
    
def unregister():
    bpy.utils.unregister_class(NEWPANEL)
    bpy.utils.unregister_class(PROPERTIRES)
    del bpy.types.Scene.my_props
    
if __name__ == "__main__":
    register()
```
$\endgroup$
3
  • $\begingroup$ For the 1st way, it pops up an error _RestrictContext object has no attribute object $\endgroup$
    – Derekcbr
    Commented Oct 6, 2022 at 3:27
  • $\begingroup$ You must have at least one object in the scene to run the script. $\endgroup$
    – Muzammil
    Commented Oct 6, 2022 at 3:37
  • $\begingroup$ Got you, thanks! $\endgroup$
    – Derekcbr
    Commented Oct 6, 2022 at 23:23

You must log in to answer this question.

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