restricted content problem animation script -> addon

Hi Blenderartists!

i have made a cool script that automatically moves acharcter around, to follow an animated target. footsteps are set by itself with a balancing algortihm… i want to share it with the community as an addon. but if i import my script and activate it, i get an restricted content error message. (AttributeError: “_RestrictContext” object has no atrribute:“object”)

I’m new to python and blender scripting, i read that i cant use scenedata during registration, but when the script executes.
I just dont know what to change on the script. I’d be glad if someone could look over, to help get this addon out to all who want to get rid of footstep animation. :slight_smile:


bl_info = {
    "name": "BalanceRun", 
    "category": "Animation"
    }


    
import bpy
import math
from bpy.props import *
import bpy.types
from math import sqrt
####
##### 
bpy.types.Object.pivot = bpy.props.StringProperty();               
bpy.types.Object.hip = bpy.props.StringProperty();  
bpy.types.Object.chest = bpy.props.StringProperty(); 
bpy.types.Object.foot1 = bpy.props.StringProperty();
bpy.types.Object.foot2 = bpy.props.StringProperty();
bpy.types.Scene.stepheight = bpy.props.IntProperty(name = "stepheight %", default = 10, description = "height of raising the walking foot, relative to hipheight (=100%)");
bpy.types.Scene.steptime = bpy.props.IntProperty(name = "steptime", default = 10, description = "how many frames does a step take");
bpy.types.Scene.startframe = bpy.props.IntProperty(name = "startframe", default = 1, description = "startframe of the animation");
bpy.types.Scene.endframe = bpy.props.IntProperty(name = "endframe", default = 125, description = "endframe of the animation");
bpy.types.Scene.leglength = bpy.props.IntProperty(name = "leglength %", default = 150, description = "leglength (startpose=100%)");
bpy.types.Scene.dynamic = bpy.props.IntProperty(name = "dynamic %", default = 50, min=0, max=100, description = "0% = static motion 100%= dynamic motion");
###user interface  
class ToolsPropsPanel(bpy.types.Panel):
    bl_label = "BalanceRun"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    def draw(self, context):
        obj = context.object
        layout = self.layout
        layout.prop_search(obj, "pivot",  context.scene, "objects")
        layout.prop_search(obj, "hip",  context.scene, "objects")
        layout.prop_search(obj, "chest",  context.scene, "objects")
        
        layout.prop_search(obj, "foot1",  context.scene, "objects")
        layout.prop_search(obj, "foot2",  context.scene, "objects")
        scene = context.scene
        #row = layout.row()
        layout.prop(scene, "startframe")
        layout.prop(scene, "endframe")
        layout.prop(scene, "steptime")
        layout.prop(scene, "stepheight")
        layout.prop(scene, "leglength")
        layout.prop(scene, "dynamic")
        row = layout.row(align=True)
        row.alignment = 'EXPAND'
        row.operator("object.button",text="run balance",  icon='LAMP_DATA')
        bpy.context.object.pivot = ""
        bpy.context.object.foot1 = ""
        bpy.context.object.foot2 = ""
###function
#balance (actualfooto, pivo, heighto,timeo,co);
bpy.utils.register_class(ToolsPropsPanel) 
class OBJECT_OT_Button(bpy.types.Operator):
    bl_idname = "object.button"
    bl_label = "Button"
    def execute(self, context):  
        pivo=bpy.context.scene.objects[bpy.context.object.pivot]
        hip=bpy.context.scene.objects[bpy.context.object.hip]
        
        chest=bpy.context.scene.objects[bpy.context.object.chest]
        hto=bpy.context.scene.stepheight/20;
        tmo=bpy.context.scene.steptime;
        startf=bpy.context.scene.startframe;
        endf=bpy.context.scene.endframe;
        co=tmo+1;
        ll=bpy.context.scene.leglength
        dynamic=bpy.context.scene.dynamic
        #feet
        actfo=bpy.context.scene.objects[bpy.context.object.foot1];
        actfo2=bpy.context.scene.objects[bpy.context.object.foot2];
        
        bpy.context.scene.frame_set(startf);  
        actfo.animation_data_clear()
        actfo2.animation_data_clear()
        actfo.animation_data_clear()
        actfo2.animation_data_clear()
        chest.animation_data_clear()
        hip.animation_data_clear()
        chest.animation_data_clear()
        hip.animation_data_clear()
            
        balance (actfo, actfo2, pivo, hto,tmo,co,ll,startf,endf,chest,hip,dynamic);
        
        return{'FINISHED'}  
####functions
def bal0 (actualfoot, actualfoot2, piv, height,time,c,leglngth):
    bpy.context.scene.frame_set(1);
    middle=[(actualfoot.location[0]-actualfoot2.location[0])/2-piv.location[0],(actualfoot.location[1]-actualfoot2.location[1])/2-piv.location[1],(actualfoot.location[2]-actualfoot2.location[2]--piv.location[2])/2];
    
    distx=round(100*(piv.location[0]- actualfoot.location[0]))/100;
    disty=round(100*(piv.location[1]- actualfoot.location[1]))/100;
    distz=round(100*(piv.location[2]- actualfoot.location[2]))/100;
    dist2x=round(100*(piv.location[0]- actualfoot2.location[0]))/100;
    dist2y=round(100*(piv.location[1]- actualfoot2.location[1]))/100;
    dist2z=round(100*(piv.location[2]- actualfoot2.location[2]))/100;
    
    oldpos=piv.location;
    pivroto=piv.rotation_euler[2];
    stepwidth= sqrt(distx**2+disty**2)*leglngth/100;
    return ([distx, disty, distz,oldpos, pivroto,stepwidth,dist2x,dist2y,dist2z,middle]) 
        
def balance (actualfoot, actualfoot2, piv, height,time,c,leglngth, startf, endf,chest,hip, dynamic):
    hipforce=1;
    c2=c;
    startinfo=bal0(actualfoot, actualfoot2, piv, height,time,c,leglngth);
    backheight= chest.location[2]-hip.location[2]
    distxo=startinfo[0];
    distyo=startinfo[1];
    distzo=startinfo[2];
     
    ###i needed to delete text here to fit into the forum textlength limitation
    ### heres just math and keyframes that are set....       
     
        
           
  
def register():
    bpy.utils.register_module(__name__)
  


def unregister():
    bpy.utils.unregister_module(__name__)
    
    del balance
    del bal0


if __name__ == "__main__":
    register()


heres a video tutorial about the script…

you need to register properties in register() and they must not use variables which depend on bpy.data and bpy.context