Forum: Poser Python Scripting


Subject: Is there any way to know if a figure is hair?

maur_2005 opened this issue on Oct 13, 2021 ยท 14 posts


structure posted Sat, 16 October 2021 at 12:27 AM Forum Coordinator

so after some experimentation - it seems that the ParentActor of a figure is always the "Body" actor.

This script finds most hairs, but also counts things like the woodgoddess hood as a hair ( it has a material called skullcap )
with some modification ( bearing in mind your personal needs ) it could be made to ignore hoods caps veils etc.

from pathlib import Path
import poser
import wx

scene = poser.Scene()
letsdebug = False

def isvalid( actor , bodypart=False ): # a modified function of a Snarlygribbly routine
    if not actor is None:
        n       =   actor.Name().lower()
        if n.endswith("con") or n.endswith("con 1"):
            return False
        data    =   [
                    'atmosphere','background','bodymorphs','centerofmass',
                    'faceroom','focusdistancecontrol','ground','grouping',
                    'universe'
                    ]
        for d in data:
            if d in n:
                return False
        if actor.IsZone() or actor.IsBase() or actor.IsDeformer():
            return False
        if actor.IsLight() or actor.IsCamera():
            return False
        if actor.IsProp() or ( actor.IsBodyPart() and bodypart == True ):
            return True
    return False

def get_geometry( item ):
    geomName = item.GeomFileName()
   
    if (geomName and not geomName == ""):
        geom = Path( geomName ).stem
        geompath = Path( geomName ).parent.resolve()
   
    return geom, geompath

def get_parent( item, parent = None ):

    if item.IsFigure():
        try:
            parent = item.ParentActor()
        except:
            parent = None
    if item.IsProp():
        try:
            parent = actor.Parent()
        except:
            parent = None
       
    if letsdebug:
        try:
            print( f"{item.Name()}\n\t{parent.Name()}\t{isithair( item )}" )
        except:
            pass

    if parent:
        if parent.Name() == "Head" or parent.Name() == "Body":
            return isithair( item, parent )

    else:
        return False

def isithair( item, parent = None ):
    # set up list of valid materials
    validmats = ( "bangs", "skullcap", "hair" )
    # validnames = ( "hair", "hr" )
    # set case independent names
    itemname = item.Name().casefold()
    parentname = parent.Name().casefold()
    # check itemname and materials before relying on parent
   
    if "hair" in itemname or itemname.startswith("hr"):
        return True
   
    if item.IsFigure():
        for material in item.Materials():
            if any( mat in material.Name() for mat in validmats ):
                return True

    if not parentname == "head" and not parentname == "Body":
        return False
    badnames = ("eye", "tongue", "teeth", "hat", "mask", "veil" )
    try:
        for actor in figure.Actors():
            targetname = actor.InternalName().casefold()
            return True if not any(badname in targetname\
                for badname in badnames) else False
    except:
        if letsdebug:
            print( item )
        targetname = item.InternalName().casefold()
        return True if not any(badname in targetname\
                for badname in badnames) else False

for figure in scene.Figures():
    hair = get_parent( figure )
    if hair:
        print(f"{get_geometry( figure )[0]}\n\t{get_geometry( figure )[1]}")
        if letsdebug:
            print ( figure.Name(), hair, figure.ParentActor().Name() )

for actor in scene.Actors():
    hair = get_parent( actor )
    if hair and isvalid( actor ):
        print(f"{get_geometry( actor )[0]}\n\t{get_geometry( actor )[1]}")
        if letsdebug:
            print ( actor.Name(), hair, actor.Parent().Name() )

Output :
bl_RebyHair X:\Poser12\Runtime\Geometries\DAZHair\Victoria4 FoxHair V4 X:\Poser12\Runtime\Geometries\Arki\FoxHair aprilyshZed X:\Poser12\Runtime\Geometries\AprilYSH\AprilHair LLF-BastHairV4 X:\Poser12\Runtime\Geometries\Littlefox\BastV4 Genie Hair2 X:\Poser12\Runtime\Geometries\! DD_KJ\Genie hair BandanaHair X:\Poser12\Runtime\Geometries\P3DArt\CarGirlV4 p3dFWHair X:\Poser12\Runtime\Geometries\Pretty3D\FWHair pl_hair_v4 X:\Poser12\Runtime\Geometries\Pretty3D\PrettyLongHair SW_MintaHR01 X:\Poser12\Runtime\Geometries\SWAM_Art\2015_MintaHR SW_Yunko01 X:\Poser12\Runtime\Geometries\SWAM_Art\2013_YunkoHR XtremePonytail X:\Poser12\Runtime\Geometries\DIGIpixel\PAXtreme

Locked Out