-
Notifications
You must be signed in to change notification settings - Fork 9
/
functions.php
executable file
·984 lines (946 loc) · 34.3 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
<?php
/**
* Copyright (C) 2007-2011 Olivier Amrein
* Author Olivier Amrein <[email protected]> 2007-2011
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This file is part of Brender.
*
* Brender is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 2 * of the License, or any later version.
*
* Brender is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with brender. If not, see <http://www.gnu.org/licenses/>.
*
* ***** END GPL LICENSE BLOCK *****
*
*/
date_default_timezone_set('Europe/Zurich'); # ----needed by php
function play_sound($sound) {
exec("afplay sounds/$sound");
}
function test() {
global $qwer;
print "qwer = $qwer\n";
}
function output($msg,$type = "info") {
# function used in the command line scripts (brender_server.php and brender_client.php) for outputing and logging things
# output can be customized in future for different types
# currently used type = info, warning, error
if ($type <> "nolog") {
brender_log($msg);
}
$when = date('Y/d/m H:i:s');
$msg = "$when : $type : $msg";
print "$msg\n";
}
function debug($msg) {
if (isset($_SESSION['debug'])) {
if ($_SESSION['debug'] == 1) {
# only display debug message on web interface when _SESSION debug is enabled
print "**** DEBUG ***** :: $msg<br/>";
}
}
if (isset($GLOBALS['debug_mode'])) {
# --- for command_line we display debug messages if debug_mode global is on
print "**** DEBUG ***** :: $msg\n";
}
}
function notification($message,$type = "email") {
$when = date('Y/d/m H:i:s');
$message = "$when : $message";
print "NOTIFICATION : $message";
if ($type == "email") {
#mail('[email protected]', 'Brender Notification', $message);
}
else {
#sms('[email protected]', 'Brender Notification', $message);
}
}
function check_job_exists($job_id) {
$query = "SELECT COUNT(scene) FROM jobs WHERE id = '$job_id'";
$results = mysql_query($query);
$qq = mysql_result($results,0);
return $qq;
}
function check_client_exists($client) {
$query = "SELECT COUNT(client) FROM clients WHERE client = '$client'";
$results = mysql_query($query);
$qq = mysql_result($results,0);
return $qq;
}
function get_client_status($client) {
$query = "SELECT status FROM clients WHERE client = '$client'";
$results = mysql_query($query);
$qq = mysql_result($results,0);
return $qq;
}
function get_client_os($client) {
$query = "SELECT machine_os FROM clients WHERE client = '$client'";
$results = mysql_query($query);
$qq = mysql_result($results,0);
return $qq;
}
function get_priority_color($priority) {
if (preg_match("/^(\d+)$/",$priority)) {
if ($priority<10) {
return "#ff1111";
}
else if ($priority<20) {
return "#ffaaaa";
}
else if ($priority<60) {
return "#ddddaa";
}
else if ($priority<95) {
return "#ddcccc";
}
else {
return "#ffffff";
}
/* experimental code to color HEX according to priority value
$hex = dechex($priority*2.5);
$color = "ff".$hex.$hex;
#print("number $color <br/>");
return $color;
*/
}
else {
print "error : not a number";
}
}
function get_css_class($status) {
# we do some regex preg_match to get the status class
if (preg_match("/rendering/",$status)) {
return "rendering";
}
else if (preg_match("/idle/",$status)) {
return "idle";
}
else if (preg_match("/disabled/",$status)) {
return "disabled";
}
else if (preg_match("/enabled/",$status)) {
return "enabled";
}
else if (preg_match("/not running/",$status)) {
return "not_running";
}
else if (preg_match("/running/",$status)) {
# for server status
return "idle";
}
else if (preg_match("/died/",$status)) {
# for server status
return "not_running";
}
elsE if (preg_match("/pause/",$status)) {
return "pause";
}
else if (preg_match("/finished/",$status)) {
#print "GLOAB : ".$GLOBALs[last_finished];
if (isset($GLOBALS['last_finished'])) {
$GLOBALS['last_finished'] = 0;
return "finished";
}
else {
$GLOBALS['last_finished'] = 1;
return "finished2";
}
}
else if (preg_match("/waiting/",$status)) {
return "waiting";
}
else if (preg_match("/inactive/",$status)) {
return "inactive";
}
else if (preg_match("/active/",$status)) {
return "active";
}
else {
# in case we can not find a status type, return finished
return "finished";
}
}
function check_if_client_should_work($client_name = "check all") {
# function used for checking if a client is during his "working hours", these are set in the database for each client.
if ($client_name <> "check all") {
$query = "SELECT (DATE_FORMAT( NOW( ) , '%T' ) BETWEEN working_hour_start AND working_hour_end) as should_work FROM clients SELECT client = '$client_name'";
$results = mysql_query($query);
$qq = mysql_result($results,0);
return($qq);
}
#checking_alive_clients();
$query = "SELECT DATE_FORMAT( NOW( ) , '%T' ) AS now,working_hour_start AS start,working_hour_end AS end,client,status,machine_type,(DATE_FORMAT( NOW( ) , '%T' ) BETWEEN working_hour_start AND working_hour_end) AS should_work FROM clients WHERE machine_type = 'workstation' AND status<>'not running'";
$results = mysql_query($query);
while ($row = mysql_fetch_object($results)) {
$client = $row->client;
$status = $row->status;
$start = $row->start;
$now = $row->now;
$end = $row->end;
$is_during_office_hours = $row->should_work;
print "$client is $status :: <br/>\n";
print "$now :: $start / $end ::::---- is during work hours? = $is_during_office_hours<br/>\n";
if (!$is_during_office_hours && $status == 'disabled') {
print "OHH $client should work, lets enable him<br/>\n";
send_order($client,"enable","","5");
}
if ($is_during_office_hours && $status == 'idle') {
print "OHH $client should not work, lets disable him<br/>\n";
send_order($client,"disable","artist@work","5");
set_info($client,"artist@work");
}
}
}
function check_project_is_active($project_id) {
$query = "SELECT COUNT(name) FROM projects WHERE id = '$project_id' AND status = 'active'";
$results = mysql_query($query);
$check_result = mysql_result($results,0);
return $check_result;
}
function check_project_exists($project) {
$query = "SELECT count(name) FROM projects WHERE name = '$project'";
$results = mysql_query($query);
$check_result = mysql_result($results,0);
return $check_result;
}
function get_path($project,$what,$os = "NONE") {
# get the output/blend path for a specific project and specific OS
#$path = $what."_".$GLOBALS['os'];
if (!check_project_exists($project)) {
debug("project $project not found");
return 0;
}
if ($os == "NONE") {$os = $GLOBALS['os'];};
$path = $what."_".$os;
$query = "SELECT $path FROM projects WHERE name = '$project'";
$results = mysql_query($query);
$qq = mysql_result($results,0);
debug (" ++++ + + + + + + + + + +GETTING PATH $query path = $path");
return $qq;
}
function windowsify_paths($path) {
#print("dir sep = " +DIRECTORY_SEPARATOR);
#if (DIRECTORY_SEPARATOR == "\/") {
# $new_path = preg_replace('/', DIRECTORY_SEPARATOR, $path);
#}
$new_path = preg_replace("/\//","\\",$path); #change slash to backslash
$newer_path = preg_replace("/\'/i","",$new_path); # needed because windows
debug("\n ***** $path ****** \n ----becomes ----- \n ******* $new_path ****** ------\n");
return $newer_path;
}
function get_blender_path() {
$query = "SELECT blender_local_path FROM clients WHERE client = '$GLOBALS[computer_name]'";
$results = mysql_query($query);
$local_path = mysql_result($results,0);
debug("****************** query = $query *****************\n");
debug("****************** local path = $local_path *****************\n");
if ($local_path) {
if (file_exists($local_path)) {
# --- there is a local_path set in the client table, so we return it
output("blender local path found :: $local_path .... niiiice!");
return $local_path;
}
else {
output("blender local path not found :: $local_path .... switching to server one","warning");
}
}
# ---there is no local_path, so we take the blender form the brender_root
switch($GLOBALS['os']) {
case "mac":
$path = "blender_bin/mac/blender.app/Contents/MacOS/blender";
break;
case "linux":
$path = "blender_bin/linux/blender";
break;
case "windows":
$path = "blender_bin\windows\blender.exe";
break;
}
return $path;
}
function change_order_owner($id,$client) {
#function allowing a client to self-assign himself to an order
# used in case an order's client value is set to ANY, so this value becomes CLIENT and no other client will execute the order
$query = "UPDATE orders SET client = '$client' WHERE id = '$id'";
mysql_unbuffered_query($query);
print "become order query $query\n";
# print "### $client deleted order $id\n";
}
function delete_node($client) {
$query = "DELETE FROM clients WHERE client = '$client'";
debug("delete query $query");
output("NODE DELETED :: $client");
mysql_query($query);
}
function remove_order($id) {
$query = "DELETE FROM orders WHERE id = '$id'";
mysql_unbuffered_query($query);
#$os = $GLOBALS['os'];
#$client = $GLOBALS['computer_name'];
#print "### $client of $os deleted order $id\n";
}
function server_stop($pid){
$query = "UPDATE server_settings SET pid = '$pid',status = 'stopped',started = now()";
# print "\n query = $query ----\n";
mysql_unbuffered_query($query);
print "STOPPED SERVER \n";
stop();
}
function server_start($pid){
$query = "UPDATE server_settings SET pid = '$pid',status = 'running',started = now()";
# print "\n query = $query ----\n";
mysql_query($query);
print "STARTED SERVER\n";
}
function check_if_client_has_order_waiting($client) {
$query = "SELECT count(orders) FROM orders WHERE client = '$client'";
$results = mysql_query($query);
$check_result = mysql_result($results,0);
return $check_result;
}
function check_client_is_running($client) {
#---to check if client is running, we send him a ping order. If he is alive, it will remove the order. If not the order will still be there after 3 sconds, meaning the server is ko
$client_status = get_client_status($client);
if ($client_status == "not running") {
return 0;
}
else if (preg_match("/rendering/",$client_status)) {
return 1;
}
send_order("$client","ping","","1");
sleep(3);
$check_query = "SELECT COUNT(orders) FROM orders WHERE orders = 'ping' AND client = '$client'";
$results = mysql_query($check_query);
$ping_result = mysql_result($results,0);
#print "ping res = $ping_result\n";
if ($ping_result == 0) {
# the ping order is still there so it sems the client is dead
return 1;
}
}
function check_server_is_dead() {
#---to check if server is running, we send him a ping order. If he is alive, it will remove the order. If not the order will still be there after 3 sconds, meaning the server is ko
send_order("server","ping","","1");
sleep(3);
$check_query = "SELECT COUNT(orders) FROM orders WHERE orders = 'ping' AND client = 'server'";
$results = mysql_query($check_query);
$ping_result = mysql_result($results,0);
return $ping_result;
}
function check_server_status(){
# print "<br/>get server status<br/>";
# command to see if the server is running or dead
#brender_log("I CHECK SERVER STATUS");
if (check_server_is_dead()){
#$GLOBALS['computer_name'] = "web_interface";
set_server_settings("status","died");
set_server_settings("pid","0");
set_server_settings("started","now()");
brender_log("server not responding (PING)");
brender_log("SERVER DIED");
//$color = "red";
$status = "SERVER DIED !!!!!!!!";
}
else {
set_server_settings("status","running");
//$color = "green";
$status = "server is running";
}
return $status;
}
function get_server_settings($setting){
$query = "SELECT $setting FROM server_settings;";
$results = mysql_query($query);
$status = mysql_result($results,0);
# debug("QUERY SERVER SETTINGS = $query");
return $status;
}
function set_server_settings($key,$value){
$query = "UPDATE server_settings SET $key = '$value'";
mysql_unbuffered_query($query);
# print "### $client status : $status $rem\n";
}
function update_lastseen($client="NONE"){
if ($client == "NONE") {
$client = $GLOBALS['computer_name'];
}
$query = "UPDATE clients SET lastseen=NOW() WHERE client = '$client'";
mysql_unbuffered_query($query);
}
function set_info($client,$info){
#$rem = str_replace("'","\'",$rem);
$query = "UPDATE clients SET info = '$info' WHERE client = '$client'";
mysql_unbuffered_query($query);
print "### INFO $client status : $info\n";
}
function get_info($client) {
$query = "SELECT info FROM clients WHERE client = '$client';";
#print "*************** infoquery $query\n";
$results = mysql_query($query);
$info = mysql_result($results,0);
#print "*************** info $info\n";
return $info;
}
function set_status($client,$status,$rem = ""){
$rem = str_replace("'","\'",$rem);
$query = "UPDATE clients SET status = '$status',rem = '$rem' WHERE client = '$client'";
mysql_unbuffered_query($query);
#print "### $client status : $status $rem\n$query\n";
}
function get_status($client) {
$query = "SELECT status FROM clients WHERE client = '$client'";
$results = mysql_query($query);
$qq = mysql_result($results,0);
return $qq;
}
function send_order($client,$orders,$rem="",$priority=30){
#print "------send_order var = $client, $orders, $rem, $priority----\n";
$query = "INSERT INTO orders (client, created, orders, priority, rem) VALUES('$client',NOW(),'$orders','$priority','$rem')";
# print "********************** order query = $query\n";
mysql_unbuffered_query($query);
}
function brender_log($log){
$computer_name = $GLOBALS['computer_name'];
$log = preg_replace("/\n$/","",$log); # we erase the trailing carriage return to avoid empty lines in the log file
$prefix = ""; // initialize prefix variable
$slash = "/"; //Normally use forward slash
$who ="";
if ($computer_name == "web_interface") {
$who = "(".$_SESSION['user'].")";
$prefix = "../";
}
elseif ($computer_name == "ajax") { //adding else to ensure that backslashes are only used when not ajax and not web interface
$prefix = "../../";
}
elseif ($GLOBALS['os'] == "windows") { //Only if type is client and os is windows right?
$slash = "\\";
}
$heure = date('Y/d/m H:i:s');
$log_what = "$heure $computer_name: $log $who\n";
#print "\n---------------------- I AM LOGGING THIS ::: $log_what-----end ----\n";
# --- we log 2 times, first time for the computer itself, and ....
$foo = fopen($prefix."logs".$slash.$computer_name.".log","a");
fwrite($foo,"$log_what");
fclose($foo);
# .... second time for the brender.log that includes all logs
$foo = fopen($prefix."logs".$slash."brender.log","a");
fwrite($foo,"$log_what");
fclose($foo);
#print "$prefix/logs/brender.log";
}
function output_progress_status_SELECT($default = "NONE") {
$list = array("blocked","layout","model","animation","lighting","compositing","finished","approved","");
foreach ($list as $item) {
#print("check default = $default and item = $item");
if ($default == $item|| $default == "new") {
print " <option value = \"$item\" SELECTED>$item </option>";
}
else {
print " <option value = \"$item\">$item</option>";
}
}
}
function scene_shot_cascading_dropdown_menus() {
# ----- WORK IN PROGRESS -----------XXX------
# to have this working, the server needs to have an server_os set, and have path access to the blend files of the project for autodiscovery of .blend files
$projects_list = get_projects_list_array("active");
$projects_options = $shot_options = $scene_options = ""; #initalize the options
foreach ($projects_list as $project) {
$scene_list = get_scene_list_array($project);
$projects_options.= "<option class = \"project\" value = \"$project\">$project</option>";
foreach ($scene_list as $scene) {
#print "*** $scene <br/>";;
$scene_options.= "<option class = \"$project\" value = \"$scene\">$scene</option>";
$shot_list = get_shot_list_array($project,$scene);
#$shot_options = "";
foreach ($shot_list as $shot) {
#$shot_options.= "<option class = \"$scene\" value = \"$shot\">$project /$scene /$shot</option>";
$shot_options.= "<option class = \"$scene\" value = \"$shot\">$shot</option>";
#print " --- $project -- $scene -- $shot<br/>";
}
}
}
?>
<select name = "project" id = "project">
<?php echo $projects_options ?>
</select><br/>
<select name = "scene" id = "scene">
<?php echo $scene_options ?>
</select><br/>
<select name = "shot" id = "shot">
<?php echo $shot_options ?>
<option class = \"warning_message\" value = \"warning_message\">please choose a scene first</option>
</select><br/>
<!-- the javascript needs to be called after <select><option> construction. No idea why. This has to be fixed -->
<script type = "text/javascript" src = "js/brender-0.5.dev.js"></script>
<?php
}
function get_projects_list_array($type = "DEFAULT") {
$projects_list = array(); // initalize projects_list
switch($type) {
case "active":
$query = "SELECT * FROM projects WHERE status = 'active' ORDER BY def DESC ";
break;
default:
$query = "SELECT * FROM projects ORDER BY def DESC ";
}
$results = mysql_query($query);
while ($row = mysql_fetch_object($results)) {
$project_name = $row->name;
$projects_list[] = $project_name;
}
#print_r($projects_list);
if (count($projects_list) == 0){
$projects_list[] = " ! no ACTIVE project, please add one !";
}
#print "dfdffdfdfddf --- $projects_list ---<br/>";
return $projects_list;
}
function get_scene_list_array($project) {
# ----- WORK IN PROGRESS -----------XXX------
# to have this working, the server needs to have an server_os set, and have path access to the blend files of the project
$server_os = get_server_settings("server_os");
$blend_path = get_path($project,"blend",$server_os);
debug("Debug path server_os = $server_os and path = $blend_path");
$scene_list = array();
$list = `ls $blend_path`;
$list = preg_split("/\n/",$list);
#$list = scandir($blend_path);
foreach ($list as $item) {
#print("<br/>check item = $item<br/>");
if (is_dir("$blend_path/$item")) {
if (!$item) {
$item = "/";
}
if($item != "." && $item!= "..") {
# we do not want to add the . and .. directories
$scene_list[] = $item;
}
}
}
if (count($scene_list) == 0){
# we try with always showing the / folder
$scene_list[] = "/";
}
return $scene_list;
}
function get_shot_list_array($project,$selected_scene = "/") {
# ----- WORK IN PROGRESS -----------XXX------
# to have this working, the server needs to have a server_os set, and have path access to the blend files of the project
$server_os = get_server_settings("server_os");
$scenes_path = get_path($project,"blend",$server_os);
debug("Debug path server_os = $server_os and path = $scenes_path");
$shot_list = array();
$list = `ls $scenes_path/$selected_scene`;
$list = preg_split("/\n/",$list);
#$list = scandir("$scenes_path/$selected_scene");
foreach ($list as $item) {
#print("check item = $item<br/>");
if (preg_match("/(.*)\.blend$/",$item,$res)) {
$filename = $res[1]; # we extract only filename without .blend from regex
$shot_list[] = $filename;
}
}
if (count($shot_list) == 0){
#print "*** error empty for $project and $selected_scene ****";
$shot_list[] = "";
}
return $shot_list;
}
function output_scene_selector($project) {
# ----- WORK IN PROGRESS -----------XXX------
# NOT USED for the moment XXX might become useful if ajaxify the cascading
# to have this working, the server needs to have an server_os set, and have path access to the blend files of the project
$server_os = get_server_settings("server_os");
$blend_path = get_path($project,"blend",$server_os);
debug("Debug path server_os = $server_os and path = $blend_path");
$list = `ls $blend_path`;
$list = preg_split("/\n/",$list);
print "<select name = \"scene\">";
print " <option value = \"\">---choose a SCENE---</option>";
foreach ($list as $item) {
print("check item = $item<br/>");
if (is_dir("$blend_path/$item")) {
print " <option value = \"$item\">$item </option>";
}
}
print "</select><br/>";
}
function output_shot_selector($project,$selected_scene = "") {
# ----- WORK IN PROGRESS -----------XXX------
# NOT USED for the moment XXX might become useful if ajaxify the cascading
# to have this working, the server needs to have a server_os set, and have path access to the blend files of the project
$server_os = get_server_settings("server_os");
$scenes_path = get_path($project,"blend",$server_os);
debug("Debug path server_os = $server_os and path = $scenes_path");
$list = `ls $scenes_path/$selected_scene`;
$list = preg_split("/\n/",$list);
print "<select name = \"shot\">";
print " <option value = \"\">---choose a shot---</option>";
foreach ($list as $item) {
print("check item = $item<br/>");
if (preg_match("/(\w*)\.blend$/",$item,$res)) {
$filename = $res[1]; # we extract only filename without .blend from regex
print " <option value = \"$filename\">$item</option>";
}
}
print "</select>";
}
function output_render_action_select($what, $default = "-") {
#if ($default == "-") {$default = $_SESSION['last_used_pre_render_action'];};
$list = scandir("../conf/$what");
array_push($list,"-");
foreach ($list as $item) {
# we do not want the "." and ".." folders
if($item != "." && $item != "..") {
if ($default == $item) {
print " <option value = \"$item\" selected>$item</option>";
}
else if ($item<>""){
print " <option value = \"$item\">$item</option>";
}
}
}
}
function output_config_select($default = "NONE") {
if ($default == "NONE") {$default = $_SESSION['last_used_config'];};
#$list = `ls ../conf/`;
#$list = preg_split("/\n/",$list);
$list = scandir("../conf/");
array_push($list,"BLEND_DEFAULT.py");
foreach ($list as $item) {
# we do not want the "." and ".." folders, and we only want .py python files
if($item != "." && $item != ".." && preg_match("/^[^.](\w*)\.py/",$item)) {
$item = preg_replace("/\.py/","",$item);
#print("check default = $default and item = $item");
if ($default == $item) {
print " <option value = \"$item\" selected>$item</option>";
}
else if ($item<>""){
print " <option value = \"$item\">$item</option>";
}
}
}
}
function checking_alive_clients() {
# print "i am checking alive clients ";
# to check alive clients we first looks who is supposed to be active, and send them a png order ....
$query = "SELECT * FROM clients WHERE status = 'idle' OR status = 'disabled'";
$results = mysql_query($query);
while ($row = mysql_fetch_object($results)){
$id = $row->id;
$client = $row->client;
$status = $row->status;
send_order($client,"ping","","15");
print "pinging $client...\n";
}
#... then we sleep 2 second, time to let a client get the order and delete it....
sleep(2);
$query = "SELECT * FROM orders WHERE orders = 'ping'";
$results = mysql_query($query);
#..... then we check which client did not reply to ping order, so we know its dead
while ($row = mysql_fetch_object($results)){
$id = $row->id;
$client = $row->client;
print "$id = $client is dead\n";
set_status("$client","not running",'client not responding (PING)');
brender_log("$client not responding (PING)");
remove_order($id);
}
# .... now checking if there are inactive clients.... means if they are stuck rendering for instance....
#print "checking inactive\n";
$query = "SELECT client,time_format(TIMEDIFF(NOW(),lastseen),'%k') AS timediff FROM clients WHERE time_format(TIMEDIFF(NOW(),lastseen),'%k') >2 AND status<>'not running'";
$results = mysql_query($query);
while ($row = mysql_fetch_object($results)){
$client = $row->client;
$timediff = $row->timediff;
set_status("$client","not running",'client not responding (inactive > '.$timediff.' hours)');
print "client $client not responding (inactive > $timediff hours)\n";
}
}
function seconds_to_hms($time_in_secs) {
# function to display time from seconds to hours:minutes:seconds
$secs = $time_in_secs % 60;
$time_in_secs -= $secs;
$time_in_secs /= 60;
$mins = $time_in_secs % 60;
$time_in_secs -= $mins;
$time_in_secs /= 60;
$hours = $time_in_secs % 24;
$time_in_secs -= $hours;
$time_in_secs /= 24;
return str_pad($hours,2,'0',STR_PAD_LEFT) . ":" . str_pad($mins,2,'0',STR_PAD_LEFT) . ":" . str_pad($secs,2,'0',STR_PAD_LEFT);
#$days = $time_in_secs;
/*if (!$days) {
}
else {
return ($days." days ".str_pad($hours,2,'0',STR_PAD_LEFT) . ":" . str_pad($mins,2,'0',STR_PAD_LEFT) . ":" . str_pad($secs,2,'0',STR_PAD_LEFT));
}
*/
}
function clean_name($name) {
# cleaning name : spaces tranformed to _ and only lowercase
#print " I WILL CLEAN $name<br/>";
$name = strtolower($name);
$name = str_replace(" ","_",$name);
#print " I CLEANED $name<br/>";
return $name;
}
function job_get($what,$id) {
if (!check_job_exists($id)) {
# job doesnt exist or was deleted, we just return o
return 0;
}
$query = "SELECT $what FROM jobs WHERE id = '$id'";
$results = mysql_query($query);
$qq = mysql_result($results,0);
#debug ("******************************************$query*****************************************");
return $qq;
}
function check_create_path($path) {
if (is_dir($path)) {
output("$path already exists");
}
else {
if (!mkdir($path,0777,true)) {
output("$path creation error", "ERROR");
}
else {
debug("dir $path created successfully");
}
}
# - function to check if a path exists, if not then create it";
/*
debug("Check & Create Path --- $path ");
#print "DEBUG --- $path chmod\n";
$paths_array = explode("/",$path);
$current_path = $path_to_check = "";
#print_r($paths_array);
foreach ($paths_array as $item) {
debug("\n");
debug("\n");
debug("--- i am trying <b>$item</b>\n");
$path_to_check = $path_to_check."/$item";
print "-------- path to check = $path_to_check\n<br/>";
if (!is_dir("$path_to_check")) {
print "$path_to_check <b>not exist</b>, lets create\n";
mkdir($path_to_check);
}
else {
print "$path_to_check exists....\n";
}
debug("$path_to_check exists, i will now do a chmod");
chmod($path_to_check,0777);
}
*/
}
function shortify_string($string,$max_length = 50) {
#$tmp = preg_replace("/(.4)(.*)/","xx $1 xx",$string); // TODO replace with REGEX...
debug("----------ORIGINAL -----<b>$string</b>--------- <br/>");
if (strlen($string)>$max_length) {
debug("--- TOO LONG----<br/>");
$divider = $max_length/2;
$start = substr($string,0,$divider);
$end = substr($string,-$divider);
$tmp = "$start......$end";
#print "----------start $start--------- <br/>";
#print "----------end $end--------- <br/>";
#print "----------TMP $tmp--------- <br/>";
#print "<br/>";
return $tmp;
}
else {
return $string;
}
}
function filetype_to_ext($filetype) {
# transform filetype of the format that blender understands PNG TGA JPEG EXR to a simple extension
switch ($filetype) {
case "PNG":
return "png";
case "TGA":
return "tga";
case "JPEG":
return "jpg";
case "EXR":
return "exr";
case "MULTILAYER":
return "exr";
}
}
function add_rendered_frames($job_id,$start,$end){
#will add a rendered frame to the table, usually it is a client that invoke this function just after finishing a render job
$client = $GLOBALS['computer_name'];
for ($i = $start;$i<$end+1;$i++) {
$query = "INSERT INTO rendered_frames VALUES('','$job_id','$i','$client',NOW(),'0')";
debug("ADD RENDER FRAME QUERY = $query");
mysql_query($query);
}
}
function parse_render_command($render_command) {
#print "parsing $render_command<br/>";
$parsed = array();
#preg_match("/(.*)\-s (.\d) \-e (.\d)\ -a \-JOB (\d*)/",$render_command,$preg_matches);
preg_match("/(.*)\-s (\d*) -e (\d*) -a -JOB (\d*)/",$render_command,$preg_matches);
$parsed["start"] = $preg_matches[2];
$parsed["end"] = $preg_matches[3];
$parsed["job_id"] = $preg_matches[4];
#$job_id = $preg_matches[2];
debug("-----------------PARSING :: $render_command--------------------------");
debug("-----------------JOB ID = ".$parsed['job_id']." start = ".$parsed["start"]." end = ".$parsed['end']." --------------------------");
return $parsed;
}
function get_thumbnail_image($job_id,$image_number,$class = "") {
# function will output the <img src> of the thumbnail of a specific job_id and frame
debug("i try to get the frame $image_number from job_id = $job_id and output the image");
$thumbnail_path = "thumbnails/";
$scene = job_get("scene",$job_id);
$shot = job_get("shot",$job_id);
$filename = basename($shot);
$filetype = filetype_to_ext(job_get("filetype",$job_id));
$project = job_get("project",$job_id);
$filetype = "png"; // temporary test for fixing job thumbnail viewing when filetype is JPG OPENEXR or TGA
$thumbnail_location = "thumbnails/$project/$scene/$shot/small_$filename".str_pad($image_number,4,0,STR_PAD_LEFT).".$filetype";
#print "**** $thumbnail_location****";
if (file_exists("../$thumbnail_location")) {
return "<img src = \"../$thumbnail_location\" class = \"$class\">";
}
else {
return false;
}
}
function create_thumbnail($job_id,$image_number) {
if ($GLOBALS['computer_name'] == "web_interface") {
#print "WEBBBBBB<br/>";
$thumbnail_path = "../thumbnails";
#$input_prefix = "../";
}
else {
$thumbnail_path = "thumbnails";
}
debug("----------------------------------");
debug ("creating a thumbnail : for image number $image_number of job with id = $job_id");
$scene = job_get("scene",$job_id);
$shot = job_get("shot",$job_id);
$filename = basename($shot);
$filetype = filetype_to_ext(job_get("filetype",$job_id));
$project = job_get("project",$job_id);
$image_name = $filename.str_pad($image_number,4,0,STR_PAD_LEFT).".$filetype";
$thumbnail_name = $filename.str_pad($image_number,4,0,STR_PAD_LEFT).".png"; // XXX SPECIAL FIX for trying to resolve non-png jobs thumbnails
$server_os = get_server_settings("server_os");
$input_path = get_path($project,"output",$server_os);
$input_image = "$input_path/$scene/$shot/$image_name";
#print "<br/>----- input = $input_image ---<br/>";
if (!file_exists($input_image)) {
# if input file doesnt exists/rendered we just close the function, dont need to make thumbnail
debug("file $input_image doesnt exists, we close function <br/>");
return 0;
}
# create and check that all path are existing
#check_create_path("$thumbnail_path/$project");
#check_create_path("$thumbnail_path/$project/$scene");
check_create_path("$thumbnail_path/$project/$scene/$shot");
$output_image = "$thumbnail_path/$project/$scene/$shot/$thumbnail_name";
$output_image_small = "$thumbnail_path/$project/$scene/$shot/small_$thumbnail_name";
if ($server_os == "windows") {
$input_image = windowsify_paths($input_image);
$output_image = windowsify_paths($output_image);
$output_image_small = windowsify_paths($output_image_small);
}
debug("----- output = $output_image ---<br/>");
#print "<b>creating thumbnail</b> $image_number jobid = $job_id<br/";
#$image_magick_home = "/Users/o/Documents/ImageMagick-6.6.5/bin/";
$commande = $GLOBALS['imagemagick_root'] . 'convert -resize 1024 "' . $input_image . '" "' . $output_image . '"';
exec($commande);
$commande = $GLOBALS['imagemagick_root'] . 'convert -resize 200 "' . $input_image . '" "' . $output_image_small . '"';
exec($commande);
debug("######### CONVERT COMMAND ####### $commande");
}
function output_progress_bar($start,$end,$current,$style = "progress_bar") {
# --- little display of progress_bars
$total = $end-$start;
if ($current == $start) {
$percent = 0;
}
else if ($current >= $end) {
$percent = 100;
}
else {
$percent = round(($current-$start)/$total*100);
}
$done = $percent/2;
$remaining = (100-$percent)/2;
$output = "<img src = \"images/cube_light_green.png\" style = \"width:".$done."px;\" class = \"$style\" alt = \"$percent% done\"/>";
#$output.= "<br/>$done / $remaining";
return $output;
}
function show_last_rendered_frame($mode = "simple") {
$query = "SELECT * FROM rendered_frames WHERE is_thumbnailed = '1' ORDER BY finished_time DESC LIMIT 1";
$results = mysql_query($query);
$row = mysql_fetch_object($results);
if (mysql_num_rows($results) == 0) {
print "no last rendered frame found";
}
else {
$job_id = $row->job_id;
$rendered_by = $row->rendered_by;
$frame = $row->frame;
$finished_time = $row->finished_time;
$thumbnail_image = get_thumbnail_image($job_id,$frame);
if ($thumbnail_image) {
if ($mode == "full") {
print "<a href=\"index.php?view=view_image&job_id=$job_id&frame=$frame\">$thumbnail_image</a><br/>";
print "$frame by <a href=\"index.php?view=view_client&client=$rendered_by\">$rendered_by</a> @ $finished_time<br/>";
}
else {
print $thumbnail_image;
}
}
else {
print "no last rendered frame found";
}
}
}
function count_rendered_frames($job_id) {
#return "00";
# counting the total number of rendered frames for a job.
# If a job is rerendered, the total might already be 100%
$query = "SELECT scene,shot,project,start,end,filetype FROM jobs WHERE id = '$job_id'";
$results = mysql_query($query);
$row = mysql_fetch_object($results);
#print "query === $query <br/>";
$scene = $row->scene;
$shot = $row->shot;
$filetype = filetype_to_ext($row->filetype);
$project = $row->project;
$server_os = get_server_settings("server_os");
$path = get_path($project,"output",$server_os);
$end = $row->end;
$a = $row->start-1;
$total = 0;
# print " i check $a to $end $output###.$filetype<br/>";
while ($a<$end){
$a++;
$filecheck = "$path/$scene/$shot/$shot".str_pad($a,4,0,STR_PAD_LEFT).".".$filetype;
if (file_exists($filecheck)) {
$ok = "ok";
$total++;
}
else {
$ok = "";
}
debug("count_rendered_frames filecheck = $filecheck == $ok");
}
# print "total $total<br/>";
return $total;
}
?>