Sync mesh for blender

Works in progress and technical screen shots.

Moderator: joepal

Re: Sync mesh for blender

Postby jcpalmer » Sun Jun 05, 2016 5:32 pm

joepal wrote:Anyway, another purpose I have with this is trying to interest more people in plugin development, so the next thing I'll do is spending some effort on writing verbose code comments.

I have not tried this yet, but I have forked it. I do not think I could do the HM side, but could probably write something on the Blender side to sync skeleton matrices.

Think refactoring the socket stuff out of SyncMHMeshOperator, and making a SyncMHOperator super class, would be a good idea, before adding more operators.

Btw, is the server side going to be in this community edition? I cannot see the vast majority of the people with enough skills to add to this would want to bother (including me), unless it was CO0 or Apache 2.
jcpalmer
 
Posts: 115
Joined: Tue Dec 16, 2014 4:14 pm

Re: Sync mesh for blender

Postby jcpalmer » Sun Jun 05, 2016 6:46 pm

For super class something like:
Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-

import bpy
import json
import pprint
import socket

pp = pprint.PrettyPrinter(indent=4)

class SyncOperator(bpy.types.Operator):
    def __init__(self, operator, requiredType):
        self.operator = operator
        self.requiredType = requiredType

    def executeJsonCall(self):     
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect(('127.0.0.1', 12345))
        client.send(self.operator)
     
        data = ""
   
        while True:
            buf = client.recv(1024)
            if len(buf) > 0:
                data += buf.strip().decode('utf-8')
            else:
                break
   
        return data;

    def callback(self,json_obj):
        raise 'needs to be overridden by subclass'

    def execute(self, context):
        print("Execute " + self.operator)
       
        obj = context.object

        json_raw = self.executeJsonCall()
        #with open("/tmp/json.json","w") as jfile:
        #    jfile.write(json_raw)
        json_obj = json.loads(json_raw)
        self.callback(json_obj)

        return {'FINISHED'}

    @classmethod
    def poll(cls, context):
        ob = context.object
        return ob and ob.type == self.requiredType


Then SyncMeshOperator, and any similar operator is simpler, like:
Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-

bl_info = {
    "name": "Synchronize MakeHuman mesh",
    "category": "Mesh",
}

import bpy
import pprint

pp = pprint.PrettyPrinter(indent=4)

class SyncMHMeshOperator(SyncOperator):
    """Synchronize the shape of a human with MH"""
    bl_idname = "mesh.sync_mh_mesh"
    bl_label = "Synchronize MH Mesh"
    bl_options = {'REGISTER', 'UNDO'}

    def __init__(self):
        super().__init__('getCoord', 'MESH')

    def callback(self,json_obj):

        print("Update mesh")

        obj = bpy.context.active_object
        data = json_obj["data"]
        l = len(data)
        print(l)
        l2 = len(obj.data.vertices)
        print(l2)

        i = 0

        while i < l and i < l2:
            obj.data.vertices[i].co[0] = data[i][0]
            obj.data.vertices[i].co[1] = -data[i][2]
            obj.data.vertices[i].co[2] = data[i][1]
            i = i + 1
jcpalmer
 
Posts: 115
Joined: Tue Dec 16, 2014 4:14 pm

Re: Sync mesh for blender

Postby joepal » Mon Jun 06, 2016 8:06 am

jcpalmer wrote:Think refactoring the socket stuff out of SyncMHMeshOperator, and making a SyncMHOperator super class, would be a good idea, before adding more operators.

Btw, is the server side going to be in this community edition? I cannot see the vast majority of the people with enough skills to add to this would want to bother (including me), unless it was CO0 or Apache 2.


Yes, I think making those things more general would be a good idea. I've been toying around with synchronizing more mesh stuff too, such as clothes. Other things, such as materials, would be read from disk rather than sent via socket (which is why I implemented the getpath operations on the MH side)

And yes, I intend for the server socket to end up in a community release. I'm still planning on what to include in that release (and the socket stuff is obviously premature as of yet), so it will probably not happen anytime before the mid of the summer. Also, I set an MIT license on the stuff I released on github for now to avoid adding any extra confusion for that code. When bundled, that'll obviously get infected with the AGPL license, but at least I'm not introducing extra complexities.

Anyway, concerning your changes: Maybe you could make a pull request? Or simply PM me your github username and I'll give you commit access.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: Sync mesh for blender

Postby jcpalmer » Mon Jun 06, 2016 4:02 pm

PR done & tested. Going to see if I can do armature sync. I then would not need Collada Facial Pose-Units, nor MHX2.
jcpalmer
 
Posts: 115
Joined: Tue Dec 16, 2014 4:14 pm

Re: Sync mesh for blender

Postby joepal » Tue Jun 07, 2016 5:26 am

Ok, merged it now.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: Sync mesh for blender

Postby jcpalmer » Tue Jun 07, 2016 9:25 pm

With more work, found poll() will need to be in each SyncOperator subclass, since it is a class method (no self). I have already changed it. Modified my file to sync poses, but not ready yet.
jcpalmer
 
Posts: 115
Joined: Tue Dec 16, 2014 4:14 pm

Re: Sync mesh for blender

Postby jcpalmer » Wed Jun 08, 2016 9:13 pm

cc: latest PR commit message
Added Pose sync (preliminary)

The pose sync works for both Collada & MHX2 exports. Right now you have to click the button multiple times. Weird, I know. I must be close though.

Any idea on getting to work with only one click? This was a pretty big first attempt with the MH code base. A little out of my depth.

Also,
- moved poll to each SyncOperator sub-class
- added trapping of errors on server
- added ability to return an error from server
jcpalmer
 
Posts: 115
Joined: Tue Dec 16, 2014 4:14 pm

Re: Sync mesh for blender

Postby joepal » Thu Jun 09, 2016 5:21 am

I merged the PR, but I think there might be some minor issues with rigs that were already posed in blender :-)

getpose.png
Character was posed before clicking the pose button


This is how character looked before clicking the pose button

getpose1.png
Character before getpose button
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: Sync mesh for blender

Postby jcpalmer » Thu Jun 09, 2016 2:19 pm

Just double checking, but are you saying more clicks would not un-ravel the arms incrementally, until the poses match? Try clicking multiple times until an additional click does not change anything more. Either the data needs to be either:
  • sorted in MH,
  • applied in a specific order in Blender, or
  • call some Blender API call which recomputes all bones.
I got your invitation, thanks. Will still PR, but then merge myself. I am set up this way already, and doing PR sends email notifications where direct merges do not on Github. As I am on East coast US time, just doing PR at my end of day & you doing a merge at 3AM was not a problem.

Will implement one of the above, but also think there needs to be a little more heft to the exchange to happen. Now that errors can be passed back, the client should also pass the version in every call. The ability to pass args might be useful, e.g. the position for detecting feet on ground. Think all calls should be:
'version-command-args'

Should probably also nest the "source" code under a directory called source. Reason is so that the client can be put in a zip file, "client.zip". I already have a xml build script that I used to great productivity advantage yesterday. Eclipse is my ide, so I just do a save / build. MH needs just needs to be restarted, & preference re-add with a uncheck - check sequence in Blender.
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<project name="deploy">
   <property name="mountPoint"   value= "/js-dev"/>
   <property name="project"      value= "${mountPoint}/community-plugins"/>
   
   <property name="client_src"   value= "${project}/mh_sync_mesh"/>
   <property name="temp-dir"     value= "${project}/temp"/>
   <property name="temp-sub-dir" value= "${temp-dir}/mh_sync_mesh"/>
   
   <property name="server_src"   value= "${project}/8_server_socket"/>
   <property name="server-dir"   value= "/usr/share/makehuman/plugins/8_server_socket"/>
    <!-- ===================== Client Section ============================ -->
    <target name="make-zip">
        <delete dir="${temp-sub-dir}" quiet="true"/>
        <delete dir="${temp-dir}" quiet="true"/>
       
        <mkdir  dir="${temp-dir}"/>
        <mkdir  dir="${temp-sub-dir}"/>
       
        <copy todir="${temp-sub-dir}">
           <fileset dir="${client_src}"/>
        </copy>
       
       <zip destfile="${project}/client.zip" basedir="${temp-dir}" level="9"></zip>
       
        <delete dir="${temp-sub-dir}" quiet="true"/>
        <delete dir="${temp-dir}" quiet="true"/>
    </target>
    <!-- ===================== Server Section ============================ -->
    <target name="to-server">
        <copy todir="${server-dir}">
           <fileset dir="${server_src}"/>
        </copy>
    </target>

</project>
jcpalmer
 
Posts: 115
Joined: Tue Dec 16, 2014 4:14 pm

Re: Sync mesh for blender

Postby joepal » Thu Jun 09, 2016 2:37 pm

I'll do something about allowing arguments, as well as consider subdirs.

Maybe it'd make sense to have separate subdirs for blender stuff and mh stuff though. And yet more subdir if plugins appear for, for example, 3dsmax.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

PreviousNext

Return to WIP (Work In Progress)

Who is online

Users browsing this forum: No registered users and 1 guest