Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Added support for python modules compiled with python versions other than 3.9
- Enable access to graph user variables via `user_vars` in Python Node scripts - #20

## [0.5.0] - 2025-07-13
### Added
Expand Down
43 changes: 40 additions & 3 deletions src/nodes/gtpy_pythonnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "gt_sharedfunction.h"

#include <intelli/nodedata.h>
#include <intelli/graphuservariables.h>
#include <intelli/data/object.h>
#include <intelli/data/string.h>
#include <intelli/data/double.h>
Expand All @@ -37,6 +38,9 @@
#include <QMenu>
#include <QDir>

namespace
{

// This raw strings is used for the internal serialization and deserialization
// of python classes
const QString p_deser_script = R"(
Expand All @@ -54,6 +58,35 @@ __deserialized_data_tmp = pickle.dumps($$var_name$$)
#gtError() << "Serialization Scipt execution finished"
)";

PyPPObject dictFromUserVars(const intelli::GraphUserVariables& vars)
{
GTPY_GIL_SCOPE

auto dict = PyPPDict_New();

vars.visit([&dict](const QString& key, const QVariant& value){
PyPPDict_SetItem(dict, key.toStdString().c_str(),
PyPPObject::fromQVariant(value));
});

return dict;
}

void userVarsToPython(int contextId, const intelli::GraphUserVariables* vars)
{
if (!vars) return;

GTPY_GIL_SCOPE

auto contextPtr = GtpyContextManager::instance()->contextPointer(contextId);
if (contextPtr.isNull()) return;

auto module = PyPPObject::Borrow(contextPtr.object());
PyPPModule_AddObject(module, "user_vars", dictFromUserVars(*vars));
}

}

void
gt::nodes::python::deserializeToContext(int context,
const intelli::ByteArrayData* pvd,
Expand Down Expand Up @@ -159,10 +192,13 @@ GtpyPythonNode::GtpyPythonNode() :
GTPY_GIL_SCOPE

GtpnPythonScriptDialog dialog;

dialog.setScript(m_script);

auto contextId = dialog.context();

/// try to add to context to have auto completion
deserializePythonData(dialog.context());
deserializePythonData(contextId);
userVarsToPython(contextId, userVariables());

QSettings settings;

Expand Down Expand Up @@ -283,6 +319,7 @@ GtpyPythonNode::eval()
/// handle python input variables and add also rest of the input ports
/// to context
deserializePythonData(context);
userVarsToPython(context, userVariables());

if (!GtpyContextManager::instance()->evalScript(context, script()))
{
Expand Down Expand Up @@ -350,7 +387,7 @@ GtpyPythonNode::deserializePythonData(int context)
}
}

GtpyContextManager::instance()->evalScript(context, "");
GtpyContextManager::instance()->evalScript(context, "", false);
}

void
Expand Down