1

I am trying to create a script that involves the creation and setup of a UV Project modifier. When it comes to set the UV Map of the projector, there is no problem at all:

import bpy

bpy.data.objects['piece'].select_set(True) bpy.ops.object.modifier_add(type='UV_PROJECT') bpy.context.object.modifiers["UVProject"].uv_layer = "UVMap"

But when I'm trying to set the projector object, I find no way to do it, as "projectors" property seems to be read-only:

https://docs.blender.org/api/current/bpy.types.UVProjectModifier.html?highlight=projection%20modifier

Q: Am I missing something or precisely that property is just not scriptable?

brockmann
  • 12,613
  • 4
  • 50
  • 93

1 Answers1

1

Similar to How to add modifiers using python script and set parameters?, the recommended way is to add a new modifier to an object using ObjectModifiers.new() which returns the reference to the modifier and allows to assign whatever you like:

import bpy

Get the object in context

obj = bpy.context.object

Add the modifier to the object in context

modifier = obj.modifiers.new(name="project", type='UV_PROJECT')

Properties

modifier.uv_layer = "UVMap" modifier.projector_count = 10

for p in modifier.projectors: p.object = bpy.context.scene.camera

projectors property is of type bpy_prop_collection, which is essentially a list of object references. In order to assign an object to each Object property of the modifier, you would have to assign an object reference like bpy.context.scene.camera.

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • Thank you very much. It worked just fine and did what I was expecting. My strategy consisted of seeing the info window and try to copy/paste instructions into a script in order to automatize them, but it seems it is not working like this. Best regards – Jaime Finat Jan 28 '21 at 19:25
  • Glad it helps @JaimeFinat – brockmann Jan 28 '21 at 19:51