mirror of
https://forge.apps.education.fr/blender-edutech/lecteur-3d-cinematique.git
synced 2024-01-27 09:43:12 +01:00
700 lines
33 KiB
Python
700 lines
33 KiB
Python
|
import bge # Bibliothèque Blender Game Engine (BGE)
|
|||
|
import webbrowser
|
|||
|
import math
|
|||
|
|
|||
|
###############################################################################
|
|||
|
# batoncolle.py
|
|||
|
# @title: Commandes pour le player 3D de l'hémomixer (système de clampage)
|
|||
|
# @project: Blender-EduTech
|
|||
|
# @lang: fr
|
|||
|
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
|||
|
# @copyright: Copyright (C) 2020-2022 Philippe Roy
|
|||
|
# @license: GNU GPL
|
|||
|
#
|
|||
|
# Commandes déclenchées par UPBGE pour le modèle de l'hémomixer (système de clampage)
|
|||
|
#
|
|||
|
# Ce player 3D est un environnement léger et spécifique pour la colorisation des solides d'un mécanisme en mouvement.
|
|||
|
# Il sert principalement pour l'apprentissage de la détection des classes d'équivalence d'un mécanisme afin de pouvoir faire sa modélisation cinématique.
|
|||
|
#
|
|||
|
###############################################################################
|
|||
|
|
|||
|
# Récupérer la scène 3D
|
|||
|
scene = bge.logic.getCurrentScene()
|
|||
|
# print("Objets de la scene : ", scene.objects)
|
|||
|
|
|||
|
# Objets 3D
|
|||
|
|
|||
|
objets=['Bloc clampeur', 'Disque', 'Moteur', 'Réducteur', 'Tige clampeur', 'Axe 1', 'Ressort', 'Palonnier', 'Axe réducteur', 'Sabot', 'Support moteur', 'Support clampeur',
|
|||
|
'Maillon droite', 'Maillon gauche', 'Maillon axe 1', 'Maillon axe 2', 'Guide palonnier', 'Bague 2', 'Bague 1', 'Ecrou 1', 'Ecrou 2', 'Ecrou 3', 'Ecrou 4', 'Ecrou 5', 'Ecrou 6',
|
|||
|
'Goujon', 'Rondelle 1', 'Rondelle 4', 'Rondelle 2', 'Rondelle 3', 'Vis 5', 'Vis 1', 'Vis 2', 'Vis 3', 'Vis 4', 'Axe 2', 'Circlips 1', 'Circlips 2', 'Circlips 3']
|
|||
|
|
|||
|
objets_anim=['Disque', 'Ressort', 'Palonnier', 'Axe réducteur', 'Maillon gauche', 'Maillon axe 1']
|
|||
|
|
|||
|
objets_couleur=['VioletF', 'TurquoiseF', 'Couleur7', 'MagentaF', 'Couleur4', 'GrisF', 'Couleur6', 'VertF', 'MagentaF', 'Couleur5', 'Couleur2', 'Couleur1', 'RougeF', 'Couleur3', 'GrisF', 'GrisF',
|
|||
|
'Gris', 'GrisC', 'GrisC', 'Gris', 'Gris', 'Gris', 'Gris', 'Gris', 'Gris', 'GrisF', 'GrisC', 'GrisC', 'GrisC', 'GrisC', 'Gris', 'Gris', 'Gris', 'Gris', 'Gris', 'GrisF', 'Gris', 'Gris', 'Gris']
|
|||
|
|
|||
|
objets_rep=[] # Objet avec un repère
|
|||
|
objets_rep_dict={}
|
|||
|
|
|||
|
# Couleurs
|
|||
|
|
|||
|
couleurs=[['Couleur1', [0.202, 0.114, 0.512,1]],
|
|||
|
['Couleur2', [0.051, 0.270, 0.279,1]],
|
|||
|
['Couleur3', [0.799, 0.031, 0.038,1]],
|
|||
|
['Couleur4', [0.799, 0.130, 0.063,1]],
|
|||
|
['Couleur5', [0.8, 0.619, 0.021,1]],
|
|||
|
['Couleur6', [0.246, 0.687, 0.078,1]],
|
|||
|
['Couleur7', [0.800, 0.005, 0.315,1]],
|
|||
|
['Couleur8', [0.75,0.751, 0.75,0]]]
|
|||
|
|
|||
|
couleurs_dict={'Couleur1' : [0.202, 0.114, 0.512, 1],
|
|||
|
'Couleur2' : [0.051, 0.270, 0.279, 1],
|
|||
|
'Couleur3' : [0.799, 0.031, 0.038, 1],
|
|||
|
'Couleur4' : [0.799, 0.130, 0.063, 1],
|
|||
|
'Couleur5' : [0.8, 0.619, 0.021, 1],
|
|||
|
'Couleur6' : [0.246, 0.687, 0.078, 1],
|
|||
|
'Couleur7' : [0.800, 0.005, 0.315, 1],
|
|||
|
'Couleur8' : [0.75,0.75, 0.75, 0],
|
|||
|
'TurquoiseF' : [0.019,0.107, 0.105, 1],
|
|||
|
'MagentaF' : [0.212,0.003, 0.089, 1],
|
|||
|
'VertF' : [0.041,0.127, 0.014, 1],
|
|||
|
'VioletF' : [0.080,0.046, 0.307, 1],
|
|||
|
'RougeF' : [0.175,0.010, 0.012, 1],
|
|||
|
'Gris' : [0.258,0.275, 0.319, 1],
|
|||
|
'GrisF' : [0.087,0.093, 0.107, 1],
|
|||
|
'GrisC' : [0.501,0.534, 0.621, 1],
|
|||
|
'Blanc' : [0.8, 0.8, 0.8, 1]}
|
|||
|
|
|||
|
couleur_cmd = [0.8, 0.8, 0.8, 1] # blanc
|
|||
|
couleur_cmd_hl = [0.8, 0.619, 0.021, 1] # jaune
|
|||
|
couleur_lien = [0.024, 0.006, 0.8, 1] # bleu
|
|||
|
couleur_lien_hl = [0.8, 0.005, 0.315, 1] # bleu
|
|||
|
|
|||
|
# Constantes
|
|||
|
|
|||
|
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
|
|||
|
JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
|
|||
|
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
|
|||
|
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
|
|||
|
|
|||
|
###############################################################################
|
|||
|
# Gestion du clavier
|
|||
|
###############################################################################
|
|||
|
|
|||
|
# Mode : Pan(1) avec Shift, Zoom (2) avec Ctrl, Orbit (0), Eclaté avec Shift
|
|||
|
def mode(cont):
|
|||
|
obj = cont.owner
|
|||
|
keyboard = bge.logic.keyboard
|
|||
|
|
|||
|
# Shift -> mode 1 : Pan (clic milieu) ou Eclaté (clic gauche)
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.LEFTSHIFTKEY].queue:
|
|||
|
obj['manip_mode']=1
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.RIGHTSHIFTKEY].queue:
|
|||
|
obj['manip_mode']=1
|
|||
|
|
|||
|
# Ctrl -> mode 2 : Zoom (clic milieu)
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.LEFTCTRLKEY].queue:
|
|||
|
obj['manip_mode']=2
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.RIGHTCTRLKEY].queue:
|
|||
|
obj['manip_mode']=2
|
|||
|
|
|||
|
# Pas de modificateur -> mode 0 : Orbit (clic milieu)
|
|||
|
if JUST_RELEASED in keyboard.inputs[bge.events.LEFTSHIFTKEY].queue:
|
|||
|
obj['manip_mode']=0
|
|||
|
if JUST_RELEASED in keyboard.inputs[bge.events.RIGHTSHIFTKEY].queue:
|
|||
|
obj['manip_mode']=0
|
|||
|
if JUST_RELEASED in keyboard.inputs[bge.events.LEFTCTRLKEY].queue:
|
|||
|
obj['manip_mode']=0
|
|||
|
if JUST_RELEASED in keyboard.inputs[bge.events.RIGHTCTRLKEY].queue:
|
|||
|
obj['manip_mode']=0
|
|||
|
|
|||
|
# Touche Home -> Reset de la vue
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.HOMEKEY].queue:
|
|||
|
scene.objects['Camera'].worldPosition.x = scene.objects['Camera']['init_lx']
|
|||
|
scene.objects['Camera'].worldPosition.y = scene.objects['Camera']['init_ly']
|
|||
|
scene.objects['Camera'].worldPosition.z = scene.objects['Camera']['init_lz']
|
|||
|
applyRotationTo(scene.objects['Mecanisme'], 0, 0, 0)
|
|||
|
for objet in objets :
|
|||
|
scene.objects[objet].setVisible(True,False)
|
|||
|
scene.objects[objet+"_Lines.GP"].setVisible(True,False)
|
|||
|
|
|||
|
# Retour après éclatement
|
|||
|
if scene.objects['Mecanisme']['eclate'] ==True:
|
|||
|
scene.objects['Mecanisme']['eclate'] = False
|
|||
|
ecla_val = scene.objects['Mecanisme']['ecla_val'] # Retour après éclatement avec la valeur d'éclatement
|
|||
|
sensibilite=0.005
|
|||
|
for objet in objets :
|
|||
|
ecla_x, ecla_y,ecla_z = 0,0,0
|
|||
|
if 'ecla_x' in scene.objects[objet]:
|
|||
|
ecla_x = scene.objects[objet]['ecla_x']
|
|||
|
if 'ecla_y' in scene.objects[objet]:
|
|||
|
ecla_y = scene.objects[objet]['ecla_y']
|
|||
|
if 'ecla_z' in scene.objects[objet]:
|
|||
|
ecla_z = scene.objects[objet]['ecla_z']
|
|||
|
scene.objects[objet].applyMovement((-ecla_val*sensibilite*ecla_x, -ecla_val*sensibilite*ecla_y , -ecla_val*sensibilite*ecla_z), False)
|
|||
|
scene.objects['Mecanisme']['ecla_val'] = 0.00
|
|||
|
if scene.objects['Mecanisme']['anim'] == False: # Play d'une frame
|
|||
|
anim_play1frame()
|
|||
|
|
|||
|
# Touche Space -> Play et Pause
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.SPACEKEY].queue:
|
|||
|
if scene.objects['Mecanisme']['anim'] == True:
|
|||
|
anim_pause()
|
|||
|
scene.objects['Pause'].setVisible(False,False)
|
|||
|
scene.objects['Play'].setVisible(True,False)
|
|||
|
else:
|
|||
|
anim_play()
|
|||
|
scene.objects['Play'].setVisible(False,False)
|
|||
|
scene.objects['Pause'].setVisible(True,False)
|
|||
|
|
|||
|
# Touche H -> Cacher l'objet
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.HKEY].queue:
|
|||
|
couleur_choix_num(8)
|
|||
|
|
|||
|
# Touche de 1 à 8 -> Couleur de 1 à 8
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.ONEKEY].queue or JUST_ACTIVATED in keyboard.inputs[bge.events.PAD1].queue:
|
|||
|
couleur_choix_num(1)
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.TWOKEY].queue or JUST_ACTIVATED in keyboard.inputs[bge.events.PAD2].queue:
|
|||
|
couleur_choix_num(2)
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.THREEKEY].queue or JUST_ACTIVATED in keyboard.inputs[bge.events.PAD3].queue:
|
|||
|
couleur_choix_num(3)
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.FOURKEY].queue or JUST_ACTIVATED in keyboard.inputs[bge.events.PAD4].queue:
|
|||
|
couleur_choix_num(4)
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.FIVEKEY].queue or JUST_ACTIVATED in keyboard.inputs[bge.events.PAD5].queue:
|
|||
|
couleur_choix_num(5)
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.SIXKEY].queue or JUST_ACTIVATED in keyboard.inputs[bge.events.PAD6].queue:
|
|||
|
couleur_choix_num(6)
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.SEVENKEY].queue or JUST_ACTIVATED in keyboard.inputs[bge.events.PAD7].queue:
|
|||
|
couleur_choix_num(7)
|
|||
|
if JUST_ACTIVATED in keyboard.inputs[bge.events.EIGHTKEY].queue or JUST_ACTIVATED in keyboard.inputs[bge.events.PAD8].queue:
|
|||
|
couleur_choix_num(8)
|
|||
|
|
|||
|
###############################################################################
|
|||
|
# Commandes
|
|||
|
###############################################################################
|
|||
|
|
|||
|
# Init
|
|||
|
def cmd_init():
|
|||
|
scene.objects['Aide-cmd-Hl'].setVisible(False,False)
|
|||
|
scene.objects['Pause-Hl'].setVisible(False,False)
|
|||
|
scene.objects['Play-Hl'].setVisible(False,False)
|
|||
|
scene.objects['Raz-couleurs-Hl'].setVisible(False,False)
|
|||
|
scene.objects['Raz-vue-Hl'].setVisible(False,False)
|
|||
|
for i in range (1, 9):
|
|||
|
scene.objects['Couleur'+str(i)+"-Hl"].setVisible(False,True)
|
|||
|
|
|||
|
# Le highlight des commandes
|
|||
|
def cmd_hl(cont):
|
|||
|
obj = cont.owner
|
|||
|
|
|||
|
# Activation
|
|||
|
if cont.sensors['MO'].status == JUST_ACTIVATED and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
if obj.name!="Play" and obj.name!="Pause" and obj.name!="Play-Hl" and obj.name!="Pause-Hl":
|
|||
|
obj.setVisible(False,True)
|
|||
|
scene.objects[obj.name+'-Hl'].setVisible(True,True)
|
|||
|
# obj.color = couleur_cmd_hl
|
|||
|
|
|||
|
# Play et pause
|
|||
|
if obj.name=="Pause" or obj.name=="Play":
|
|||
|
if scene.objects['Mecanisme']['anim'] == True:
|
|||
|
scene.objects['Pause'].setVisible(False,False)
|
|||
|
scene.objects['Pause-Hl'].setVisible(True,False)
|
|||
|
else:
|
|||
|
scene.objects['Play'].setVisible(False,False)
|
|||
|
scene.objects['Play-Hl'].setVisible(True,False)
|
|||
|
|
|||
|
# Désactivation
|
|||
|
if cont.sensors['MO'].status == JUST_RELEASED and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
if obj.name!="Play" and obj.name!="Pause" and obj.name!="Play-Hl" and obj.name!="Pause-Hl":
|
|||
|
scene.objects[obj.name+'-Hl'].setVisible(False,True)
|
|||
|
obj.setVisible(True,True)
|
|||
|
|
|||
|
# Play et pause
|
|||
|
if obj.name=="Pause" or obj.name=="Play":
|
|||
|
if scene.objects['Mecanisme']['anim'] == True:
|
|||
|
scene.objects['Pause-Hl'].setVisible(False,False)
|
|||
|
scene.objects['Pause'].setVisible(True,False)
|
|||
|
else:
|
|||
|
scene.objects['Play-Hl'].setVisible(False,False)
|
|||
|
scene.objects['Play'].setVisible(True,False)
|
|||
|
|
|||
|
###############################################################################
|
|||
|
# Animation
|
|||
|
###############################################################################
|
|||
|
|
|||
|
# Initialisation de l'animation -> Play
|
|||
|
def anim_init(cont):
|
|||
|
start = 1
|
|||
|
end = 250
|
|||
|
layer = 0
|
|||
|
priority = 1
|
|||
|
blendin = 1.0
|
|||
|
mode = bge.logic.KX_ACTION_MODE_LOOP
|
|||
|
layerWeight = 0.0
|
|||
|
ipoFlags = 0
|
|||
|
speed = 1.0
|
|||
|
for objet in objets_anim :
|
|||
|
scene.objects[objet].playAction(objet+'-Action', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
|||
|
scene.objects['Play'].setVisible(False,False)
|
|||
|
scene.objects['Pause'].setVisible(True,False)
|
|||
|
scene.objects['Mecanisme']['anim'] = True
|
|||
|
|
|||
|
# Animation en pause (bas niveau)
|
|||
|
def anim_pause():
|
|||
|
layer = 0
|
|||
|
for objet in objets_anim :
|
|||
|
scene.objects['Mecanisme']['anim_frame'] = scene.objects[objet].getActionFrame(layer)
|
|||
|
scene.objects[objet].stopAction(layer)
|
|||
|
scene.objects['Mecanisme']['anim'] = False
|
|||
|
|
|||
|
# Animation en pseudo-pause
|
|||
|
# Play d'une frame pour remettre les pièces en place
|
|||
|
def anim_play1frame():
|
|||
|
start = scene.objects['Mecanisme']['anim_frame']
|
|||
|
end = scene.objects['Mecanisme']['anim_frame']+1
|
|||
|
layer = 0
|
|||
|
priority = 1
|
|||
|
blendin = 1.0
|
|||
|
mode = bge.logic.KX_ACTION_MODE_PLAY
|
|||
|
layerWeight = 0.0
|
|||
|
ipoFlags = 0
|
|||
|
speed = 1.0
|
|||
|
for objet in objets_anim :
|
|||
|
scene.objects[objet].playAction(objet+'-Action', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
|||
|
scene.objects['Mecanisme']['anim_frame'] = end
|
|||
|
|
|||
|
# Reprise de l'animation (bas niveau)
|
|||
|
def anim_play():
|
|||
|
start = scene.objects['Mecanisme']['anim_frame']
|
|||
|
end = 250
|
|||
|
layer = 0
|
|||
|
priority = 1
|
|||
|
blendin = 1.0
|
|||
|
mode = bge.logic.KX_ACTION_MODE_PLAY
|
|||
|
layerWeight = 0.0
|
|||
|
ipoFlags = 0
|
|||
|
speed = 1.0
|
|||
|
for objet in objets_anim :
|
|||
|
scene.objects[objet].playAction(objet+'-Action', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
|||
|
scene.objects['Mecanisme']['anim'] = True
|
|||
|
|
|||
|
# Pause et replay par les commandes
|
|||
|
def anim_cmd(cont):
|
|||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
|
|||
|
# Pause
|
|||
|
if scene.objects['Mecanisme']['anim'] == True:
|
|||
|
anim_pause()
|
|||
|
scene.objects['Pause'].setVisible(False,False)
|
|||
|
scene.objects['Pause-Hl'].setVisible(False,False)
|
|||
|
scene.objects['Play-Hl'].setVisible(True,False)
|
|||
|
else:
|
|||
|
# Play
|
|||
|
anim_play()
|
|||
|
scene.objects['Play'].setVisible(False,False)
|
|||
|
scene.objects['Play-Hl'].setVisible(False,False)
|
|||
|
scene.objects['Pause-Hl'].setVisible(True,False)
|
|||
|
|
|||
|
# Play en continu
|
|||
|
def anim_boucle(cont):
|
|||
|
if scene.objects['Mecanisme']['anim'] == True and scene.objects[objets_anim[0]].isPlayingAction(0) == False:
|
|||
|
# scene.objects['Mecanisme']['anim_frame']=1
|
|||
|
start = 1
|
|||
|
end = 250
|
|||
|
layer = 0
|
|||
|
priority = 1
|
|||
|
blendin = 1.0
|
|||
|
mode = bge.logic.KX_ACTION_MODE_LOOP
|
|||
|
layerWeight = 0.0
|
|||
|
ipoFlags = 0
|
|||
|
speed = 1.0
|
|||
|
for objet in objets_anim :
|
|||
|
scene.objects[objet].playAction(objet+'-Action', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
|||
|
|
|||
|
###############################################################################
|
|||
|
# Manipulation du mécanisme
|
|||
|
###############################################################################
|
|||
|
|
|||
|
# Mémorisation de la position et orientation initiales du modèle et de la caméra
|
|||
|
def manip_init(cont):
|
|||
|
for objet in objets :
|
|||
|
scene.objects[objet]['init_lx']=scene.objects[objet].worldPosition.x
|
|||
|
scene.objects[objet]['init_ly']=scene.objects[objet].worldPosition.y
|
|||
|
scene.objects[objet]['init_lz']=scene.objects[objet].worldPosition.z
|
|||
|
if 'init_rx' in scene.objects[objet]:
|
|||
|
scene.objects[objet]['init_rx'] = scene.objects[objet].worldOrientation.to_euler().x
|
|||
|
if 'init_ry' in scene.objects[objet]:
|
|||
|
scene.objects[objet]['init_ry'] = scene.objects[objet].worldOrientation.to_euler().y
|
|||
|
if 'init_rz' in scene.objects[objet]:
|
|||
|
scene.objects[objet]['init_rz'] = scene.objects[objet].worldOrientation.to_euler().z
|
|||
|
scene.objects['Camera']['init_lx']=scene.objects['Camera'].worldPosition.x
|
|||
|
scene.objects['Camera']['init_ly']=scene.objects['Camera'].worldPosition.y
|
|||
|
scene.objects['Camera']['init_lz']=scene.objects['Camera'].worldPosition.z
|
|||
|
scene.objects['Mecanisme']['init_lx']=scene.objects['Mecanisme'].worldPosition.x
|
|||
|
scene.objects['Mecanisme']['init_ly']=scene.objects['Mecanisme'].worldPosition.y
|
|||
|
scene.objects['Mecanisme']['init_lz']=scene.objects['Mecanisme'].worldPosition.z
|
|||
|
scene.objects['Mecanisme']['init_rx']=scene.objects['Mecanisme'].worldOrientation.to_euler().x
|
|||
|
scene.objects['Mecanisme']['init_ry']=scene.objects['Mecanisme'].worldOrientation.to_euler().y
|
|||
|
scene.objects['Mecanisme']['init_rz']=scene.objects['Mecanisme'].worldOrientation.to_euler().z
|
|||
|
|
|||
|
# def applyMovemenTo(obj, lx,ly,lz,rx,ry,rz):
|
|||
|
# lres=0.001 # resolution location
|
|||
|
# rres=0.001 # resolution rotation
|
|||
|
# while (obj.worldPosition.x-lx > lres) or (obj.worldPosition.y-ly > lres) or (obj.worldPosition.z-lz > lres) or (obj.worldOrientation.to_euler().x-rx > rres) or (obj.worldOrientation.to_euler().y-ry > rres) or (obj.worldOrientation.to_euler().z-rz > rres):
|
|||
|
# if obj.worldPosition.x-lx > lres:
|
|||
|
# obj. applyMovement(lres,0,0)
|
|||
|
# if obj.worldPosition.y-ly > lres:
|
|||
|
# obj. applyMovement(0,lres,0 )
|
|||
|
# if obj.worldPosition.z-lz > lres:
|
|||
|
# obj. applyMovement(0,0,lres)
|
|||
|
# if obj.worldOrientation.to_euler().x-rx > rres:
|
|||
|
# obj.applyRotation(rres,0,0)
|
|||
|
# if obj.worldOrientation.to_euler().y-ry > rres:
|
|||
|
# obj.applyRotation(0,rres,0)
|
|||
|
# if obj.worldOrientation.to_euler().z-ry > rres:
|
|||
|
# obj.applyRotation(0,0,rres)
|
|||
|
|
|||
|
# def applyTranslationTo(obj, lx,ly,lz):
|
|||
|
# lres=0.001 # resolution location
|
|||
|
# while (obj.worldPosition.x-lx > lres) or (obj.worldPosition.y-ly > lres) or (obj.worldPosition.z-lz > lres):
|
|||
|
# if obj.worldPosition.x-lx > lres:
|
|||
|
# obj. applyMovement(lres,0,0)
|
|||
|
# if obj.worldPosition.y-ly > lres:
|
|||
|
# obj. applyMovement(0,lres,0 )
|
|||
|
# if obj.worldPosition.z-lz > lres:
|
|||
|
# obj. applyMovement(0,0,lres)
|
|||
|
|
|||
|
# Atteindre une orientation (bas niveau)
|
|||
|
def applyRotationTo(obj, rx=None, ry=None, rz=None):
|
|||
|
rres=0.001 # resolution rotation
|
|||
|
|
|||
|
# x
|
|||
|
if rx is not None:
|
|||
|
while (abs(rx-obj.worldOrientation.to_euler().x) > rres) :
|
|||
|
if obj.worldOrientation.to_euler().x-rx > rres:
|
|||
|
obj.applyRotation((-rres, 0, 0), True)
|
|||
|
if rx-obj.worldOrientation.to_euler().x > rres:
|
|||
|
obj.applyRotation((rres, 0, 0), True)
|
|||
|
# print ("delta x ",rx-obj.worldOrientation.to_euler().x)
|
|||
|
|
|||
|
# y
|
|||
|
if ry is not None:
|
|||
|
while (abs(ry-obj.worldOrientation.to_euler().y) > rres) :
|
|||
|
if obj.worldOrientation.to_euler().y-ry > rres:
|
|||
|
obj.applyRotation((0, -rres, 0), True)
|
|||
|
if ry-obj.worldOrientation.to_euler().y > rres:
|
|||
|
obj.applyRotation((0, rres, 0), True)
|
|||
|
# print ("delta y ",ry-obj.worldOrientation.to_euler().y)
|
|||
|
|
|||
|
# z
|
|||
|
if rz is not None:
|
|||
|
while (abs(rz-obj.worldOrientation.to_euler().z) > rres) :
|
|||
|
if obj.worldOrientation.to_euler().z-rz > rres:
|
|||
|
obj.applyRotation((0, 0, -rres), True)
|
|||
|
if rz-obj.worldOrientation.to_euler().z > rres:
|
|||
|
obj.applyRotation((0, 0, rres), True)
|
|||
|
# print ("delta z ",rz-obj.worldOrientation.to_euler().z)
|
|||
|
|
|||
|
# Reset de la manipulation de la vue
|
|||
|
def manip_reset(cont):
|
|||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
scene.objects['Camera'].worldPosition.x = scene.objects['Camera']['init_lx']
|
|||
|
scene.objects['Camera'].worldPosition.y = scene.objects['Camera']['init_ly']
|
|||
|
scene.objects['Camera'].worldPosition.z = scene.objects['Camera']['init_lz']
|
|||
|
applyRotationTo(scene.objects['Mecanisme'], 0, 0, 0)
|
|||
|
for objet in objets :
|
|||
|
scene.objects[objet].setVisible(True,False)
|
|||
|
scene.objects[objet+"_Lines.GP"].setVisible(True,False)
|
|||
|
|
|||
|
# Retour après éclatement
|
|||
|
if scene.objects['Mecanisme']['eclate'] ==True:
|
|||
|
scene.objects['Mecanisme']['eclate'] = False
|
|||
|
ecla_val = scene.objects['Mecanisme']['ecla_val'] # Retour après éclatement avec la valeur d'éclatement
|
|||
|
sensibilite=0.005
|
|||
|
for objet in objets :
|
|||
|
ecla_x, ecla_y,ecla_z = 0,0,0
|
|||
|
if 'ecla_x' in scene.objects[objet]:
|
|||
|
ecla_x = scene.objects[objet]['ecla_x']
|
|||
|
if 'ecla_y' in scene.objects[objet]:
|
|||
|
ecla_y = scene.objects[objet]['ecla_y']
|
|||
|
if 'ecla_z' in scene.objects[objet]:
|
|||
|
ecla_z = scene.objects[objet]['ecla_z']
|
|||
|
scene.objects[objet].applyMovement((-ecla_val*sensibilite*ecla_x, -ecla_val*sensibilite*ecla_y , -ecla_val*sensibilite*ecla_z), False)
|
|||
|
scene.objects['Mecanisme']['ecla_val'] = 0.00
|
|||
|
if scene.objects['Mecanisme']['anim'] == False: # Play d'une frame
|
|||
|
anim_play1frame()
|
|||
|
|
|||
|
# Position de départ pour la manipulation de la vue
|
|||
|
def manip_start(cont):
|
|||
|
obj = cont.owner
|
|||
|
obj['click_x']=cont.sensors['ClickM'].position[0]
|
|||
|
obj['click_y']=cont.sensors['ClickM'].position[1]
|
|||
|
|
|||
|
# Cacher le cercle de la manipulation Orbit
|
|||
|
def manip_stop(cont):
|
|||
|
scene.objects['Orbit'].setVisible(False,False)
|
|||
|
|
|||
|
# Manipulation du modèle ou de la caméra
|
|||
|
def manip(cont):
|
|||
|
obj = cont.owner
|
|||
|
sensibilite_orbit=0.0005
|
|||
|
sensibilite_pan=0.005
|
|||
|
sensibilite_zoom=0.01
|
|||
|
delta_x=cont.sensors['DownM'].position[0]-obj['click_x']
|
|||
|
delta_y=cont.sensors['DownM'].position[1]-obj['click_y']
|
|||
|
|
|||
|
# Orbit (1280 * 720 px)
|
|||
|
if obj['manip_mode']==0:
|
|||
|
scene.objects['Orbit'].color=couleur_cmd
|
|||
|
scene.objects['Orbit'].setVisible(True,False)
|
|||
|
dist_orbit = math.sqrt(((1280/2)-obj['click_x'])**2+((720/2)-obj['click_y'])**2)
|
|||
|
if dist_orbit<235 : # Orbit sur x et z
|
|||
|
n=10
|
|||
|
pas_x=(delta_x*40*sensibilite_orbit)/n
|
|||
|
pas_y=(((1280/2)-cont.sensors['DownM'].position[0])+((720/2)-cont.sensors['DownM'].position[1]))*0.005
|
|||
|
pas_z=(delta_y*40*sensibilite_orbit)/n
|
|||
|
for i in range (n):
|
|||
|
bge.render.drawLine([scene.objects['Orbit'].worldPosition.x+pas_x*i, scene.objects['Orbit'].worldPosition.y+abs(pas_y*math.sin((3.14*i)/n)), scene.objects['Orbit'].worldPosition.z-pas_z*i],
|
|||
|
[scene.objects['Orbit'].worldPosition.x+pas_x*(i+1), scene.objects['Orbit'].worldPosition.y+abs(pas_y*math.sin((3.14*(i+1))/n)), scene.objects['Orbit'].worldPosition.z-pas_z*(i+1)],
|
|||
|
[0.8, 0.619, 0.021])
|
|||
|
scene.objects['Mecanisme'].applyRotation((delta_y*sensibilite_orbit, 0, delta_x*sensibilite_orbit), True)
|
|||
|
else: # Orbit sur y
|
|||
|
scene.objects['Orbit'].color=couleur_cmd_hl
|
|||
|
if abs(delta_x) >= abs(delta_y):
|
|||
|
scene.objects['Mecanisme'].applyRotation((0, delta_x*sensibilite_orbit, 0), True)
|
|||
|
else:
|
|||
|
scene.objects['Mecanisme'].applyRotation((0, delta_y*sensibilite_orbit, 0), True)
|
|||
|
# if obj['manip_mode']==0:
|
|||
|
# scene.objects['Mecanisme'].applyRotation((delta_y*sensibilite_orbit, 0, delta_x*sensibilite_orbit), True)
|
|||
|
|
|||
|
# Pan
|
|||
|
if obj['manip_mode']==1: # Shift
|
|||
|
scene.objects['Camera'].applyMovement((delta_x*-sensibilite_pan, delta_y*sensibilite_pan, 0), True)
|
|||
|
|
|||
|
# Zoom
|
|||
|
if obj['manip_mode']==2: # Ctrl
|
|||
|
scene.objects['Camera'].applyMovement((0, 0, (delta_x+delta_y)*sensibilite_zoom), True)
|
|||
|
|
|||
|
# Manipulation du modèle ou de la caméra
|
|||
|
def manip_wheel(cont):
|
|||
|
obj = cont.owner
|
|||
|
sensibilite_wheel = 20
|
|||
|
if cont.sensors['WheelUp'].positive:
|
|||
|
scene.objects['Camera'].applyMovement((0, 0, -sensibilite_wheel), True)
|
|||
|
if cont.sensors['WheelDown'].positive:
|
|||
|
scene.objects['Camera'].applyMovement((0, 0, sensibilite_wheel), True)
|
|||
|
|
|||
|
###############################################################################
|
|||
|
# Eclaté
|
|||
|
###############################################################################
|
|||
|
|
|||
|
# Position de départ pour l'éclatement de la vue
|
|||
|
def eclate_start(cont):
|
|||
|
if scene.objects['Mecanisme']['manip_mode']==1:
|
|||
|
obj = cont.owner
|
|||
|
obj['click_x']=cont.sensors['Click'].position[0]
|
|||
|
obj['click_y']=cont.sensors['Click'].position[1]
|
|||
|
|
|||
|
# Déplacement des composants en vue éclatée
|
|||
|
def eclate(cont):
|
|||
|
obj = cont.owner
|
|||
|
sensibilite=0.005
|
|||
|
delta=(cont.sensors['Down'].position[0]-obj['click_x'])+(cont.sensors['Down'].position[1]-obj['click_y'])
|
|||
|
if obj['manip_mode']==1: # Shift
|
|||
|
scene.objects['Mecanisme']['eclate'] =True
|
|||
|
scene.objects['Mecanisme']['ecla_val'] =scene.objects['Mecanisme']['ecla_val']+delta
|
|||
|
|
|||
|
# Déplacement dans le repère du mécanisme
|
|||
|
x_axis = scene.objects['Mecanisme'].orientation.col[0]
|
|||
|
y_axis = scene.objects['Mecanisme'].orientation.col[1]
|
|||
|
z_axis = scene.objects['Mecanisme'].orientation.col[2]
|
|||
|
for objet in objets :
|
|||
|
ecla_x, ecla_y,ecla_z = 0,0,0
|
|||
|
if 'ecla_x' in scene.objects[objet]:
|
|||
|
ecla_x = scene.objects[objet]['ecla_x']
|
|||
|
if 'ecla_y' in scene.objects[objet]:
|
|||
|
ecla_y = scene.objects[objet]['ecla_y']
|
|||
|
if 'ecla_z' in scene.objects[objet]:
|
|||
|
ecla_z = scene.objects[objet]['ecla_z']
|
|||
|
scene.objects[objet].applyMovement((delta*sensibilite*ecla_x*x_axis[0], delta*sensibilite*ecla_x*x_axis[1] , delta*sensibilite*ecla_x*x_axis[2]), False)
|
|||
|
scene.objects[objet].applyMovement((delta*sensibilite*ecla_y*y_axis[0], delta*sensibilite*ecla_y*y_axis[1] , delta*sensibilite*ecla_y*y_axis[2]), False)
|
|||
|
scene.objects[objet].applyMovement((delta*sensibilite*ecla_z*z_axis[0], delta*sensibilite*ecla_z*z_axis[1] , delta*sensibilite*ecla_z*z_axis[2]), False)
|
|||
|
|
|||
|
###############################################################################
|
|||
|
# Couleur
|
|||
|
###############################################################################
|
|||
|
|
|||
|
# Initialisation des couleurs du mécanisme
|
|||
|
def couleur_init():
|
|||
|
# print("Objets de la scene : ", scene.objects)
|
|||
|
i=0
|
|||
|
for objet in objets :
|
|||
|
scene.objects[objet].color = couleurs_dict[objets_couleur[i]]
|
|||
|
scene.objects[objet].setVisible(True,False)
|
|||
|
scene.objects[objet+"_Lines.GP"].setVisible(True,False)
|
|||
|
scene.objects[objet+"-Nom"].color = couleurs_dict[objets_couleur[i]]
|
|||
|
scene.objects[objet+"-Nom-Hl"].color = couleurs_dict[objets_couleur[i]]
|
|||
|
scene.objects[objet+"-Nom-Hl"].setVisible(False,False)
|
|||
|
i +=1
|
|||
|
|
|||
|
# Reset des couleurs du mécanisme
|
|||
|
def couleur_reset(cont):
|
|||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
couleur_init()
|
|||
|
|
|||
|
# Sélection couleur dans la palette avec son numéro de couleur (bas niveau)
|
|||
|
def couleur_choix_num(num):
|
|||
|
for i in range (1, 9):
|
|||
|
scene.objects['Couleur'+str(i)].worldScale=[3, 3, 3]
|
|||
|
scene.objects['Couleur'+str(i)+"-Hl"].worldScale=[3, 3, 3]
|
|||
|
scene.objects['Couleur'+str(num)].worldScale=[4, 4, 4]
|
|||
|
scene.objects['Couleur'+str(num)+'-Hl'].worldScale=[4, 4, 4]
|
|||
|
scene.objects['Couleurs']['couleur_select']=num
|
|||
|
|
|||
|
# Sélection couleur dans la palette
|
|||
|
def couleur_choix(cont):
|
|||
|
obj = cont.owner
|
|||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
hitObject = cont.sensors['MO'].hitObject
|
|||
|
if hitObject is not None and "-Hl" not in hitObject .name:
|
|||
|
couleur_choix_num(int(hitObject .name[7]))
|
|||
|
|
|||
|
# Le highlight de 'couleur_choix'
|
|||
|
def couleur_choix_hl(cont):
|
|||
|
if cont.sensors['MO'].positive and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
hitObject = cont.sensors['MO'].hitObject
|
|||
|
if hitObject is not None and "-Hl" not in hitObject .name:
|
|||
|
hitObject.setVisible(False,True)
|
|||
|
scene.objects[hitObject.name+'-Hl'].setVisible(True,True)
|
|||
|
# obj.color = couleur_cmd_hl
|
|||
|
if cont.sensors['MO'].status == JUST_RELEASED:
|
|||
|
for i in range (1, 9):
|
|||
|
scene.objects['Couleur'+str(i)].setVisible(True,True)
|
|||
|
scene.objects['Couleur'+str(i)+"-Hl"].setVisible(False,True)
|
|||
|
|
|||
|
# Colorisation de la pièce
|
|||
|
def couleur_objet(cont):
|
|||
|
if cont.sensors['Click'].positive and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
hitObject = cont.sensors['MO'].hitObject
|
|||
|
if hitObject is not None and hitObject.visible:
|
|||
|
couleur_select = scene.objects['Couleurs']['couleur_select']
|
|||
|
if couleur_select != 0 and couleur_select != 8:
|
|||
|
hitObject.color = couleurs[couleur_select-1][1]
|
|||
|
scene.objects[hitObject.name+"-Nom"].color = couleurs[couleur_select-1][1]
|
|||
|
scene.objects[hitObject.name+"-Nom-Hl"].color = couleurs[couleur_select-1][1]
|
|||
|
if couleur_select == 8: # Couleur transparente -> disparition
|
|||
|
hitObject.setVisible(False,False)
|
|||
|
scene.objects[hitObject.name+"_Lines.GP"].setVisible(False,False)
|
|||
|
|
|||
|
# Le highlight de 'couleur_objet'
|
|||
|
def couleur_objet_hl(cont):
|
|||
|
if cont.sensors['MO'].status == JUST_ACTIVATED and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
hitObject = cont.sensors['MO'].hitObject
|
|||
|
if hitObject is not None and hitObject.visible:
|
|||
|
scene.objects[hitObject.name+"-Nom"].setVisible(False,False)
|
|||
|
scene.objects[hitObject.name+"-Nom-Hl"].setVisible(True,False)
|
|||
|
if cont.sensors['MO'].status == JUST_RELEASED:
|
|||
|
for objet in objets :
|
|||
|
scene.objects[objet+"-Nom"].setVisible(True,False)
|
|||
|
scene.objects[objet+"-Nom-Hl"].setVisible(False,False)
|
|||
|
|
|||
|
###############################################################################
|
|||
|
# Nomenclature
|
|||
|
###############################################################################
|
|||
|
|
|||
|
# Click sur la nomeclature
|
|||
|
def nom(cont):
|
|||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
hitObject = cont.sensors['MO'].hitObject
|
|||
|
if hitObject is not None:
|
|||
|
|
|||
|
# Afficher/cacher les repères de la pièce
|
|||
|
if "-Nom-Rep" in hitObject.name:
|
|||
|
obj=hitObject.name[:len(hitObject.name)-8]
|
|||
|
if scene.objects[obj]['rep']==False:
|
|||
|
scene.objects[obj]['rep']=True
|
|||
|
hitObject.color=couleur_rep_actif
|
|||
|
for rep in objets_rep_dict[obj]:
|
|||
|
scene.objects[rep].setVisible(True,False)
|
|||
|
else:
|
|||
|
scene.objects[obj]['rep']=False
|
|||
|
hitObject.color=couleur_rep_pasactif
|
|||
|
for rep in objets_rep_dict[obj]:
|
|||
|
scene.objects[rep].setVisible(False,False)
|
|||
|
|
|||
|
# Colorisation de la pièce par la nomenclature
|
|||
|
else:
|
|||
|
couleur_select = scene.objects['Couleurs']['couleur_select']
|
|||
|
if couleur_select != 0 and couleur_select != 8:
|
|||
|
hitObject.color = couleurs[couleur_select-1][1]
|
|||
|
scene.objects[hitObject.name+"-Hl"].color = couleurs[couleur_select-1][1]
|
|||
|
scene.objects[hitObject.name[0:len(hitObject.name)-4]].setVisible(True,False)
|
|||
|
scene.objects[hitObject.name[0:len(hitObject.name)-4]].color = couleurs[couleur_select-1][1]
|
|||
|
|
|||
|
if couleur_select == 8: # Couleur transparente -> disparition / apparition
|
|||
|
obj=scene.objects[hitObject.name[0:len(hitObject.name)-4]]
|
|||
|
obj_GP=scene.objects[hitObject.name[0:len(hitObject.name)-4]+"_Lines.GP"]
|
|||
|
if obj.visible==True:
|
|||
|
obj.setVisible(False,False)
|
|||
|
obj_GP.setVisible(False,False)
|
|||
|
else:
|
|||
|
obj.setVisible(True,False)
|
|||
|
obj_GP.setVisible(True,False)
|
|||
|
# if couleur_select == 8: # Couleur transparente -> disparition
|
|||
|
# scene.objects[hitObject.name[0:len(hitObject.name)-4]].setVisible(False,False)
|
|||
|
|
|||
|
# Le highlight de la nomenclature
|
|||
|
def nom_hl(cont):
|
|||
|
if cont.sensors['MO'].status == JUST_ACTIVATED and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
hitObject = cont.sensors['MO'].hitObject
|
|||
|
if hitObject is not None:
|
|||
|
hitObject.setVisible(False,False)
|
|||
|
scene.objects[hitObject.name+'-Hl'].setVisible(True,False)
|
|||
|
# obj.color = couleur_cmd_hl
|
|||
|
if cont.sensors['MO'].status == JUST_RELEASED and scene.objects['Mecanisme']['manip_mode']==0:
|
|||
|
for objet in objets :
|
|||
|
scene.objects[objet+"-Nom"].setVisible(True,False)
|
|||
|
scene.objects[objet+"-Nom-Hl"].setVisible(False,False)
|
|||
|
for objet in objets_rep :
|
|||
|
scene.objects[objet+'-Nom-Rep'].setVisible(True,False)
|
|||
|
scene.objects[objet+'-Nom-Rep-Hl'].setVisible(False,False)
|
|||
|
|
|||
|
###############################################################################
|
|||
|
# Aide
|
|||
|
###############################################################################
|
|||
|
|
|||
|
# Ouvrir la page d'aide
|
|||
|
def aide(cont):
|
|||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
|
|||
|
# scene.replace('Scene-Aide') # Bug Eevee -> même scene mais camera différente
|
|||
|
scene.active_camera=scene.objects['Aide-Camera']
|
|||
|
scene.objects['Apropos-Lien_projet'].color= couleur_lien
|
|||
|
scene.objects['Apropos-Lien_blender'].color= couleur_lien
|
|||
|
scene.objects['Apropos-Lien_upbge'].color= couleur_lien
|
|||
|
|
|||
|
# Fermer la page d'aide
|
|||
|
def aide_fermer(cont):
|
|||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
|
|||
|
# bge.logic.addScene('Scene') # Bug Eevee -> même scene mais camera différente
|
|||
|
scene.active_camera=scene.objects['Camera']
|
|||
|
|
|||
|
# Aller sur les liens
|
|||
|
def aide_apropos(cont):
|
|||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
|
|||
|
obj = cont.owner
|
|||
|
if obj.name == "Apropos-Lien_projet" :
|
|||
|
webbrowser.open('https://gitlab.com/blender-edutech')
|
|||
|
if obj.name == "Apropos-Lien_blender" :
|
|||
|
webbrowser.open('https://blender.org')
|
|||
|
if obj.name == "Apropos-Lien_upbge" :
|
|||
|
webbrowser.open('https://upbge.org')
|
|||
|
|
|||
|
# Le highlight des liens
|
|||
|
def aide_apropos_hl(cont):
|
|||
|
if cont.sensors['MO'].status == JUST_ACTIVATED :
|
|||
|
obj = cont.owner
|
|||
|
obj.color = couleur_lien_hl
|
|||
|
if cont.sensors['MO'].status == JUST_RELEASED :
|
|||
|
obj = cont.owner
|
|||
|
obj.color = couleur_lien
|