Beispiel #1
0
    def process(self, ga):
        result = ga.evaluate(ga.data, "AddNull Null")
        result = ga.evaluate(ga.data, "Position 0 1 0")

        # if 'autokey' is not turned on, we need to explicitly
        # create keys for the object at the current time offset

        interface_info = lwsdk.LWInterfaceInfo()
        if not (interface_info.generalFlags & lwsdk.LWGENF_AUTOKEY):
            ga.evaluate(ga.data, "CreateKey %f" % interface_info.curTime)

        return lwsdk.AFUNC_OK
Beispiel #2
0
    def get_world_bone_axis(self, bone):
        cur_time = lwsdk.LWInterfaceInfo().curTime

        if bone.type == lwsdk.LWBONETYPE_ZAXIS:
            pos = self._item_info.param(bone.id, lwsdk.LWIP_W_POSITION, cur_time)
            forward = self._item_info.param(bone.id, lwsdk.LWIP_FORWARD, cur_time)

            return (pos, forward * bone.restlength)

        pos = self._item_info.param(bone.id, lwsdk.LWIP_W_POSITION, cur_time)
        ppos = self._item_info.param(bone.parent, lwsdk.LWIP_W_POSITION, cur_time)
        forward = pos - ppos

        return (ppos, forward)
Beispiel #3
0
    def tool_draw(self, ca):
        self._bones = self.get_current_bones()
        cur_time = lwsdk.LWInterfaceInfo().curTime

        for bone in self._bones:
            if bone.type == lwsdk.LWBONETYPE_ZAXIS:
                len = bone.restlength;
                dir = Vector(0,0,1)

                ca.setCSysItem(ca.dispData, bone.id);

            else:

                pos = self._item_info.param(bone.id, lwsdk.LWIP_POSITION, cur_time)
                len = Vector.magnitude(pos)
                dir = pos / len;

                ca.setCSysItem(ca.dispData, bone.parent);

            ca.setDrawMode(ca.dispData, 8 + 4)

            q1 = Vector(-0.1,-0.1,0)
            q2 = Vector( 0.1,-0.1,0)
            q3 = Vector( 0.1, 0.1,0)
            q4 = Vector(-0.1, 0.1,0)

            q1 += dir * self._frac
            q1 *= len
            q2 += dir * self._frac
            q2 *= len
            q3 += dir * self._frac
            q3 *= len
            q4 += dir * self._frac
            q4 *= len

            # color can be a lwsdk.Vector, lwsdk.Color or a Python sequence
            ca.setColor(ca.dispData, [1.0, 0.3, 0.1, 1.0])

            ca.line(ca.dispData, q1, q2, lwsdk.LWCSYS_OBJECT)
            ca.line(ca.dispData, q2, q3, lwsdk.LWCSYS_OBJECT)
            ca.line(ca.dispData, q3, q4, lwsdk.LWCSYS_OBJECT)
            ca.line(ca.dispData, q4, q1, lwsdk.LWCSYS_OBJECT)
            ca.line(ca.dispData, q1, q3, lwsdk.LWCSYS_OBJECT)
            ca.line(ca.dispData, q2, q4, lwsdk.LWCSYS_OBJECT)

            ca.setColor(ca.dispData, [1.0, 0.3, 0.1, 0.2])
            ca.quad(ca.dispData, q1, q2, q3, q4, lwsdk.LWCSYS_OBJECT)
Beispiel #4
0
    def get_current_bones(self):
        selected_bones = []

        selection = lwsdk.LWInterfaceInfo().selected_items()
        if selection == None:
            return selected_bones

        for item in selection:
            if self._item_info.type(item) == lwsdk.LWI_BONE:
                bone_type = self._bone_info.type(item)
                parent = self._item_info.parent(item)
                if bone_type == lwsdk.LWBONETYPE_ZAXIS:
                    selected_bones.append(PyBone(item, parent, bone_type, self._bone_info.restLength(item)))
                else:
                    if parent != None:
                        if self._item_info.type(parent) == lwsdk.LWI_BONE:
                            bone_type = self._bone_info.type(parent)
                            if bone_type == lwsdk.LWBONETYPE_JOINT:
                                selected_bones.append(PyBone(item, parent, bone_type, self._bone_info.restLength(item)))

        return selected_bones
Beispiel #5
0
    def tool_up(self, event):
        if (self._handle >= 0) and self._moved:
            new_bones = deepcopy(self._bones)

            # Make sure ParentInPlace is OFF
            pip = ((lwsdk.LWInterfaceInfo().generalFlags & lwsdk.LWGENF_PARENTINPLACE) == lwsdk.LWGENF_PARENTINPLACE)
            if pip:
                lwsdk.command("ParentInPlace")

            for bone in self._bones:
                new_bones.append(self.split_bone(bone, self._frac))

            if pip:
                lwsdk.command("ParentInPlace")

            for i in range(len(new_bones)):
                if i == 0:
                    lwsdk.command("SelectItem %s" % lwsdk.itemid_to_str(new_bones[i].id))
                else:
                    lwsdk.command("AddToSelection %s" % lwsdk.itemid_to_str(new_bones[i].id))

        self._handle = -1
Beispiel #6
0
    def split_bone(self, bone, split):
        cur_time = lwsdk.LWTimeInfo().time;
        pos = self._item_info.param(bone.id, lwsdk.LWIP_W_POSITION, cur_time)
        rest_pos = Vector(self._bone_info.restParam(bone.id, lwsdk.LWIP_POSITION))
        rest_rot = Vector(self._bone_info.restParam(bone.id, lwsdk.LWIP_ROTATION))
        rest_rot *= math.pi / 180.0
        rest_len = bone.restlength

        # Clone the existing bone
        # Make the new bone the parent, existing bone the child

        lwsdk.command("SelectItem %s" % lwsdk.itemid_to_str(bone.id))
        lwsdk.command("Clone 1")

        selected_items = lwsdk.LWInterfaceInfo().selected_items()
        new_bone = PyBone(selected_items[0],
                        self._item_info.parent(selected_items[0]),
                        self._bone_info.type(selected_items[0]),
                        self._bone_info.restLength(selected_items[0]))

        # Make new bone child of original parent
        lwsdk.command("ParentItem %s" % lwsdk.itemid_to_str(bone.parent))

        # Motion options corrections:
        # - Don't copy over the goal
        lwsdk.command("GoalItem 0")

        if new_bone.type == lwsdk.LWBONETYPE_ZAXIS:
            temp_vector = Vector(0,0,1)
            split_pos = temp_vector * (rest_len * split)

            # Make new bone shorter to the split
            lwsdk.command("BoneRestLength %g" % (rest_len * split))

            # Make corrections to existing bone
            # - Parent it to the new bone
            # - Place at the split position
            # - Shorten it to the remainder of original length
            # - Reset scale and rotation (as those are now done by new bone)

            lwsdk.command("SelectItem %s" % lwsdk.itemid_to_str(bone.id))
            lwsdk.command("ParentItem %s" % lwsdk.itemid_to_str(new_bone.id))

            # Set positions
            self.set_vec_chan_to_value(bone.id, 0, split_pos)
            lwsdk.command("PivotPosition 0 0 0")
            lwsdk.command("BoneRestPosition %s" % str(split_pos))

            lwsdk.command("BoneRestLength %g" % (rest_len * (1-split)))

            # Set rotations
            self.set_vec_chan_to_value(bone.id, 3, Vector(0,0,0))
            lwsdk.command("PivotRotation 0 0 0")
            lwsdk.command("BoneRestRotation 0 0 0")

            # Set scales
            self.set_vec_chan_to_value(bone.id, 6, Vector(1,1,1));

            lwsdk.command("DirtyMotion")
            lwsdk.command("UpdateMotion")

            # Move children to take into account shortened bone
            child_id = self._item_info.firstChild(bone.id)
            while child_id:
                lwsdk.command("SelectItem %s" % lwsdk.itemid_to_str(child_id))
                self.add_value_to_vec_chan(child_id, 0, -split_pos);
                if self._item_info.type(child_id) == lwsdk.LWI_BONE:
                    child_pos = Vector(self._bone_info.restParam(child_id, lwsdk.LWIP_POSITION))
                    child_pos -= split_pos;
                    lwsdk.command("BoneRestPosition %s" % str(child_pos))

                lwsdk.command("DirtyMotion")
                lwsdk.command("UpdateMotion")

                child_id = self._item_info.nextChild(bone.id, child_id)
        else:
            axispos, axis = self.get_world_bone_axis(bone)

            # Move new bone to split position
            new_rest_pos = rest_pos * split
            lwsdk.command("BoneRestPosition %s" % str(new_rest_pos))
            # newPos = split * pos
            self.mul_value_to_vec_chan(new_bone.id, 0, split)
            lwsdk.command("BoneRestRotation 0 0 0")
            self.set_vec_chan_to_value(new_bone.id, 3, Vector(0,0,0))
            self.set_vec_chan_to_value(new_bone.id, 6, Vector(1,1,1))

            lwsdk.command("DirtyMotion")
            lwsdk.command("UpdateMotion")

            # Make corrections to existing bone
            # - Parent it to the new bone
            lwsdk.command("SelectItem %s" % lwsdk.itemid_to_str(bone.id))
            lwsdk.command("ParentItem %s" % lwsdk.itemid_to_str(new_bone.id))

            # - Correct place to account for new bone parent
            #   New bone parent has no rotation and unit scale,
            #   so only difference is a position offset
            new_rest_pos = rest_pos * (1.0 - split)
            lwsdk.command("BoneRestPosition %s" % str(new_rest_pos))
            # newPos = (1.0 - split) * pos
            self.mul_value_to_vec_chan(bone.id, 0, 1.0 - split)

            lwsdk.command("DirtyMotion")
            lwsdk.command("UpdateMotion")

        return new_bone
Beispiel #7
0
    def process(self, ga):
        ui = lwsdk.LWPanels()
        panel = ui.create('Final Fantasy 7:Remake  Import')

        controlWidth = 64
        c1 = panel.load_ctl('Select File', controlWidth)
        c1.set_str(self._filepath)

        if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_CANCEL) == 0:
            ui.destroy(panel)
            return lwsdk.AFUNC_OK

        self.filepath = c1.get_str()
        progress_count = 8

        t1 = time.time()

        interface_info = lwsdk.LWInterfaceInfo()
        if not (interface_info.generalFlags & lwsdk.LWGENF_PARENTINPLACE):
            ga.evaluate(ga.data, "ParentInPlace")

        #set bind pose at frame -10
        ga.evaluate(ga.data, "GoToFrame -10")

        interface_info = lwsdk.LWInterfaceInfo()
        selected_items = interface_info.selected_items()

        #if a mesh not selected create a parent null for the bones
        if not selected_items:
            result = ga.evaluate(ga.data, "AddNull Root")
            interface_info = lwsdk.LWInterfaceInfo()
            selected_items = interface_info.selected_items()
            buildItemID = selected_items[0]
        else:
            item_info = lwsdk.LWItemInfo()
            mytype = item_info.type(selected_items[0])
            if mytype == lwsdk.LWI_OBJECT:
                buildItemID = selected_items[0]
            else:
                result = ga.evaluate(ga.data, "AddNull Root")
                interface_info = lwsdk.LWInterfaceInfo()
                selected_items = interface_info.selected_items()
                buildItemID = selected_items[0]

        #get weightmap count
        object_functions = lwsdk.LWObjectFuncs()
        numWeights = object_functions.numVMaps(lwsdk.LWVMAP_WGHT)

        files = path_wrangler(self.filepath)
        files.get_files()

        md = open(files.data['uexp'], 'rb')
        ua = open(files.data['uasset'], 'rb')

        meshName = files.data['meshName']
        submesh_name = files.data['submesh_name']

        arm = False
        weightData = {}
        Weight_array = []
        vertexArray = []
        NA = []
        normal_array = []

        UVs0 = []
        UVs1 = []
        UVs2 = []
        UVs3 = []
        UVs4 = []

        faces = []

        names = readUasset(ua)
        ua.close()

        pattern0 = re.compile(
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00........')
        for x in xrange(20000):
            s1 = struct.unpack("18s", md.read(18))[0]
            if pattern0.match(s1):
                c0 = struct.unpack("<L", md.read(4))[0]
                c1 = struct.unpack("<L", md.read(4))[0]
                c2 = struct.unpack("<L", md.read(4))[0]
                c3 = struct.unpack("<L", md.read(4))[0]
                c4 = struct.unpack("<L", md.read(4))[0]
                if (c0 and c1 and c2 and c3 and c4 > 1000000000):
                    break
                else:
                    md.seek(-20, 1)
            md.seek(-17, 1)

        materialCount = struct.unpack("<L", md.read(4))[0]

        materials = {}
        for m in xrange(materialCount):
            materials[m] = {}
            materials[m]['val0'] = struct.unpack("<l", md.read(4))[0]
            stringIndex = struct.unpack("<L", md.read(4))[0]
            unk0 = struct.unpack("<L", md.read(4))[0]
            unk1 = struct.unpack("<L", md.read(4))[0]
            unk2 = struct.unpack("<L", md.read(4))[0]
            unk3 = struct.unpack("<f", md.read(4))[0]
            unk4 = struct.unpack("<f", md.read(4))[0]
            unk5 = struct.unpack("<L", md.read(4))[0]
            unk6 = struct.unpack("<L", md.read(4))[0]

            materials[m]['name'] = names[stringIndex]

        boneCount = struct.unpack("<L", md.read(4))[0]
        joint_data = {}
        joint_names = []
        for i in xrange(boneCount):
            string_index = struct.unpack("<L", md.read(4))[0]
            jName = names[string_index]
            unk = struct.unpack("<L", md.read(4))[0]
            parent = struct.unpack("<l", md.read(4))[0]

            joint_data[i] = {"name": jName, "parent": parent}

        boneCount2 = struct.unpack("<L", md.read(4))[0]

        progress_count += boneCount2
        if not moninit(progress_count, "Importing", title='Progress'):
            raise Exception('Hell!')

        boneArray = []
        psn = {}
        IDs = []
        for bn in range(boneCount2):
            boneName = joint_data[bn]["name"]
            BNparent = joint_data[bn]["parent"]

            m1 = struct.unpack("<10f", md.read(40))

            ps0 = (m1[4], m1[5], m1[6])
            #BNps = np.asarray(ps0)

            rt0_ = (m1[3], m1[0], m1[1], m1[2])
            rt0 = quat2mat(rt0_)

            if BNparent == -1:
                BNparent = 0

            #BNps = [mi[3,1], mi[3,2], mi[3,0]]
            BNps = ps0
            psn[bn] = BNps

            #BNrt = mat2euler(mi, axes='ryxz')

            ga.evaluate(ga.data,
                        "SelectItem %s" % lwsdk.itemid_to_str(buildItemID))
            layoutCommand = "AddBone " + boneName
            result = ga.evaluate(ga.data, layoutCommand)

            # no influence except for weight maps
            result = ga.evaluate(ga.data, 'BoneStrength 0')
            interface_info = lwsdk.LWInterfaceInfo()
            selected_items = interface_info.selected_items()
            itemID = selected_items[0]
            IDs.append(itemID)

            #UpdateMotion needed for parent in place
            if bn != 0:
                ga.evaluate(ga.data, 'UpdateMotion')
                ga.evaluate(
                    ga.data,
                    "ParentItem %s" % lwsdk.itemid_to_str(IDs[BNparent]))

            layoutCommand = "Position " + str(BNps[0]) + " " + str(
                BNps[1]) + " " + str(BNps[2])
            result = ga.evaluate(ga.data, layoutCommand)

            if bn == 0:
                result = ga.evaluate(ga.data, "BoneRestLength 0.5")
            else:
                lenX = psn[BNparent][0] - BNps[0]
                lenY = psn[BNparent][1] - BNps[1]
                lenZ = psn[BNparent][2] - BNps[2]

                #dont use full bone length....dummy
                boneLength = math.sqrt(lenX * lenX + lenY * lenY +
                                       lenZ * lenZ) * 0.6
                result = ga.evaluate(ga.data,
                                     "BoneRestLength " + str(boneLength))

            #BNrt = quat2euler(rt0_, axes='szyx')
            BNrt = quat2euler(rt0_, axes='ryxz')
            rotx = (180.0 / math.pi) * BNrt[0]
            roty = (180.0 / math.pi) * BNrt[1]
            rotz = (180.0 / math.pi) * BNrt[2]

            layoutCommand = "Rotation " + str(rotx) + " " + str(
                roty) + " " + str(rotz)
            result = ga.evaluate(ga.data, layoutCommand)

            if bn == 0:
                layoutCommand = "Rotation " + str(180.0) + " " + str(
                    180.0) + " " + str(0.0)
                result = ga.evaluate(ga.data, layoutCommand)

            # if 'autokey' is not turned on, we need to explicitly
            # create keys for the object at the current time offset
            if not (interface_info.generalFlags & lwsdk.LWGENF_AUTOKEY):
                ga.evaluate(ga.data, "CreateKey %f" % interface_info.curTime)

            #keys created at frame -10 so delete any at frame 0
            ga.evaluate(ga.data, 'DeleteKey 0')

            if (numWeights > 0):
                if bn != 0:
                    for b in range(numWeights):
                        weightName = object_functions.vmapName(
                            lwsdk.LWVMAP_WGHT, b)
                        if weightName == boneName:
                            layoutCommand = 'BoneWeightMapName ' + boneName
                            result = ga.evaluate(ga.data, layoutCommand)
                            result = ga.evaluate(ga.data, 'BoneWeightMapOnly')
            ga.evaluate(ga.data, 'RecordRestPosition')

            monstep()

        #-----------------------------------------------------------#
        boneCount3 = struct.unpack("<L", md.read(4))[0]
        md.seek(boneCount3 * 12, 1)
        vertexGroups = {}
        unk0 = struct.unpack("<L", md.read(4))[0]
        unk1 = struct.unpack("B", md.read(1))[0]
        unk2 = struct.unpack("B", md.read(1))[0]
        groupCount = struct.unpack("<L", md.read(4))[0]
        for m in xrange(groupCount):
            z1 = struct.unpack("<H", md.read(2))[0]
            ID = struct.unpack("<H", md.read(2))[0]

            md.seek(24, 1)
            vertexGroups[ID] = {'range': 0, 'bones': []}

            # pragma region bone palette
            start = struct.unpack("<L", md.read(4))[0]
            count = struct.unpack("<L", md.read(4))[0]
            vertexGroups[ID]['bones_np'] = np.zeros((boneCount2, ), dtype=int)
            bone_names = []
            for bn in xrange(count):
                bid = struct.unpack("<H", md.read(2))[0]
                vertexGroups[ID]['bones_np'][bn] = bid
                vertexGroups[ID]['bones'].append(bid)
                bone_names.append(joint_data[bid]["name"])
            # pragma endregion bone palette

            size = struct.unpack("<L", md.read(4))[0]
            stop = start + size
            vertexGroups[ID]['range'] = np.arange(start, stop)
            vertexGroups[ID]["start"] = start
            vertexGroups[ID]["stop"] = stop
            vertexGroups[ID]["size"] = size
            vertexGroups[ID]["names"] = bone_names

            md.seek(34, 1)
            FFx4 = readHexString(md, 4)
            flag = struct.unpack("<L", md.read(4))[0]
            if flag:  # extra data for this group
                count = struct.unpack("<L", md.read(4))[0]
                md.seek(count * 16, 1)
            else:
                null = struct.unpack("<L", md.read(4))[0]

        process_surfaces(files, vertexGroups, materials)
        #-----------------------------------------------------------#

        ga.evaluate(ga.data, 'SelectAllBones')
        #ga.evaluate(ga.data, 'RecordRestPosition')
        bonemayastyledraw()
        itemiconscale(0.2)

        monend()

        elapsed = time.time() - t1
        print "Time: " + str(elapsed)

        md.close()

        return lwsdk.AFUNC_OK
Beispiel #8
0
    def process(self, ga):
        ui = lwsdk.LWPanels()
        panel = ui.create('Scatter Objects')

        c1 = panel.dist_ctl('Radius')
        c2 = panel.bool_ctl('X')
        c3 = panel.bool_ctl('Y')
        c4 = panel.bool_ctl('Z')

        c1.set_float(self._radius)
        c2.set_int(1 if self._include_x else 0)
        c3.set_int(1 if self._include_y else 0)
        c4.set_int(1 if self._include_z else 0)

        panel.align_controls_vertical([c1, c2, c3, c4])
        panel.size_to_layout(5, 5)

        if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_CANCEL) == 0:
            ui.destroy(panel)
            return lwsdk.AFUNC_OK

        self._radius = c1.get_float()
        self._include_x = (c2.get_int() == 1)
        self._include_y = (c3.get_int() == 1)
        self._include_z = (c4.get_int() == 1)

        ui.destroy(panel)

        item_info = lwsdk.LWItemInfo()

        interface_info = lwsdk.LWInterfaceInfo()
        current_time = interface_info.curTime
        autokey_is_on = ((interface_info.generalFlags & lwsdk.LWGENF_AUTOKEY) != 0)
        selected_items = interface_info.selected_items()

        for obj in selected_items:
            r = random()
            x_invert = -1 if r < 0.5 else 1
            x_value = 0.0
            if self._include_x:
                x_value = random() * self._radius * x_invert

            r = random()
            y_invert = -1 if r < 0.5 else 1
            y_value = 0.0
            if self._include_y:
                y_value = random() * self._radius * y_invert

            r = random()
            z_invert = -1 if r < 0.5 else 1
            z_value = 0.0
            if self._include_z:
                z_value = random() * self._radius * z_invert

            ga.evaluate(ga.data, "SelectItem %s" % lwsdk.itemid_to_str(obj))
            ga.evaluate(ga.data, "Position %f %f %f" % (x_value, y_value, z_value))

            if not autokey_is_on:
                ga.evaluate(ga.data, "CreateKey %f" % current_time)

        return lwsdk.AFUNC_OK
Beispiel #9
0
    def process(self, ga):
        item_info = lwsdk.LWItemInfo()
        object_info = lwsdk.LWObjectInfo()
        instancer_funcs = lwsdk.LWItemInstancerFuncs()
        instance_info = lwsdk.LWItemInstanceInfo()
        interface_info = lwsdk.LWInterfaceInfo()

        current_time = interface_info.curTime
        autokey_is_on = ((interface_info.generalFlags & lwsdk.LWGENF_AUTOKEY)
                         != 0)

        selected_item = interface_info.selected_items()[0]
        selected_item_id = lwsdk.itemid_to_str(selected_item)

        # find the Instancer that owns the selected object
        found_instancer = None
        obj = item_info.first(lwsdk.LWI_OBJECT, None)
        while obj and (not found_instancer):
            instancer = object_info.instancer(obj)
            if instancer:
                instance = instancer_funcs.first(instancer)
                while instance and (not found_instancer):
                    if instance_info.item(instance) == selected_item:
                        # found it!
                        found_instancer = instancer
                        break
                    instance = instancer_funcs.next(instancer, instance)
            obj = item_info.next(obj)

        if not found_instancer:
            print >> sys.stderr, 'Object "%s" is NOT instanced!' % item_info.name(
                selected_item)
            return lwsdk.AFUNC_OK

        instances = []
        instance = instancer_funcs.first(found_instancer)
        while instance:
            if instance_info.item(instance) == selected_item:
                instances.append(instance)
            instance = instancer_funcs.next(found_instancer, instance)

        # values used for progress
        current_index = 1.0
        target_index = len(instances) * 1.0

        for instance in instances:
            pos = instance_info.pos(instance, 0)
            # Note: InstanceInfo.rotation() returns radians!
            rot = instance_info.rotation(instance, 0)
            scl = instance_info.scale(instance, 0)

            lwsdk.command("SelectItem %s" % selected_item_id)
            lwsdk.command("Clone 1")

            lwsdk.command("Position %s" % str(pos))
            # Note: Rotation expects degrees!
            lwsdk.command("Rotation %s" % str(Vector.to_degrees(rot)))
            lwsdk.command("Scale %s" % str(scl))
            if not autokey_is_on:
                lwsdk.command("CreateKey %f" % current_time)

            lwsdk.command("StatusMsg {%f}Baking instances..." %
                          (current_index / target_index))
            current_index += 1.0

        lwsdk.command("SelectItem %s" % selected_item_id)

        lwsdk.command("StatusMsg Baking complete.")

        return lwsdk.AFUNC_OK