initSceneProperties - proper intialization ???

Hi there,

This is my first attempt to create an Add-on, Find and Replace Text/String in Selected Objects Names, but I do not know how to do initSceneProperties(bpy.context.scene) properly so it starts together with the add-on … without the need of the “refresh” initSceneProperties.button.


I tried to place initSceneProperties(bpy.context.scene) on several places all over the script, but it gave me errors at Add-on install.

I would like to get rid of the initSceneProperties.button :wink:

Thank you for your most valuable help

with friendly regards

Jozef

bl_info = {
     "name": "Tool_by_Zajo",
     "description": "Finds and Replaces strings within names of selected objects.",
     "author": "Jozef Zajac alias Zajo",
     "version": (1, 0),
     "blender": (2, 69, 8),
     "location": "View3D > Properties",
     "warning": "WIP", # used for warning icon and text in addons panel
     "wiki_url": "",
     "category": "Mesh"}
 
 #----------------------------------------------------------
 # File Tool_by_Zajo.py
 #----------------------------------------------------------
 
 import bpy
 from bpy.props import *
 
 #
 #    Store properties in the active scene
 #
 
 def initSceneProperties(scn):
     bpy.types.Scene.FindString = StringProperty(
         name = "Find")
     scn['FindString'] = "Text / String"
 
     bpy.types.Scene.ReplaceString = StringProperty(
         name = "Replace")
     scn['ReplaceString'] = "Text / String"
 
     bpy.types.Scene.SearchString = StringProperty(
         name = "Search")
     scn['SearchString'] = "Text / String"
     return
 
 #
 #    Menu in UI region
 #
 
 class ColorPanel(bpy.types.Panel):
     bl_label = "Tool by Zajo"
     bl_space_type = "VIEW_3D"
     bl_region_type = "UI"
 
     def draw(self, context):
         layout = self.layout
         scn = context.scene
         layout.operator("initialize.button")
         layout.prop(scn, 'FindString')
         layout.prop(scn, 'ReplaceString')
         layout.operator("findreplace.button") # must be lower case !!!
         layout.label("")
         layout.prop(scn, 'SearchString')
         layout.operator("select.button")
         return
 
 #
 #    The buttons actions
 #
 
  class InitializeButton(bpy.types.Operator):
     bl_idname = "initialize.button"
     bl_label = "initSceneProperties"
 
     def execute(self, context):
         initSceneProperties(bpy.context.scene)
         return{'FINISHED'}
 
 class FindReplaceButton(bpy.types.Operator):
     bl_idname = "findreplace.button"
     bl_label = "in Object Name"
 
     def execute(self, context):
         for object in bpy.context.selected_objects:
             named = object.name
             print(named)
             renamed = named.replace(bpy.context.scene.FindString, bpy.context.scene.ReplaceString)
             print(renamed)
             object.name = renamed
         return{'FINISHED'} 
 
 class SelectButton(bpy.types.Operator):
     bl_idname = "select.button"
     bl_label = "Select containing"
     def execute(self, context): 
         bpy.ops.object.select_pattern(pattern="*"+bpy.context.scene.SearchString+"*")
         return{'FINISHED'}
 
 #    Registration
 def register():
     bpy.utils.register_module(__name__)
 def unregister():
     bpy.utils.unregister_module(__name__)
 

It’s not allowed anywhere, where it gets called automatically during the addon loading process:

http://wiki.blender.org/index.php/Extensions:2.6/Py/API_Changes#Restricted_Context

A button to initilize the data is the appropriate way to go. You could also init when the actual operation is run (by another button), just add a static class variable to remember if the initilization has been executed before.

There’s also a hack to circumvent the Restricted Context limitation: Add a scene_update_post app handler that removes itself (thus it will get called once) and do the init there, this event should occur after addon registration (so after the context restriction)

Hi,

yes I know that it is a restricted one.

your first solution, I tried, it was inside the “in Object name” button … but I had to click it first, to reveal the input fields, fill the Find and Replace ant hit it the second time. And nobody would know that it is working this way, except me.

the second solution seems to be the one I am looking for. Could you please post here the code how to do that, and where to put that, I am not so familiar with python yet.

Thanks in advance

Jozef

Still had no time to check it :frowning: