2
$\begingroup$

is it possible to check a script status running by Text Editor 'Run Script' or running as Addon? I wrote a Script to manipulate Vertices, and the script adds an menu entry. For testing this could be very helpful to avoid tons of menu entrys.

def register():
bpy.utils.register_module(__name__)
# the hypothetical request:
if script runasAddon: # (or something like that)
    bpy.types.VIEW3D_MT_edit_mesh_vertices.append(menu_func)
$\endgroup$
  • 1
    $\begingroup$ Hi, added an answer, wondering if it's an idea to change question a bit to make it more "Making sure there is only one draw method ap(pre)pended to a Menu / Panel class." as I assume you still want one menu entry either way. $\endgroup$ – batFINGER Sep 10 '16 at 10:41
  • $\begingroup$ possible duplicate blender.stackexchange.com/questions/3394/… $\endgroup$ – zeffii Sep 10 '16 at 14:06
  • $\begingroup$ When working with addons I prefer to use an external text editor and disable/enable the addon so that items get removed. When running non-addon scripts I use this addon to run the script within the python console so that I can see any output and access the variables after the script has run. $\endgroup$ – sambler Sep 10 '16 at 15:37
0
$\begingroup$

It is possible to know that your script is being run within blender, you most likely have seen the line if __name__ == "__main__": in many blender scripts. This resolves to true when the script is run as a script, when false the script is being started as an addon.

Note that the else clause will be run when you first enable the addon or if you modify the addon file and disable/enable it again, it will not be run if you disable then enable an unmodified addon file.

if __name__ == "__main__":
    print('run as script')
else:
    print('starting addon')
$\endgroup$
  • $\begingroup$ thank you, That's exactly what I need def register(): bpy.utils.register_module(name) if name != "main": bpy.types.VIEW3D_MT_edit_mesh_vertices.append(menu_func) works fine. $\endgroup$ – UncleTeo Sep 11 '16 at 15:46
2
$\begingroup$

Rather than adding code as you suggest, If you are testing addon code from the script editor, you can press F8 which is the equivalent of

bpy.ops.script.reload()

which will take out all the menu entries added by scripts run from the text editor.

Another method is to remove draw methods from a panel (or menu) class. Here is a quick example using remove_draw_pend(...) from sound_drivers.utils In sound_drivers I spit a lot of draw methods into PROPERTY panels that have an "SD_" prefix, I remove these in one fell swoop with remove_draw_pend(panelclass, "SD_")

Test script, running the script below multiple times will result in "FUNKY" only appearing once in menu.

import bpy

def remove_draw_pend(paneltype, prefix):
    '''
    remove all functions with name beginning 
    with prefix (pre/ap)pended to paneltype
    '''
    draw_funcs = [f for f in paneltype._dyn_ui_initialize()
                  if f.__name__.startswith(prefix)]

    for f in draw_funcs:
        paneltype.remove(f)

def funky(self, context):
    self.layout.label("FUNKY")

def register():
    if True: # __package__ is None: # 
        remove_draw_pend(bpy.types.VIEW3D_MT_edit_mesh_vertices, "funky")    
    bpy.types.VIEW3D_MT_edit_mesh_vertices.append(funky)

if __name__ == "__main__":
    register()
$\endgroup$

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.