3
$\begingroup$

Python code to add objects to the 3D Cursor

I added a cube and looked at the Python Console

bpy.ops.mesh.primitive_cube_add(enter_ editmode=False, align='WORLD', location=(0, 0, 0)))

The code looked like this

When you run this code, it adds the cube to the first location you put it in, not the location of the 3D Cursor (location=(0, 0, 0))

I want to use this add-on, so the Python code has to be one line

https://github.com/InamuraJIN/CommandRecorder

How do I add an Object to the 3D Cursor's position?

Translated with www.DeepL.com/Translator (free version)

$\endgroup$
7
  • 2
    $\begingroup$ Cursor belongs to scene = context.scene then in operator location=scene.cursor.location or alternatively use align='CURSOR' See blender.stackexchange.com/questions/13828/… and blender.stackexchange.com/questions/164734/… $\endgroup$ Commented Jul 4, 2020 at 6:06
  • $\begingroup$ Thank you. I'm terrible at Python.The following code could not be executed How should I describe it? bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=scene.cursor.location and bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, scene = context.scene, location=scene.cursor.location $\endgroup$ Commented Jul 4, 2020 at 6:15
  • 1
    $\begingroup$ bpy.ops.mesh.primitive_cube_add(size=2, location=bpy.context.scene.cursor.location) or bpy.ops.mesh.primitive_cube_add(size=2, align='CURSOR') Enter edit mode False is default. Defaults need not be included in call. $\endgroup$ Commented Jul 4, 2020 at 6:18
  • $\begingroup$ Thank you so much! Now you can Add object to the cursor position BTW, can I disable Cursor rotation? $\endgroup$ Commented Jul 4, 2020 at 6:21
  • $\begingroup$ Use first if only want to use location. Can set cursor rotation to zero AFAIK can't disable it (could hide the UI) $\endgroup$ Commented Jul 4, 2020 at 6:23

1 Answer 1

2
$\begingroup$

Add new object at cursor

Default values.

If you auto complete on the operator in the python console, it shows the properties and their default values. There is no need to pass any default value to the operator.

>>> bpy.ops.mesh.primitive_cube_add(
primitive_cube_add()
bpy.ops.mesh.primitive_cube_add(size=2, calc_uvs=True, enter_editmode=False, align='WORLD', location=(0, 0, 0), rotation=(0, 0, 0), scale=(0, 0, 0))
Construct a cube mesh

To add a cube with location and rotation of 3D cursor

bpy.ops.mesh.primitive_cube_add(align='CURSOR')

To add at cursor location.

bpy.ops.mesh.primitive_cube_add(location=bpy.context.scene.cursor.location)

or equivalently, align to cursor but override rotation. Even though the zero rotation passed is default, just passing it (in tests so far) overrides cursor rotation.

bpy.ops.mesh.primitive_cube_add(align='CURSOR', rotation=(0, 0, 0))

Related

Precisely move the 3D cursor

How to move selected object to the curser location using a python command?

$\endgroup$
6
  • $\begingroup$ Very straightforward! Thank you so much! $\endgroup$ Commented Jul 9, 2020 at 4:52
  • $\begingroup$ bpy.ops.mesh.primitive_plane_add(align='CURSOR', rotation=(-90, 0, 0)) If (0, 0, 0) the rotation of 3DCursor is ignored. However, (-90, 0, 0) seems to add the rotation of 3DCursor and -90 What should I do? $\endgroup$ Commented Jul 9, 2020 at 11:28
  • $\begingroup$ Rotations in blender are radians. 360 degrees = 2 pi radians. $\endgroup$ Commented Jul 9, 2020 at 11:37
  • $\begingroup$ OK, I see. It's hard to... (1.5708, 0, 0) Is this correct? $\endgroup$ Commented Jul 9, 2020 at 13:51
  • $\begingroup$ close enough... $\endgroup$ Commented Jul 9, 2020 at 13:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.