7
$\begingroup$

I can't quite figure this out, so I am hoping someone can help.

In Python, I want to create an empty object, and add a Cube primitive to the object. After that I want to create a new material, and set the object to use the material.

# Create an empty object.
basic_cube = bpy.data.objects.new("Basic_Cube", None)

# Add the object into the scene.
bpyscene.objects.link(basic_cube)
bpyscene.objects.active = basic_cube
basic_cube.select = True

# Add a Cube primitive to the empty object.
bpy.ops.mesh.primitive_cube_add(location=(0.0, 0.0, 0.0))

basic_cube_mesh.update()

But this does not add the cube primitive to the object. It adds it directly to the scene.

Any suggestions on how to get this to work?

$\endgroup$
1
  • 1
    $\begingroup$ The Add New Object python script template, in the text editor, creates an operator "Add New Mesh" which adds a cube to the scene, $\endgroup$
    – batFINGER
    Commented Sep 28, 2016 at 5:48

2 Answers 2

7
$\begingroup$

You have to create the object with an empty mesh first, as you cannot assign a mesh to an empty later.

Further: primitive_cube_add(...) is supposed to create an object, since it is the same operator called by Add->Mesh->Cube from the menu.

So you can use BMesh to create your cube later and assign it to the empty mesh:

import bpy
import bmesh

bpyscene = bpy.context.scene

# Create an empty mesh and the object.
mesh = bpy.data.meshes.new('Basic_Cube')
basic_cube = bpy.data.objects.new("Basic_Cube", mesh)

# Add the object into the scene.
bpyscene.objects.link(basic_cube)
bpyscene.objects.active = basic_cube
basic_cube.select = True

# Construct the bmesh cube and assign it to the blender mesh.
bm = bmesh.new()
bmesh.ops.create_cube(bm, size=1.0)
bm.to_mesh(mesh)
bm.free()
$\endgroup$
0
$\begingroup$
import bpy

# Clear existing data
bpy.ops.wm.read_factory_settings(use_empty=True)

# Create a simple human-like model (a basic UV Sphere as a placeholder)
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=(0, 0, 1))
human = bpy.context.active_object
human.name = "Man"

# Create a simple TV model (a basic cube)
bpy.ops.mesh.primitive_cube_add(size=2, location=(3, 0, 1))
tv = bpy.context.active_object
tv.name = "TV"

# Create a floor
bpy.ops.mesh.primitive_plane_add(size=10, location=(0, 0, 0))
floor = bpy.context.active_object
floor.name = "Floor"

# Set up the camera
camera = bpy.data.objects['Camera']
camera.location = (7, -3, 3)
camera.rotation_euler = (1.1, 0, 1.1)

# Set up lighting
bpy.ops.object.light_add(type='SUN', location=(5, 5, 10))
light = bpy.context.active_object
light.name = "SunLight"

# Animate the man (simple head turn)
bpy.context.view_layer.objects.active = human
human.rotation_euler = (0, 0, 0)
human.keyframe_insert(data_path="rotation_euler", frame=1)

human.rotation_euler = (0, 0.3, 0)
human.keyframe_insert(data_path="rotation_euler", frame=50)

# Set the animation end frame
bpy.context.scene.frame_end = 100

# Save the file if needed
# bpy.ops.wm.save_as_mainfile(filepath="/path/to/your/file.blend")
$\endgroup$
3
  • $\begingroup$ Hi, thanks for the post. This site is not a regular forum, answers should be substantial, stand on their own, and thoroughly explain the solution and required steps. One liners and code only posts rarely make for a good answer. If you can, edit your post and provide some more details about the procedure and how it works, perhaps add a few images illustrating some steps and final result. See How to write a good answer?, otherwise it may be converted to a comment. $\endgroup$ Commented Aug 28 at 12:33
  • $\begingroup$ Also please use proper code formatting when posting code snippets. $\endgroup$ Commented Aug 28 at 12:33
  • 1
    $\begingroup$ This is GPT generated, and doesn't actually attempt to answer OP's question, because it adds a cube object, not cube primitive inside the object. $\endgroup$ Commented Aug 28 at 13:05

You must log in to answer this question.

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