Example #1
0
File: New.py Project: Irmitya/zpy
def bone(context, armature, name="", edit=None, pose=None, overwrite=False):
    "Insert a bone into an armature object"

    if getattr(armature, 'type', None) != 'ARMATURE':
        return

    # active = Get.active(context)
    # Set.active(context, armature)
    # Set.select(armature, True)
    # Set.visible(context, armature, True)
    # mode = armature.mode.replace('EDIT_ARMATURE', 'EDIT')
    Set.in_scene(context, armature)
    is_visible = Is.visible(context, armature)
    Set.visible(context, armature, True)

    mode = armature.mode
    # mode = context.mode.replace('EDIT_ARMATURE', 'EDIT')

    # Go into Edit mode and create a new bone
    ebones = armature.data.edit_bones
    if armature.mode != 'EDIT':
        Set.mode(context, 'EDIT', armature)
    mirror = armature.data.use_mirror_x
    armature.data.use_mirror_x = False
    children = list()
    if overwrite and name in ebones:
        for child in ebones[name].children:
            children.append((child, child.use_connect))
            child.use_connect = False
        ebones.remove(ebones[name])
    bone = ebones.new(name)
    for child, child_connect in children:
        child.parent = bone
        child.use_connect = child_connect
    bone.tail = ((0, 0, 1))
    # If the bone's head AND tail stay at 0,
    # it gets deleted when leaving edit mode
    name = bone.name
    if edit:
        edit(bone)
    armature.data.use_mirror_x = mirror

    pbones = armature.pose.bones
    if pose:
        Set.mode(context, 'POSE', armature)
        bone = pbones[name]
        pose(bone)

    # Revert mode change
    if mode != armature.mode:
        # Set.active(active)
        Set.mode(context, mode, armature)
    Set.visible(context, armature, is_visible)

    if armature.mode == 'EDIT':
        bone = ebones[name]
    else:
        bone = pbones[name]

    return bone
Example #2
0
def returnToArmature(context, widget):
    bone = fromWidgetFindBone(widget)
    armature = bone.id_data

    # Unhide collection if it was hidden in a previous run
    if widget.users_collection:  # Try to use the widget's collection
        collection = widget.users_collection[0]
    else:  # otherwise use default
        collection = get_collection(context)
    Set.visible(context, collection)

    Set.mode(context, 'OBJECT', widget)

    # collection = get_collection(context)
    if [x for x in armature.users_collection if x != collection]:
        # Don't hide the active collection
        collection.hide_viewport = True
    "New version doesn't have this"
    # get_collection_view_layer(collection).hide_viewport = True

    if getattr(context.space_data, 'local_view', None):
        bpy.ops.view3d.localview()

    Set.active(context, armature)
    Set.select(armature, True)

    Set.mode(context, 'POSE', armature)
    # Set.select(bone, True)
    # Set.active(context, bone)
    armature.data.bones[bone.name].select = True
    armature.data.bones.active = armature.data.bones[bone.name]
Example #3
0
def editWidget(context, active_bone):
    widget = active_bone.custom_shape

    armature = active_bone.id_data
    Set.mode(context, 'OBJECT', armature)
    Set.select(armature, False)
    '''  # 2.7
    if context.space_data.lock_camera_and_layers == False :
        visibleLayers = numpy.array(bpy.context.space_data.layers)+widget.layers-armature.layers
        bpy.context.space_data.layers = visibleLayers.tolist()

    else :
        visibleLayers = numpy.array(bpy.context.scene.layers)+widget.layers-armature.layers
        bpy.context.scene.layers = visibleLayers.tolist()
    '''

    "Commented because the new version only uses get_collection"
    if widget.users_collection:
        collection = widget.users_collection[0]
    else:
        collection = get_collection(context)
        collection.objects.link(widget)
    Set.in_scene(context, collection)
    Set.visible(context, collection)
    "Commented because new version uses operator"
    # get_collection_view_layer(collection).hide_viewport = False
    if getattr(context.space_data, 'local_view', None):
        bpy.ops.view3d.localview()

    # select object and make it active
    Set.select(widget, True)
    Set.active(context, widget)
    Set.mode(context, 'EDIT', widget)
Example #4
0
    def convert(self, context, obj):
        scn = context.scene
        Set.active_select(context, obj, isolate=True)

        coll_instanced = False  # Whether or not to instance collection in full macro

        head = self.head
        group = obj.name + '-Mannequin'

        coll = bpy.data.collections.get(group)
        if coll:
            # Regenerating mannequin
            coll_instanced = True

            for ob in coll.objects.values():
                if Is.mesh(ob) and ob.DazMannequin:
                    bpy.data.objects.remove(ob)
        else:
            coll = bpy.data.collections.new(group)
            scn.collection.children.link(coll)

        # "temporarily" unhide collection if hidden
        in_view = Is.in_view(context, coll)
        if not in_view:
            Set.visible(context, coll, view_layer=True)
        visible = Is.visible(context, coll)
        if not visible:
            Set.visible(context, coll)

        # Add mannequin objects for current mesh
        self.generate(context, obj, obj.parent, coll)

        if self.macro:
            has_mann = False
            for ob in coll.objects.values():
                if Is.mesh(ob):
                    Set.select(ob)
                    has_mann = True

            if has_mann:
                bpy.ops.object.data_transfer_mannequin_preset()
                # if obj.data.materials:
                # bpy.ops.object.data_transfer_materials()

            # Hide the collection and create an instancer of it
            # if coll:
            # # Set.visible(context, obj, value=False)
            # Set.visible(context, coll, value=False, view_layer=True)
            # if not coll_instanced:
            # inst = New.object(context, name=coll.name)
            # inst.instance_type = 'COLLECTION'
            # inst.instance_collection = coll
            # Set.empty_size(inst, 0)
            # return inst

            for ob in coll.objects.values():
                Set.select(ob, value=False)

        if not visible:
            Set.visible(context, coll, False)
        if not in_view:
            Set.visible(context, coll, False, view_layer=True)

        return obj
Example #5
0
def createWidget(context,
                 bone,
                 widget,
                 relative,
                 size,
                 scale,
                 slide,
                 rotate,
                 collection=inf):
    bw_widget_prefix = prefs().widget_prefix + '-' + bone.id_data.name + '_'
    wgt_name = wgt_data_name = bw_widget_prefix + bone.name

    if collection is inf:
        collection = get_collection(context)

    # Custom check to "keep visiblity?"
    isolate_collection = collection.hide_viewport
    Set.visible(context, collection)

    if bone.custom_shape_transform:
        matrixBone = bone.custom_shape_transform
    else:
        matrixBone = bone

    if bone.custom_shape:
        old_shape = bone.custom_shape
        wgt_name = old_shape.name
        wgt_data_name = old_shape.data.name
        old_shape.name += "_old"
        old_shape.data.name += "_old"
        if collection.objects.get(old_shape.name):
            collection.objects.unlink(old_shape)

    # make the data name include the prefix
    newData = bpy.data.meshes.new(wgt_data_name)

    if relative:
        boneLength = 1
    else:
        boneLength = (1 / bone.bone.length)

    # print('\n\n\n', *rotate,'\n\n\n')
    newData.from_pydata(
        numpy.array(widget['vertices']) * [
            size * scale[0] * boneLength, size * scale[2] * boneLength,
            size * scale[1] * boneLength
        ] + [*slide],  # [0, slide, 0],
        widget['edges'],
        widget['faces'],
    )
    for v in newData.vertices:
        v.co.rotate(Euler((*rotate, )))

    # newData.from_pydata(
    # numpy.array(widget['vertices']) * [
    # size*scale[0]*boneLength,
    # size*scale[2]*boneLength,
    # size*scale[1]*boneLength
    # ] + [0,slide,0],
    # widget['edges'],
    # widget['faces']
    # )

    newData.update(calc_edges=True)

    newObject = bpy.data.objects.new(wgt_name, newData)

    # context.scene.collection.objects.link(newObject)
    collection.objects.link(newObject)
    # if isolate_collection:
    # utils.update(context)
    collection.hide_viewport = isolate_collection

    # When it creates the widget it still doesn't take the armature scale into account
    "original uses matrix_world. I don't know if I care"
    # newObject.matrix_local = matrixBone.bone.matrix_local
    newObject.matrix_world = matrixBone.bone.matrix_local
    newObject.scale = [matrixBone.bone.length] * 3
    # context.scene.update()

    context.view_layer.update()

    bone.custom_shape = newObject
    bone.bone.show_wire = True