def new_game():
    """
    Starts a new game, with a default player on level 1 of the dungeon.
    Returns the player object.
    """
    # Must initialize the log before we do anything that might emit a message.
    log.init()

    fighter_component = Fighter(hp=100, defense=1, power=2, xp=0, death_function=player_death)
    player = Object(algebra.Location(0, 0), '@', 'player', libtcod.white, blocks=True, fighter=fighter_component)
    player.inventory = []
    player.level = 1
    player.game_state = 'playing'
    # True if there's a (hostile) fighter in FOV
    player.endangered = False

    obj = miscellany.dagger()
    player.inventory.append(obj)
    actions.equip(player, obj.equipment, False)
    obj.always_visible = True

    cartographer.make_map(player, 1)
    renderer.clear_console()
    renderer.update_camera(player)

    log.message('Welcome stranger! Prepare to perish in the Tombs of the Ancient Kings.', libtcod.red)
    log.message('Press ? or F1 for help.')

    return player
Beispiel #2
0
 def updateModelingParameters(self, dictOfParameterNameAndValue):
     log.message("SCRIPT: updateModelingParameters("+str(dictOfParameterNameAndValue)+")")
     for key, value in dictOfParameterNameAndValue.iteritems():
         modifier = self.human.getModifier(key)
         modifier.setValue(value)
     self.human.applyAllTargets()
     mh.redraw()
Beispiel #3
0
def getShader(path, cache=None):
    shader = None
    cache = cache or _shaderCache

    path1 = path + '_vertex_shader.txt'
    path2 = path + '_fragment_shader.txt'
    path3 = path + '_geometry_shader.txt'
    paths = [p for p in [path1, path2, path3] if os.path.isfile(p)]
    if not paths:
        cache[path] = False
        return False

    mtime = max(os.path.getmtime(p) for p in paths)

    if path in cache:
        shader = cache[path]
        if shader is False:
            return shader

        if mtime >= shader.modified:
            log.message('reloading %s', path)
            try:
                shader.initShader()
                shader.modified = mtime
            except RuntimeError, text:
                log.error("Error loading shader %s", path, exc_info=True)
                shader = False
def player_move_or_attack(player, direction, try_running):
    """
    Returns true if the player makes an attack or moves successfully;
    false if the attempt to move fails.
    """
    goal = player.pos + direction
    if (goal.x < 0 or goal.y < 0 or
            goal.x >= player.current_map.width or
            goal.y >= player.current_map.height):
        log.message(player.current_map.out_of_bounds(goal))
        return False

    # Is there an attackable object?
    target = None
    for object in player.current_map.objects:
        if object.fighter and object.pos == goal:
            target = object
            break

    if target is not None:
        actions.attack(player.fighter, target)
        return True
    else:
        if actions.move(player, direction):
            player.current_map.fov_needs_recompute = True
            if try_running:
                player.game_state = 'running'
                player.run_direction = direction
            return True

    return False
Beispiel #5
0
 def screenShot(self,fileName):
     log.message("SCRIPT: screenShot(" + fileName + ")")
     width = G.windowWidth;
     height = G.windowHeight;
     width = width - 3;
     height = height - 3;
     mh.grabScreen(1,1,width,height,fileName)
Beispiel #6
0
def PARonBeforeMove(pokemon, target = None, move = None):
	if (fakerandom.fakerandom() < 0.25):
		log.message(pokemon.template.species + " was paralyzed and couldn't move")
		# return (False, PAR)
		return False
	# return (True,)
	return True
Beispiel #7
0
def _do_open_door(actor, target):
    replacement = open_door(target.pos)
    actor.current_map.objects.insert(0, replacement)
    actor.current_map.objects.remove(target)
    actor.current_map.fov_needs_recompute = True
    actor.current_map.fov_elevation_changed = True
    log.message(actor.name.capitalize() + ' opens a door.')
 def export(self):
     log.message("Exporting")
     oldTransp = self.humanTransparent
     self.setHumanTransparency(False)
     animName = self.anim.name
     self.renderAnimation(animName)
     self.setHumanTransparency(oldTransp)
Beispiel #9
0
    def selectProxy(self, mhclofile):
        """
        Called when a new proxy has been selected.
        If this library selects only a single proxy, specifying None as
        mhclofile parameter will deselect the current proxy and set the selection
        to "none".
        If this library allows selecting multiple proxies, specifying None as
        mhclofile will have no effect.
        """
        if not mhclofile:
            if self.multiProxy:
                return
            else:
                self.deselectProxy(None)
                return

        log.message('Selecting proxy file "%s" from %s library.', mhclofile, self.proxyName)
        human = self.human

        pxy = None
        mhcloId = getpath.canonicalPath(mhclofile)
        if mhcloId in self._proxyCache:
            pxy = self._proxyCache[mhcloId]
            if pxy.mtime < os.path.getmtime(mhclofile):
                pxy = None

        if not pxy:
            pxy = proxy.loadProxy(human, mhclofile, type=self.proxyName.capitalize())
            self._proxyCache[mhcloId] = pxy

        if pxy.uuid in [p.uuid for p in self.getSelection() if p is not None]:
            log.debug("Proxy with UUID %s (%s) already loaded in %s library. Skipping.", pxy.uuid, pxy.file, self.proxyName)
            return

        if not self.multiProxy and self.isProxySelected():
            # Deselect previously selected proxy
            self.deselectProxy(None, suppressSignal = True)

        mesh,obj = pxy.loadMeshAndObject(human)
        mesh.setPickable(True)  # Allow mouse picking for proxies attached to human
        
        if not mesh:
            return

        gui3d.app.addObject(obj)

        self.filechooser.selectItem(mhclofile)


        self.adaptProxyToHuman(pxy, obj)
        obj.setSubdivided(human.isSubdivided()) # Copy subdivided state of human

        # Add to selection
        self.selectedProxies.append(pxy)

        self.filechooser.selectItem(mhclofile)

        self.proxySelected(pxy)

        self.signalChange()
def searchRecord(archivePath, field):
    """
    This function reads the file specified, searches for the specified field
    and returns a list of the records that contain that field 
    (ie a list of strings containing recordIDs).
    
    Parameters
    ----------
   
    archivePath:     
      *string*.  The file system path to the file containing the set of records.
      
    field:     
      *string*.  The field to search for.
    """

    time1 = time.time()
    f = open(archivePath)
    recordIDs = []
    for line in f:
        if line.find(field) != -1:
            recordIDs.append(line.split()[0])
    f.close()
    log.message('Found %s records in %s sec', len(recordIDs), time.time() - time1)
    return recordIDs
Beispiel #11
0
    def add(self, path, hash, silent=False):
        """
        Add the given path to the configuration.

        Args:
            path: the path to the file to add
            hash: the hash of the file
            silent: if True, this operation will not emit any messages
        """

        parts = self.__path_components(path)
        if not parts:
            return False

        changed = False
        dict = self.__config
        for i in range(len(parts)):
            if i is len(parts) - 1:
                if parts[i] not in dict or dict[parts[i]] != hash:
                    dict[parts[i]] = hash
                    changed = True
                
                if changed and not (self.__silent or silent):
                    log.message("adding: " + os.path.join(*parts))

                return changed

            if parts[i] not in dict:
                dict[parts[i]] = {}
                changed = True

            dict = dict[parts[i]]

        return False    # shouldn't get here
Beispiel #12
0
def OnExit():
    # Deactivate the pointers to vertex and normal array
    glDisableClientState(GL_VERTEX_ARRAY)
    glDisableClientState(GL_NORMAL_ARRAY)
    # glDisableClientState(GL_TEXTURE_COORD_ARRAY)
    glDisableClientState(GL_COLOR_ARRAY)
    log.message("Exit from event loop\n")
def loadRecord(archivePath, recordID):
    """
    This function reads the file specified, searches for the specified record ID
    and returns that record if found or *None* if not found. 
    The record is returned as a list of strings containing the record ID in the '0' 
    element and successive fields in the following elements.
    
    Parameters
    ----------
   
    archivePath:     
      *string*.  The file system path to the file containing the set of records.
      
    recordID:     
      *string*.  The ID of the record to load.
    """

    time1 = time.time()
    f = open(archivePath)
    record = None
    for line in f:
        if line.find(recordID) != -1:
            record = line.split()
            log.message('Found %s fields in %s sec', len(record), time.time() - time1)
            break
    f.close()
    return record
def which(program):
    """
    Checks whether a program exists, similar to http://en.wikipedia.org/wiki/Which_(Unix)
    """

    import os
    import sys
    
    if sys.platform == "win32" and not program.endswith(".exe"):
        program += ".exe"

    log.message("looking for %s", program)
        
    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            log.message("testing %s", exe_file)
            if is_exe(exe_file):
                return exe_file

    return None
Beispiel #15
0
def getShader(path, defines=[], cache=None):
    shader = None
    cache = cache or _shaderCache

    path1 = path + '_vertex_shader.txt'
    path2 = path + '_fragment_shader.txt'
    path3 = path + '_geometry_shader.txt'
    paths = [p for p in [path1, path2, path3] if os.path.isfile(p)]
    if not paths:
        log.error('No shader file found at path %s. Shader not loaded.', path)
        cache[path] = False
        return False

    mtime = max(os.path.getmtime(p) for p in paths)

    cacheName = path
    if defines:
        # It's important that the defines are sorted alfpabetically here
        cacheName = cacheName + "@" + "|".join(defines)

    if cacheName in cache:
        shader = cache[cacheName]
        if shader is False:
            return shader

        if mtime > shader.modified:
            log.message('reloading %s', cacheName)
            try:
                shader.initShader()
                shader.modified = mtime
            except RuntimeError, _:
                log.error("Error loading shader %s", cacheName, exc_info=True)
                shader = False
Beispiel #16
0
 def do(self, action):
     if action.do():
         self.undoStack.append(action)
         del self.redoStack[:]
         self.setFileModified(True)
         log.message('do %s', action.name)
         self.syncUndoRedo()
	def createAccount(self, sid, login, passwd, nick, email):
		log.message('Creating account', login, nick, email)
		# check limit
		if len(self.accounts.keys()) > 2:
			raise SecurityException("No room for another account")
		# check login, nick and uid
		for key in self.accounts.keys():
			account = self.accounts[key]
			if account.login == login:
				raise SecurityException('Login already used.')
			elif account.nick == nick:
				raise SecurityException('Nick already used.')
			elif account.email == email:
				raise SecurityException('E-mail already used.')
		# create account
		account = Account()
		# update
		account.login = login
		account.passwd = passwd
		account.nick = nick
		account.email = email
		account.confToken = md5.new('%s%s%d' % (login, email, time.time())).hexdigest()
		self.accounts[account.login] = account
		self.nick2login[account.nick] = account.login
		log.message('Account created, confirmation token:', account.confToken)
		# TODO send confirmation token to the email address
		return 1, None
Beispiel #18
0
def loadProxy(human, path, type="Clothes"):
    try:
        npzpath = os.path.splitext(path)[0] + '.mhpxy'
        try:
            if not os.path.isfile(npzpath):
                log.message('compiled proxy file missing: %s', npzpath)
                raise RuntimeError('compiled proxy file missing: %s', npzpath)
            if os.path.isfile(path) and os.path.getmtime(path) > os.path.getmtime(npzpath):
                log.message('compiled proxy file out of date: %s', npzpath)
                raise RuntimeError('compiled file out of date: %s', npzpath)
            proxy = loadBinaryProxy(npzpath, human, type)
        except Exception as e:
            showTrace = not isinstance(e, RuntimeError)
            log.warning("Problem loading binary proxy: %s", e, exc_info=showTrace)
            proxy = loadTextProxy(human, path, type)    # TODO perhaps proxy type should be stored in .mhclo file too
            if getpath.isSubPath(npzpath, getpath.getPath()):
                # Only write compiled binary proxies to user data path
                try:
                    saveBinaryProxy(proxy, npzpath)
                except StandardError:
                    log.notice('unable to save compiled proxy: %s', npzpath, exc_info=True)
            else:
                log.debug('Not writing compiled proxies to system paths (%s).', npzpath)
    except:
        log.error('Unable to load proxy file: %s', path, exc_info=True)
        return None

    return proxy
 def setImage(self, path):
     if isinstance(path, basestring):
         self.path = path
     else:
         self.path = None
     self.image.setImage(path)
     log.message('Image "%s" loaded in image viewer.', path)
    def onHumanChanging(self, event):

        human = event.human
        if event.change == 'reset':
            log.message('deleting proxy')
            human.setProxy(None)
            self.filechooser.deselectAll()
	def __init__(self, directory, dbName, cache = 128):
		log.message("Opening database", dbName)
		self.dbName = dbName
		self.nextID = 10000
		self.cacheSize = cache
		#
		try:
			os.makedirs(directory)
		except OSError:
			pass
		# open db
		self.storage = metakit.storage(os.path.join(directory, dbName), 1)
		view = self.storage.getas(self.dbSchema).blocked()
		map = self.storage.getas("map[_H:I,_R:I]")
		self.view = view.hash(map, 1)
		# cache
		self.cache = {}
		self.cacheLinks = {
			"__first__": [None, "__last__"],
			"__last__": ["__first__", None],
		}
		# stats
		self.statCount = 0
		self.statHit = 0
		self.statSwap = 0
		self.statMiss = 0
		self.statCleanSwap = 0
Beispiel #22
0
    def addDeformBones(self, generic, boneInfo):
        """
        Add deform bones with CopyTransform constraints to the original bone.
        Deform bones start with self.deformPrefix, as in Rigify.
        Don't add deform bones for split forearms, becaues that is done elsewhere.
        """

        amt = self.armature
        options = amt.options

        for bname in generic.keys():
            try:
                bone = boneInfo[bname]
            except KeyError:
                log.message("Warning: deform bone %s does not exist" % bname)
                continue
            if not bone.deform:
                log.message("Not deform: %s" % bname)
                continue

            base,ext = splitBoneName(bname)
            if not ((options.useSplitBones and
                     base in self.splitBones.keys())):
                headTail = self.headsTails[bname]
                bone.deform = False
                defParent = self.getDeformParent(bname, boneInfo)
                defName = self.deformPrefix+bname
                self.headsTails[defName] = headTail
                defBone = boneInfo[defName] = Bone(amt, defName)
                defBone.fromInfo((bone, defParent, F_DEF, L_DEF))
                self.addConstraint(defName, copyTransform(bone.name, bone.name))
Beispiel #23
0
	def __init__(self, directory, dbName, cache = 128):
		log.message("Opening database", dbName)
		self.dbName = dbName
		self.nextID = 10000
		self.cacheSize = cache
		#
		try:
			os.makedirs(directory)
		except OSError:
			pass
		# open db
		self.connection = sqlite3.connect(os.path.join(directory, "%s.sqlite" % dbName))
		# allow 8-bits strings to be handled correctly (default is unicode)
		self.connection.text_factory = str
		self.cursor = self.connection.cursor()
		self.cursor.execute("create table if not exists %s" % self.dbSchema)
		self.connection.commit()
		# cache
		self.cache = {}
		self.cacheLinks = {
			"__first__": [None, "__last__"],
			"__last__": ["__first__", None],
		}
		# stats
		self.statCount = 0
		self.statHit = 0
		self.statSwap = 0
		self.statMiss = 0
		self.statCleanSwap = 0
Beispiel #24
0
def writeAnimation(human, linebuffer, animTrack, config):
    import numpy as np
    progress = Progress(len(human.getSkeleton().getBones()))
    log.message("Exporting animation %s.", animTrack.name)
    linebuffer.append('        <animation name="%s" length="%s">' % (animTrack.name, animTrack.getPlaytime()))
    linebuffer.append('            <tracks>')
    for bIdx, bone in enumerate(human.getSkeleton().getBones()):
        # Note: OgreXMLConverter will optimize out unused (not moving) animation tracks
        linebuffer.append('                <track bone="%s">' % bone.name)
        linebuffer.append('                    <keyframes>')
        frameTime = 1.0/float(animTrack.frameRate)
        for frameIdx in xrange(animTrack.nFrames):
            poseMat = animTrack.getAtFramePos(frameIdx)[bIdx]
            translation = poseMat[:3,3]
            angle, axis, _ = transformations.rotation_from_matrix(poseMat)
            axis = np.asarray(axis * np.matrix(bone.getRestMatrix(offsetVect=config.offset)))[0]
            linebuffer.append('                        <keyframe time="%s">' % (float(frameIdx) * frameTime))
            linebuffer.append('                            <translate x="%s" y="%s" z="%s" />' % (translation[0], translation[1], translation[2]))
            # TODO account for scale
            linebuffer.append('                            <rotate angle="%s">' % angle)
            linebuffer.append('                                <axis x="%s" y="%s" z="%s" />' % (axis[0], axis[1], axis[2]))
            linebuffer.append('                            </rotate>')
            linebuffer.append('                        </keyframe>')
        linebuffer.append('                    </keyframes>')
        linebuffer.append('                </track>')
        progress.step()
    linebuffer.append('            </tracks>')
    linebuffer.append('        </animation>')
Beispiel #25
0
    def copyTextureToNewLocation(self, filepath):
        import os
        import shutil

        srcDir = os.path.abspath(os.path.expanduser(os.path.dirname(filepath)))
        filename = os.path.basename(filepath)

        newpath = os.path.abspath( os.path.join(self.texFolder, filename) )
        try:
            self._copiedFiles[filepath]
            done = True
        except:
            done = False
        if not done:
            try:
                shutil.copyfile(filepath, newpath)
            except:
                log.message("Unable to copy \"%s\" -> \"%s\"" % (filepath, newpath))
            self._copiedFiles[filepath] = True

        if not self.useRelPaths:
            return newpath
        else:
            relpath = os.path.relpath(newpath, self.outFolder)
            return str(os.path.normpath(relpath))
Beispiel #26
0
    def matrixLocalFromBone(self):        
    
        u = self.tail.sub(self.head)
        length = sqrt(u.dot(u))
        if length < 1e-3:
            log.message("Zero-length bone %s. Removed" % self.name)
            self.matrix_local.matrix[:3,3] = self.head.vector
            return
        u = u.div(length)

        yu = Bone.ey.dot(u)        
        if abs(yu) > 0.99999:
            axis = Bone.ey
            if yu > 0:
                angle = 0
            else:
                angle = pi
        else:        
            axis = Bone.ey.cross(u)
            length = sqrt(axis.dot(axis))
            axis = axis.div(length)
            angle = acos(yu)

        mat = tm.rotation_matrix(angle,axis)
        if self.roll:
            roll = tm.rotation_matrix(self.roll, Bone.ey)
            mat = dot(mat, roll)
        self.matrix_local = Matrix(mat)
        self.matrix_local.matrix[:3,3] = self.head.vector
Beispiel #27
0
	def __init__(self, gameID, config, clientMngr, msgMngr, database):
		log.debug("Runtime mode", ige.igeRuntimeMode)
		self.status = GS_INIT
		self.gameID = gameID
		self.clientMngr = clientMngr
		self.msgMngr = msgMngr
		self.cmdPool = {}
		self.db = database
		self.config = config
		self.scheduler = Scheduler(self)
		# register command objects
		# None here
		# metaserver
		if config.metaserver.url:
			log.message("METASERVER - using mestaserver", config.metaserver.url)
			self.metaserver = xmlrpclib.ServerProxy(config.metaserver.url)
			# login
			self.metaserverSID, challenge = self.metaserver.metasrvr.hello()
			rsp = md5.new(config.metaserver.password + challenge).hexdigest()
			self.metaserver.metasrvr.login(self.metaserverSID, config.metaserver.login, rsp)
			log.message("METASERVER - logged in, announcing game")
			# announce game
			self.metaserver.metasrvr.announceGame(
				self.metaserverSID,
				config.server.name, 
				config.server.host,
				int(config.server.port),
				config.server.realm, 
				int(config.server.rank), 
				config.server.info,
			)
			# notify ClientMngr about metaserver
			self.clientMngr.setMetaserver(self.metaserver, self.metaserverSID)
		else:
			self.metaserver = None
Beispiel #28
0
    def getRealBoneName(self, bname, boneInfo, raiseError=True):
        try:
            boneInfo[bname]
            return bname
        except KeyError:
            pass

        altname = bname
        if bname[0:4] == "DEF-":
            altname = bname[4:]
        else:
            altname = "DEF-"+bname

        log.message("Missing bone %s. Trying %s" % (bname, altname))
        try:
            boneInfo[altname]
            return altname
        except KeyError:
            pass

        if raiseError:
            log.message(str(boneInfo.keys()))
            raise NameError("Missing %s and %s" % (bname, altname))
        else:
            return bname
Beispiel #29
0
def inventory_menu(player, header):
    """
    Show a menu with each item of the inventory as an option.
    """
    if len(player.inventory) == 0:
        renderer.menu(header, 'Inventory is empty.', INVENTORY_WIDTH)
        return None

    options = []
    for obj in player.inventory:
        text = obj.name
        # Show additional information, in case it's equipped.
        if obj.item.count > 1:
            text = text + ' (x' + str(obj.item.count) + ')'
        if obj.equipment and obj.equipment.is_equipped:
            text = text + ' (' + obj.equipment.slot + ')'
        options.append(text)

    (char, index) = renderer.menu(header, options, INVENTORY_WIDTH)

    if index is not None:
        return player.inventory[index].item

    if char == ord('x'):
        (c2, i2) = renderer.menu('Press the key next to an item to examine it, or any other to cancel.\n', options, INVENTORY_WIDTH)
        if i2 is not None and player.inventory[i2].item.description is not None:
            # renderer.msgbox(player.inventory[i2].item.description)
            log.message(player.inventory[i2].item.description)

    return None
Beispiel #30
0
    def renameConstraints(self, constraints, boneInfo):
        for bname in constraints.keys():
            try:
                self.constraints[bname]
            except KeyError:
                log.message("No attr %s" % bname)
                continue

            for cns in self.constraints[bname]:
                try:
                    boneInfo[cns.subtar]
                    ignore = True
                except KeyError:
                    ignore = False
                if not ignore:
                    defTarget = self.deformPrefix + cns.subtar
                    try:
                        boneInfo[defTarget]
                        cns.subtar = defTarget
                    except:
                        log.message("Bone %s constraint %s has neither target %s nor %s" % (bname, cns, cns.subtar, defTarget))

            defname = self.deformPrefix + bname
            self.constraints[defname] = self.constraints[bname]
            del self.constraints[bname]
Beispiel #31
0
 def modifyRotationX(self, xmod):
     log.message("SCRIPT: modifyRotationX(" + str(xmod) + ")")
     rot = self.human.getRotation()
     rot[0] = rot[0] + xmod
     self.human.setRotation(rot)
     mh.redraw()
Beispiel #32
0
 def getZoom(self):
     log.message("SCRIPT: getZoom()")
     return self.cam.zoomFactor
Beispiel #33
0
 def modifyRotationY(self, ymod):
     log.message("SCRIPT: modifyRotationY(" + str(ymod) + ")")
     rot = self.human.getRotation()
     rot[2] = rot[2] + ymod
     self.human.setRotation(rot)
     mh.redraw()
Beispiel #34
0
 def getRotationY(self):
     log.message("SCRIPT: getRotationY()")
     rot = self.human.getRotation()
     return rot[2]
Beispiel #35
0
 def setRotationY(self, yrot):
     log.message("SCRIPT: setRotationY(" + str(yrot) + ")")
     rot = self.human.getRotation()
     rot[2] = yrot
     self.human.setRotation(rot)
     mh.redraw()
Beispiel #36
0
 def modifyRotationZ(self, zmod):
     log.message("SCRIPT: modifyRotationZ(" + str(zmod) + ")")
     rot = self.human.getRotation()
     rot[1] = rot[1] + zmod
     self.human.setRotation(rot)
     mh.redraw()
Beispiel #37
0
 def setRotationZ(self, zrot):
     log.message("SCRIPT: setRotationZ(" + str(zrot) + ")")
     rot = self.human.getRotation()
     rot[1] = zrot
     self.human.setRotation(rot)
     mh.redraw()
Beispiel #38
0
 def modifyPositionY(self, ymod):
     log.message("SCRIPT: modifyPositionY(" + str(ymod) + ")")
     pos = self.human.getPosition()
     pos[2] = pos[2] + ymod
     self.human.setPosition(pos)
     mh.redraw()
Beispiel #39
0
 def setPositionY(self, ypos):
     log.message("SCRIPT: setPositionY(" + str(ypos) + ")")
     pos = self.human.getPosition()
     pos[2] = ypos
     self.human.setPosition(pos)
     mh.redraw()
Beispiel #40
0
 def setRotationX(self, xrot):
     log.message("SCRIPT: setRotationX(" + str(xrot) + ")")
     rot = self.human.getRotation()
     rot[0] = xrot
     self.human.setRotation(rot)
     mh.redraw()
Beispiel #41
0
 def setPositionZ(self, zpos):
     log.message("SCRIPT: setPositionZ(" + str(zpos) + ")")
     pos = self.human.getPosition()
     pos[1] = zpos
     self.human.setPosition(pos)
     mh.redraw()
Beispiel #42
0
 def getPositionY(self):
     log.message("SCRIPT: getPositionY()")
     pos = self.human.getPosition()
     return pos[2]
Beispiel #43
0
 def setPositionX(self, xpos):
     log.message("SCRIPT: setPositionX(" + str(xpos) + ")")
     pos = self.human.getPosition()
     pos[0] = xpos
     self.human.setPosition(pos)
     mh.redraw()
Beispiel #44
0
 def modifyPositionZ(self, zmod):
     log.message("SCRIPT: modifyPositionZ(" + str(zmod) + ")")
     pos = self.human.getPosition()
     pos[1] = pos[1] + zmod
     self.human.setPosition(pos)
     mh.redraw()
Beispiel #45
0
 def updateModelingParameter(self, parameterName, value):
     log.message("SCRIPT: updateModelingParameter(parameterName, value)")
     modifier = self.human.getModifier(parameterName)
     modifier.setValue(value)
     self.human.applyAllTargets()
     mh.redraw()
Beispiel #46
0
 def modifyPositionX(self, xmod):
     log.message("SCRIPT: modifyPositionX(" + str(xmod) + ")")
     pos = self.human.getPosition()
     pos[0] = pos[0] + xmod
     self.human.setPosition(pos)
     mh.redraw()
Beispiel #47
0
 def setMaterial(self, mhmat_filename):
     log.message("SCRIPT: setMaterial(" + mhmat_filename + ")")
     # The file must be realtive to the App Resources directory,
     # e.g.: 'data/skins/young_caucasian_female/young_caucasian_female.mhmat'
     mat = material.fromFile(mhmat_filename)
     self.human.material = mat
Beispiel #48
0
 def setHeadSquareness(self, squareness):
     log.message("SCRIPT: setHeadSquareness(" + str(squareness) + ")")
     modifier = self.human.getModifier('head/head-square')
     modifier.setValue(squareness)
     self.human.applyAllTargets()
     mh.redraw()
Beispiel #49
0
 def setAge(self, age):
     log.message("SCRIPT: setAge(" + str(age) + ")")
     self.human.setAge(age)
     mh.redraw()
Beispiel #50
0
 def getModelingParameters(self):
     log.message("SCRIPT: getModelingParameters()")
     modifierNamesList = sorted(self.human.modifierNames)
     print("Modifier names:")
     print("\n".join(modifierNamesList))
Beispiel #51
0
 def saveObj(self, name, path=mh.getPath('exports')):
     log.message("SCRIPT: saveObj(" + name + "," + path + ")")
     filename = os.path.join(path, name + ".obj")
     import wavefront
     wavefront.writeObjFile(filename, self.human.mesh)
Beispiel #52
0
 def setWeight(self, weight):
     log.message("SCRIPT: setWeight(" + str(weight) + ")")
     self.human.setWeight(weight)
     mh.redraw()
Beispiel #53
0
 def saveModel(self, name, path=mh.getPath('models')):
     log.message("SCRIPT: saveModel(" + name + "," + path + ")")
     filename = os.path.join(path, name + ".mhm")
     self.human.save(filename, name)
Beispiel #54
0
 def printDetailStack(self):
     log.message("SCRIPT: printDetailStack()")
     for target in list(self.human.targetsDetailStack.keys()):
         print(str(self.human.targetsDetailStack[target]) + "\t" + target)
Beispiel #55
0
 def storeMesh(self, human):
     log.message("Storing mesh status")
     self.meshStored = human.meshData.coord.copy()
     self.meshStoredNormals = human.meshData.vnorm.copy()
Beispiel #56
0
 def loadModel(self, name, path=mh.getPath('models')):
     log.message("SCRIPT: loadModel(" + name + "," + path + ")")
     filename = os.path.join(path, name + ".mhm")
     self.human.load(filename, True)
Beispiel #57
0
def exportCollada(filepath, config):
    progress = Progress()

    time1 = time.clock()
    human = config.human
    config.setupTexFolder(filepath)
    filename = os.path.basename(filepath)
    name = config.goodName(os.path.splitext(filename)[0])

    progress(0, 0.5, "Preparing")

    objects = human.getObjects(excludeZeroFaceObjs=True)
    # Clone meshes with desired scale and hidden faces/vertices filtered out
    meshes = [obj.mesh.clone(config.scale, True) for obj in objects]

    # Scale skeleton
    skel = human.getSkeleton()
    if skel:
        skel = skel.scaled(config.scale)

    # TODO a shared method for properly naming meshes would be a good idea
    for mesh in meshes:
        if mesh.object.proxy:
            mesh.name = mesh.object.proxy.name
        mesh.name = os.path.splitext(mesh.name)[0]
        mesh.name = name + '-' + config.goodName(mesh.name)

    try:
        progress(0.5, 0.55, "Exporting %s", filepath)

        try:
            fp = codecs.open(filepath, 'w', encoding="utf-8")
            log.message("Writing Collada file %s" % filepath)
        except:
            fp = None
            log.error("Unable to open file for writing %s" % filepath)

        date = time.strftime(u"%a, %d %b %Y %H:%M:%S +0000".encode('utf-8'),
                             time.localtime()).decode('utf-8')
        # TODO revise to make this enum-like
        if config.yUpFaceZ or config.yUpFaceX:
            upvector = "Y_UP"
        else:
            upvector = "Z_UP"
        fp.write(
            '<?xml version="1.0" encoding="utf-8"?>\n' +
            '<COLLADA version="1.4.0" xmlns="http://www.collada.org/2005/11/COLLADASchema">\n'
            + '  <asset>\n' + '    <contributor>\n' +
            '      <author>www.makehuman.org</author>\n' +
            '    </contributor>\n' + '    <created>%s</created>\n' % date +
            '    <modified>%s</modified>\n' % date +
            '    <unit meter="%.4f" name="%s"/>\n' %
            (0.1 / config.scale, config.unit) +
            '    <up_axis>%s</up_axis>\n' % upvector + '  </asset>\n')

        progress(0.55, 0.6, "Exporting images")
        dae_materials.writeLibraryImages(fp, objects, config)

        progress(0.6, 0.65, "Exporting effects")
        dae_materials.writeLibraryEffects(fp, objects, config)

        progress(0.65, 0.7, "Exporting materials")
        dae_materials.writeLibraryMaterials(fp, objects, config)

        progress(0.7, 0.75, "Exporting controllers")
        dae_controller.writeLibraryControllers(fp, human, meshes, skel, config)

        progress(0.75, 0.8, "Exporting animations")
        dae_animation.writeLibraryAnimations(fp, human, config)

        progress(0.75, 0.9, "Exporting geometry")
        dae_geometry.writeLibraryGeometry(fp, meshes, config)

        progress(0.9, 0.99, "Exporting scene")
        dae_node.writeLibraryVisualScenes(fp, meshes, skel, config, name)

        fp.write('  <scene>\n' +
                 '    <instance_visual_scene url="#Scene"/>\n' +
                 '  </scene>\n' + '</COLLADA>\n')

        progress(1, None, "Export finished.")
        time2 = time.clock()
        log.message("Wrote Collada file in %g s: %s", time2 - time1, filepath)

    finally:
        if fp:
            fp.close()
Beispiel #58
0
def loadModifiers(filename, human):
    """
    Load modifiers from a modifier definition file.
    """
    log.debug("Loading modifiers from %s", filename)
    import json
    import os
    from collections import OrderedDict
    modifiers = []
    lookup = OrderedDict()
    data = json.load(open(filename, 'rb'), object_pairs_hook=OrderedDict)
    for modifierGroup in data:
        groupName = modifierGroup['group']
        for mDef in modifierGroup['modifiers']:
            # Construct modifier
            if "modifierType" in mDef:
                modifierClass = globals()[mDef["modifierType"]]
            elif 'macrovar' in mDef:
                modifierClass = MacroModifier
            else:
                modifierClass = UniversalModifier

            if 'macrovar' in mDef:
                modifier = modifierClass(groupName, mDef['macrovar'])
            else:
                modifier = modifierClass(groupName, mDef['target'],
                                         mDef.get('min', None),
                                         mDef.get('max', None),
                                         mDef.get('mid', None))

            if "defaultValue" in mDef:
                modifier._defaultValue = mDef["defaultValue"]

            modifiers.append(modifier)
            lookup[modifier.fullName] = modifier
    if human is not None:
        for modifier in modifiers:
            modifier.setHuman(human)
    log.message('Loaded %s modifiers from file %s', len(modifiers), filename)

    # Attempt to load modifier descriptions
    _tmp = os.path.splitext(filename)
    descFile = _tmp[0] + '_desc' + _tmp[1]
    hasDesc = OrderedDict([(key, False) for key in lookup.keys()])
    if os.path.isfile(descFile):
        data = json.load(open(descFile, 'rb'), object_pairs_hook=OrderedDict)
        dCount = 0
        for mName, mDesc in data.items():
            try:
                mod = lookup[mName]
                mod.description = mDesc
                dCount += 1
                hasDesc[mName] = True
            except:
                log.warning(
                    "Loaded description for %s but modifier does not exist!",
                    mName)
        log.message("Loaded %s modifier descriptions from file %s", dCount,
                    descFile)
    for mName, mHasDesc in hasDesc.items():
        if not mHasDesc:
            log.warning("No description defined for modifier %s!", mName)

    return modifiers
Beispiel #59
0
    byteList = []

    try:
        f = open(filename, 'rb')
    except IOError, (errno, strerror):
        log.error('I/O error(%s): %s', errno, strerror)
        return None
    fileReaded = f.read()
    for i in xrange(len(fileReaded)):
        byteList.append(ord(fileReaded[i]))
    f.close()

    # byteList[0] is the Identification Field.

    lengthOfID = byteList[0]
    log.message('Identification Field lentgh = %i', lengthOfID)

    # byteList[1] is the Color Map Type.

    if byteList[1] != 0:
        log.warning(
            'this module work only with true color image, no mapped type')
        return None

    # byteList[2] is the image type field.

    if byteList[2] != 2:
        log.message('Image type = %s', byteList[2])
        log.warning('This module work only with uncompressed true color image')
        return None
Beispiel #60
0
def Render(settings):
    progress = Progress.begin()

    if not mh.hasRenderToRenderbuffer():
        settings['dimensions'] = (G.windowWidth, G.windowHeight)

    if settings['lightmapSSS']:
        progress(0, 0.05, "Storing data")
        import material
        human = G.app.selectedHuman
        materialBackup = material.Material(human.material)

        progress(0.05, 0.1, "Projecting lightmaps")
        diffuse = imgop.Image(data=human.material.diffuseTexture)
        lmap = projection.mapSceneLighting(settings['scene'],
                                           border=human.material.sssRScale)
        progress(0.1, 0.4, "Applying medium scattering")
        lmapG = imgop.blurred(lmap, human.material.sssGScale, 13)
        progress(0.4, 0.7, "Applying high scattering")
        lmapR = imgop.blurred(lmap, human.material.sssRScale, 13)
        lmap = imgop.compose([lmapR, lmapG, lmap])
        if not diffuse.isEmpty:
            progress(0.7, 0.8, "Combining textures")
            lmap = imgop.resized(lmap,
                                 diffuse.width,
                                 diffuse.height,
                                 filter=image.FILTER_BILINEAR)
            progress(0.8, 0.9)
            lmap = imgop.multiply(lmap, diffuse)
        lmap.sourcePath = "Internal_Renderer_Lightmap_SSS_Texture"

        progress(0.9, 0.95, "Setting up renderer")
        human.material.diffuseTexture = lmap
        human.configureShading(diffuse=True)
        human.shadeless = True
        progress(0.95, 0.98, None)
    else:
        progress(0, 0.99, None)

    if not mh.hasRenderToRenderbuffer():
        # Limited fallback mode, read from screen buffer
        log.message("Fallback render: grab screen")
        img = mh.grabScreen(0, 0, G.windowWidth, G.windowHeight)
        alphaImg = None
    else:
        # Render to framebuffer object
        renderprog = Progress()
        renderprog(0, 0.99 - 0.59 * settings['AA'], "Rendering")
        width, height = settings['dimensions']
        log.message("Rendering at %sx%s", width, height)
        if settings['AA']:
            width = width * 2
            height = height * 2
        img = mh.renderToBuffer(width, height)
        alphaImg = mh.renderAlphaMask(width, height)
        img = imgop.addAlpha(img, imgop.getChannel(alphaImg, 0))

        if settings['AA']:
            renderprog(0.4, 0.99, "AntiAliasing")
            # Resize to 50% using bi-linear filtering
            img = img.resized(width / 2,
                              height / 2,
                              filter=image.FILTER_BILINEAR)
            # TODO still haven't figured out where components get swapped, but this hack appears to be necessary
            img.data[:, :, :] = img.data[:, :, (2, 1, 0, 3)]
        renderprog.finish()

    if settings['lightmapSSS']:
        progress(0.98, 0.99, "Restoring data")
        human.material = materialBackup

    progress(1, None, 'Rendering complete')

    gui3d.app.getCategory('Rendering').getTaskByName('Viewer').setImage(img)
    mh.changeTask('Rendering', 'Viewer')
    gui3d.app.statusPersist('Rendering complete')