1
$\begingroup$

I created a section of a rope and assigned it to a curve. I now have to repeat the exact same process for other curves, each time duplicating the section of rope and changing the target curve in the modifiers. Is there a way to automate the process with a script?

The object to be duplicated is called "00_Rope" The curves collection is called "Curves" The curves names are "ObjNurb", "ObjNurb.001", etc

I need to add an array modifier set to "FIT_CURVE" to each duplicate and a Curve modifier.

enter image description here

$\endgroup$
4
  • $\begingroup$ Yes. This is exactly the sort of thing that Python scripts are good at. You might want to follow the "Scripting for Artists" tutorials on YouTube to see how easy it would be to write your own. $\endgroup$ Jan 28, 2022 at 17:46
  • 1
    $\begingroup$ if you want that somebody of us should write it for you, you have to give us much more details - e.g. what you mean with "section of a curve" - what the names should be and so on. With so less information we cannot do so much...or you learn it by yourself as Marty said, but as bloody beginner you would need weeks without help - i would guess. But yes, the answer to your question is yes. $\endgroup$
    – Chris
    Jan 28, 2022 at 17:51
  • $\begingroup$ I added the information you suggested, as I reckon I have not been specific enough. I tried scripting some myself but I seem to have the most problems in getting the the data right when getting the object to duplicate and where to get each new curve. @Chris $\endgroup$ Jan 29, 2022 at 17:14
  • $\begingroup$ You might be interested in a 'Ropify' GN group $\endgroup$
    – Robin Betts
    Jan 31, 2022 at 11:22

1 Answer 1

0
$\begingroup$

ok, this script does what you want:

Before you run it, save your blend file with a different name so you can get it back if it doesn't work. (in other words: make a backup)

Select your rope object, then run the script.

Big thank you to relaxed who helped and explained me how to improve the script:

import bpy


def addCurveToNewObject(obj, curve):
        
    bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(-0, 0, 0), "constraint_axis":(False, False, False)})            
    bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
      
    if len(bpy.context.selected_objects[0].modifiers) == 0:
        bpy.ops.object.modifier_add(type='CURVE')
    
    bpy.context.object.modifiers["Curve"].object = bpy.data.objects[curve.name]
    

col = bpy.data.collections["Curves"]

for curve in col.objects:
    
    obj = bpy.data.objects["00_Rope"]
    bpy.context.view_layer.objects.active = obj #sets the obj accessible to bpy.ops
 
    addCurveToNewObject(obj, curve)
$\endgroup$
3
  • 1
    $\begingroup$ @ Chris bpy.ops.object.duplicate_move() creates a new object that shares most of the data blocks with the original by default, including modifiers. The Curve modifier is already there, only the target curve needs to be changed. In the function you might delete everything after line newCreated = bpy.context.selected_objects[0] and insert newCreated.modifiers['Curve'].object = bpy.data.objects[curve.name]. $\endgroup$
    – relaxed
    Jan 30, 2022 at 13:15
  • 1
    $\begingroup$ You probably noticed that there is one rope object more than curves after execution. The original object is already assigned to a curve, but a duplicate is created for each curve, including the first one. One way would be to change the loop call to for item in range(1, len(col.objects)): and call the function with addCurveToNewObject(obj, col.objects[item]). $\endgroup$
    – relaxed
    Jan 30, 2022 at 13:17
  • $\begingroup$ Thank you relaxed! I will update my answer soon. Not at my laptop in the moment. $\endgroup$
    – Chris
    Jan 30, 2022 at 14:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .