[TOOL] WIP Blender script to add collision information automatically

I like to model playable environments in Blender then export to Unreal. I can model quickly, but going back to duplicating geometry and rename to create collision boundaries I find tedious.

I have started working on a Blender based python script to automate this step. It is not yet complete.

Here is the first version of it. It will change the selected object names. Final behaviour will need to add a step to duplicate the objects first.


#17 December 2017
#Dingo_aus 
#Script adds geometry that Unreal Engine interprets to be the collision volumes.
#script requires objects to be selected for collision to be added

import bpy
print("Start---------------- ")
#Duplicate object...then rename to be the collision

#Duplicate Object



#rename objects
n = 1
for obj in bpy.context.selected_objects:
    n= n +1
    oldname = obj.name
    if oldname:3] != "UCX":
        #add prefix
        obj.name = "UCX_" + oldname 
        #add postfix
        oldname = obj.name
        obj.name = oldname + "_0" + str(n)
    print(obj.name)
print("Success!")



Version 1.0 of the script:


#22 December 2017
#Dingo_aus 
#Script adds geometry that Unreal Engine interprets to be the collision volumes.
#script requires objects to be selected for collision to be added

#1) Duplicates all selected objects then
#2) runs through a loop and finds the duplicated items with ".001, .002, .003 etc" and renames these 
#   objects to be collision objects



import bpy
print("Start---------------- ")
#Duplicate selected objects...then rename to be the collision
bpy.ops.object.duplicate()

#rename objects
n = 1
for obj in bpy.context.selected_objects:
    n= n +1
    oldname = obj.name
    temp_str = oldname-4:]
    temp_str2 = "."
    print(temp_str)
    if temp_str:1] == temp_str2:
        print("THE CANDIDATE HAS BEEN FOUND: " + oldname)
        #add prefix
        obj.name = "UCX_" + obj.name 
        #remove last four characters
        obj.name = obj.name:-4]
        #add postfix
        postfix_str = str(n)
        obj.name = obj.name + "_" + postfix_str.rjust(2, '0')
    print(obj.name)
print("Success!")


Version 1.1:

This now is more robust. I have demonstrated it working with complex scenes (>100 volumes) from Blender


#23 December 2017
#Dingo_aus 
#Script adds geometry that Unreal Engine interprets to be the collision volumes.
#script requires objects to be selected for collision to be added

#0) remove the full stop from previsouly duplicated names
#1) Duplicates all selected objects then
#2) runs through a loop and finds the duplicated items with ".001, .002, .003 etc" and renames these 
#   objects to be collision objects



import bpy
print("Start---------------- ")
#0)

for obj in bpy.context.selected_objects:
    obj.name = obj.name.replace(".","_")



#1)
#Duplicate selected objects...then rename to be the collision
bpy.ops.object.duplicate()

#3)
#rename objects
n = 1
for obj in bpy.context.selected_objects:
    n= n +1
    oldname = obj.name
    temp_str = oldname-4:]
    temp_str2 = "."
    print(temp_str)
    if temp_str:1] == temp_str2:
        print("THE CANDIDATE HAS BEEN FOUND: " + oldname)
        #add prefix
        obj.name = "UCX_" + obj.name 
        #remove last four characters
        obj.name = obj.name:-4]
        #add postfix
        postfix_str = str(n)
        obj.name = obj.name + "_" + postfix_str.rjust(2, '0')
    print(obj.name)
print("Success!")

Oh nice!

Thank you so much!

Looks really interesting, is there any way of simplifying that mesh easily? :slight_smile:

I’ll maintain the script at github:

@HeadClot, you’re welcome.

@HostileEnvironment when you refer to simplifying the mesh, what exactly are you seeking?

The script will:

  • remove the full stop from previously duplicated names
  • Smart Project Unwrap each item (calculate UVs)
  • Duplicates all selected objects then…
  • runs through a loop and finds the duplicated items with “.001, .002, .003 etc” and renames these
    objects to be collision objects starting with “UCX_”. It will also put the collision volumes on Layer 2.

I have not attempted to change the underlying geometry. If you are thinking of a feature where the a small complex mesh is replaced by a single collision volume then I think I would implement this as a manual over-ride. e.g. if you set a flag, place geometry on a specific layer, start the name with a postfix then that geometry will be ignored, or something like that.

I don’t think automatically calculating the complexity of a volume and replacing it with a cube is going to produce a good result but it you have specific rules/triggers in mind then feel free to write them up here and I can comment on them.