Dynamic PropertyGroup classes no longer possible in 2.93 #86719

Closed
opened 2021-03-18 22:07:03 +01:00 by Brian Savery (AMD) · 14 comments

System Information
Operating system: *
Graphics card: *

Blender Version
Broken: Blender 2.93 (after https://developer.blender.org/rBc44c611c6d8c6ae071b48efb5fc07168f18cd17e)
Worked: (newest version of Blender that worked as expected)

Short description of error
This is somewhat rare, but IMO perfectly valid Python use case of dynamically creating a new class and then wanting to register it. That is, we want to dynamically subtype a PropertyGroup class, add some properties to it, and then register the class

One example of "why", is for example MaterialX gives us a whole bunch of shader node types specified in xml files that we would like to create custom node types from, so we write a small script to parse the XML and create node classes on the fly.

Exact steps for others to reproduce the error

import bpy

data = {
             'bl_label': "MyNewType",
             'bl_idname': "test.MyNewType",
             '__annotations__': {
                "MyIntVal": (bpy.props.IntProperty, {'name': "MyIntVal", 'default': 42})
             }
         }

my_new_type = type("MyNewType", (bpy.types.PropertyGroup,), data)

bpy.utils.register_class(my_new_type)

bpy.types.Object.my_prop_grp = bpy.props.PointerProperty(type=my_new_type)
print(bpy.data.objects[0].my_prop_grp.MyIntVal)

also attached a blend file with the above script. In 2.93 does not seem like it registers the int property.example.blend. I'm tagging @ideasman42 in this because it seems related to the DeferredProperty when we inspect deeper. If there is another way to achieve creating the properties for these dynamic types let me know.

**System Information** Operating system: * Graphics card: * **Blender Version** Broken: Blender 2.93 (after https://developer.blender.org/rBc44c611c6d8c6ae071b48efb5fc07168f18cd17e) Worked: (newest version of Blender that worked as expected) **Short description of error** This is somewhat rare, but IMO perfectly valid Python use case of dynamically creating a new class and then wanting to register it. That is, we want to dynamically subtype a PropertyGroup class, add some properties to it, and then register the class One example of "why", is for example MaterialX gives us a whole bunch of shader node types specified in xml files that we would like to create custom node types from, so we write a small script to parse the XML and create node classes on the fly. **Exact steps for others to reproduce the error** ``` import bpy data = { 'bl_label': "MyNewType", 'bl_idname': "test.MyNewType", '__annotations__': { "MyIntVal": (bpy.props.IntProperty, {'name': "MyIntVal", 'default': 42}) } } my_new_type = type("MyNewType", (bpy.types.PropertyGroup,), data) bpy.utils.register_class(my_new_type) bpy.types.Object.my_prop_grp = bpy.props.PointerProperty(type=my_new_type) print(bpy.data.objects[0].my_prop_grp.MyIntVal) ``` also attached a blend file with the above script. In 2.93 does not seem like it registers the int property.[example.blend](https://archive.blender.org/developer/F9898605/example.blend). I'm tagging @ideasman42 in this because it seems related to the DeferredProperty when we inspect deeper. If there is another way to achieve creating the properties for these dynamic types let me know.
Author
Member

Added subscribers: @ideasman42, @BrianSavery

Added subscribers: @ideasman42, @BrianSavery

Added subscriber: @bnagirniak

Added subscriber: @bnagirniak

Added subscriber: @PhilippeCrassous

Added subscriber: @PhilippeCrassous

Added subscriber: @AquaticNightmare

Added subscriber: @AquaticNightmare

Added subscriber: @karlisup

Added subscriber: @karlisup

Added subscriber: @JoshuaKnauber

Added subscriber: @JoshuaKnauber

Added subscriber: @RayMairlot

Added subscriber: @RayMairlot
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Member

Looks like a valid usecase, but not sure how to classify.
Will confirm for now though and even dare setting this to High priority -- since I would not be aware of a way around this "new limitation/regression" for now.

Looks like a valid usecase, but not sure how to classify. Will confirm for now though and even dare setting this to High priority -- since I would not be aware of a way around this "new limitation/regression" for now.

Changed status from 'Confirmed' to: 'Archived'

Changed status from 'Confirmed' to: 'Archived'

Creating classes dynamically is still supported. The example given relied on an internal representation of properties which was never documented or intended for script authors to depend on.

This works as expected:

import bpy

data = {
    'bl_label': "MyNewType",
    'bl_idname': "test.MyNewType",
    '__annotations__': {
        "MyIntVal": bpy.props.IntProperty(name="MyIntVal",default=42),
    }
}

my_new_type = type("MyNewType", (bpy.types.PropertyGroup,), data)

bpy.utils.register_class(my_new_type)

bpy.types.Object.my_prop_grp = bpy.props.PointerProperty(type=my_new_type)
print(bpy.data.objects[0].my_prop_grp.MyIntVal)
Creating classes dynamically is still supported. The example given relied on an internal representation of properties which was never documented or intended for script authors to depend on. This works as expected: ``` import bpy data = { 'bl_label': "MyNewType", 'bl_idname': "test.MyNewType", '__annotations__': { "MyIntVal": bpy.props.IntProperty(name="MyIntVal",default=42), } } my_new_type = type("MyNewType", (bpy.types.PropertyGroup,), data) bpy.utils.register_class(my_new_type) bpy.types.Object.my_prop_grp = bpy.props.PointerProperty(type=my_new_type) print(bpy.data.objects[0].my_prop_grp.MyIntVal) ```
Member

Ah, good to know, thx @ideasman42

Ah, good to know, thx @ideasman42

This example is working, thanks!

This example is working, thanks!
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
9 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#86719
No description provided.