6
$\begingroup$

I'm creating a custom property for each object in my scene but don't want the user to be able to edit it. Is it possible to block the property in ui ? Here is my code :

import bpy

allObjects = list(bpy.data.objects)

for element in allObjects:
    element["myProp"] = "My value"
$\endgroup$

2 Answers 2

9
$\begingroup$

You might create a custom property and pass only a get method:

import bpy

#default getter
def get_my_prop(self):
    return self['my_prop']

#omit the setter with a given getter to make it read-only
bpy.types.Object.my_prop = bpy.props.StringProperty(get=get_my_prop)

for obj in bpy.context.selected_objects:
    #assign the value directly
    obj['my_prop'] = "My Value"

It looks like this in the properties panel in blender 2.71: getter

$\endgroup$
4
  • $\begingroup$ That works very well but when I close and open the blend file, the custom property is editable again. I tried to use the options={'HIDDEN'} in my stringProperty but it doesn't hide my property... $\endgroup$
    – Caligari
    Jul 16, 2014 at 9:44
  • $\begingroup$ How can I keep the property not editable by the user from a session to another? I don't need to have it visible in the interface. $\endgroup$
    – Caligari
    Jul 16, 2014 at 9:50
  • 1
    $\begingroup$ You can either check the Register option in the text editor to run the script when the blend file is loaded or create an addon. #1 #2 $\endgroup$ Jul 16, 2014 at 22:18
  • 2
    $\begingroup$ In order to run the script upon scene loading, you could add your function in a permanent handler, with an @persistent decorator, as seen here: blender.org/documentation/blender_python_api_2_60_6/… This way, when you load a scene, the script runs automatically. $\endgroup$ Jul 17, 2014 at 12:59
0
$\begingroup$

I was not satisifed with the way the field is dimmed and the value can't be accessed in a color field when it is read-only.

I added custom get and set methods to ensure the user can't modify it from the interface, while being able to click the field and copy the values inside the RGB widget.

import bpy


bpy.types.Object.my_prop = bpy.props.FloatVectorProperty(
    subtype="COLOR", 
    set=lambda s, c: None, 
    get=lambda s: s["my_prop"], 
    min=0, 
    max=1
)


for obj in bpy.data.objects:
    obj["my_prop"] = (0.5, 0.3, 0.2)

enter image description here

You can still change the property with a custom Hex value using the interface, but I guess this is managed differently so I don't know how to prevent that. You can also change the HSV sliders but the changes will be discarded when you exit the widget.

$\endgroup$

You must log in to answer this question.

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