0
$\begingroup$

im trying to set custom object properties on many objects. I found this python code in another thread here, and it does work to at least do it 1 object at a time via the Python panel, but altering the code to do it for all selceted objects would be even better. I know almost nothing about python, so my attempts to modify the code havent worked out. I also tried Bing AI, and it doesnt seem to know how to format Blender python, because i get all sorts of errors. Here's where im at so far...

import bpy

obj = bpy.context.active_object

float = "Opacity"
obj[float] = (1.0)
obj.id_properties_ensure()
property_manager = obj.id_properties_ui(float)
property_manager.update( 
default=1.0,
    min=0.0,
    max=1.0,
    subtype="FACTOR",
    precision=2
)
$\endgroup$
1
  • $\begingroup$ I would suggest you don't override builtins, float is a python builtin (hence orange color in the formatted code) which can have unintended consequences in your scripts $\endgroup$
    – Gorgious
    Commented Feb 28 at 12:06

1 Answer 1

1
$\begingroup$

I reckon it would be:

import bpy

for obj in bpy.context.selected_objects:

    float = "Opacity"
    obj[float] = (1.0)
    obj.id_properties_ensure()
    property_manager = obj.id_properties_ui(float)
    property_manager.update( 
    default=1.0,
        min=0.0,
        max=1.0,
        subtype="FACTOR",
        precision=2
    )

It's just a for loop iterating through each item in the list formed by bpy.context.selected_objects

$\endgroup$
4
  • $\begingroup$ you reckon correct! that worked. i suspected the FOR was needed, as i saw it in Bings attempt, but since there were other errors, i didnt realy know how to fix them. Would it be possible to turn that into some sort of menu accessible command? Maybe with a dialogue for options? I assume thats getting into the world of Add Ons? $\endgroup$
    – Eikon
    Commented Feb 27 at 18:20
  • $\begingroup$ Yes, but have you tried to look that up? There are tons of tutorials that show how to use python and bpy, tons of people on BSE who got similar questions before, there are even templates in Blender to do exactly what you ask. $\endgroup$
    – Lauloque
    Commented Feb 27 at 22:28
  • 1
    $\begingroup$ Have not looked yet. Im pretty new to blender, and as i said above, pretty much 0 python experience. So, it was more just a general question of it was possible. I'll look into it eventually. Thanks again $\endgroup$
    – Eikon
    Commented Feb 28 at 1:50
  • $\begingroup$ Check the Text Editor's Templates Menu. The Blender Foundation published a short video guide Scripting for Artists. These could be a nice starting point. $\endgroup$
    – Lauloque
    Commented Feb 28 at 12:31

You must log in to answer this question.

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