Can't access actually used values of macro operator properties from Python #40762

Closed
opened 2014-06-23 02:22:01 +02:00 by CodeManX · 24 comments
Member

System Information
Windows 7, 64bit

Blender Version
Broken: 2.71 RC2

Short description of error
Logged operators in WindowManager.operators also include Macros, but if their arguments are retrieved, they are all at their default values.

Exact steps for others to reproduce the error

  • Go to Scripting view
  • Shift + D to duplicate linked
  • Paste and Run script:
import bpy
from collections import Iterable
from mathutils import Vector, Matrix
from _bpy import StructMetaPropGroup

def main():
    for op in bpy.context.window_manager.operators:
        cmd = format_logged_op(op)
        print(cmd)

def format_logged_op(op):
    tmp = []
    for k in op.properties.keys():
        p = getattr(op.properties, k)
        if isinstance(type(p), StructMetaPropGroup):
            props = format_macro_props(p, p.bl_rna.properties.keys())
            tmp.append("%s={%s}" % (p.bl_rna.identifier, props))
        else:
            tmp.append(format_key_value(k, p))

    return "bpy.ops.%s(%s)" % (format_idname(op), ", ".join(tmp))

def format_macro_props(prop, keys):
    tmp = []
    for k in keys:
        if k == "rna_type":
            continue
        p = getattr(prop, k)
        tmp.append(format_key_value(k, p, fmt='"%s": %r'))
    return ", ".join(tmp)
    
def format_key_value(k, p, fmt="%s=%r"):
    if isinstance(p, Vector):
        p = p.to_tuple()
    elif isinstance(p, Matrix):
        p = "(%s)" % ", ".join(repr(vec.to_tuple()) for vec in p)
    elif not isinstance(p, str) and isinstance(p, Iterable):
        p = p[:]
    return fmt % (k, p)


def format_idname(op):
    return ".".join(op.bl_idname.split("_OT_", maxsplit=1)).lower()
    
main()

It will print


bpy.ops.mesh.duplicate_move(MESH_OT_duplicate={"mode":1}, TRANSFORM_OT_translate
={"value":(0.0, 0.0, 0.0), "constraint_axis":(False, False, False), "constraint_
orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_
edit_falloff":'SMOOTH', "proportional_size":1.0, "snap":False, "snap_target":'CL
OSEST', "snap_point":(0.0, 0.0, 0.0), "snap_align":False, "snap_normal":(0.0, 0.

0, 0.0), "texture_space":False, "remove_on_cancel":False, "release_confirm":Fals

e})

so everything False, 0.0 or whatever is default...

**System Information** Windows 7, 64bit **Blender Version** Broken: 2.71 RC2 **Short description of error** Logged operators in `WindowManager.operators` also include Macros, but if their arguments are retrieved, they are all at their default values. **Exact steps for others to reproduce the error** - Go to Scripting view - Shift + D to duplicate linked - Paste and Run script: ``` import bpy from collections import Iterable from mathutils import Vector, Matrix from _bpy import StructMetaPropGroup def main(): for op in bpy.context.window_manager.operators: cmd = format_logged_op(op) print(cmd) def format_logged_op(op): tmp = [] for k in op.properties.keys(): p = getattr(op.properties, k) if isinstance(type(p), StructMetaPropGroup): props = format_macro_props(p, p.bl_rna.properties.keys()) tmp.append("%s={%s}" % (p.bl_rna.identifier, props)) else: tmp.append(format_key_value(k, p)) return "bpy.ops.%s(%s)" % (format_idname(op), ", ".join(tmp)) def format_macro_props(prop, keys): tmp = [] for k in keys: if k == "rna_type": continue p = getattr(prop, k) tmp.append(format_key_value(k, p, fmt='"%s": %r')) return ", ".join(tmp) def format_key_value(k, p, fmt="%s=%r"): if isinstance(p, Vector): p = p.to_tuple() elif isinstance(p, Matrix): p = "(%s)" % ", ".join(repr(vec.to_tuple()) for vec in p) elif not isinstance(p, str) and isinstance(p, Iterable): p = p[:] return fmt % (k, p) def format_idname(op): return ".".join(op.bl_idname.split("_OT_", maxsplit=1)).lower() main() ``` It will print ``` bpy.ops.mesh.duplicate_move(MESH_OT_duplicate={"mode":1}, TRANSFORM_OT_translate ={"value":(0.0, 0.0, 0.0), "constraint_axis":(False, False, False), "constraint_ orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_ edit_falloff":'SMOOTH', "proportional_size":1.0, "snap":False, "snap_target":'CL OSEST', "snap_point":(0.0, 0.0, 0.0), "snap_align":False, "snap_normal":(0.0, 0. ``` 0, 0.0), "texture_space":False, "remove_on_cancel":False, "release_confirm":Fals ``` e}) ``` so everything False, 0.0 or whatever is default...
Author
Member

Changed status to: 'Open'

Changed status to: 'Open'
Author
Member

Added subscriber: @CodeManX

Added subscriber: @CodeManX
Member

Added subscribers: @ideasman42, @LukasTonne

Added subscribers: @ideasman42, @LukasTonne
Member

I may be wrong, but IIRC macro operators in fact don't have properties of their own (or at least they don't inherit properties from nested operators by default). Including nested operator properties in the macro's dict would be dangerous. How would you handle name conflicts etc.?

If anything, the nested operators' properties should be accessible as a collection inside the macro (something like window_manager.operators[-1].macro- [x].properties). This is actually how properties are stored for macros and how the redo panel accesses and displays them. I don't know why these are not exposed already in the RNA, maybe @ideasman42 can say?

About dynamic enums: Yes, this is annoying ... In principle dynamic enums can depend on context even, but in most cases they're just used to select a fixed list based on some other property. There are classmethods in UILayout which could be used for resolving enum names, descriptions and icons, but not for mapping index <-> identifier. Something like this would be very handy (if it doesn't exist somewhere else already?).

I may be wrong, but IIRC macro operators in fact don't have properties of their own (or at least they don't inherit properties from nested operators by default). Including nested operator properties in the macro's dict would be dangerous. How would you handle name conflicts etc.? If anything, the nested operators' properties should be accessible as a collection inside the macro (something like `window_manager.operators[-1].macro- [x].properties`). This is actually how properties are stored for macros and how the redo panel accesses and displays them. I don't know why these are not exposed already in the RNA, maybe @ideasman42 can say? About dynamic enums: Yes, this is annoying ... In principle dynamic enums can depend on context even, but in most cases they're just used to select a fixed list based on some other property. There are classmethods in UILayout which could be used for resolving enum names, descriptions and icons, but not for mapping index <-> identifier. Something like this would be very handy (if it doesn't exist somewhere else already?).
iPLEOMAX commented 2014-06-23 12:38:02 +02:00 (Migrated from localhost:3001)

Added subscriber: @iPLEOMAX

Added subscriber: @iPLEOMAX
iPLEOMAX commented 2014-06-23 12:38:02 +02:00 (Migrated from localhost:3001)

Use attributes instead of dictionary keys.

bpy.context.window_manager.operators[-1].properties['constraint_orientation']

bpy.context.window_manager.operators[-1].properties.constraint_orientation

The dict keys() can be used to getattr()

Use attributes instead of dictionary keys. bpy.context.window_manager.operators[-1].properties['constraint_orientation'] - > bpy.context.window_manager.operators[-1].properties.constraint_orientation The dict keys() can be used to getattr()
Member

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Lukas Tönne self-assigned this 2014-06-23 14:18:39 +02:00
Member

Ah yes, @iPLEOMAX is right. The nested operator properties can be accessed as attributes. To get all their properties you could use the RNA type info:

props = bpy.context.window_manager.operators[-1].properties.OBJECT_OT_duplicate.bl_rna.properties.keys()

Closing this report.

Ah yes, @iPLEOMAX is right. The nested operator properties can be accessed as attributes. To get all their properties you could use the RNA type info: ``` props = bpy.context.window_manager.operators[-1].properties.OBJECT_OT_duplicate.bl_rna.properties.keys() ``` Closing this report.
CodeManX changed title from WindowManager.operators does not resolve macro properties to Can't access actually used values of macro operator properties from Python 2014-06-24 21:31:01 +02:00
Author
Member

Changed status from 'Archived' to: 'Open'

Changed status from 'Archived' to: 'Open'
CodeManX reopened this issue 2014-06-24 21:31:01 +02:00
Author
Member
Re-opened, because macro operator arguments can't be accessed. http://blenderartists.org/forum/showthread.php?340868-Get-lines-from-INFO-area&p=2674292&viewfull=1#post2674292

Added subscriber: @mont29

Added subscriber: @mont29

Yeah… I had to fight that issue in C as well, to allow the log/info window to show valid parameters in this case too, some time ago… Sounds more like a TODO to me, tbh.

Yeah… I had to fight that issue in C as well, to allow the log/info window to show valid parameters in this case too, some time ago… Sounds more like a TODO to me, tbh.
Lukas Tönne removed their assignment 2014-07-23 15:51:27 +02:00
Member

IDK what the problem here is actually ...

IDK what the problem here is actually ...
Campbell Barton was assigned by Sergey Sharybin 2014-07-24 12:59:07 +02:00

Added subscriber: @Sergey

Added subscriber: @Sergey

@ideasman42, not sure what's the design specification here, mind having a look?

@ideasman42, not sure what's the design specification here, mind having a look?

Confirmed. but changing Py/RNA API right before release is too risky (unless its obvious fix for mistake)

Confirmed. but changing Py/RNA API right before release is too risky (unless its obvious fix for mistake)
Member

Added subscriber: @JulianEisel

Added subscriber: @JulianEisel
Member

Hmm, any news here?

Hmm, any news here?
Member

Asking again ;)

Asking again ;)

This issue was referenced by blender/blender@3160740421

This issue was referenced by blender/blender@3160740421cfef1cee61478baadddd256af2dfca

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'

Closed by commit blender/blender@3160740421.

Closed by commit blender/blender@3160740421.

Expose via operator.macros, a list of macros whos properties you can access.

This is a bit awkward but its how the data is stored internally.

Expose via operator.macros, a list of macros whos properties you can access. This is a bit awkward but its how the data is stored internally.
Author
Member

Better silly than sorry ;-)

Better silly than sorry ;-)
Sign in to join this conversation.
No Milestone
No project
No Assignees
7 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-addons#40762
No description provided.