示例#1
0
def construct_scene(lbase,
                    modelpath,
                    bgpath,
                    scale,
                    pos,
                    hpr,
                    bgscale,
                    bghp,
                    texture=None,
                    internal_canonical=False,
                    check_penetration=False,
                    light_spec=None,
                    use_envmap=False,
                    shader=None,
                    world_coords=False):
    """ Constructs the scene per the parameters. """

    # Default scene is lbase's rootnode
    if bgpath is not None:
        bgpath = mt.resolve_bg_path(bgpath)
    rootnode = lbase.rootnode
    # Modelpath points to the model .egg/.bam file

    if isstring(modelpath):
        modelpaths = [modelpath]
        scales = [scale]
        poses = [pos]
        hprs = [hpr]
        textures = [texture]
    else:
        modelpaths = modelpath
        scales = scale
        poses = pos
        hprs = hpr
        textures = texture

    texmodes = []
    for _i, _t in enumerate(textures):
        if isinstance(_t, tuple):
            texfile, texmode = _t
            texmodes.append(texmode)
            textures[_i] = texfile
        else:
            texmodes.append(TexGenAttrib.MWorldNormal)

    assert hasattr(modelpaths, '__iter__')
    assert hasattr(scales, '__iter__')
    assert hasattr(poses, '__iter__')
    assert hasattr(hprs, '__iter__')
    assert hasattr(textures, '__iter__')
    assert len(modelpaths) == len(scales) == len(hprs) == len(poses) == len(
        textures), (len(modelpaths), len(scales), len(hprs), len(poses),
                    len(textures))

    modelpaths = map(mt.resolve_model_path, modelpaths)
    modelpaths = map(cm.autogen_egg, modelpaths)
    textures = map(mt.resolve_texture_path, textures)
    objnodes = []
    for mpth, scale, hpr, pos, t, tm in zip(modelpaths, scales, hprs, poses,
                                            textures, texmodes):
        objnode = tools.read_file(lbase.loader.loadModel, mpth)
        if t is not None:
            #ts = TextureStage('ts')
            ts = TextureStage.get_default()
            ts.setMode(TextureStage.MReplace)
            tex = tools.read_file(lbase.loader.loadTexture, t)
            objnode.setTexGen(ts, tm)
            objnode.setTexture(tex, 6)

        robjnode = rootnode.attachNewNode('root_' + objnode.get_name())
        objnode.reparentTo(robjnode)
        if internal_canonical:
            vertices = np.array(objnode.getTightBounds())
            initial_scale_factor = max(abs(vertices[0] - vertices[1]))
            cscale = 1.2 / initial_scale_factor
            ppos = vertices.mean(0) * cscale
            objnode.setPos(-ppos[0], -ppos[1], -ppos[2])
            objnode.setScale(cscale, cscale, cscale)

        if world_coords:
            refnodeX = rootnode.attachNewNode('world_coords_x')
            refnodeY = rootnode.attachNewNode('world_coords_y')
            refnodeZ = rootnode.attachNewNode('world_coords_z')

            robjnode.wrtReparentTo(refnodeZ)
            refnodeZ.setH(refnodeX, hpr[2])
            robjnode.wrtReparentTo(refnodeY)
            refnodeY.setP(refnodeX, hpr[1])
            robjnode.wrtReparentTo(refnodeX)
            refnodeX.setR(refnodeX, hpr[0])
            robjnode.wrtReparentTo(rootnode)
        else:
            robjnode.setHpr(hpr[2], hpr[1], hpr[0])

        robjnode.setScale(scale[0], scale[0], scale[0])
        robjnode.setPos(pos[0], -pos[2], pos[1])
        robjnode.setTwoSided(1)

        objnodes.append(robjnode)

    if check_penetration:
        for (i, n1) in enumerate(objnodes):
            for j, n2 in enumerate(objnodes[i + 1:]):
                p = is_penetrating(n1, n2)
                if p:
                    for onode in objnodes:
                        onode.removeNode()
                    raise PenetrationError(i, j, n1, n2)

    # Environment map
    if bgpath and use_envmap:
        envtex = tools.read_file(lbase.loader.loadTexture, bgpath)
        # Map onto object
        ts = TextureStage('env')
        ts.setMode(TextureStage.MBlendColorScale)
        if not isinstance(use_envmap, list):
            use_envmap = [use_envmap] * len(objnodes)
        for _objnode, ue in zip(objnodes, use_envmap):
            if ue:
                if isstring(ue):
                    envtex0 = tools.read_file(lbase.loader.loadTexture,
                                              mt.resolve_texture_path(ue))
                else:
                    envtex0 = envtex
                _objnode.setTexGen(ts, TexGenAttrib.MEyeSphereMap)
                _objnode.setTexture(ts, envtex0)

    if bgpath and not np.isinf(bghp[0]):
        bgtex = tools.read_file(lbase.loader.loadTexture, bgpath)
        # Set as background
        #plane = cm.autogen_egg(mt.resolve_model_path('plane2d'))
        bgnode = lbase.loader.loadModel('smiley')
        # Get material list
        bgnode.clearMaterial()
        bgnode.clearTexture()
        bgnode.setAttrib(
            CullFaceAttrib.make(CullFaceAttrib.MCullCounterClockwise))
        bgnode.setTexture(bgtex, 2)
        c = 5.
        bgnode.setScale(c * bgscale[0], c * bgscale[0], c * bgscale[0])
        bgnode.setPos(0, 0, 0)  #0)
        bgnode.setHpr(bghp[0], bghp[1], 0.)
        # Detach point light
        plight1 = lbase.rootnode.find('**/plight1')
        if plight1:
            plight1.detachNode()
    elif bgpath:
        bgnode = NodePath("empty-bgnode")
        imageObject = OnscreenImage(image=bgpath,
                                    pos=(0, 0, 0),
                                    scale=tuple(bgscale),
                                    parent=bgnode,
                                    base=lbase)
    else:
        bgnode = NodePath("empty-bgnode")
    bgnode.reparentTo(rootnode)

    if shader is not None:
        vshaderpath, fshaderpath = shader
        rootnode.setShaderAuto()
        shader = Shader.load(Shader.SLGLSL, vshaderpath, fshaderpath)
        rootnode.setShader(shader)

    if light_spec is not None:
        lbase.rootnode.clearLight()
        lights = LightBase.make_lights(light_spec=light_spec,
                                       lname='scene_lights')
        lights.reparentTo(rootnode)
        for light in lights.getChildren():
            rootnode.setLight(light)

    return objnodes, bgnode
示例#2
0
文件: renderer.py 项目: jiaxx/genthor
def construct_scene(lbase, modelpath, bgpath, scale, pos, hpr, 
                    bgscale, bghp,
                    texture=None,
                    internal_canonical=False,
                    check_penetration=False, 
                    light_spec=None, 
                    use_envmap=False):
    """ Constructs the scene per the parameters. """

    # Default scene is lbase's rootnode
    if bgpath is not None:
        bgpath = mt.resolve_bg_path(bgpath)
    rootnode = lbase.rootnode
    # Modelpath points to the model .egg/.bam file

    if isinstance(modelpath, str):
        modelpaths = [modelpath]
        scales = [scale]
        poses = [pos]
        hprs = [hpr]
        textures = [texture]
    else:  
        modelpaths = modelpath
        scales = scale
        poses = pos
        hprs = hpr
        textures = texture

    texmodes = []
    for _i, _t in enumerate(textures):
        if isinstance(_t, tuple):
            texfile, texmode = _t
            texmodes.append(texmode)
            textures[_i] = texfile
        else:
            texmodes.append(TexGenAttrib.MWorldNormal)    

    assert hasattr(modelpaths, '__iter__')
    assert hasattr(scales, '__iter__')
    assert hasattr(poses, '__iter__')
    assert hasattr(hprs, '__iter__')
    assert hasattr(textures, '__iter__')
    assert len(modelpaths) == len(scales) == len(hprs) == len(poses) == len(textures), (len(modelpaths), len(scales), len(hprs), len(poses), len(textures))
        
    modelpaths = map(mt.resolve_model_path, modelpaths)
    modelpaths = map(cm.autogen_egg, modelpaths)
    textures = map(mt.resolve_texture_path, textures)
    objnodes = []
    for mpth, scale, hpr, pos, t, tm in zip(modelpaths, scales, hprs, poses, textures, texmodes):
        objnode = tools.read_file(lbase.loader.loadModel, mpth)
        if t is not None: 
            #ts = TextureStage('ts')
            ts = TextureStage.get_default()
            ts.setMode(TextureStage.MReplace) 
            tex = tools.read_file(lbase.loader.loadTexture, t) 
            objnode.setTexGen(ts, tm)
            objnode.setTexture(tex, 6)
        
        robjnode = rootnode.attachNewNode('root_' + objnode.get_name())
        objnode.reparentTo(robjnode)
        if internal_canonical:
            vertices = np.array(objnode.getTightBounds())
            initial_scale_factor = max(abs(vertices[0]-vertices[1]))
            cscale = 1.2/initial_scale_factor
            ppos = vertices.mean(0) * cscale
            objnode.setPos(-ppos[0], -ppos[1], -ppos[2])
            objnode.setScale(cscale, cscale, cscale)
            
        robjnode.setScale(scale[0], scale[0], scale[0])
        robjnode.setPos(pos[0], -pos[2], pos[1])
        robjnode.setHpr(hpr[2], hpr[1], hpr[0])
        robjnode.setTwoSided(1)

        objnodes.append(robjnode)

    if check_penetration:
        for (i, n1) in enumerate(objnodes):
            for j, n2 in enumerate(objnodes[i+1:]):
                p = is_penetrating(n1, n2)
                if p:
                    for onode in objnodes:
                        onode.removeNode()
                    raise PenetrationError(i, j, n1, n2)

    # Environment map
    if bgpath and use_envmap:
        envtex = tools.read_file(lbase.loader.loadTexture, bgpath)
        # Map onto object
        ts = TextureStage('env')
        ts.setMode(TextureStage.MBlendColorScale)
        if not isinstance(use_envmap, list):
            use_envmap = [use_envmap] * len(objnodes)
        for _objnode, ue in zip(objnodes, use_envmap):
            if ue:
                if isinstance(ue, str):
                    envtex0 = tools.read_file(lbase.loader.loadTexture, mt.resolve_texture_path(ue))
                else:
                    envtex0 = envtex
                _objnode.setTexGen(ts, TexGenAttrib.MEyeSphereMap)
                _objnode.setTexture(ts, envtex0)

    if bgpath and not np.isinf(bghp[0]):
        bgtex = tools.read_file(lbase.loader.loadTexture, bgpath)
        # Set as background
        #plane = cm.autogen_egg(mt.resolve_model_path('plane2d'))
        bgnode = lbase.loader.loadModel('smiley')
        # Get material list
        bgnode.clearMaterial()
        bgnode.clearTexture()
        bgnode.setAttrib(CullFaceAttrib.make(
            CullFaceAttrib.MCullCounterClockwise))
        bgnode.setTexture(bgtex, 2)
        c = 5.
        bgnode.setScale(c * bgscale[0], c * bgscale[0], c * bgscale[0])
        bgnode.setPos(0, 0, 0) #0)
        bgnode.setHpr(bghp[0], bghp[1], 0.) 
        # Detach point light
        plight1 = lbase.rootnode.find('**/plight1')
        if plight1:
            plight1.detachNode()
    elif bgpath:
        bgnode = NodePath("empty-bgnode")
        imageObject = OnscreenImage(image = bgpath, pos = (0, 0, 0), scale=tuple(bgscale), parent=bgnode, base=lbase)
    else:
        bgnode = NodePath("empty-bgnode")
    bgnode.reparentTo(rootnode)

    if light_spec is not None:
        lbase.rootnode.clearLight()
        lights = LightBase.make_lights(light_spec=light_spec, lname='scene_lights')
        lights.reparentTo(rootnode)
        for light in lights.getChildren():
            rootnode.setLight(light)

    return objnodes, bgnode
示例#3
0
        img = lbase.get_tex_image(tex)
    else:
        img = None

    return lbase, img


if __name__ == "__main__":
    # Command line usage
    args = sys.argv[1:]

    # Defaults
    # args = (modelpath, bgpath, scale, pos, hpr, bgscale, bghp)
    args = [
        mt.resolve_model_path("bloodhound"),  #MB26897"),
        mt.resolve_bg_path("DH214SN.jpg"),  #DH201SN.jpg"),
        # os.path.join(os.environ["HOME"],
        #              "Dropbox/genthor/rendering/backgrounds/Hires_pano.jpg"),
        (
            1., ),
        (0., 0.),
        (0., 0., 0.),
        (1., ),
        (0., 0.),
    ]
    #args[1] = ""

    lbase = run(args)

    raw_input("press ENTER to exit...")
示例#4
0
文件: renderer.py 项目: jiaxx/genthor
        img = lbase.get_tex_image(tex)
    else:
        img = None

    return lbase, img


if __name__ == "__main__":
    # Command line usage
    args = sys.argv[1:]

    # Defaults
    # args = (modelpath, bgpath, scale, pos, hpr, bgscale, bghp)
    args = [
        mt.resolve_model_path("bloodhound"), #MB26897"),
        mt.resolve_bg_path("DH214SN.jpg"), #DH201SN.jpg"),
        # os.path.join(os.environ["HOME"],
        #              "Dropbox/genthor/rendering/backgrounds/Hires_pano.jpg"),
        (1.,),
        (0., 0.),
        (0., 0., 0.),
        (1.,),
        (0., 0.),
        ]
    #args[1] = ""
    
    lbase = run(args)

    raw_input("press ENTER to exit...")