Newbie problems getting started with Python API

MakeHuman python API, python plugins, etc

Moderator: joepal

Newbie problems getting started with Python API

Postby simon_brooke » Fri May 03, 2024 12:18 pm

First, yes, I've looked at the FAQ, but did not find anything relevant.

Second, I'm an experienced software engineer with 40 years experience of Lisp-like languages, but I don't know Python; the answer to this question is probably exceedingly obvious, but I can't see it.

Background: I'm working on a game which will have hundreds of thousands of non-player characters. That means, obviously, that storing all the models will be problematic and that I would rather generate them programmatically; but also, I want characters who are represented as being related to one another to look systematically similar. Consequently I'm experimenting with a system of simulated genetics such that I'm using a bit vector to represent the 'genome' of a character, and bitwise operations on the 'genome' of a 'mother' and a 'father' character to produce genomes for child characters. I then want to use information from the bit vector to manipulate a character model. I'm hoping to be able to use MakeHuman (or more precisely MHAPI to do this part, at least for proof-of-concept.

What I'm doing: First, I'm working in Clojure, which probably makes things more complicated! But second, I'm failing at the 'from core import G' step.

The code I'm sending to Python is

Code: Select all
import sys
sys.path.append('/home/simon/bin/mh/makehuman')
from lib.core import G
G.app.mhapi.internals.getHuman()


At which point it fails with

Code: Select all
Execution error at libpython-clj2.python.ffi/check-error-throw (ffi.clj:707).
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'mhapi'


This issue relates. So the problem is that 'G' will only be valid in the same Python virtual machine as a current instance of the MakeHuman app is running.

So I hypothesised that if I started the MakeHuman app in my own Python session, I would be able to access 'G', but this too fails in the same way.

Code: Select all
(ns cc.journeyman.simulated-genetics.makehuman-bridge
  (:require [libpython-clj2.require :refer [require-python]]
            [libpython-clj2.python
             :refer [as-python as-jvm
                     ->python ->jvm
                     get-attr call-attr call-attr-kw
                     get-item initialize!
                     run-simple-string
                     add-module module-dict
                     import-module
                     py. py.. py.-
                     python-type
                     ;; dir
                     ]
             :as py]
            [taoensso.telemere :refer [error! trace!]]))

(defn initialise-makehuman!
  "Initialise the local instance of MakeHuman. `mh-path` should be a valid
   path to the directory in which MakeHuman is installed, i.e. the directory
   which contains `makehuman.py`."
  [^String mh-path]
  (initialize!)
  (map #(trace! (run-simple-string %))
       ["import sys"
        (format "sys.path.append('%s')" mh-path)
        (format "exec(open('%s/makehuman.py').read())" mh-path)
        "from lib.core import G"
        "G.app.mhapi.internals.getHuman()"
        ]))


As I say, this is still failing with the 'AttributeError: 'NoneType' object has no attribute 'mhapi'' error.

Am I simply barking up the wrong tree, or is there a way to make this work?
simon_brooke
 
Posts: 1
Joined: Fri May 03, 2024 11:08 am
Location: Scotland

Re: Newbie problems getting started with Python API

Postby joepal » Mon May 06, 2024 3:31 pm

You're in a world of hurt if you try to use the makehuman codebase without a running makehuman instance. There have been attempts at creating a headless instance of makehuman, but they've ultimately failed because the core logic is thoroughly mixed with the UI logic, for historical reasons.

The two ways you'll be able to use MHAPI is either in the script window inside MakeHuman, or via the tcp socket connection. The latter option have very limited functionality exposed.

For your use case, I'd suggest replicating the logic in your own standalone python scripts. Mostly of the logic behind generating a body shape isn't that complicated in itself. But for such a project, I'd suggest you rather start from the files in MPFB, in the blender4 branch at https://github.com/makehumancommunity/m ... 4/src/mpfb (look in the entities and services folders). You'd still have to extract the logic, but at least the code base is much cleaner.

Another option is to run MPFB through a headless blender. This is perfectly possible, as can be seen by the unit tests in the MPFB code folder. But then you'd have to deal with the complexity of blender too.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4622
Joined: Wed Jun 04, 2008 11:20 am


Return to Python scripts

Who is online

Users browsing this forum: No registered users and 1 guest