def execute(self, context):
        try:
            mngr = ZS.ZP_armature_manager(context)
           
            if not self.check_rotations(context, mngr.target, mngr.source):
                return {'CANCELLED'}


            # TODO: subrotutine for orphans, and check verification algorithm!q
            if mngr.target.data.orphans != 0:
                ZPu.mode_set(mngr.target, mngr.context, "OBJECT")
                print("orphans")
                self.report({"ERROR"}, "orphans!")
                return {'CANCELLED'}

            simplfier = ZS.ZP_simplifier(mngr)
            simplfier.run()

            self.prog.current = 0
            animator = ZS.ZP_animation_transfer(mngr)
            animator.run(self.prog)
            
        except Exception as e:
            print("ERROR: ", e ,"\n", print_exc())
            self.report({"ERROR"}, str(e))
            return {'CANCELLED'}

        self.report({"INFO"}, self._success)

        return {"FINISHED"}
예제 #2
0
    def collect_information(self):
        ##########################
        # Basebones, and initial displacements
        self.target_basebone = ZPu.get_basebone(self.target.data)
        self.target_init_loc = ZPu.get_prop_values_at(self.target, "location",
                                                      0)
        self.target_init_loc = Vector(self.target_init_loc)

        self.source_basebone = ZPu.get_basebone(self.source.data)
        self.source_bbone_init_loc = \
         ZPu.get_prop_values_at(self.source.pose.bones[self.source_basebone], "location", 0)
        self.source_bbone_init_loc = Vector(self.source_bbone_init_loc)

        ##########################
        # PREV_STATE (bones stuff)
        if self.target.mode != "EDIT":
            ZPu.mode_set(self.target, self.context)

        self.prev_state = {}
        for b in self.target.data.bones:
            self.prev_state[b.name] = {
                "head": b.head.copy(),
                "tail": b.tail.copy(),
                "magnitude": b.vector.magnitude
            }

        for b in self.target.data.edit_bones:
            others = []
            for zp in b.zp_bone:
                if zp.name in self.source.pose.bones.keys():
                    others.append(self.source.pose.bones[zp.name])
            others.sort(key=ZPu.genealogy)

            assert ZPu.verify_chain(others)
            self.prev_state[b.name]["zp_bone"] = others
예제 #3
0
    def run(self):
        basebone = self.mngr.target.data.edit_bones[self.mngr.target_basebone]
        self.mngr.source.data.pose_position = "REST"
        self.mngr.source.data.update_tag()
        self.mngr.context.scene.update()

        ZPu.walk_bones(basebone, self._simplify)

        ZPu.mode_set(self.mngr.target, self.mngr.context, "OBJECT")
예제 #4
0
 def copy_source_edit_bones(self):
     ZPu.mode_set(self.source, self.context)
     twopi = 2 * pi
     self.source_edit_bones = {}
     for k in self.source.data.bones.keys():
         editbone = self.source.data.edit_bones[k]
         editbone.roll = editbone.roll % twopi  #this way there are no negative rotations
         self.source_edit_bones[k] = (editbone.name, editbone.roll,
                                      editbone.vector.copy())
     ZPu.mode_set(self.target, self.context)
    def check_rotations(self, context, target, source):
        s_rot = ZPu.get_prop_values_at(source, "matrix_world", 0)
        t_rot = ZPu.get_prop_values_at(target, "matrix_world", 0)

        q = Quaternion((1,0,0,0))
        rots = s_rot.to_quaternion().dot(q) + t_rot.to_quaternion().dot(q) 

        if rots < (2- 1e-8):
            ZPu.mode_set(target, context, "OBJECT")
            self.report({"ERROR"}, self._rot_error)
            return False
        
        return True
예제 #6
0
    def run(self, prog):
        self.mngr.source.data.pose_position = "POSE"
        ZPu.mode_set(self.mngr.target, self.mngr.context, "POSE")
        self.mngr.source.data.update_tag()
        self.mngr.context.scene.update()

        basebone = self.mngr.target.pose.bones[self.mngr.target_basebone]

        if self.mngr.source.animation_data.action:
            self._walk_animation(basebone, prog)
        else:
            self._copy_pose_all(basebone)

        self.mngr.context.scene.frame_set(self.mngr.frame_initial)
        ZPu.mode_set(self.mngr.target, self.mngr.context, "OBJECT")
예제 #7
0
    def __init__(self, context):
        self.context = context
        self.source = context.object.data.zp_source
        self.target = context.object
        self.target.animation_data_clear()
        self.target.animation_data_create()
        self.range = {
            "min": context.scene.frame_start,
            "max": context.scene.frame_end
        }
        self.frame_initial = context.scene.frame_current

        ZPu.mode_set(self.target, context)

        self.collect_information()

        self.copy_source_edit_bones()
def add_fake_bone(context=bpy.context,
                  filename='custom_bone.json',
                  location=None,
                  color=(1, .16, 0),
                  layers=(False, ) * 19 + (True, ),
                  name=prefix,
                  mat=None):

    # print(name, color)

    prev_ob = context.object
    ZPu.mode_set(prev_ob, context, mode="OBJECT")

    #TODO: change to the script location
    filename = path.join(path.dirname(bpy.data.filepath), filename)

    #Create or get mesh
    if "%smesh" % prefix in D.meshes.keys():
        mesh = D.meshes["%smesh" % prefix]

    else:
        with open(filename, "r") as f:
            json_readed = f.read()

        decoded_json = json.loads(json_readed)
        verts_loc = decoded_json["vertices"]
        faces = decoded_json["faces"]

        mesh = bpy.data.meshes.new("%s_mesh" % prefix)
        bm = bmesh.new()

        for v_co in verts_loc:
            bm.verts.new(v_co)

        bm.verts.ensure_lookup_table()

        for f_idx in faces:
            bm.faces.new([bm.verts[i] for i in f_idx])

        bm.to_mesh(mesh)
        mesh.update()

    #Create Object
    from bpy_extras import object_utils
    ob = object_utils.object_data_add(bpy.context, mesh, name=prefix + name)
    ob = ob.object
    ob.layers = layers

    if location:
        ob.location = location

    if not mat:
        if type(color) not in [tuple, list, Color]:
            mat_name = color + mat_sufix
            color = hex_to_rgb(color)
        else:
            mat_name = "lala" + tuple(color) + mat_sufix

        if mat_name in bpy.data.materials:
            mat = bpy.data.materials[mat_name]
        else:
            mat = bpy.data.materials.new(name=mat_name)
            mat.diffuse_color = color

    if len(ob.material_slots) < 1:
        override = context.copy()
        override["object"] = ob
        bpy.ops.object.material_slot_add(override)

    ob.material_slots[0].link = "OBJECT"
    ob.material_slots[0].material = mat

    if remove_fake_from_scene:
        ob.use_fake_user = True
        context.scene.objects.unlink(ob)

    ZPu.mode_set(prev_ob, context, mode="EDIT")
    return ob
    def execute(self, context):

        # context.window_manager.progress_begin(0, 1)
        # context.window_manager.progress_update(0.5)
        ZPu.clean_empties(["X", "S", "Cube", "Empty"])
        source = self.source = context.object.data.zp_source###
        target = self.target = context.object###
        target.animation_data_clear()###
        target.animation_data_create()###

        # print("response", res)
        # return {'FINISHED'}

        s_rot = ZPu.get_prop_values_at(source, "rotation_quaternion", 0)
        t_rot = ZPu.get_prop_values_at(target, "rotation_quaternion", 0)

        if s_rot != [1,0,0,0] or t_rot != [1,0,0,0]:
            pass 
            # res = bpy.ops.armature.zpose_confirm_rotated('INVOKE_DEFAULT')
        #     self.report({'ERROR'}, "YES!")

        
        debug(" ",  "*"*20, "\n", 
            "Start conversion from {} to {}\n".format(source.name, target.name),
            "*"*20)

        ###~~~
        self.target_basebone = ZPu.get_basebone(self.target.data)
        self.target_init_loc = ZPu.get_prop_values_at(self.target, "location", 0)
        self.target_init_loc = Vector(self.target_init_loc)


        self.source_basebone = ZPu.get_basebone(self.source.data)
        self.source_bbone_init_loc = \
            ZPu.get_prop_values_at(self.source.pose.bones[self.source_basebone], "location", 0)
        self.source_bbone_init_loc = Vector(self.source_bbone_init_loc)


        self.context = context
        ###^^^

        ###~~~
        #keep a copy of source's edit_bones collection
        ZPu.mode_set(self.source, context)
        twopi = 2*pi
        self.source_edit_bones = {}
        for k in source.data.bones.keys():
            editbone = source.data.edit_bones[k]
            editbone.roll = editbone.roll % twopi #this way there are no negative rotations
            self.source_edit_bones[k] = (editbone.name, editbone.roll, editbone.vector.copy())
            # print(k, self.source_edit_bones[k].)


        ZPu.mode_set(self.target, context)
        self.collect_information()
        basebone = target.data.edit_bones[self.target_basebone]
        ###^^^

        debug("Basebone: ",basebone.name)

        ###~~~ created_simplied_zp
        self.source.data.pose_position = "REST"
        self.source.data.update_tag()
        context.scene.update()

        # self.simplify(basebone)
        # self.target.data.update_tag()
        # self.target.update_tag({'OBJECT', 'DATA', 'TIME'})
        # self.context.scene.update()

        ZPu.walk_bones(basebone, self.simplify)
        ###^^^

        self.source.data.pose_position = "POSE"
        ZPu.mode_set(self.target, context, "POSE")
        self.source.data.update_tag()
        context.scene.update()
        debug("***********COPY POSE********")
        basebone = self.target.pose.bones[self.target_basebone]
        self.slerps = {}
        self.copy_pose_bone(basebone)
        self.target.data.update_tag()
        self.target.update_tag({'OBJECT', 'DATA', 'TIME'})
        self.context.scene.update()

        if self.source.animation_data.action:
            self.walk_animation(basebone)
        else:
            self.copy_pose_all(basebone)

        self.wm.progress_end()

        return {"FINISHED"} #end execute(self,context)