0
$\begingroup$

So I found this example from another answer here. I'm new to programing and don't fully understand how it all flows. I've only used "def execute" to run stuff. I would like to create a class that creates a popup menu. I would like the popup to change the selected object when I adjust variable within it (without hitting okay). Is it even possible? I defined "athing" below to test. I want it to go to edit mode, select everything and divide. It says it can't modify blend data in this state. Thank you for the help.

class DialogOperator(bpy.types.Operator):
bl_idname = "object.dialog_operator"
bl_label = "Simple Dialog Operator"

my_float = bpy.props.FloatProperty(name="Some Floating Point")
my_bool = bpy.props.BoolProperty(name="Toggle Option")
my_string = bpy.props.StringProperty(name="String Value")

def execute(self, context):
    message = "Popup Values: %f, %d, '%s'" % \
        (self.my_float, self.my_bool, self.my_string)
    self.report({'INFO'}, message)
    return {'FINISHED'}

def check(self, context):
    return True

def athing(self, context):
    bpy.ops.object.mode_set(mode = 'EDIT')  
    bpy.ops.mesh.select_all(action='SELECT')

    #SUBDIVED VIA "subs" INPUT
    bpy.ops.mesh.subdivide(smoothness=0)
    return {'FINISHED'}

def draw(self, context):
    layout = self.layout
    layout.prop(self, "my_bool")
    if self.my_bool:
        layout.label("It's TRUE")
        layout.prop(self, "my_string")
        self.athing(self)

def invoke(self, context, event):
    wm = context.window_manager
    return wm.invoke_props_dialog(self)
$\endgroup$
3
$\begingroup$

Use an update method on a property.

Give that whole "athing" thing away. You CANNOT use draw methods to run code to update objects. (The you cannot modify blenddata in this state error)

You can define a button to click that calls an operator, or a property that when changed runs an update. .. which I believe is what you want update method on properties Example below, on changing my_bool if True will call the athing(self, context) method. self will be a reference tot the operator.

import bpy
class DialogOperator(bpy.types.Operator):
    bl_idname = "object.dialog_operator"
    bl_label = "Simple Dialog Operator"

    def athing(self, context):
        if self.my_bool:
            bpy.ops.object.mode_set(mode = 'EDIT')  
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.mesh.subdivide(smoothness=0)
        return None

    my_float = bpy.props.FloatProperty(name="Some Floating Point")
    my_bool = bpy.props.BoolProperty(name="Toggle Option", update=athing)
    my_string = bpy.props.StringProperty(name="String Value")

    def execute(self, context):
        message = "Popup Values: %f, %d, '%s'" % \
            (self.my_float, self.my_bool, self.my_string)
        self.report({'INFO'}, message)
        return {'FINISHED'}

    def check(self, context):
        return True

    def draw(self, context):
        layout = self.layout
        layout.prop(self, "my_bool")


    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self)

bpy.utils.register_class(DialogOperator)
# test call
bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
$\endgroup$
  • $\begingroup$ Thank you batFINGER! This has been tormenting me for so long. I understand this so much better now. :) $\endgroup$ – admbro Oct 13 '17 at 21:53

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.