0
$\begingroup$

I'm trying to enable an addon from a basic python template I saved, but it wont let me check the box to activate it. Here's what the problem looks like: https://www.youtube.com/watch?v=tGFOwwfikAA&feature=youtu.be

Is there any reason it's not letting me do this? Thank you very much for your time!

import bpy


def main(context):
    for ob in context.scene.objects:
        print(ob)


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}

bpy.ops.transform.translate(value=(0, 2.20362, 0), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(False, True, False), mirror=True, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True)


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.simple_operator()
$\endgroup$
  • 1
    $\begingroup$ Can you include the code or at least the module registration code in your Question? The error message indicates there is a context mismatch i.e. you are doing something in the registration of the module that you should not be. $\endgroup$ – rob Apr 9 at 11:17
  • $\begingroup$ Sorry for the ugly formatting. I can't seem to figure out how to put the code in proper format from the 'add-comment' button. Here is the code again by itself. $\endgroup$ – GoodNight Apr 9 at 18:04
  • $\begingroup$ bpy.ops.transform.translate(value=(0, -3.05949, 0), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(False, True, False), mirror=True, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True) $\endgroup$ – GoodNight Apr 9 at 18:04
  • $\begingroup$ It's just code I got from the info window to move the cube left a bit. $\endgroup$ – GoodNight Apr 9 at 18:05
  • 1
    $\begingroup$ Edit your original question and paste in your Python then select what you have added and click the button with the two curly braces, this will format the display as code. pastebin.com is a free web site where you can paste in code and have it formatted, all without having to signup $\endgroup$ – rob Apr 16 at 11:05
1
$\begingroup$

You have put your operational code in as part of the Class SimpleOperator.

This means that when the register creates the Class your code is run, which is the wrong time and has no context.

Take the bpy.ops.transform.translate code and move it out of the Class definition and move it into the main function e.g.

def main(context):
    for ob in context.scene.objects:
        print(ob)
    bpy.ops.transform.translate(value=(0, 2.20362, 0), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(False, True, False), mirror=True, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True)

Now when you run the test call

bpy.ops.object.simple_operator()

It will print out all the Object names and then try and Translate the currently selected Object

$\endgroup$
  • 1
    $\begingroup$ Worth noting, can ditch the main method all together and simply return bpy.ops.transform.translate(...) as a one line execute method. Also the naming convention for 2.8x would suggest this class be defined as class OBJECT_OT_simple_operator(...) $\endgroup$ – batFINGER Apr 18 at 16:19

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.