BallanceBlenderHelper/bbp_ng/OP_OBJECT_snoop_group_then_to_mesh.py

51 lines
2.0 KiB
Python
Raw Normal View History

import bpy
import typing
from . import PROP_virtools_group
class BBP_OT_snoop_group_then_to_mesh(bpy.types.Operator):
"""Convert selected objects into mesh objects and try to copy the Virtools Group infos of their associated curve bevel object if they have. """
bl_idname = "bbp.snoop_group_then_to_mesh"
bl_label = "Snoop Group then to Mesh"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
return len(context.selected_objects) != 0
def execute(self, context):
for obj in context.selected_objects:
# skip all non-curve object
if obj.type != 'CURVE': continue
# fetch curve data block
curve: bpy.types.Curve = typing.cast(bpy.types.Curve, obj.data)
# if bevel mode is not object, skip
if curve.bevel_mode != 'OBJECT': continue
# if bevel object is None, skip
bevel_obj: bpy.types.Object | None = curve.bevel_object
if bevel_obj is None: continue
# copy bevel object group info into current object
# MARK: VirtoolsGroupsHelper is self-mutex.
# we only can operate one VirtoolsGroupsHelper at the same time.
# so we extract bevel object group infos then apply to target later.
group_infos: tuple[str, ...]
with PROP_virtools_group.VirtoolsGroupsHelper(bevel_obj) as bevel_gp:
group_infos = tuple(bevel_gp.iterate_groups())
with PROP_virtools_group.VirtoolsGroupsHelper(obj) as this_gp:
this_gp.clear_groups()
this_gp.add_groups(group_infos)
# convert all selected object to mesh
# no matter the success of copying virtools group infos and whether selected object is curve
bpy.ops.object.convert(target = 'MESH')
return {'FINISHED'}
def register() -> None:
bpy.utils.register_class(BBP_OT_snoop_group_then_to_mesh)
def unregister() -> None:
bpy.utils.unregister_class(BBP_OT_snoop_group_then_to_mesh)