0
$\begingroup$

I am writing a script for applying a particle system on Blender. In the options I want to set a solid primitive cube as the duplicate object, however I can't figure out how to do it without creating a cube in the scene first. Is there a way to do this or must I create the cube as well? If so, I want to be able to delete it after applying the transformation. This is the code I have that does not work:

settings.dupli_object = bpy.data.objects.get("Cube")

If there is no "Cube" to reference in blender's database, what would the syntax be for creating it with the name "Cube" and then deleting it after the transformation is applied?

$\endgroup$
  • $\begingroup$ Would add a new one if the cube option is chosen. If they want to use the old one or any object for that matter, have a use context object or select object option. Directly after running a create primitive object then new_object = context.object. $\endgroup$ – batFINGER Oct 1 '18 at 13:56
0
$\begingroup$
# create solid primitive cube to use as duplicate object
bpyscene = bpy.context.scene

# Create an empty mesh and the object.
mesh = bpy.data.meshes.new('Cube')
basic_cube = bpy.data.objects.new("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()

and then delete

bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['Cube'].select = True
bpy.ops.object.delete()
$\endgroup$
  • $\begingroup$ Why not simply bpy.ops.mesh.primitive_cube_add() ? It is after all exactly what your first script is doing (but in one line). $\endgroup$ – batFINGER Oct 2 '18 at 11:39
  • $\begingroup$ how do you choose what the cube is called with this? $\endgroup$ – alex.l Oct 2 '18 at 11:51
  • $\begingroup$ After running the op: context.object.name = "Blah" the naming system will append numbers if another exists with same name (same with your script wont get "Cube" if name already exists). More important is the reference not the name. After op newly_added_cube = context.object $\endgroup$ – batFINGER Oct 2 '18 at 12:22

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.