Problem with custom properties

Hi,
I spent the large part of the weekend trying to advance my add on, but sadly things are not going as smooth as I expect.
To better explain my problem here is a stripped down version of the script.

bl_info = {
	"name": "Newton Physics",
	"category": "Physics",
	"author": "Newton Dynamics",
	"version": (1, 0, 0),
	"blender": (2, 93, 0),
	#"location": "Properties > Scene > Blend Info panel",
    "location": "3D Viewport > Sidebar > Item tab",
	"description": "Add Newton Physics to meshes",
}

import os
import sys
import bpy

blenderPath = os.getenv('Blender') + '/scripts/addons/newtonPy'
print (blenderPath)

if not blenderPath in sys.path:
	sys.path.append(blenderPath)

import newton
import newtonWorld

class NewtonWorldCreate(bpy.types.Operator):
    """Creates a newton world"""
    bl_idname = 'view3d.newton_world_create'
    bl_label = 'create newton world'

    def execute(self, context):
        scene = context.scene
        #scene.newton_world = newtonWorld.NewtonWorld()
        scene.newton_world = bpy.data.objects.new('newton_world', None) 
        #scene.objects.link(scene.newton_world)
        return {'FINISHED'}

class NewtonWorldDestroy(bpy.types.Operator):
    """Destroy a newton world"""
    bl_idname = 'view3d.newton_world_destroy'
    bl_label = 'delete newton world'

    def execute(self, context):
        scene = context.scene

        #scene.objects.unlink(scene.newton_world)
        bpy.data.objects.remove(scene.newton_world) 
        scene.newton_world = None
        return {'FINISHED'}

class NewtonWorldPanel(bpy.types.Panel):
    bl_label = "Newton Physics"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_category = "scene"

    def draw(self, context):
        scene = context.scene
        layout = self.layout
        #layout.prop(context.scene, "newton_world")
        #layout.prop(context.scene, "newton_world", text = "xxxxx")
        
        world = scene.newton_world
        if world is None:
            layout.operator("view3d.newton_world_create")
        else:
            layout.operator("view3d.newton_world_destroy")

            #list engine parameters
            layout.label(text="Engine Configuration")
            row = layout.row()
            row.prop(world, "name")
            #row.prop(scene, "substeps")
            #row.prop(scene, "iterations")
            #row.alignment = 'RIGHT'



# register classes
classes = [
    NewtonWorldCreate,
    NewtonWorldDestroy,
    NewtonWorldPanel
]

def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.newton_world = bpy.props.PointerProperty(
		type = bpy.types.Object,
		name = "newton world",
	)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()

basically I am trying to Create a system that will add properties to mesh in a scene.
These objects will be processed by a global manager, when running the animation.
I am not at that point yet, I am at the stage of making the global manager.

My idea is to add the manager as a Custom scene property.
looking around of the many classes that can be custom property, it seems that only the Object is accepted. This is done in the last line

    bpy.types.Scene.newton_world = bpy.props.PointerProperty(
		type = bpy.types.Object,
		name = "newton world",
	)

My first question is, I want to add an object to the scene, that can have an update function that is called in each animation step.
I am ok with making an object, but after that I do not really see how I can add members to that object.
I try sub classing it, but that does but that required registering the class and that failed.

I look at some of the other subclass of bpy_struct, Object, ObjectBase and try to subclass form those like this

#class TestManager(bpy.types.bpy_struct):
class TestManager(bpy.types.ObjectBase):
“”“create and interface to the newton workd”""

but I get this error when I try to register the class.

  File "\ndbpy.py", line 117, in <module>
    register()
  File "\ndbpy.py", line 104, in register
    bpy.utils.register_class(cls)
ValueError: register_class(...): expected a subclass of a registerable RNA type (ObjectBase does not support registration)
Error: Python script failed, check the message in the system console

Am I doing this all wrong? I am really stuck here because, I have not idea how to make a class that I can either subclass from one of base classes or add members.

well,
it seems no one either knows an answer or suggestions, or will bother answer.
And now it seems I did something wrong or something that bothered some one since I can’t even post a different question.
I deleted the post of my insights in figuring out this out, and I am narrowing my question to this simple one. “why I can’t rename a created object from an operator.?”
I am sorry if I broke any forum rule, it was not my intension.
but here is the operator in case any one wants to provide an answer.
if not, that is also ok, and I will not bother anymore with questions.

class NewtonWorldCreateHomeObject(bpy.types.Operator):
    """Creates a newton world home"""
    bl_label = 'create newton world home'
    bl_idname = 'view3d.newton_world_create_home'
    bl_description = "create newton world home"

    def execute(self, context):
        #scene = context.scene
        obj = bpy.ops.mesh.primitive_cube_add(size=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
        obj.name = 'newtonHome'
        return {'FINISHED'}

but it fail with this error.

Error: Python: Traceback (most recent call last):
File “C:\Program Files\Blender Foundation\Blender 2.93\2.93/scripts/addons/newtonPy\newtonWorld.py”, line 36, in execute
obj.name = ‘newtonHome’
AttributeError: ‘set’ object has no attribute ‘name’

location: :-1

I am coping this literally from the Blender Log window. and I even done it to a object that is already in the scene. so why it fails when I made the object?

bpy.ops.mesh.primitive_cube_add doesn’t return an object, you need to get a reference in another way.

Mesh Operators — Blender Python API

ah very good, changed to this,

    def execute(self, context):
        scene = context.scene
        bpy.ops.mesh.primitive_cube_add(size=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
        context.active_object.name = 'newtonHome'
        return {'FINISHED'}

and is works now.
thank you very much.

actually it is partially working.

If I run the script from the script windows, is does make a new cube, and rename it.
but if instead sfter loading the script, I switch to the modeling window and apply the operatior.

some how the creation function fail and it simply uses the selected cube that was already in the scene.
The docs say that a created object is selected by default, so this new error does even make sence because after that operation returns I do an exhaustive search for that object ‘newtonHome’ before I apply the operator that changes it to my manager.

here is the panel code,

class NewtonWorldPanel(bpy.types.Panel):
    bl_label = "Newton Physics"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "newton"

    def findHome(self, scene):
        col = bpy.data.collections.get("Collection")
        if col:
            for obj in col.objects:
                if obj.name == 'newtonHome':
                    return obj
        return None
    
    def draw(self, context):
        scene = context.scene
        layout = self.layout
        
        world = scene.newton_world
        if world is None:
            newtonHome = self.findHome(scene)
            if newtonHome is None:
                layout.operator("view3d.newton_world_create_home")

            print (context.active_object.name)
            layout.operator("view3d.newton_world_create")
        else:
            layout.operator("view3d.newton_world_destroy")

            #list engine parameters
            layout.label(text="Engine Configuration")
            row = layout.row()
            row.prop(world, "name")
            #row.prop(scene, "substeps")
            #row.prop(scene, "iterations")
            #row.alignment = 'RIGHT'

and these are the three operators

class TestManager(bpy.types.Object):
    """create and interface to the newton workd"""

    def __init__(self, object):
        #print ("crate managet one once")
        self.name = 'newton_world'


class NewtonWorldCreateHomeObject(bpy.types.Operator):
    """Creates a newton world home"""
    bl_label = 'create newton world home'
    bl_idname = 'view3d.newton_world_create_home'
    bl_description = "create newton world home"

    def execute(self, context):
        scene = context.scene
        bpy.ops.mesh.primitive_cube_add(size=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
        context.active_object.name = 'newtonHome'
        return {'FINISHED'}

class NewtonWorldCreate(bpy.types.Operator):
    """Creates a newton world"""
    bl_label = 'create newton world'
    bl_idname = 'view3d.newton_world_create'
    bl_description = "create a newton world"

    def execute(self, context):
        scene = context.scene

        # this does not works.
        #scene.newton_world = bpy.data.objects.new('newton_world', None) 
        scene.newton_world = TestManager(context.active_object)
        return {'FINISHED'}

class NewtonWorldDestroy(bpy.types.Operator):
    """Destroy a newton world"""
    bl_label = 'delete newton world'
    bl_idname = 'view3d.newton_world_destroy'
    bl_description = "destroy a newton world"

    def execute(self, context):
        scene = context.scene

        scene.newton_world.name = 'newtonHome'
        scene.newton_world = None
        return {'FINISHED'}

again this works as long as I run it from the Script editor, but fail every where else.

here is another detail, I modify the draw function to be like this

   def draw(self, context):
        scene = context.scene
        layout = self.layout
        
        world = scene.newton_world
        if world is None:
            newtonHome = self.findHome(scene)
            if newtonHome is None:
                layout.operator("view3d.newton_world_create_home")
                newtonHome = self.findHome(scene)
                print (newtonHome.name)
            if newtonHome:
                layout.operator("view3d.newton_world_create")
        else:
             ...
              ...

and when I running from the Modeling window afte I load the script It gives me this error.

location: <unknown location>:-1
Python: Traceback (most recent call last):
  File "\ndbpy.py", line 61, in draw
    print (newtonHome.name)
AttributeError: 'NoneType' object has no attribute 'name'

location: <unknown location>:-1

so some how the command bellow only seems to work when called from the script window.

    bpy.ops.mesh.primitive_cube_add(size=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))

Ah, I see what is going on.
Somehow when switching from the modeling window to the script window and back.
Blender changes the modeling window to ‘edit mode’
And order that mode it is creation of an object is not allowed.

I guess that makes sence and it is acceptable.

Question is why blender changes the modeling window mode when switching tabs. But that’s not really my problem.

This is now working.
Thanks for the help.
Julio

it’s a per-workspace option:
image

pretty logical when you think about it- when you click on the ‘sculpt’ tab chances are you want to be in “sculpt” mode, etc.