0
$\begingroup$

I need to access the string in 'my_type' string property which reside in the class Append_OT_Operator from the class Apply_OT_Operator. How can I make this 'my_type' a variable that I can access globally? Or is there a better way?

This is my code in my_operators.py:

class Append_OT_Operator(bpy.types.Operator):
    bl_idname = "wm.appending_basic_objects"
    bl_label = "Append the basic objects"

    my_type : bpy.props.StringProperty()

    def execute(self, context):
        path = (r'C:/Users/')
        blendfile = "Assets" + ".blend"

        bpy.ops.wm.append(
            filepath = blendfile,
            directory = path + '/' + blendfile + "/Object/",
            filename = self.my_type)   

        return {'FINISHED'}

class Apply_OT_Operator(bpy.types.Operator):
    bl_idname = "wm.apply"
    bl_label = "Apply most modifiers for UV unwrapping phase"

    def execute(self, context):

        print(my_type) #return an error, instead of printing my_type string

To clarify, 'my_type' is set when the user click on a button of the UI in my ui_panel.py:

class UI_PT_Panel(bpy.types.Panel):
    bl_idname = "UI_PT_Panel"
    bl_label = "Bijoux Panel"
    bl_category = "Bijoux Addon"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout

        row = layout.row()
        row.operator('wm.appending_basic_objects', text = "append my_object1").my_type = 'my_object1'

        row = layout.row()
        row.operator('wm.appending_basic_objects', text = "append my_object2").my_type = 'my_object2'
$\endgroup$
0
3
$\begingroup$

Window Manager keeps track of registered operators

Lets say we add a property to the simple operator template, and make it register - undo

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    bl_options = {'REGISTER', 'UNDO'}
    xxx : bpy.props.StringProperty(default="foo")

Invoke it from the UI

bl_options = {'REGISTER', 'UNDO'}

Then in python console use WindowManager.operator_properties_last

>>> op = C.window_manager.operator_properties_last("object.simple_operator")
>>> op.xxx
'ggggg'

Note the template class name is SimpleOperator but really should be OBJECT_OT_simple_operator to match its id name "object.simple_operator" as per the naming convention

Store in blend objects

Alternatively store properties on ID objects in blender, eg scene, object, mesh etc. Scene is often used as a "global".

from bpy.props import StringProperty

bpy.types.Scene.xxx = StringProperty()

set in an operator with

context.scene.xxx = self.my_string

retrieve with

print(context.scene.xxx)

layout with

layout.prop(context.scene, "xxx")
$\endgroup$
6
  • $\begingroup$ @RobertGützkow Works without. Test with simple operator template. $\endgroup$ – batFINGER Nov 28 '19 at 15:35
  • $\begingroup$ I've tested it and only C.window_manager.operator_properties_last("object.simple_operator") works properly. C.window_manager.operators['OBJECT_OT_simple_operator'] fails without UNDO. I think managing the state in the add-on yourself may be the cleaner solution. $\endgroup$ – Robert Gützkow Nov 28 '19 at 15:44
  • $\begingroup$ Hi, thank you for your answer, isn't there a more straightforward way? It feels like more of a workaround to my problem than a clean solution. Is there no way to declare a variable that is global to the whole addon? Also I'm really not sure how to include your solution to my code, you are talking about printing and using in layout and invoking from UI but this are all things that aren't connected to my questions from what I understand. Could be wrong though, if you could clarify/simplify your answer it would really help. Thanks again $\endgroup$ – Arthur Blaquart Nov 29 '19 at 10:54
  • $\begingroup$ Could be. I believe they are all connected to the question. Hence posting as an answer. Pressing a button is "invoking from UI". Used print as an example akn to print(my_type) in question. A panel or operator has layout in its draw method. Re simple global eg adding a scene property, Seen in answer here too. blender.stackexchange.com/questions/132944/… you may wsh to draw it in a panel. $\endgroup$ – batFINGER Nov 29 '19 at 11:50
  • $\begingroup$ I tried the first one-liner (bpy.types.Scene.xxx = StringProperty() ) in the console and get "NameError: name 'StringPropety' is not defined". $\endgroup$ – Tango Feb 22 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.