So basically i have a few cubes in my scene, i want to parent all the thin cubes to the empty in python, i know i would start with a sort of detection system in python to detect which cubes to select, however i have scene other answers on this topic syaing to just add child_of constraint, this will not work for me, is there a way to parent these objects to the empty properly in python?
$\begingroup$
$\endgroup$
2
-
$\begingroup$ here's how to parent using python: blender.stackexchange.com/a/178871/142292 so you also want the algo to select the thin cubes? $\endgroup$– Harry McKenzie ♦Commented Aug 2, 2022 at 7:39
-
$\begingroup$ what if you set the names of the thin cube differently like "thin_cube.001", "thin_cube.002" etc is that feasible? so you loop through all objects with "thin_" in their name and select them. then select the parent last in the python code. $\endgroup$– Harry McKenzie ♦Commented Aug 2, 2022 at 7:44
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
This script uses the height of the "thin" cubes as condition to select them. In this case I've set the THRESHOLD_HEIGHT
to 0.9 but adjust this value if need be. (I'm assuming that your cubes have a dimension of 2 meters)
import bpy
THRESHOLD_HEIGHT = 0.9
parent_obj = bpy.data.objects["Empty"]
bpy.ops.object.select_all(action='DESELECT')
for obj in bpy.data.objects:
if obj.type == 'MESH' and obj.dimensions.z < THRESHOLD_HEIGHT:
obj.select_set(True)
parent_obj.select_set(True)
bpy.context.view_layer.objects.active = parent_obj
bpy.ops.object.parent_set(type='OBJECT')
If you want to base the selection algo of the "thin" cubes based on their name you can use this type of selection loop:
for obj in bpy.data.objects:
if "thin_" in obj.name and obj.type == 'MESH':
obj.select_set(True)