3
$\begingroup$

I would like to add a Force field. In blender 2.93 I could do it just like that:

Wind = bpy.data.objects.new("TS_Wind_Empty", None)
bpy.context.scene.collection.children['Collection'].objects.link(Wind) # link object
Wind.location[2] = 10
Wind.rotation_euler[0] = 3.1415926536 # radian rotation
Wind.scale = 60, 60, 60
Wind.field.type = 'WIND'
Wind.field.strength = 0.2
Wind.field.noise = 1

But in blender 3.0 the error comes:

'NoneType' object has no attribute 'type' . 

In the changelog I saw that:

Force fields and collision settings (Object.field, Object.collision, ParticleSettings.force_field_1 and ParticleSettings.force_field_2). Object ones can be None and need to be added explicitly now (Collision modifier and forcefield_toggle operator), particle ones are always generated together with particles settings.

But how can I give the Empty the Field type? It is not a constraint and not a modifier. Physics Properties do not exist.

What is the solution?

$\endgroup$

1 Answer 1

2
$\begingroup$

check this out:

import bpy

Wind = bpy.data.objects.new("TS_Wind_Empty", None)
bpy.context.scene.collection.children['Collection'].objects.link(Wind) # link object
Wind.location[2] = 10
Wind.rotation_euler[0] = 3.1415926536 # radian rotation
Wind.scale = 60, 60, 60


objectToSelect = bpy.data.objects["TS_Wind_Empty"]
objectToSelect.select_set(True)    
bpy.context.view_layer.objects.active = objectToSelect

bpy.ops.object.forcefield_toggle()

Wind.field.type = 'WIND'
Wind.field.strength = 0.2
Wind.field.noise = 1
$\endgroup$
6
  • $\begingroup$ Thanks, Chris, but I'm using the script in an add-on and there I want to avoid operators at all costs. Is there a way via bpy.context or bpy.data? $\endgroup$
    – Andi
    Commented Nov 9, 2021 at 12:21
  • $\begingroup$ i don't know. I was pretty proud to find out that... ;) $\endgroup$
    – Chris
    Commented Nov 9, 2021 at 12:23
  • $\begingroup$ Yes, I see what you mean. It is absolutely not easy to find something like that. The operator is even mentioned in the changelog I posted. But I need a way over context or data. I would be interested to know where someone got the knowledge how to find the way via context or data, then I need to ask less questions. $\endgroup$
    – Andi
    Commented Nov 9, 2021 at 12:29
  • $\begingroup$ @Andi: I totally agree with you. The blender docu is....ähm...let's say ok, but improvable. I wish there would be more example code...best if every command would have an example. Maybe this is kind of a business idea ;) sell a blender docu with example code for each object/method. I would pay for it!! $\endgroup$
    – Chris
    Commented Nov 9, 2021 at 12:34
  • $\begingroup$ Now that I have searched further and found nothing, I will use your suggested solution after all. Supposedly you should avoid ops in add-ons, but it all seems to work great. Thanks for your help! $\endgroup$
    – Andi
    Commented Nov 10, 2021 at 9:38

You must log in to answer this question.

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