2
$\begingroup$

I checked Batex addon source code and other sources on internet

Is there no safer way to define properties?

I mean, what if an addon sets the same property in Scene for instance ?

bpy.types.Scene.apply_transform = BoolProperty(name="Apply transform",
            default=True,
            description="Applies scale and transform (Experimental)")

this looks so "bad design"

$\endgroup$
2
  • $\begingroup$ I think this is a topic for devtalk.blender.org $\endgroup$
    – Crantisz
    Apr 26 at 9:58
  • $\begingroup$ This is not bad design, as in any programming language the potential namespace collisions have to be handled by the programmer, not by the language. $\endgroup$
    – Gorgious
    Apr 26 at 11:03

2 Answers 2

3
$\begingroup$

This is definitely not a problem unique to Blender, nor even to Python. It's an old enough problem that it has been called by the name name collision for at least 30 years.

Wikipedia lists three of the well know methods of dealing with it. I'll enumerate others. None of them are perfect.

  1. Renaming - the least desirable. When you find the name you are using collides you simply change it.

  2. Prefixing - Try to find a unique prefix that you routinely use on your names, in the hopes of avoiding collisions. This is the oldest technique used for name collision among separate libraries in large systems.

  3. Encapsulation - Wikipedia calls this 'namespaces', but namespaces are a language specific implementation of encapsulation. There are other ways to accomplish it, as mentioned in another answer.

Often these three approaches are intermixed. This answer describes an approach where one form of encapsulation (enclosure in an object that hides the names) is combined with one form of prefixing.

Here are approaches not mentioned in Wikipedia:

  1. prefix registration - Prefixes are only a partial solution because prefixes can collide. To avoid this, there are situations where name collision is avoided by requiring prefixes to be registered on a first come first served basis. This is not widely used in programming, because it administratively difficult. It is routinely used in networking, where name collisions are far more likely.

  2. Guid assignment A Globally Unique Identifier (GUID) solves the administrative problem of prefix registration by automating the generation of such IDs. Microsoft uses this approach in some of their products, for instance.

$\endgroup$
3
  • $\begingroup$ Python should avoid these javascript-like nonsenses. I have been suggested to use addon preferences or property groups $\endgroup$
    – Phil
    Apr 27 at 10:03
  • 1
    $\begingroup$ @Phil neither of these things solve the problem. Add-on names can collide. $\endgroup$ Apr 27 at 13:50
  • $\begingroup$ well I understand the issue, a hash could be generated randomly and blender would just deal with it...anyway I just want to avoid adding stuffs in blender "built in" data. thx $\endgroup$
    – Phil
    Apr 28 at 15:40
3
$\begingroup$

I always try to use add-on name in such properties and pack them into property group

__init__.py
# vox - add-on name
bpy.types.Object.vox_settings = bpy.props.PointerProperty(
    type=vox_object_settings.vox_object_settings
)

vox_object_settings.py
class vox_object_settings(bpy.types.PropertyGroup):
    # in PropertyGroup I free to use any names:
    active: bpy.props.BoolProperty(name="Use as block", options=set(), default=True)
    ....
$\endgroup$
3
  • 1
    $\begingroup$ I'm sure you're aware of it but in python, class names should be CamelCase by convention. It's not enforced but it made me double take how you could set a variable as a type in the PointerProperty $\endgroup$
    – Gorgious
    Apr 26 at 11:06
  • $\begingroup$ which only postpones the problem to name collision among add-on names, themselves. $\endgroup$ Apr 26 at 16:08
  • $\begingroup$ @MartyFouts I agree, but there are more likely issues - for example than the add-on overrides part of the interface (had to deal with it in the past) $\endgroup$
    – Crantisz
    Apr 27 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.