1+ #if WITH_EDITOR
2+
3+ #include " UnrealEnginePythonPrivatePCH.h"
4+
5+ #include " UEPySPythonShelf.h"
6+
7+ #include " Editor/ContentBrowser/Public/ContentBrowserModule.h"
8+ #include " Editor/ContentBrowser/Public/IContentBrowserSingleton.h"
9+
10+ static PyObject *ue_PySPythonShelf_str (ue_PySPythonShelf *self)
11+ {
12+ return PyUnicode_FromFormat (" <unreal_engine.SPythonShelf '%p'>" ,
13+ self->s_compound_widget .s_widget .s_widget );
14+ }
15+
16+ static PyMethodDef ue_PySPythonShelf_methods[] = {
17+ { NULL } /* Sentinel */
18+ };
19+
20+ PyTypeObject ue_PySPythonShelfType = {
21+ PyVarObject_HEAD_INIT (NULL , 0 )
22+ " unreal_engine.SPythonShelf" , /* tp_name */
23+ sizeof (ue_PySPythonShelf), /* tp_basicsize */
24+ 0 , /* tp_itemsize */
25+ 0 , /* tp_dealloc */
26+ 0 , /* tp_print */
27+ 0 , /* tp_getattr */
28+ 0 , /* tp_setattr */
29+ 0 , /* tp_reserved */
30+ 0 , /* tp_repr */
31+ 0 , /* tp_as_number */
32+ 0 , /* tp_as_sequence */
33+ 0 , /* tp_as_mapping */
34+ 0 , /* tp_hash */
35+ 0 , /* tp_call */
36+ (reprfunc)ue_PySPythonShelf_str, /* tp_str */
37+ 0 , /* tp_getattro */
38+ 0 , /* tp_setattro */
39+ 0 , /* tp_as_buffer */
40+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
41+ " Unreal Engine SPythonShelf" , /* tp_doc */
42+ 0 , /* tp_traverse */
43+ 0 , /* tp_clear */
44+ 0 , /* tp_richcompare */
45+ 0 , /* tp_weaklistoffset */
46+ 0 , /* tp_iter */
47+ 0 , /* tp_iternext */
48+ ue_PySPythonShelf_methods, /* tp_methods */
49+ };
50+
51+ static int ue_py_spython_shelf_init (ue_PySPythonShelf *self, PyObject *args, PyObject *kwargs) {
52+ PyObject *py_classes_iterable = nullptr ;
53+ PyObject *py_collections_iterable = nullptr ;
54+
55+ PyObject *py_callable_double_clicked = nullptr ;
56+ PyObject *py_callable_get_context_menu = nullptr ;
57+
58+ char *kwlist[] = { (char *)" classes" , (char *)" collections" , (char *)" on_asset_double_clicked" , (char *)" on_get_asset_context_menu" , nullptr };
59+ if (!PyArg_ParseTupleAndKeywords (args, kwargs, " |OOOO" , kwlist,
60+ &py_classes_iterable,
61+ &py_collections_iterable,
62+ &py_callable_double_clicked,
63+ &py_callable_get_context_menu)) {
64+ return -1 ;
65+ }
66+
67+ if (py_classes_iterable) {
68+ py_classes_iterable = PyObject_GetIter (py_classes_iterable);
69+ if (!py_classes_iterable) {
70+ PyErr_SetString (PyExc_Exception, " argument is not an iterable" );
71+ return -1 ;
72+ }
73+ }
74+
75+ if (py_collections_iterable) {
76+ py_collections_iterable = PyObject_GetIter (py_collections_iterable);
77+ if (!py_collections_iterable) {
78+ PyErr_SetString (PyExc_Exception, " argument is not an iterable" );
79+ return -1 ;
80+ }
81+ }
82+
83+ if (py_callable_double_clicked && !PyCallable_Check (py_callable_double_clicked)) {
84+ PyErr_SetString (PyExc_Exception, " argument is not callable" );
85+ return -1 ;
86+ }
87+
88+ FContentBrowserModule& module = FModuleManager::Get ().LoadModuleChecked <FContentBrowserModule>(TEXT (" ContentBrowser" ));
89+
90+ FAssetPickerConfig asset_picker_config;
91+ asset_picker_config.InitialAssetViewType = EAssetViewType::Tile;
92+ asset_picker_config.bAllowNullSelection = false ;
93+ asset_picker_config.bShowBottomToolbar = false ;
94+ asset_picker_config.bAutohideSearchBar = false ;
95+
96+ if (py_classes_iterable) {
97+ while (PyObject *item = PyIter_Next (py_classes_iterable)) {
98+ if (PyUnicode_Check (item)) {
99+ FName class_name = FName (UTF8_TO_TCHAR (PyUnicode_AsUTF8 (item)));
100+ asset_picker_config.Filter .ClassNames .Add (class_name);
101+ }
102+ }
103+ Py_DECREF (py_classes_iterable);
104+ }
105+
106+ if (py_collections_iterable) {
107+ while (PyObject *item = PyIter_Next (py_collections_iterable)) {
108+ if (PyUnicode_Check (item)) {
109+ FName collection_name = FName (UTF8_TO_TCHAR (PyUnicode_AsUTF8 (item)));
110+ asset_picker_config.Collections .Add (FCollectionNameType (collection_name, ECollectionShareType::CST_Local));
111+ }
112+ }
113+ Py_DECREF (py_collections_iterable);
114+ }
115+
116+ if (py_callable_double_clicked) {
117+ FOnAssetDoubleClicked handler;
118+ UPythonSlateDelegate *py_delegate = NewObject<UPythonSlateDelegate>();
119+ py_delegate->SetPyCallable (py_callable_double_clicked);
120+ py_delegate->AddToRoot ();
121+ handler.BindUObject (py_delegate, &UPythonSlateDelegate::OnAssetDoubleClicked);
122+
123+ asset_picker_config.OnAssetDoubleClicked = handler;
124+ }
125+
126+
127+ self->s_compound_widget .s_widget .s_widget_owned = module .Get ().CreateAssetPicker (asset_picker_config);
128+ self->s_compound_widget .s_widget .s_widget = &self->s_compound_widget .s_widget .s_widget_owned .Get ();
129+ return 0 ;
130+ }
131+
132+ void ue_python_init_spython_shelf (PyObject *ue_module) {
133+ ue_PySPythonShelfType.tp_new = PyType_GenericNew;
134+
135+ ue_PySPythonShelfType.tp_init = (initproc)ue_py_spython_shelf_init;
136+
137+ ue_PySPythonShelfType.tp_base = &ue_PySCompoundWidgetType;
138+
139+ if (PyType_Ready (&ue_PySPythonShelfType) < 0 )
140+ return ;
141+
142+ Py_INCREF (&ue_PySPythonShelfType);
143+ PyModule_AddObject (ue_module, " SPythonShelf" , (PyObject *)&ue_PySPythonShelfType);
144+ }
145+
146+
147+ #endif
0 commit comments