-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgame_engine_add_basic_character.py
338 lines (251 loc) · 14.2 KB
/
game_engine_add_basic_character.py
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
# This is free software under the terms of the GNU General Public License
# you may redistribute it, and/or modify it.
#
# This code 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 (http://www.gnu.org/licenses/) for more details.
#
# ***** END GPL LICENSE BLOCK *****
# #########################################################################
bl_info = {
"name": "Add character",
"description": "Create basic bge character and fly camera",
"author": "Moaaa",
"version": (0, 0, 4),
"blender": (2, 77, 0),
"location": "View3D > TOOLS",
"warning": "WIP - Frequent changes for known issues and enhancements",
"support": "TESTING",
"wiki_url": "https://github.com/UPBGE/blender-addons/wiki/Basic-Character-addon",
"tracker_url": "",
"category": "Game Engine"
}
import bpy
import os
from bpy.types import Operator, Panel
bpy.types.Scene.character_size = bpy.props.FloatProperty(name="character_size", default=2.0, min=1.0, max=10.0)
bpy.types.Scene.key_sensitive = bpy.props.FloatProperty(name="key sensitive", default=0.2, min=0.1, max=5.0)
bpy.types.Scene.mouse_sensitive = bpy.props.FloatProperty(name="mouse sensitive", default=0.5, min=0.1, max=5.0)
bpy.types.Scene.character_name = bpy.props.StringProperty(name="character_name", default="character")
bpy.types.Scene.character_jump = bpy.props.BoolProperty(name="character_jump")
key_mode = [("0", "Arrow Keys", "Arrow Keys"), ("1", "ZSDQ", "ZSDQ"), ("2", "WSDA", "WSDA"), ]
bpy.types.Scene.character_keys = bpy.props.EnumProperty(items=key_mode, name="character_keys")
class simple_character(Operator):
bl_label = 'Add character'
bl_idname = 'character.gen'
bl_description = 'Generate a simple character'
bl_context = 'objectmode'
def execute(self, context):
bpy.context.scene.render.engine = 'BLENDER_GAME'
bpy.context.scene.objects.active = None
bpy.ops.mesh.primitive_cone_add(vertices=16, radius1=bpy.context.scene.character_size, radius2=0.0, depth=bpy.context.scene.character_size, end_fill_type='TRIFAN', view_align=False,
enter_editmode=False, location=bpy.context.scene.cursor_location, rotation=(0.0, 0.0, 0.0),
layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
# configure character
obj = bpy.context.selected_objects[0]
obj.name = bpy.context.scene.character_name
obj.game.physics_type = 'CHARACTER'
obj.game.use_actor = True
obj.game.use_collision_bounds = True
obj.game.collision_bounds_type = 'CONE'
obj.hide_render = True
sensors = obj.game.sensors
controllers = obj.game.controllers
actuators = obj.game.actuators
bpy.ops.logic.sensor_add(type="MOUSE", object=obj.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", object=obj.name)
bpy.ops.logic.actuator_add(type='MOUSE', name="BodyTurn", object=obj.name)
sensor = sensors[-1]
sensor.mouse_event = 'MOVEMENT'
sensor.use_pulse_true_level = True
controller = controllers[-1]
actuator = actuators[-1]
actuator.mode = 'LOOK'
actuator.use_axis_y = False
actuator.sensitivity_x = bpy.context.scene.mouse_sensitive
sensor.link(controller)
actuator.link(controller)
keys_list = [("UP_ARROW", "DOWN_ARROW", "RIGHT_ARROW", "LEFT_ARROW", "RIGHT_CTRL"), ("Z", "S", "D", "Q", "SPACE"), ("W", "S", "D", "A", "SPACE")]
bpy.ops.logic.sensor_add(type="KEYBOARD", name="Forward", object=obj.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="Forward", object=obj.name)
bpy.ops.logic.actuator_add(type='MOTION', name="Forward", object=obj.name)
sensors["Forward"].key = keys_list[int(bpy.context.scene.character_keys)][0]
actuators["Forward"].mode = "OBJECT_CHARACTER"
actuators["Forward"].offset_location[1] = bpy.context.scene.key_sensitive
sensors["Forward"].link(controllers["Forward"])
actuators["Forward"].link(controllers["Forward"])
bpy.ops.logic.sensor_add(type="KEYBOARD", name="back", object=obj.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="back", object=obj.name)
bpy.ops.logic.actuator_add(type='MOTION', name="back", object=obj.name)
sensors["back"].key = keys_list[int(bpy.context.scene.character_keys)][1]
actuators["back"].mode = "OBJECT_CHARACTER"
actuators["back"].offset_location[1] = -bpy.context.scene.key_sensitive
sensors["back"].link(controllers["back"])
actuators["back"].link(controllers["back"])
bpy.ops.logic.sensor_add(type="KEYBOARD", name="right", object=obj.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="right", object=obj.name)
bpy.ops.logic.actuator_add(type='MOTION', name="right", object=obj.name)
sensors["right"].key = keys_list[int(bpy.context.scene.character_keys)][2]
actuators["right"].mode = "OBJECT_CHARACTER"
actuators["right"].offset_location[0] = bpy.context.scene.key_sensitive
sensors["right"].link(controllers["right"])
actuators["right"].link(controllers["right"])
bpy.ops.logic.sensor_add(type="KEYBOARD", name="left", object=obj.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="left", object=obj.name)
bpy.ops.logic.actuator_add(type='MOTION', name="left", object=obj.name)
sensors["left"].key = keys_list[int(bpy.context.scene.character_keys)][3]
actuators["left"].mode = "OBJECT_CHARACTER"
actuators["left"].offset_location[0] = -bpy.context.scene.key_sensitive
sensors["left"].link(controllers["left"])
actuators["left"].link(controllers["left"])
if bpy.context.scene.character_jump == True:
bpy.ops.logic.sensor_add(type="KEYBOARD", name="jump", object=obj.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="jump", object=obj.name)
bpy.ops.logic.actuator_add(type='MOTION', name="jump", object=obj.name)
sensors["jump"].key = keys_list[int(bpy.context.scene.character_keys)][4]
actuators["jump"].mode = "OBJECT_CHARACTER"
actuators["jump"].use_character_jump = True
sensors["jump"].link(controllers["jump"])
actuators["jump"].link(controllers["jump"])
for sen in sensors:
sen.show_expanded = False
for act in actuators:
act.show_expanded = False
#create camera and configure
cam = bpy.data.cameras.new("CameraM")
cam_ob = bpy.data.objects.new("CameraM", cam)
bpy.context.scene.objects.link(cam_ob)
cam_ob.location = (bpy.context.scene.cursor_location[0],
bpy.context.scene.cursor_location[1], bpy.context.scene.cursor_location[2]+(bpy.context.scene.character_size/2.2))
cam_ob.rotation_euler = (1.5708, 0, 0)
cam_ob.name = "cam" + bpy.context.scene.character_name
sensors = cam_ob.game.sensors
controllers = cam_ob.game.controllers
actuators = cam_ob.game.actuators
bpy.ops.logic.sensor_add(type="MOUSE", object=cam_ob.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", object=cam_ob.name)
bpy.ops.logic.actuator_add(type='MOUSE', name="HeadTurn", object=cam_ob.name)
sensor = sensors[-1]
sensor.mouse_event = 'MOVEMENT'
sensor.use_pulse_true_level = True
controller = controllers[-1]
actuator = actuators[-1]
actuator.mode = 'LOOK'
actuator.use_axis_x = False
actuator.sensitivity_y = bpy.context.scene.mouse_sensitive
sensor.link(controller)
actuator.link(controller)
bpy.context.scene.objects.active = None
obj.location = (obj.location[0], obj.location[1], obj.location[2]+(bpy.context.scene.character_size/2))
cam_ob.location = (cam_ob.location[0], cam_ob.location[1], cam_ob.location[2]+(bpy.context.scene.character_size/2))
obj.select = True
cam_ob.select = True
bpy.context.scene.objects.active = obj
bpy.ops.object.parent_set(type='OBJECT', keep_transform=False)
bpy.context.scene.objects.active = cam_ob
bpy.ops.view3d.object_as_camera()
return {'FINISHED'}
class fly_camera(Operator):
bl_label = 'Add Fly Camera'
bl_idname = 'fly_camera.gen'
bl_description = 'Generate a simple fly camera'
bl_context = 'objectmode'
def execute(self, context):
bpy.context.scene.render.engine = 'BLENDER_GAME'
bpy.context.scene.objects.active = None
#create camera and configure
cam = bpy.data.cameras.new("CameraM")
cam_object = bpy.data.objects.new("CameraM", cam)
bpy.context.scene.objects.link(cam_object)
cam_object.location = (bpy.context.scene.cursor_location)
cam_object.rotation_euler = (1.5708, 0, 0)
cam_object.name = bpy.context.scene.character_name
bpy.context.scene.objects.active = cam_object
sensors = cam_object.game.sensors
controllers = cam_object.game.controllers
actuators = cam_object.game.actuators
bpy.ops.logic.sensor_add(type="MOUSE", name="MouseMove", object=cam_object.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="MouseMove", object=cam_object.name)
bpy.ops.logic.actuator_add(type='MOUSE', name="MouseMove", object=cam_object.name)
sensors["MouseMove"].use_pulse_true_level = True
sensors["MouseMove"].mouse_event = 'MOVEMENT'
actuators["MouseMove"].mode = 'LOOK'
actuators["MouseMove"].sensitivity_x = bpy.context.scene.mouse_sensitive
actuators["MouseMove"].sensitivity_y = bpy.context.scene.mouse_sensitive
sensors["MouseMove"].link(controllers["MouseMove"])
actuators["MouseMove"].link(controllers["MouseMove"])
keys_list = [("UP_ARROW", "DOWN_ARROW", "RIGHT_ARROW", "LEFT_ARROW", "RIGHT_CTRL"), ("Z", "S", "D", "Q", "SPACE"), ("W", "S", "D", "A", "SPACE")]
bpy.ops.logic.sensor_add(type="KEYBOARD", name="Forward", object=cam_object.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="Forward", object=cam_object.name)
bpy.ops.logic.actuator_add(type='MOTION', name="Forward", object=cam_object.name)
sensors["Forward"].key = keys_list[int(bpy.context.scene.character_keys)][0]
actuators["Forward"].mode = "OBJECT_CHARACTER"
actuators["Forward"].offset_location[1] = bpy.context.scene.key_sensitive
sensors["Forward"].link(controllers["Forward"])
actuators["Forward"].link(controllers["Forward"])
bpy.ops.logic.sensor_add(type="KEYBOARD", name="back", object=cam_object.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="back", object=cam_object.name)
bpy.ops.logic.actuator_add(type='MOTION', name="back", object=cam_object.name)
sensors["back"].key = keys_list[int(bpy.context.scene.character_keys)][1]
actuators["back"].mode = "OBJECT_CHARACTER"
actuators["back"].offset_location[1] = -bpy.context.scene.key_sensitive
sensors["back"].link(controllers["back"])
actuators["back"].link(controllers["back"])
bpy.ops.logic.sensor_add(type="KEYBOARD", name="right", object=cam_object.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="right", object=cam_object.name)
bpy.ops.logic.actuator_add(type='MOTION', name="right", object=cam_object.name)
sensors["right"].key = keys_list[int(bpy.context.scene.character_keys)][2]
actuators["right"].mode = "OBJECT_CHARACTER"
actuators["right"].offset_location[0] = bpy.context.scene.key_sensitive
sensors["right"].link(controllers["right"])
actuators["right"].link(controllers["right"])
bpy.ops.logic.sensor_add(type="KEYBOARD", name="left", object=cam_object.name)
bpy.ops.logic.controller_add(type="LOGIC_AND", name="left", object=cam_object.name)
bpy.ops.logic.actuator_add(type='MOTION', name="left", object=cam_object.name)
sensors["left"].key = keys_list[int(bpy.context.scene.character_keys)][3]
actuators["left"].mode = "OBJECT_CHARACTER"
actuators["left"].offset_location[0] = -bpy.context.scene.key_sensitive
sensors["left"].link(controllers["left"])
actuators["left"].link(controllers["left"])
for sen in sensors:
sen.show_expanded = False
for act in actuators:
act.show_expanded = False
bpy.context.scene.objects.active = cam_object
bpy.ops.view3d.object_as_camera()
return {'FINISHED'}
class addcharacter(Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_label = 'Simple Character'
bl_context = 'objectmode'
bl_category = 'Add Character'
def draw(self, context):
layout = self.layout
row = layout.row()
row.prop(context.scene, "character_name", text="Name")
row = layout.row()
row.prop(context.scene, "character_size", text="Size")
row = layout.row()
row.prop(context.scene, "character_keys", text="Keys")
row = layout.row()
row.prop(context.scene, "key_sensitive", text="Key sensitive")
row = layout.row()
row.prop(context.scene, "mouse_sensitive", text="Mouse sensitive")
row = layout.row()
row.prop(context.scene, "character_jump", text="Jump available")
row = layout.row()
row.operator(simple_character.bl_idname, text='Add Character')
row = layout.row()
row.operator(fly_camera.bl_idname, text='Add Fly Camera')
def register():
bpy.utils.register_class(addcharacter)
bpy.utils.register_class(fly_camera)
bpy.utils.register_class(simple_character)
def unregister():
bpy.utils.unregister_class(addcharacter)
bpy.utils.unregister_class(fly_camera)
bpy.utils.unregister_class(simple_character)
if __name__ == '__main__':
register()