Arma Reforger Explorer
1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Toggle main menu visibility
Loading...
Searching...
No Matches
BlenderRestAPI.c
Go to the documentation of this file.
1
// // The BlenderRestAPI class provides a way to communicate with Blender from Workbench using a REST API.
2
// // See BlenderEndpoints.c for info about available endpoints.
3
//
4
// // Example:
5
//
6
// // Create a connection to Blender.
7
// // This will handle launching the Blender process and starting the HTTP server.
8
// // If a port is not specified, a random port will be used in the range (50000-65000).
9
// // Multiple connections can exist at a time.
10
// auto connection = BlenderRestAPI.Create();
11
//
12
// // Load the specified .blend file.
13
// auto open_blend_result = OpenBlendFile(connection, "W:/my_blender_file.blend");
14
//
15
// if (!open_blend_result)
16
// {
17
// Print("Unable to open blend file", LogLevel.ERROR);
18
// return;
19
// }
20
//
21
// // Get a list of the objects in the scene
22
// array<string> objects = GetObjects(connection);
23
//
24
// foreach (string object : objects) {
25
// auto transform = GetTransform(connection, object);
26
// Print(transform.location);
27
// Print(transform.rotation);
28
// Print(transform.scale);
29
// }
30
// connection.Destroy();
31
// return;
32
33
34
35
enum
BlenderRestAPIState
{
36
WAITING
= 0,
37
SUCCESS
= 1,
38
ERROR
= 2,
39
};
40
41
class
BlenderRestAPI
42
{
43
private
void
OnRestSuccessCallback(
RestCallback
cb)
44
{
45
m_RestState =
BlenderRestAPIState
.SUCCESS;
46
m_Response = cb.GetData();
47
}
48
49
private
void
OnRestErrorCallback(
RestCallback
cb)
50
{
51
m_RestState =
BlenderRestAPIState
.ERROR;
52
}
53
54
55
// HTTP API
56
57
bool
Get(
string
endpoint, out
string
data
=
""
)
58
{
59
this.m_Response =
""
;
60
m_RestState =
BlenderRestAPIState
.WAITING;
61
62
m_RestContext.GET(m_RestCallback, endpoint);
63
64
while
(m_RestState ==
BlenderRestAPIState
.WAITING) {
65
Sleep(10);
66
}
67
68
data
= m_Response;
69
return
m_RestState ==
BlenderRestAPIState
.SUCCESS;
70
}
71
72
// TODO POST, maybe others?
73
74
75
static
BlenderRestAPI
Create(
int
port = -1)
76
{
77
string
path
;
78
79
if
(!EBTConfigPlugin.GetDefaultBlenderPath(
path
)) {
80
Print
(
"Failed to get Blender path. Make sure you have Blender setup using EBT: Config"
,
LogLevel
.ERROR);
81
return
null;
82
}
83
84
// See https://en.wikipedia.org/wiki/Ephemeral_port
85
if
(port == -1) {
86
port = 50000 +
Math
.RandomInt(0, 15000);
87
}
88
89
string
address =
string
.Format(
"http://localhost:%1"
, port);
90
91
string
python_command =
string
.Format(
"import bpy; bpy.ops.ebt.start_http_server(port=%1)"
, port);
92
string
cmd =
string
.Format(
"\"%1\" --background --python-expr \"%2\""
,
path
, python_command);
93
94
Print
(
string
.Format(
"Running Blender Instance on %1"
, address));
95
96
auto
blender_process = Workbench.RunProcess(cmd);
97
98
if
(!blender_process || !Workbench.IsRunningProcess(blender_process))
99
{
100
Print
(
"Unable begin Blender process"
,
LogLevel
.ERROR);
101
return
null;
102
}
103
104
auto
rest_context =
GetGame
().GetRestApi().GetContext(address);
105
106
if
(!rest_context)
107
{
108
Print
(
"Unable to obtain REST context for "
+ address,
LogLevel
.ERROR);
109
return
null;
110
}
111
112
auto
instance =
BlenderRestAPI
();
113
instance.m_BlenderProcess = blender_process;
114
instance.m_RestContext = rest_context;
115
instance.m_RestContext.SetTimeout(200);
116
instance.m_RestCallback =
RestCallback
();
117
instance.m_RestCallback.SetOnSuccess(instance.OnRestSuccessCallback);
118
instance.m_RestCallback.SetOnError(instance.OnRestErrorCallback);
119
120
return
instance;
121
}
122
123
void
Destroy() {
124
if
(Workbench.IsRunningProcess(m_BlenderProcess)) {
125
Workbench.KillProcess(m_BlenderProcess);
126
}
127
}
128
129
private
ProcessHandle m_BlenderProcess;
130
private
RestContext
m_RestContext;
131
private
ref
RestCallback
m_RestCallback;
132
133
// Response Handling
134
private
string
m_Response =
""
;
135
private
BlenderRestAPIState
m_RestState =
BlenderRestAPIState
.WAITING;
136
}
137
138
path
string path
Definition
AddonBuildInfoTool.c:249
BlenderRestAPIState
BlenderRestAPIState
Definition
BlenderRestAPI.c:35
GetGame
ArmaReforgerScripted GetGame()
Definition
game.c:1398
SUCCESS
@ SUCCESS
Definition
SCR_AICombatMoveState.c:5
WAITING
@ WAITING
Definition
SCR_AICommsHandler.c:14
data
Get all prefabs that have the spawner data
Definition
SCR_EntityCatalogManagerComponent.c:320
BlenderRestAPI
Definition
BlenderRestAPI.c:42
Math
Definition
Math.c:13
RestCallback
Definition
RestCallback.c:72
RestContext
Script accessible REST context.
Definition
RestContext.c:14
Print
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
LogLevel
Enum with severity of the logging message.
Definition
LogLevel.c:14
ERROR
@ ERROR
Same as DELETE, but global error count for OnAfterLoad is affected.
Definition
LogLevel.c:20
scripts
WorkbenchGameCommon
EnfusionBlenderTools
BlenderAPI
BlenderRestAPI.c
Generated by
1.17.0