< Soya
# -*- indent-tabs-mode: t -*-
# Soya 3D tutorial
# Copyright (C) 2001-2004 Jean-Baptiste LAMY
#
# This program 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
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# character-animation-1: annimation d'un personnage avec Cal3D : ici, nous utilisons Balazar le sorcier !
# Ou comment écrire un viewer pour Cal3D en moins de 20lignes...
# Pour information, le personnage a été créé avec blender et exporté avec Cal3D
# , avec mon script Blender2Cal3D : http://oomadness.nekeme.net/en/blender2cal3d/index.html
# Regardez la documentation Cal3D pour plus d'informations sur le sujet et sur l'annimation de modèles et aussi
# pour les Body, ou vous aurez des informations poussées pour le chargement de materiaux
# (comment se passer des materiaux Cal3D avec ceux pour Soya).
# Import et initialisation de Soya.
import sys, os, os.path, soya, soya.widget as widget
soya.init()
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
# Création d'une scène.
scene = soya.World()
# Chargement d'un modèle Cal3D.
# Les modèles Cal3D sont sauvés dans le sous répertoir 'models' de soya.path ; chaque modèle cal3d
# est un sous repertoire et non un fichier (voir tutorial/data/models/balazar pour ceux ayant
# la version complète du tutorial). Il contient les squelettes, animations, mesh et materiaux, et
# un fichier ".cfg" ayant le même nom que le sous repertoire.
# Vous pouvez utiliser cal3d.parse_cfg_file(filename).
sorcerer_model = soya.AnimatedModel.get("balazar")
# Vous pouvez avoir une liste des meshs disponibles et les noms des animations ainsi :
print "Available meshes :", sorcerer_model.meshes .keys()
print "Available animations:", sorcerer_model.animations.keys()
# Création du body Cal3D, utilisant le sorcerer_model.
# Voir la docstrings du module soya.cal3d pour en apprendre plus sur les attachement et détachement des mesh
# possibles (voir Body.__init__, Body.attach et Body.detach).
# Il est aussi possible de l'utiliser, pour le démembrement, ou le changement d'une arme d'un personnage.
sorcerer = soya.Body(scene, sorcerer_model)
# Rotation de Balazar le sorcier
sorcerer.rotate_y(-120.0)
# Début de l'animation cyclique (qui se répète) appellée "marche".
sorcerer.animate_blend_cycle("marche")
# Pour arrêter l'animation :
#
#sorcerer.animate_clear_cycle("marche")
#
# Pour un mouvement non répetitif :
#
#sorcerer.animate_execute_action("marche")
#
# Pour plus d'infos sur les arguments optionnels, voir le docstring de cal3d.Body.animate*
# Ajout d'une camera, d'un label montrant les fps, d'une lampe et execution de la boucle principale.
camera = soya.Camera(scene)
camera.set_xyz(0.0, 1.5, 3.0)
soya.set_root_widget(widget.Group())
soya.root_widget.add(camera)
soya.root_widget.add(widget.FPSLabel())
soya.Light(scene).set_xyz(5.0, 5.0, 2.0)
# import time
# main_loop = soya.MainLoop(scene)
# for i in range(3):
# for j in range(10):
# time.sleep(0.1)
# main_loop.update()
# soya.render()
# print "."
# soya.screenshot().resize((320, 240)).save(os.path.join(os.path.dirname(sys.argv[0]), "results", os.path.basename(sys.argv[0])[:-3] + "_%s.jpeg" % i))
soya.MainLoop(scene).main_loop()
Cet article est issu de Wikibooks. Le texte est sous licence Creative Commons – Attribution – Partage à l’identique. Des conditions supplémentaires peuvent s’appliquer aux fichiers multimédias.