join_same_name_objects.py (746 Bytes) Hi Guys, sorry to bother you. Im really new with python scripting . I’m hopping you could give me some insight.
This is my situation: Im importing a model from Revit via IFC and it generates objects with long names.
This is an example of the object names: (the full list about 800 objects)
bpy.context.scene.objects[
…
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:520337’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:520341’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:520345’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:520349’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:525707’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:525841’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:527695’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:527825’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:573925’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:637305’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:670420’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:670425’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:670557’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm C:670562’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:517485’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:517486’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:520390’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:520394’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:520398’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:520415’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:524163’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:615345’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:615384’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:615455’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:626248’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:650751’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:686053’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:686117’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:688973’]
‘IfcWallStandardCase/Muro básico:Generic - 150mm:689092’]
‘IfcWallStandardCase/Muro básico:Generic - 200mm:571706’]
‘IfcWallStandardCase/Muro básico:Generic - 200mm:571868’]
‘IfcWallStandardCase/Muro básico:LAmina F:541198’]
‘IfcWallStandardCase/Muro básico:LAmina F:563937’]
‘IfcWallStandardCase/Muro básico:LAmina F:563998’]
‘IfcWallStandardCase/Muro básico:LAmina F:564496’]
‘IfcWallStandardCase/Muro básico:Lamina:520472’]
‘IfcWallStandardCase/Muro básico:Lamina:520476’]
‘IfcWallStandardCase/Muro básico:Panel Fachada:562901’]
‘IfcWallStandardCase/Muro básico:Panel Fachada:563203’]
‘IfcWallStandardCase/Muro básico:Panel Fachada:563605’]
‘IfcWallStandardCase/Muro básico:Panel Fachada:565785’]
‘IfcWindow/Deslizante con cubrejuntas:1830 x 1220 mm 10:527089’]
‘IfcWindow/Deslizante con cubrejuntas:1830 x 1220 mm 11:650850’]
‘IfcWindow/Deslizante con cubrejuntas:600 x 600 mm 2:639533’]
‘IfcWindow/Deslizante con cubrejuntas:600 x 600 mm 2:639820’]
‘IfcWindow/Deslizante con cubrejuntas:600 x 600 mm 2:639940’]
‘IfcWindow/Window_4_pane_slider_10391:Window_4_pane_slider_10391’]
I need to join objects with similar names to be able to simplify the objects in the scene.
After a little study and watching some example scripts I manage to write this one:
import bpy
bpy.ops.object.select_all(action=‘DESELECT’) #This one Deselects all previous selections
for obs in bpy.context.scene.objects:
nocolon = obs.name.rsplit(sep=‘:’) # break down long names separating strings along the “:”
tname = nocolon[0].rsplit(sep=‘.’) # Removes the “.” and passes name variable “tname”
bpy.ops.object.select_pattern(pattern= tname[0]+‘*’, case_sensitive=False, extend=True)
obs.select_set(True) #Marks the selection
bpy.context.view_layer.objects.active = obs #Sets the active object
bpy.ops.object.join() #Joins the selection
bpy.ops.object.select_all(action=‘DESELECT’) #Deselects all for the next iterationprint(‘done’)
Im kinda lost here, because sometimes that I run the scripts I get diferent behaviour:
1.- It finishes selecting the objects and joining objects correctly but gives error RuntimeError: Error: Object ‘Cube.red’ can’t be selected because it is not in View Layer ‘View Layer’!
2.- Sometime it freezes blender for long time.
3.- Sometimes blender closes
4.- Sometimes gives me the error RuntimeError: Operator bpy.ops.object.join.poll() failed, context is incorrect
I hope you guys cold give me some insights of what could be causing this issue.
You could test the script by doing duplicates of different objects.
Example:
Cube
Cube.001
Cube.002
Sphere
Sphere.001
Sphere.002
Cone
Cone.001
Cone.002
It should be able to join the objects with the same name.
I hope you could advise me.