1
$\begingroup$

I am having trouble creating an emitter via python scripting (using Blender 3.2.1).

My problem is the following: suppose I have a cylinder and a plane (which will play the emitter role).

I can create those by writing

import bpy

# create plane
bpy.ops.mesh.primitive_plane_add()
emitter = bpy.context.object
emitter.name = "Emitter"

# Create cyclinder
bpy.ops.mesh.primitive_cylinder_add()
my_cyl = bpy.context.object
my_cyl.name = "My_Cyl"
my_cyl.select_set(False)

# Select (only) the emitter
objectToSelect = bpy.data.objects[emitter.name].select_set(True)
bpy.context.view_layer.objects.active = objectToSelect

From there, if I use the graphical user interface (GUI), select the plane, go in "Particle Properties", and click on Add Particle System Slot, the info displays

bpy.context.space_data.context = "PARTICLES"
bpy.ops.object.particle_system_add()

If in my script I write those above lines, I get the error message

Python: Traceback (most recent call last):
  File "...\simple_tests.blend\script001.py", line 18, in <module>
AttributeError: 'SpaceTextEditor' object has no attribute 'context'

My question is: how can I manage to write the script to make it such that the plane is the emitter?

$\endgroup$

1 Answer 1

1
$\begingroup$

try this:

import bpy

# create plane
bpy.ops.mesh.primitive_plane_add()
emitter = bpy.context.object
emitter.name = "Emitter"


bpy.ops.object.particle_system_add()


# Create cyclinder
bpy.ops.mesh.primitive_cylinder_add()
my_cyl = bpy.context.object
my_cyl.name = "My_Cyl"
my_cyl.select_set(False)

Why do you want to make it that complicated? If you create a new object, it is the current active object which the ops operators use. If you create your cylinder later, you don't have to select that manually.

$\endgroup$
2
  • $\begingroup$ Hi @Chris your answer did the trick, thank you very much. I was wondering how it could be done through a function, which would be easier than copy-pasting always the same code? $\endgroup$
    – aheuchamps
    Commented Apr 11, 2023 at 13:26
  • $\begingroup$ i understand that. Then you should phrase exactly that in your question - then my answer would be different ;) please just open a new post with your question -> you can open as many posts as you want. $\endgroup$
    – Chris
    Commented Apr 11, 2023 at 13:44

You must log in to answer this question.

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