| 893 | } |
| 894 | |
| 895 | void register_python_operator_to_cpp(nb::object cls) { |
| 896 | std::string id = get_class_id(cls); |
| 897 | std::string label; |
| 898 | std::string description; |
| 899 | |
| 900 | if (nb::hasattr(cls, "label")) { |
| 901 | label = nb::cast<std::string>(cls.attr("label")); |
| 902 | } |
| 903 | if (nb::hasattr(cls, "description")) { |
| 904 | description = nb::cast<std::string>(cls.attr("description")); |
| 905 | } |
| 906 | |
| 907 | nb::object instance; |
| 908 | try { |
| 909 | instance = cls(); |
| 910 | } catch (const std::exception& e) { |
| 911 | LOG_ERROR("register_python_operator: failed to create instance for '{}': {}", id, e.what()); |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | bool has_poll = nb::hasattr(cls, "poll"); |
| 916 | bool has_invoke = nb::hasattr(instance, "invoke"); |
| 917 | bool has_execute = nb::hasattr(instance, "execute"); |
| 918 | bool has_modal = nb::hasattr(instance, "modal"); |
| 919 | bool has_cancel = nb::hasattr(instance, "cancel"); |
| 920 | bool has_undo = nb::hasattr(instance, "undo") && nb::hasattr(instance, "redo"); |
| 921 | |
| 922 | vis::op::OperatorDescriptor desc; |
| 923 | desc.python_class_id = id; |
| 924 | desc.label = label.empty() ? id : label; |
| 925 | desc.description = description; |
| 926 | desc.source = vis::op::OperatorSource::PYTHON; |
| 927 | desc.flags = vis::op::OperatorFlags::REGISTER; |
| 928 | if (has_undo) { |
| 929 | desc.flags = desc.flags | vis::op::OperatorFlags::UNDO; |
| 930 | } |
| 931 | if (has_modal) { |
| 932 | desc.flags = desc.flags | vis::op::OperatorFlags::MODAL; |
| 933 | } |
| 934 | |
| 935 | vis::op::CallbackOperator callbacks; |
| 936 | |
| 937 | if (has_poll) { |
| 938 | callbacks.poll = [class_id = id]() -> bool { |
| 939 | nb::gil_scoped_acquire gil; |
| 940 | nb::object instance = get_python_operator_instance(class_id); |
| 941 | if (!instance.is_valid() || instance.is_none()) { |
| 942 | LOG_ERROR("Python operator '{}' not found during poll", class_id); |
| 943 | return false; |
| 944 | } |
| 945 | try { |
| 946 | nb::object cls = nb::borrow(instance.type()); |
| 947 | return nb::cast<bool>(cls.attr("poll")(nb::none())); |
| 948 | } catch (const std::exception& e) { |
| 949 | LOG_ERROR("Operator poll error: {}", e.what()); |
| 950 | return false; |
| 951 | } |
| 952 | }; |
no test coverage detected