1
$\begingroup$

I have an object A that is constrained to move on a path and I want to create still copies of it while it moves on the path. I tried the following code but it failed. In particular, it arbitrarily moves the object A itself and all the copies go crazy. Could you please help me fix this? Thank you!

A = context.active_object
        
for i in range(1, copies_number+1):

    A.constraints["Follow Path"].offset_factor = (i-1) / (self.copies_number-1)

    A.constraints["Follow Path"].keyframe_insert(data_path='offset_factor', frame=i)

    copy = duplicate(A) #this is a function that I have introduce to duplicate objects

    bpy.context.view_layer.objects.active = copy

    bpy.ops.object.visual_transform_apply()

    for constraint in copy.constraints:

       copy.constraints.remove(constraint)
$\endgroup$
3
$\begingroup$

Without operators

Here is an example of doing this without operators.

  • Have the constrained object as active
  • Create a copy, strip the follow path constraints from it.
  • Create all the copies needed from the copy
  • Loop thru the copies,
    • calculate the frame,

    • set and keyframe offset based on frame

    • change to that frame

      • give the copy the same matrix world as original at that frame
    • link copy to collection

Test script:

import bpy

N = 10 # number of copies

context = bpy.context
ob = context.object
scene = context.scene
collection = context.collection

copy = ob.copy()
cons = [c for c in copy.constraints if c.type == 'FOLLOW_PATH']
while cons:
    copy.constraints.remove(cons.pop())
copies = [copy.copy() for i in range(N - 1)]
copies.append(copy)
    
frames = 250    
for i, copy in enumerate(copies):
    f = i * frames / N
    # find constraint properly 0 for example sake
    ob.constraints[0].offset_factor = f / frames
    ob.constraints[0].keyframe_insert("offset_factor", frame=f)
    
    scene.frame_set(f)
    copy.matrix_world = ob.matrix_world
    collection.objects.link(copy)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Amazing thank you very much! By the way, I found the mistake in my code. I am not deselecting everything and then selecting only the copy. If I take care of this selection business then it works. I am not sure if I am supposed to ask this here, but is it also possible to remove from each copy any possible drivers that the copies might inherit from the original object (in case the original object has any drivers)? We remove the constraints from the copies, but I do not know if it is also possible for drivers. Thank you! $\endgroup$ – Stefare Jun 12 at 13:04

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.