import bpy
<i>#needs follow-path-constraint on active object
#use obj.constraints.new("FOLLOW_PATH")
#to create constraint via script</i>
group=bpy.data.groups.get("Duplicates")
if(group is None):
group=bpy.data.groups.new("Duplicates")
handled={}
def hndl(context):
for str in handled.keys():
curve=bpy.data.objects.get(str)
stack,obj=handled[str]
if(
curve is not None and
curve.is_updated and
curve["count"]>=0 and
type(curve["count"]) is int
):
dn=curve["count"]-len(stack)
if(dn<0):
for i in range(-dn):
dupli=stack.pop()
group.objects.unlink(dupli)
bpy.context.scene.objects.unlink(dupli)
bpy.data.objects.remove(dupli)
elif(dn>0):
for i in range(dn):
dupli=obj.copy() #also copies constraints!
stack.append(dupli)
group.objects.link(dupli)
bpy.context.scene.objects.link(dupli)
for i,dupli in enumerate(stack):
dupli.constraints[0].offset=curve["offset"]*i+obj.constraints[0].offset
elif(curve is None):
del handled[str]
if(len(handled)==0):
bpy.app.handlers.scene_update_post.remove(hndl)
def main(context):
obj=bpy.context.object
curve=obj.constraints[0].target
curve["offset"]=4.0
curve["count"]=10
handled[curve.name]=([],obj)
if(bpy.app.handlers.scene_update_post.count(hndl)==0):
bpy.app.handlers.scene_update_post.append(hndl)
curve.update_tag()
class FollowPathArray(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.fpath_array"
bl_label = "FollowPathArray"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
main(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(FollowPathArray)
def unregister():
bpy.utils.unregister_class(FollowPathArray)
if __name__ == "__main__":
register()
For each curve i am storing a list of duplis in a dictionary named “handled” in the module. Is there another way to realize this which is more suitable?
Is it possible to attach a list to an object?