5
$\begingroup$

In Blender, using Python you can create objects via:

import bpy
obdata = bpy.data.meshes.new('name')
ob = bpy.data.objects.new('name', obdata)

This would create an empty mesh, which could later on be linked to a scene etc.

But what if one would like to extend the possibilities a bit, and generate something like a custom mesh type? Something like:

import bpy
obdata = bpy.data.my_custom_mesh_type.new('name')
ob = bpy.data.objects.new('name', obdata)

where my_custom_mesh_type is defined via:

class MyCustomMeshType(bpy.types.Mesh)
    ...

Is this possible at all?

$\endgroup$
7
  • $\begingroup$ I believe it's not possible because you try to change the syntax of the python Api itself. $\endgroup$
    – Brain
    Sep 29, 2017 at 6:27
  • $\begingroup$ @Brain I don't quite understand your comment. What do you mean with 'You try to change the syntax of the Python API'? $\endgroup$
    – aliasguru
    Sep 29, 2017 at 6:56
  • $\begingroup$ @aliasguru can you elaborate on "why". How would this mesh be different from a mesh? and you'd need to have a new object type too, because data.objects.new("name", data) expects to be one of the known data types, or None for an empty object. You can certainly "enhance" the mesh type with inheritance using my_enhanced_mesh = MyCustomMeshType(mesh) but I believe you would still need to pass mesh to data.objects.new(...) $\endgroup$
    – batFINGER
    Oct 11, 2017 at 12:23
  • $\begingroup$ @batFINGER I was thinking about abusing the system. I have an existing self-created Add-on, which stores a bunch of properties (a mid number I'd say, so a few hundred or a lower thousand number of items - strings, integers, enums, etc). Currently, I store that in a scene. However, the drawback is that we're using multiple scenes, and I always need to reference a specific one of them. Also, one of the users expressed a wish to be able to link that data into an empty file and use it there, while still having the link intact. So the source file changes -> target file updates. $\endgroup$
    – aliasguru
    Oct 11, 2017 at 17:37
  • $\begingroup$ @batFINGER So my thinking was to attatch those properties to one of the object types, and never even link them to a scene. Just have them in bpy.data with a fake user. But: I don't want all of those to show up in EVERY mesh or empty or curve or whatever type I'm abusing for this task, to avoid slowdowns for other users. Thus, I thought "hey, just invent a new data type, which no one will ever use". Plan B is to attatch the data to something else, like the Speaker type, which we never use. But I'm curious if there's a solution to the riddle. $\endgroup$
    – aliasguru
    Oct 11, 2017 at 17:39

3 Answers 3

2
+50
$\begingroup$

Python is just a way to access Blender data. You can of course extend Blender itself, but that requires changing its C code.

Of course nothing is stopping you from creating custom Python classes, but these won't be part of the blend file.

$\endgroup$
1
2
$\begingroup$

One can extend the objects in Blender (if you want to make your own). For example take a look at BoltFactory, IvyGen etc, many addons add objects. However keep in mind that Blender Python is implemented as an API Adding stuff by inheriting from a base class is possible in python however the rest of the program will not know it, ea dont suspect that cycles or eevee or fluidsims or particles will understand your custom object attributes /methods.

If its only about adding some extra valeu property (maybe fro some other script) then in the normal view of object properties you can add custom attributes as well, perhaps thats usefull to you. (object tab => add custom properties)

You might not even require python to do so, but if you want to that in code:

bpy.ops.wm.properties_edit(data_path="object", property="demo", value="1.0", min=0, max=1, use_soft_limits=False, soft_min=0, soft_max=1, description="this is demo tool tip")
$\endgroup$
3
  • $\begingroup$ Interesting approach, it only has one flaw for me unfortunately: Properties attatched to Objects in the way you described can only be of type int, float, string or arrays of such. I'm using collection properties in my backend implementation, so I can't really remap that to simple object props. $\endgroup$
    – aliasguru
    Oct 12, 2017 at 9:44
  • $\begingroup$ not sure what you do, a string usually con convert to arrays and visa versa While collections can usually be described in XML or Json, and can be turned into a string as well (a xml without line breaks). Perhaps this is usefull for you : tutorialspoint.com/python/python_xml_processing.htm $\endgroup$
    – Peter
    Oct 12, 2017 at 9:57
  • 1
    $\begingroup$ @aliasguru I store any data (collections also) as strings in objects without issue. It's probably the best way. The data just has to be encoded/decoded into/from strings. $\endgroup$ Oct 12, 2017 at 20:15
1
$\begingroup$

You can extend Blender data types by defining custom properties like that:

bpy.types.Object.my_prop = bpy.props.PointerProperty(type=bpy.types.Object, name="My prop", description="this is the description")

obj = bpy.context.active_object
obj.my_prop = another_obj
$\endgroup$

You must log in to answer this question.

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