2
$\begingroup$

I need to update my text every time I alter my mesh in edit or object mode or if I edit the Tool panel. I use this script:

# [snip]
# line 308
def update_scene(context):
    obj = bpy.context.active_object
    if obj.is_updated or obj.data.is_updated:
        handle_add(self, context)
        print("%s - Object data is_updated (pre)" % obj.data.name)

def handle_add(self, context):
    VIEW3D_OT_ADH_display_text._handle = bpy.types.SpaceView3D.draw_handler_add(
       draw_text_callback, 
       (self, context),
       'WINDOW', 'POST_PIXEL')

def handle_remove(context):
    _handle = VIEW3D_OT_ADH_display_text._handle
    if _handle != None:
        bpy.types.SpaceView3D.draw_handler_remove(_handle, 'WINDOW')
    VIEW3D_OT_ADH_display_text._handle = None


class VIEW3D_OT_ADH_display_text(bpy.types.Operator):
    bl_idname = "view3d.adh_display_text"
    bl_label = "Display text"
    _handle = None

    @classmethod
    def poll(cls, context):
        return update_scene not in bpy.app.handlers.scene_update_post

    def execute(self, context):
        st = context.window_manager.show_text

        handle_add(self, context)
        bpy.app.handlers.scene_update_post.append(update_scene)

        st.enabled = True     

        return{'FINISHED'}

class VIEW3D_OT_ADH_remove_text(bpy.types.Operator):
    bl_idname = "view3d.adh_remove_text"
    bl_label = "Remove text"

    @classmethod
    def poll(cls, context):
        return update_scene in bpy.app.handlers.scene_update_post

    def execute(self, context):
        st = context.window_manager.show_text

        handle_remove(context)
        bpy.app.handlers.scene_update_post.remove(update_scene)

        st.enabled = False

        return {'FINISHED'}

But when I run the script like that, the console shows the following error:

line 312, in update_scene
NameError: name 'self' is not defined

If I add self in the update_scene arguments, another error shows in the console:

TypeError: update_scene() missing 1 required positional argument: 'context'

I have not managed to solve this problem, which is likely an easy fix. How can I do that please ?

$\endgroup$
4
  • $\begingroup$ Nobody can help you if you strip important parts from the script. E.g. I can't see where you define draw_text_callback $\endgroup$ Jun 7, 2015 at 3:52
  • $\begingroup$ Yes, a copy of a fully functional script would be better since we will probably need to run it locally to test it. $\endgroup$ Jun 7, 2015 at 7:20
  • 1
    $\begingroup$ @WorldSEnder it may seem that way, but the problem here is with the logic of adding the handlers. the draw_text_callback function isn't so important. $\endgroup$
    – zeffii
    Jun 7, 2015 at 7:20
  • $\begingroup$ I wanted to see the definition of draw_text_callback to decide if you could safely remove self from its args-list $\endgroup$ Jun 7, 2015 at 14:32

1 Answer 1

3
$\begingroup$

The error NameError: name 'self' is not defined is pretty self explanatory, you are using self as a variable name and it has not been defined at the point you are using it. self is a variable name normally only used within a class definition while you are trying to use it in a module function.

self is normally defined to point to the object that the current function belongs to. In the context that you are trying to use it, it will be the first argument sent to the callback function, meaning it should point to the object that will be handling the callback.

I think to achieve what you want, try adding a variable to the scene object that holds a list of data that draw_text_callback draws (See the example for CollectionProperty), your update_scene maintains the list and draw_text_callback goes through it to draw what it needs to.

The second error is because you can't add another parameter to the function used for scene_update_post as it must match the single argument prototype that is defined to be used.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .