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
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__)