2
$\begingroup$

How to parent two object without selecting them manually in blender python scripting console? I want parent object which stored in variable like

Add a cube at the origin:

a=bpy.ops.mesh.primitive_cube_add()

Create an empty object:

b=bpy.ops.object.empty_add(type="SPHERE")

How to parent a to b?

$\endgroup$
3
$\begingroup$
bpy.ops.mesh.primitive_cube_add()
a = bpy.context.object
bpy.ops.object.empty_add(type="SPHERE")
b = bpy.context.object
const = b.constraints.new(type='CHILD_OF')
const.target = a
$\endgroup$
0
$\begingroup$

The simplest way to set a parent for an object is by accessing its parent property:

import bpy

# Create cube and reference the cube object
bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.object

# Create sphere empty and reference the empty object
bpy.ops.object.empty_add(type="SPHERE")
empty = bpy.context.object

# Set the sphere empty as the parent of the cube object
cube.parent = empty
$\endgroup$
  • 2
    $\begingroup$ The drawback there is that the child will snap to the parents location, if they had been positioned first. You can select both, make the parent active and use bpy.ops.object.parent_set(type='OBJECT') to fill in the matrix to keep the child offset. $\endgroup$ – sambler Sep 6 '18 at 9:58

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.