Forum: Poser Python Scripting


Subject: Setting xTran, yTran, zTran

OberonRex opened this issue on Oct 15, 2023 ยท 8 posts


Bastep posted Mon, 16 October 2023 at 2:36 AM

Hello and good morning
I dug out an old script for conversion. It reads the Poser.ini file and searches for the set unit. It provides two methods for the conversion. The whole thing is quite easy to handle.

Have a nice day

#----------------------------------------------------------------------------------
# ConvertUnitsClass.py
#
#
# Date:                 07.11.2020 08:13:22
# Last Change:          25.12.2020 00:13:26
# Version:              1.0
# Author:               Bastep
# Contact:              via Renderocity
#
# The code is free
#----------------------------------------------------------------------------------

import poser
from pathlib import Path

#-- Mode Codes for cvFromPU
PU_FROM_PU    = 0
INCH_FROM_PU  = 1
FEET_FROM_PU  = 2
MM_FROM_PU    = 3
CM_FROM_PU    = 4
M_FROM_PU     = 5

#-- Mode Codes for cvToPU
PU_TO_PU    = 0
INCH_TO_PU  = 1
FEET_TO_PU  = 2
MM_TO_PU    = 3
CM_TO_PU    = 4
M_TO_PU     = 5

#----------------------------------------------------------------------------------
# Definition UnitsClass
#----------------------------------------------------------------------------------

class ConvertUnitsClass:
    def __init__(self):
        
        self.__UnitData   = (
                                ('pu', 1.0), ('inch', 103.2000016), ('feet', 8.600000131252514),
                                ('mm', 2621.2802657335), ('cm', 262.1280265733), ('m', 2.6212802657335)
                            )

        self.__Unit         = 0
        self.__UnitName     = ''
        self.__UnitValue    = 0.0
        self.SetUnit()
        
    #----------------------------------------------------------------------------------
    # By default, the method gets its conversion factor from the Poser.ini file.
    # But if you use one of the above codes you can convert as you like.
    #----------------------------------------------------------------------------------
    def cvFromPU(self, value, mode=None):
        if mode:
            return (value * self.__UnitData[mode][1])
        else:
            return (value * self.__UnitData[self.__Unit][1])

    #----------------------------------------------------------------------------------
    # By default, the method gets its conversion factor from the Poser.ini file.
    # But if you use one of the above codes you can convert as you like.
    #----------------------------------------------------------------------------------
    def cvToPU(self, value, mode=None):
        if mode:
            return (value / self.__UnitData[mode][1])
        else:
            return (value / self.__UnitData[self.__Unit][1])

    #----------------------------------------------------------------------------------
    # Get
    #----------------------------------------------------------------------------------

    def GetUnitName(self):
        return self.__UnitData[self.__Unit][0]

    def GetUnitValue(self):
        return self.__UnitData

    #----------------------------------------------------------------------------------
    # Initalizes the whole
    #----------------------------------------------------------------------------------
    def SetUnit(self):
        self.__Unit = self.UnitFromPoser()
        if self.__Unit < 0:
            if self.__Unit == -1:
                print('Path not exist.')
            elif self.__Unit == -2:
                print('Error reading the file.')
            elif self.__Unit == -3:
                print('unit_scale_type not found.')
            self.__Unit = 4
        self.__UnitName     = self.__UnitData[self.__Unit][0]
        self.__UnitValue    = self.__UnitData[self.__Unit][1]

    #----------------------------------------------------------------------------------
    # Loads the Poser.ini file and gets the data for
    # the units
    #----------------------------------------------------------------------------------
    def UnitFromPoser(self):
        poserini=Path(poser.PrefsLocation(),'poser.ini')
        if poserini.exists():
            try:
                inifile=open(poserini,'r').readlines()
                for line in inifile:
                    if 'unit_scale_type' in line.lower():
                        line = line.split(' ')
                        return int(line[1])
                return -3
            except:
                return -2
        return -1

_______________________________________________________

And here is the program for testing:

import poser

import ConvertUnitsClass as cuc
import importlib
importlib.reload(cuc)

converter = cuc.ConvertUnitsClass()
scene = poser.Scene()
#-- Take the internalname of any actor in the scene.
actor = scene.ActorByInternalName('Morphable Column')
print(actor.InternalName())
pX = actor.ParameterByCode(poser.kParmCodeXTRAN)
pY = actor.ParameterByCode(poser.kParmCodeYTRAN)
pZ = actor.ParameterByCode(poser.kParmCodeZTRAN)
#-- default
print ('Default from Poser.ini')
print (converter.cvFromPU(pX.Value()))
print (converter.cvFromPU(pY.Value()))
print (converter.cvFromPU(pZ.Value()))
#-- with codes
print ('With Codes')
print (converter.cvFromPU(pX.Value(), cuc.FEET_FROM_PU))
print (converter.cvFromPU(pY.Value(), cuc.FEET_FROM_PU))
print (converter.cvFromPU(pZ.Value(), cuc.FEET_FROM_PU))