[QUESTION] Python function to model targets?

MakeHuman python API, python plugins, etc

Moderator: joepal

[QUESTION] Python function to model targets?

Postby kvaluruk » Mon Dec 11, 2017 7:34 am

Hey everyone! My first post here! And also, I'd like to apologize for being too much of a newbie.

Let me start by describing what I am planning to build.
I want to build a near-realistic human model for users automatically. I've done the framing of the body using macromodifiers since that can be directly done using setAge(), setWeight() etc. functions.

Now, the problem I am facing is modelling the face. For this, the approach I want to follow is taking a photograph from the user, getting facial landmark details and manipulating the data obtained to model the face proportionally (all of this happening with no intervention from a human). Since it is automatic, I can't use GUI tools, Blender or other software. So, how do I change target values?

I tried MakeTarget but it I am not able to understand how it changes the targets, else I'd have used its functions.
I even tried
Code: Select all
humanoid.getModifier('...').setValue(...)
humanoid.applyAllTargets()
but that doesn't seem to work as well. Anything I am missing?
Any help will be appreciated. Thanks!
Last edited by kvaluruk on Tue Dec 12, 2017 8:18 am, edited 1 time in total.
kvaluruk
 
Posts: 5
Joined: Mon Dec 11, 2017 6:52 am

Re: [QUESTION] Python function to model targets?

Postby joepal » Mon Dec 11, 2017 9:24 am

It is not entirely clear to me what you want to do here. Do you want to modify the targets in themselves? Or simply apply targets to the human?

Independently of the answer, maybe you can find something to help you along in MHAPI, which you can find here: https://github.com/makehumancommunity/c ... gins-mhapi.

If all you want to do is to apply targets, you can take a look in https://github.com/makehumancommunity/c ... difiers.py to see how it's done. My guess is you've simply missed "mh.redraw()".

If you want to modify actual targets, then this is going to be a longer discussion.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: [QUESTION] Python function to model targets?

Postby kvaluruk » Mon Dec 11, 2017 11:19 am

Thanks for a quick reply @joepal
Going in for more details on the project, it is a web application built on Django. Currently, I can take height, weight, age etc. of a user using a web form, creating a human object instance and saving it as a .obj. Later I can automatically render it on the web application using three.js. Now I want to take it one step forward and make the face a little more user like (using the image provided by the user)
I'd like to apologize again as I am more of a developer than a modeller.

joepal wrote:It is not entirely clear to me what you want to do here. Do you want to modify the targets in themselves? Or simply apply targets to the human?


Pardon me for being a little too much of a layman here. All I want to do is change modifier details like the target 'nose/nose-volume' using some function running on the server. I'd love if I get to stay as far as from modelling as possible.

joepal wrote:Independently of the answer, maybe you can find something to help you along in MHAPI, which you can find here: https://github.com/makehumancommunity/c ... gins-mhapi.

If all you want to do is to apply targets, you can take a look in https://github.com/makehumancommunity/c ... difiers.py to see how it's done. My guess is you've simply missed "mh.redraw()".

If you want to modify actual targets, then this is going to be a longer discussion.


Going through MHAPI, I guess that is what I was looking for! And it is pretty nicely documented as well. I'll play around with it and I am hoping it works. Sorry for not researching properly as the repository was right there in the open. As I am a beginner, if I get any doubts related to MHAPI, where should I contact?

Thanks and cheers! Keen on finishing the project and displaying it to Y'all folks :)
kvaluruk
 
Posts: 5
Joined: Mon Dec 11, 2017 6:52 am

Re: [QUESTION] Python function to model targets?

Postby Aranuvir » Mon Dec 11, 2017 12:08 pm

You're probably using a "release version". In this version some plugins are deactivated. The pluigins of interest for you start with 7 (especially: 7_data, 7_scripting, 7_shell and 7_example)

A simple copy and paste from 7_scripting.py:

Code: Select all
def applyTarget(self,targetName,power):
       log.message("SCRIPT: applyTarget(" + targetName + ", " + str(power) + ")")
       self.human.setDetail(mh.getSysDataPath("targets/" + targetName + ".target"), power)
       self.human.applyAllTargets()
       mh.redraw()


Though, I didn't test it, probably it should be the code you are looking for.
Aranuvir
 
Posts: 1314
Joined: Sun Oct 12, 2014 2:12 pm

Re: [QUESTION] Python function to model targets?

Postby joepal » Mon Dec 11, 2017 1:35 pm

You may also be interested in not digging into MH directly. You can acheive some of the same by using the socket plugin and communicating with MH from outside. This way you can ask MH to apply a target and then fetch mesh information directly, all via a local TCP socket.

See https://github.com/makehumancommunity/c ... ver_socket and https://github.com/makehumancommunity/c ... odifier.py

However, the socket interface is somewhat rudimentary as of yet.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: [QUESTION] Python function to model targets?

Postby joepal » Mon Dec 11, 2017 1:37 pm

As I am a beginner, if I get any doubts related to MHAPI, where should I contact?


And asking here is perfectly ok. If you have a bug report or a feature request, posting an issue on github is also fine.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: [QUESTION] Python function to model targets?

Postby kvaluruk » Tue Dec 12, 2017 7:55 am

Hey!
Thanks, @joepal and @Aranuvir for quick and helpful replies and bearing with me. This is my first time on any forum and it feels good to get such nice support.

So, I tried the scripting code, that seems to work but I have a few more problems/doubts.

First, I am not using the G.app instance, so mh.redraw() gives an error
Code: Select all
File "./lib/mh.py", line 92, in redraw
G.app.redraw()
AttributeError: 'NoneType' object has no attribute 'redraw'


Is using mh.redraw() necessary in such a case? I can't seem to find the file which contains the G.app.redraw() to debug the code.
If it may help, here is a small snippet of what I am using-

Code: Select all
import os
import makehuman
makehuman.set_sys_path()
import human
import files3d
import wavefront
import humanmodifier

humanoid = human.Human(files3d.loadMesh(human.getSysDataPath("3dobjs/base.obj")))

genmod = humanmodifier.MacroModifier('macrodetails','Gender')
agemod = humanmodifier.MacroModifier('macrodetails','Age')
#...other macromodifiers
heightmod.setHuman(humanoid)
agemod.setHuman(humanoid)
#...other mods

#  TODO scripting details and targets
humanoid.setDetail(os.path.abspath(os.curdir) + '/data/targets/nose/nose-volume-decr.target', 0.88)
humanoid.applyAllTargets()
#import mh
#mh.redraw()

#setting up path where .obj is to be stored
fpa = os.path.abspath(os.curdir) + '/room/static/room/assets/models/'

#function to update humanoid and then save it
def saviour(agee,gendere,weighte,musclee,heighte,ide):
   if (gendere == 1):
      humanoid.setHeight(mcmtofr(heighte, agee))
   else:
      humanoid.setHeight(fcmtofr(heighte, agee))
   humanoid.setAgeYears(agee)
   #...other macromodifiers

   wavefront.writeObjFile(fpa + str(ide) + '.obj' , humanoid.mesh)


Also, if I am using setDetail(), what is exactly happening? Does it take the .target files, use their information in some way to update the mesh?
kvaluruk
 
Posts: 5
Joined: Mon Dec 11, 2017 6:52 am

Re: [QUESTION] Python function to model targets?

Postby joepal » Tue Dec 12, 2017 8:53 am

"app" is a PyQt4 application. "app.redraw()" is defined in lib/qtui.py, line 292. The redraw call is only necessary if you want to see what's happening on screen.

Again refering to MHAPI, you could take a look in https://github.com/makehumancommunity/c ... difiers.py, lines 26-40, to see what you need to do to actually write the targets to the body. The way I remember it, the setDetail / setModifier etc only puts the modifier name on the target stack. The actual update of the mesh happens later in an update loop.

However, I was pretty certain "applyAllTargets()" was what did the actual update. Maybe we're missing something here.

Just speculating without debugging, but I think possibly you're working directly towards some internal objects. As MH was written as a GUI application and not a library, it's possible you're missing some of the magic that happens when you pull a slider. Unfortunately the separation between view and model is not as strict as one could have wished.

If at all feasible, it would probably help you if you could have an instance of the MH application running in order to maintain state, and do all operations against that running state instead of trying to recreate the same functionality bare-bones.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: [QUESTION] Python function to model targets?

Postby kvaluruk » Tue Dec 12, 2017 11:37 am

Hey! Thanks for the help. I figured that the in the build I am using, applyAllTargets() calls self.fullUpdate(). Both these functions in a union do exactly the same steps as in lines 24-40 of the MHAPI _modifiers.py file. I just had to unlink a few Progress() function calls since they were GUI functions and called G (which I was not using and gave an error). I can now (I guess) safely assume that my targets are modified since it makes all the calls required similar to the GUI application.

Now, I can focus on modelling the face according to parameters. Thankfully all that is nicely documented and presented. One advice I may need is there are hundreds of targets for facial features. I don't a 100% accurate model but just a vaguely similar one. So, any place I can get some data on which targets are the most important?

Thanks again @joepal and @Aranuvir, I was stuck on changing target values for some time and it is a huge relief to have it solved. :)
kvaluruk
 
Posts: 5
Joined: Mon Dec 11, 2017 6:52 am

Re: [QUESTION] Python function to model targets?

Postby joepal » Tue Dec 12, 2017 4:14 pm

kvaluruk wrote:So, any place I can get some data on which targets are the most important?


Define "important".

All targets are treated the same in the application. The only significant exceptions to this are the macro details (age/gender/ethnicity...).
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Next

Return to Python scripts

Who is online

Users browsing this forum: No registered users and 1 guest