-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarDriver.py
More file actions
834 lines (566 loc) · 23.8 KB
/
CarDriver.py
File metadata and controls
834 lines (566 loc) · 23.8 KB
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
import os, RLPy, math
from typing import Text
from winreg import *
from PySide2 import QtWidgets
from PySide2.shiboken2 import wrapInstance
from PySide2.QtCore import *
import random
class Car:
def __init__(self):
self.__Prop = None
self.__EndTime = 0
self.__InMotion = False
self.__Name = ""
self.__StartTime = 0
self.__WheelsRotated = False
def SetProp(self, prop):
self.__Prop = prop
def GetProp(self):
return self.__Prop
def SetEndTime(self, endTime):
self.__EndTime = endTime
def GetEndTime(self):
return self.__EndTime
def SetInMotion(self, inMotion):
self.__InMotion = inMotion
def GetInMotion(self):
return self.__InMotion
def SetName(self, name):
self.__Name = name
def GetName(self):
return self.__Name
def SetStartTime(self, startTime):
self.__StartTime = startTime
def GetStartTime(self):
return self.__StartTime
def SetWheelsRotated(self, wheelsRotated):
self.__WheelsRotated = wheelsRotated
def GetWheelsRotated(self):
return self.__WheelsRotated
Prop=property(GetProp, SetProp)
EndTime=property(GetEndTime, SetEndTime)
InMotion=property(GetInMotion, SetInMotion)
Name=property(GetName, SetName)
StartTime=property(GetStartTime, SetStartTime)
WheelsRotated=property(GetWheelsRotated, SetWheelsRotated)
class PropInfo:
def __init__(self):
self.__Prop = None
self.__Name = ""
self.__Index = 0
def SetProp(self, prop):
self.__Prop = prop
def GetProp(self):
return self.__Prop
def SetName(self, name):
self.__Name = name
def GetName(self):
return self.__Name
def SetIndex(self, index):
self.__Index = index
def GetIndex(self):
return self.__Index
Prop=property(GetProp, SetProp)
Name=property(GetName, SetName)
Index=property(GetIndex, SetIndex)
def GetRandomValueInRange(max, subtractAmount):
return GetRandomNumber(max, 0) - subtractAmount
def GetRandomNumber(max, increment):
randomNumber = (random.randint(101771, 124193) % (max)) + increment
# return value
return randomNumber
def RegisterCars():
# create
cars = []
for i in range(len(Props)):
# get the name
name = Props[i].Name
if (name.startswith("Car")):
# add this Prop
cars.append(Props[i])
# return value
return cars
def GetWheels(carName):
# initial value
wheels = []
if (Props is not None):
# iterate the Props
for i in range(len(Props)):
# get the name of this prop
name = Props[i].Name
# if the text ends in the car name and name starts with Wheel
if ((name.endswith(carName) and (name.startswith("Wheel")))):
# add this object
wheels.append(Props[i])
else:
text_edit.insertPlainText("Props does not exist in GetWheels method" + ".\r\n")
# return value
return wheels
def GetAllWheels():
# initial value
wheels = []
# if the props exist
if (Props is not None):
# iterate the Props
for i in range(len(Props)):
# get the name of this prop
name = Props[i].Name
# if this is a wheel
if (name.startswith("Wheel")):
# add this wheel
wheels.append(Props[i])
text_edit.insertPlainText("Wheels found: " + str(len(wheels)) + "\r\n")
else:
text_edit.insertPlainText("Props do not exist in GetAllWheels method." + "\r\n")
# return value
return wheels
def RotateWheels(car, wheels, direction, startTime, endTime):
# get the rotation speed
rotationSpeed = (SpeedSlider.value() * 8)
# get a number to use for frames, not the actual frames
frames = int(endTime - startTime)
text_edit.insertPlainText("Frames: " + str(frames) + "\r\n")
# if the wheels exists
if (wheels is not None):
for x in range(len(wheels)):
# get the wheel name
wheel = wheels[x]
ts_control = wheel.Prop.GetControl("Transform")
ts_data_block = ts_control.GetDataBlock()
# get the rotation value for this prop
transform = RLPy.RTransform()
ts_control.GetValue(RLPy.RTime(startTime), transform)
rotationY = transform.R().y # Get Prop Y Rotation
if (direction == 0):
# get the rotation value
rotationValue = rotationY + (127 * frames * rotationSpeed)
else:
rotationValue = rotationY - (127 * frames * rotationSpeed)
#-- Set Rotation Z = by a random amount
ts_data_block.SetData("Rotation/RotationY", RLPy.RTime(endTime), RLPy.RVariant(rotationValue * RLPy.RMath.CONST_DEG_TO_RAD))
# Change the TransitionType
ChangeTransitionType(ts_control, endTime, RLPy.ETransitionType_Linear)
# set the parent
wheel.Prop.SetParent(car.Prop)
def RepositionCars():
# start position
posX = -16861.480
posY = -2270.022
posZ = 0
# Get the Cars
cars = GetCars()
# initial value
lastCar = ""
if (cars is not None):
for i in range(len(cars)):
# get the car at this index
car = cars[i]
# show each car for this, make sure to hide them before Create Traffic
car.Prop.SetVisible(RLPy.RTime(0), True)
# get a local reference to the name of this car
name = car.Name
# position the prop
PositionProp(car.Prop, posX, posY, posZ, 0, RLPy.ETransitionType_Step, 0)
# set name
lastCar = name
# show number of cars found
text_edit.insertPlainText("Parked " + str(len(cars)) + " cars." + "\r\n")
def GetCars():
cars = []
# Get the props that start with Car
tempCars = RegisterCars()
# if the cars were found
if (tempCars is not None):
for i in range(len(tempCars)):
# create a car
car = Car()
# set the properties
car.Prop = tempCars[i].Prop
# add the name
car.Name = tempCars[i].Name
# hide all cars at the start
car.Prop.SetVisible(RLPy.RTime(0), False)
cars.append(car)
# return value
return cars
def CreateTraffic():
# get all the props that start with Car
cars = GetCars()
if (cars is not None):
# Get the current time
frameTime = RLPy.RGlobal.GetTime()
currentTime = frameTime.GetValue()
# currentFrame seems to need the Add 1 to get the current frame value from IClone
currentFrame = round(currentTime * .001 * 60, 0) + 1
framesLength = RLPy.RGlobal.GetProjectLength()
endTime = framesLength.GetValue()
frames = endTime * .001 * 60
randomOrder = RandomOrderCheckBox.isChecked()
oneWayTraffic = OneWayCheckBox.isChecked()
# show number of cars found
# text_edit.insertPlainText("Registered " + str(len(cars)) + " cars." + "\r\n")
speed = (SpeedSlider.value() * 40)
# show speed
# text_edit.insertPlainText("Speed: " + str(speed) + "." + "\r\n")
interval = 36000 - speed;
# safeguard
if (interval < 10):
interval = 10
# lower the multiplier (7) if you want bigger gaps between cars or raise it for shorter.
congestion = CongestionSlider.value() * 7
# lower the 2400 here to get shorter gaps or raise it to get larget gaps
congestionValue = 2400 - congestion
# get the end time for this car
carEndTime = currentTime + interval
# local
keyFrames = 0
# used for positioning
posX = 0
posY = 0
posZ = 0
posX2 = 0
posY2 = 0
posZ2 = 0
direction = 0
if (oneWayTraffic):
# show animating message
# text_edit.insertPlainText("One way traffic. Direction: " + str(direction) + "\r\n")
# get a 0 or a 1
direction = GetRandomNumber(2, 0)
if (direction == 0):
# left to right
# show animating message
text_edit.insertPlainText("One way traffic. Direction: Left To Right\r\n")
# start position
posX = -16861.480
posY = -2270.022
posZ = 0
# end position
posX2 = 11877
posY2 = -3702
posZ2 = 0
else:
# right to left
# show animating message
# text_edit.insertPlainText("One way traffic. Direction: Right To Left\r\n")
# start position
posX = 11877.729
posY = -3240.758
posZ = 0
#end position
posX = -16861
posY = -1756.000
posZ = 0
progress_bar.setRange(1, endTime)
while (currentTime < endTime):
if (randomOrder):
number = GetRandomNumber(len(cars), 0)
else:
number = number + 1
# if out of range
if (number >= len(cars)):
# reset
number = 0
if (cars is not None):
if (number == len(cars)):
# safeguard
number = number - 1
# safeguard
if (number < 0):
# reset
number = 0
car = cars[number]
# if this car is not already in motion
if (cars[number].InMotion == False):
# Set to inmotion to true
cars[number].InMotion = True
# if traffic is two way
if (not oneWayTraffic):
# traffic goes in both directions
# get a 0 or a 1
direction = GetRandomNumber(2, 0)
# show animating message
# text_edit.insertPlainText("Two way traffic. Direction: " + str(direction) + "\r\n")
if (direction == 0):
# show animating message
# text_edit.insertPlainText("Two way traffic. Direction: Left To Right\r\n")
# setup start position of this car
posX = -16861.480
posY = -2270.022
posZ = 0
elif (direction == 1):
# show animating message
# text_edit.insertPlainText("Two way traffic. Direction: Right To Left\r\n")
# change to right to left start position
posX = 11877.729
posY = -3240.758
posZ = 0
# increment
keyFrames = keyFrames + 1
# get a random number between 30 and negative 30
carStartTime = currentTime + GetRandomNumber(60, -30)
if (carStartTime < 1):
# reset
carStartTime = 1
# get the end time for this car
carEndTime = carStartTime + interval
# set the start time
cars[number].StartTime = carStartTime
# set the endTime
cars[number].EndTime = carEndTime
# show keyFrames added message
text_edit.insertPlainText("Preparing to rotate wheels for car: " + car.Name + ".\r\n")
# if the wheels have NOT been rotated yet
if (not cars[number].WheelsRotated):
# get the wheels for this car
wheels = GetWheels(car.Name)
# rotate this wheel
RotateWheels(cars[number], wheels, direction, carStartTime, endTime)
# Toggle
cars[number].WheelsRotated = True
# show keyFrames added message
text_edit.insertPlainText("Rotated wheels for car: " + car.Name + ".\r\n")
# show keyFrames added message
# text_edit.insertPlainText("Car start Time: " + str(carStartTime) + ".\r\n")
# Show this car before the prop starts
car.Prop.SetVisible(RLPy.RTime(carStartTime - 1), True)
# show keyFrames added message
text_edit.insertPlainText("Preparing to position car at start for car: " + car.Name + ".\r\n")
# set the prop to the start frame
PositionProp(car.Prop, posX, posY, posZ, carStartTime, RLPy.ETransitionType_Step, direction)
# show keyFrames added message
text_edit.insertPlainText("Start position set for car: " + car.Name + ".\r\n")
if (not oneWayTraffic):
if (direction == 0):
# left to right
# end position
posX2 = 11877
posY2 = -3702
posZ2 = 0
elif (direction == 1):
# right to left
# end position
posX2 = -16861
posY2 = -1756.000
posZ2 = 0
# increment
keyFrames = keyFrames + 1
# set the prop to the start frame
PositionProp(car.Prop, posX2, posY2, posZ2, carEndTime, RLPy.ETransitionType_Linear, direction)
# now increment the current time
currentTime = int(currentTime + congestionValue)
# Hide this car after the prop finishes
car.Prop.SetVisible(RLPy.RTime(carEndTime + 1), False)
# show current time
text_edit.insertPlainText("Current Time: " + str(currentTime) + "\r\n")
else:
# now increment the current time a little, else an infinite loop crashes IClone
currentTime = int(currentTime + (congestionValue * .1))
# show current time
text_edit.insertPlainText("Current Time: " + str(currentTime) + "\r\n")
# if not the max yet
if (currentTime < endTime):
# update progress
progress_bar.setValue(currentTime);
else:
# set to max
progress_bar.setValue(endTime);
# Recycle cars - update the InMotion value if Time has expired
for i in range (len(cars)):
# if we have reached end of this car's motion
# release the car back to idle
if ((cars[i].InMotion) and (currentTime >= cars[i].EndTime)):
# this car is no longer in motion
cars[i].InMotion = False
def ChangeTransitionType(control, currentTime, transitionType):
# set transition
control.SetKeyTransition(RLPy.RTime(currentTime), transitionType, 1.0)
def ConvertProps(all_props):
# initial value
props = []
# counter
count = 0
# ensure the all_props exist
if (all_props is not None):
# iterate all_props
for i in range (len(all_props)):
# create a new class
prop = PropInfo()
# set the properties
prop.Prop = all_props[i]
prop.Name = prop.Prop.GetName()
prop.Index = count
# add this item
props.append(prop)
# increment
count = count + 1
# return value
return props
def ResetPivot():
# get all the wheels
wheels = GetAllWheels()
if ((wheels is not None) and (len(wheels) > 0)):
progress_bar.setRange(1, len(wheels))
for i in range (len(wheels)):
wheel = wheels[i]
ts_control = wheel.Prop.GetControl("Transform")
time = RLPy.RTime()
transform_for_ref = RLPy.RTransform()
ts_control.GetValue(time, transform_for_ref)
posX = transform_for_ref.T().x # Get Prop X Position
posY = transform_for_ref.T().y # Get Prop Y Position
posZ = transform_for_ref.T().z # Get Prop Z Position
#set pivot, keep rotation to existing position
pos = RLPy.RVector3(posX, posY, posZ)
rot = RLPy.RVector3(0, 0, 0)
wheel.Prop.SetPivot(pos, rot)
# update the graph
progress_bar.setValue(i + 1);
text_edit.insertPlainText("Reset " + str(len(wheels)) + " wheels.\r\n")
else:
# show a message
text_edit.insertPlainText("No wheels were found.\r\n")
def AttachWheels():
# get the cars
cars = GetCars()
# local
count = 0
if (cars is not None):
for i in range (len(cars)):
# get this car
car = cars[i]
# Get the wheels
wheels = GetWheels(car.Name)
if (wheels is not None):
for i in range (len(wheels)):
# get a reference to this wheel
wheel = wheels[i]
# set the parent
wheel.Prop.SetParent(car.Prop)
# increment
count = count + 1
# Show the car
car.Prop.SetVisible(RLPy.RTime(0), True)
# show a message
text_edit.insertPlainText("Wheels attached: " + str(count) + ".\r\n")
def HideCars():
# Get the cars
cars = GetCars()
# if there are one or more cars
if ((cars is not None) and (len(cars) > 0)):
# get all cars
for i in range(len(cars)):
# get a reference to this car
car = cars[i]
# show each car for this, make sure to hide them before Create Traffic
car.Prop.SetVisible(RLPy.RTime(0), False)
# show the cars hidden
text_edit.insertPlainText("Hide car: " + car.Name + "\r\n")
def LoadProps():
# convert the props
props = ConvertProps(all_props)
# if the props were found
if ((Props is not None) and (len(props) > 0)):
CreateTrafficButton.setEnabled(True)
RepositionButton.setEnabled(True)
ResetPivotButton.setEnabled(True)
AttachWheelsButton.setEnabled(True)
HideCarsButton.setEnabled(True)
# Show a message
text_edit.insertPlainText("Loaded " + str(len(props)) + " props.\r\n")
# show a message the program is ready to use
text_edit.insertPlainText("Ready." + "\r\n")
else:
# Should never happen
text_edit.insertPlainText("Something went wrong." + "\r\n")
return props
def PositionProp(prop, moveX, moveY, moveZ, currentTime, transitionType, direction):
# get access to the control
ts_control = prop.GetControl("Transform")
ts_data_block = ts_control.GetDataBlock()
# now posiiton
ts_data_block.SetData("Position/PositionX", RLPy.RTime(currentTime), RLPy.RVariant(moveX))
ts_data_block.SetData("Position/PositionY", RLPy.RTime(currentTime), RLPy.RVariant(moveY))
ts_data_block.SetData("Position/PositionZ", RLPy.RTime(currentTime), RLPy.RVariant(moveZ))
time = RLPy.RTime(currentTime)
transform = RLPy.RTransform()
ts_control.GetValue(time, transform)
rotationZ = transform.R().z # Get Prop Z Rotation
if ((direction == 1) and (rotationZ != 180)):
# rotate 180
transform.R().z = 180
ts_control.SetValue(time, transform)
elif ((direction == 0) and (rotationZ != 0)):
# reset to original
transform.R().z = 0
ts_control.SetValue(time, transform)
# Change the transition type
ChangeTransitionType(ts_control, currentTime, transitionType)
# Create an iClone Dock Widget
dockable_window = RLPy.RUi.CreateRDockWidget()
dockable_window.SetWindowTitle("Drive Cars")
# Use wrapInstance to convert the dockable window to something that Python can understand, in this case a Dock Widget
dock = wrapInstance(int(dockable_window.GetWindow()), QtWidgets.QDockWidget)
dock.setFixedSize(640, 600)
main_widget = QtWidgets.QWidget()
dock.setWidget(main_widget)
main_widget_layout = QtWidgets.QVBoxLayout()
main_widget.setLayout(main_widget_layout)
progress_bar = QtWidgets.QProgressBar()
text_edit = QtWidgets.QTextEdit(readOnly=True)
# checkbox and slider for Glow Channel Strength
RandomOrderCheckBox = QtWidgets.QCheckBox("Random Order")
RandomOrderCheckBox.setChecked(True)
# checkbox and slider for Glow Channel Strength
OneWayCheckBox = QtWidgets.QCheckBox("One Way Traffic")
OneWayCheckBox.setChecked(False)
# Car speedsCar
SpeedSliderLabel = QtWidgets.QLabel("Speed (1 - 100)")
SpeedSlider = QtWidgets.QSlider(orientation=Qt.Horizontal)
# Slower speed takes longer to animate as more passes for each car
SpeedSlider.setRange(1, 100)
SpeedSlider.setSingleStep(1)
# Default to 50
SpeedSlider.setValue(50)
# Car speedsCar
CongestionSliderLabel = QtWidgets.QLabel("Congestion (0 - 100)")
CongestionSlider = QtWidgets.QSlider(orientation=Qt.Horizontal)
# 100 equals = more congestion (interval between cars)
CongestionSlider.setRange(0, 100)
CongestionSlider.setSingleStep(1)
# Default to 50
CongestionSlider.setValue(50)
# Button to create the traffic animation
CreateTrafficButton = QtWidgets.QPushButton("Create Traffic")
CreateTrafficButton.clicked.connect(CreateTraffic)
CreateTrafficButton.setEnabled(False)
# Button to line up the cars
RepositionButton = QtWidgets.QPushButton("Reposition Cars")
RepositionButton.clicked.connect(RepositionCars)
RepositionButton.setEnabled(False)
# Rename Wheels
ResetPivotButton = QtWidgets.QPushButton("Reset Pivot")
ResetPivotButton.clicked.connect(ResetPivot)
ResetPivotButton.setEnabled(False)
# Rename Wheels
AttachWheelsButton = QtWidgets.QPushButton("Attach Wheels")
AttachWheelsButton.clicked.connect(AttachWheels)
AttachWheelsButton.setEnabled(False)
# Hide Cars
HideCarsButton = QtWidgets.QPushButton("Hide Cars")
HideCarsButton.clicked.connect(HideCars)
HideCarsButton.setEnabled(False)
# Grab all props in the scene
all_props = RLPy.RScene.FindObjects(RLPy.EObjectType_Prop)
# Has To Be Loaded Later For Some Reason
Props = []
# Margin Label
marginLabel = QtWidgets.QLabel("")
for widget in [progress_bar, text_edit, SpeedSliderLabel, SpeedSlider, CongestionSliderLabel, CongestionSlider, RandomOrderCheckBox, OneWayCheckBox, marginLabel, CreateTrafficButton, RepositionButton, ResetPivotButton, AttachWheelsButton, HideCarsButton]:
main_widget_layout.addWidget(widget)
dockable_window.Show()
# Load the PropInfo objects
Props = LoadProps()
# File Info
# Version 1.0.0
# New Features / Fixes: