1
$\begingroup$

Very beginner question, but after extensive searching through the addon tutorials and similar questions I do not see a clear way to run an existing enabled add on via the Python API.

In this case I am trying to run the Cell Fracture plugin on a new plane:

import bpy
bpy.ops.mesh.primitive_plane_add(size=50)
bpy.ops.object.particle_system_add()
# run cell fracture plugin 

I do have the add on:

import addon_utils
for mod in addon_utils.modules():
    if mod.bl_info['name'] == 'Cell Fracture': 
        print(mod.bl_info)

# {'name': 'Cell Fracture', 'author': 'ideasman42, phymec, Sergey Sharybin', 'version': (0, 2), 'blender': (2, 80, 0), 'location': 'Viewport Object Menu -> Quick Effects', 'description': 'Fractured Object, Bomb, Projectile, Recorder', 'warning': '', 'wiki_url': 'http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Object/CellFracture', 'category': 'Object'}

But I don't see anything named cell anything in bpy.ops.object or bpy.context.preferences.addons or even sys.modules.

Thanks for any help!

$\endgroup$
3
$\begingroup$

Addons register panels and operators

An addon when enabled registers panels and operators and properties etc it uses. An operator, or set of, is generally the means an addon uses to "run".

The addon tells us where to look for the operator in the UI, in this case object menu > quick effects > Cell Fracture. With developer extras turned on will see the operator is bpy.ops.object.add_fracture_cell()

enter image description here

To the python console. What's important here is the module name, using code from question

>>> for mod in addon_utils.modules():
...     if mod.bl_info['name'] == 'Cell Fracture': 
...         mod
...         
<module 'object_fracture_cell' from '/home/batfinger/blender-git/v2.8/bin/2.83/scripts/addons/object_fracture_cell/__init__.py'>

To enable the addon

>>> addon_utils.enable("object_fracture_cell")
<module 'object_fracture_cell' from '/home/batfinger/blender-git/v2.8/bin/2.83/scripts/addons/object_fracture_cell/__init__.py'>

Note this pretty much calls the modules register() method. and could also be done via

object_fracture_cell.register()

Inspect the module. Could eyeball this, via code or UI as described above.

Similarly to https://blender.stackexchange.com/a/163366/15543 instead of panels will look for operators

>>> for p in dir(object_fracture_cell):
...     x = getattr(object_fracture_cell, p)
...     if inspect.isclass(x) and issubclass(x, bpy.types.Operator):
...         x, getattr(x, "bl_idname", "None")
...         
(<class 'object_fracture_cell.FractureCell'>, 'object.add_fracture_cell_objects')
(<class 'bpy_types.Operator'>, "None")

>>> 

Telling us that cell fracture has one operator

bpy.ops.object.add_fracture_cell_objects()

Autocomplete gives us the arguments available to the operator

>>> bpy.ops.object.add_fracture_cell_objects(
add_fracture_cell_objects()
    >>> bpy.ops.object.add_fracture_cell_objects(
add_fracture_cell_objects()
bpy.ops.object.add_fracture_cell_objects(
        source={'PARTICLE_OWN'},
        source_limit=100, 
        source_noise=0, 
        cell_scale=(1, 1, 1), 
        recursion=0, 
        recursion_source_limit=8, 
        recursion_clamp=250, 
        recursion_chance=0.25, 
        recursion_chance_select='SIZE_MIN', 
        use_smooth_faces=False, 
        use_sharp_edges=True, 
        use_sharp_edges_apply=True, 
        use_data_match=True, 
        use_island_split=True, 
        margin=0.001, 
        material_index=0, 
        use_interior_vgroup=False, 
        mass_mode='VOLUME', 
        mass=1, 
        use_recenter=True, 
        use_remove_original=True, 
        collection_name="", 
        use_debug_points=False, 
        use_debug_redraw=True, 
        use_debug_bool=False)
(undocumented operator)

In lieu of usng documentation link, while migratory

$\endgroup$
2
  • 1
    $\begingroup$ Thank you so much for the detailed response, that works perfectly and gave me a great amount of relative knowledge as well I am sure that will come in handy! $\endgroup$ Jan 19 '20 at 4:23
  • $\begingroup$ Great explanation. Thanks a lot! $\endgroup$ Jul 10 at 6:49

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.