Esempio n. 1
0
    def execute(self, context):
        SurfList = []
        scene = context.scene
        print("Transfert UV start")
        for item in context.object.IsoSurf:
            if item.active == True:
                if item.obj != '':
                    if item.psys != '':
                        SurfList.append([item.obj, item.psys])
        for obj, psys in SurfList:
            if bpy.data.objects[obj].data.uv_layers.active == None:
                print("   ... local UV transfert from:", obj, psys)
                bpy.data.objects[obj].particle_systems[psys].settings['IsoLocalUV'] = [0.0] * len(bpy.data.objects[obj].particle_systems[psys].particles) * 3
                bpy.data.objects[obj].particle_systems[psys].particles.foreach_get("location", bpy.data.objects[obj].particle_systems[psys].settings['IsoLocalUV'])
            else:

                object = bpy.data.objects[obj]
                print('   ...start bake uv from:', obj, psys)

                obdata = object.data.copy()
                object2 = bpy.data.objects.new(name="mol_uv_temp", object_data=obdata)
                object2.matrix_world = object.matrix_world

                context.scene.objects.link(object2)
                mod = object2.modifiers.new("tri_for_uv", "TRIANGULATE")
                mod.quad_method = "BEAUTY"
                mod.ngon_method = "BEAUTY"
                newmesh = object2.to_mesh(bpy.context.scene, True, "RENDER", True, False)
                object2.data = newmesh
                context.scene.update()
                """
                oldmesh = object.data
                newmesh = object.data.copy()
                object.data = newmesh
                mod = object.modifiers.new("tri_for_uv","TRIANGULATE")
                mod.use_beauty = False
                bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)
                """
                # print('-------------start------------')
                attr = []
                for par in object.particle_systems[psys].particles:
                    parloc = (par.location * object2.matrix_world) - object2.location
                    point = object2.closest_point_on_mesh(parloc)
                    # print('closest:',par.location,point[0],point[2])
                    vindex1 = object2.data.polygons[point[3]].vertices[0]
                    vindex2 = object2.data.polygons[point[3]].vertices[1]
                    vindex3 = object2.data.polygons[point[3]].vertices[2]
                    v1 = (object2.matrix_world * object2.data.vertices[vindex1].co).to_tuple()
                    v2 = (object2.matrix_world * object2.data.vertices[vindex2].co).to_tuple()
                    v3 = (object2.matrix_world * object2.data.vertices[vindex3].co).to_tuple()
                    uvindex1 = object2.data.polygons[point[3]].loop_start + 0
                    uvindex2 = object2.data.polygons[point[3]].loop_start + 1
                    uvindex3 = object2.data.polygons[point[3]].loop_start + 2
                    uv1 = object2.data.uv_layers.active.data[uvindex1].uv.to_3d()
                    uv2 = object2.data.uv_layers.active.data[uvindex2].uv.to_3d()
                    uv3 = object2.data.uv_layers.active.data[uvindex3].uv.to_3d()
                    # print(vertices1.co,vertices2.co,vertices3.co)
                    # print(uv1,uv2,uv3)
                    p = object2.matrix_world * point[1]
                    v1 = Vector(v1)
                    v2 = Vector(v2)
                    v3 = Vector(v3)
                    uv1 = Vector(uv1)
                    uv2 = Vector(uv2)
                    uv3 = Vector(uv3)
                    # print(a,b,c,uv1,uv2,uv3,p)
                    newuv = barycentric(p, v1, v2, v3, uv1, uv2, uv3)
                    #print('New UVs:',newuv)
                    parloc = par.location * object2.matrix_world
                    dist = (Vector((parloc[0] - p[0], parloc[1] - p[1], parloc[2] - p[2]))).length
                    newuv[2] = dist
                    newuv = newuv.to_tuple()
                    attr.append(newuv[0])
                    attr.append(newuv[1])
                    attr.append(newuv[2])
                    #par.angular_velocity = newuv
                object.particle_systems[psys].settings['IsoLocalUV'] = attr
                scene.objects.unlink(object2)
                bpy.data.objects.remove(object2)
                bpy.data.meshes.remove(newmesh)

        print("Transfert UV end")
        return {'FINISHED'}
Esempio n. 2
0
    def execute(self, context):
        scene = context.scene
        obj = get_object(context, context.object)

        scene.mol_objuvbake = obj.name
        scene.mol_psysuvbake = obj.particle_systems.active.name

        if not obj.data.uv_layers.active:
            return {'FINISHED'}

        print('  start bake uv from:', obj.name)

        obdata = obj.data.copy()
        object2 = bpy.data.objects.new(name="mol_uv_temp", object_data=obdata)
        object2.matrix_world = obj.matrix_world

        if is_blender_28():
            context.scene.collection.objects.link(object2)
        else:
            context.scene.objects.link(object2)
        mod = object2.modifiers.new("tri_for_uv", "TRIANGULATE")
        mod.ngon_method = 'BEAUTY'
        mod.quad_method = 'BEAUTY'
        if is_blender_28():
            depsgraph = bpy.context.evaluated_depsgraph_get()
            newmesh = object2.to_mesh(True, depsgraph)
        else:
            newmesh = object2.to_mesh(context.scene, True, "RENDER", True,
                                      False)
        object2.data = newmesh
        context.scene.update()
        psys = obj.particle_systems[scene.mol_psysuvbake]

        for par in psys.particles:

            if is_blender_28():
                parloc = (
                    par.location @ object2.matrix_world) - object2.location
            else:
                parloc = (par.location *
                          object2.matrix_world) - object2.location

            point = object2.closest_point_on_mesh(parloc)
            vindex1 = object2.data.polygons[point[3]].vertices[0]
            vindex2 = object2.data.polygons[point[3]].vertices[1]
            vindex3 = object2.data.polygons[point[3]].vertices[2]

            if is_blender_28():
                v1 = (object2.matrix_world
                      @ object2.data.vertices[vindex1].co).to_tuple()
                v2 = (object2.matrix_world
                      @ object2.data.vertices[vindex2].co).to_tuple()
                v3 = (object2.matrix_world
                      @ object2.data.vertices[vindex3].co).to_tuple()
            else:
                v1 = (object2.matrix_world *
                      object2.data.vertices[vindex1].co).to_tuple()
                v2 = (object2.matrix_world *
                      object2.data.vertices[vindex2].co).to_tuple()
                v3 = (object2.matrix_world *
                      object2.data.vertices[vindex3].co).to_tuple()

            uvindex1 = object2.data.polygons[point[3]].loop_start + 0
            uvindex2 = object2.data.polygons[point[3]].loop_start + 1
            uvindex3 = object2.data.polygons[point[3]].loop_start + 2
            uv1 = object2.data.uv_layers.active.data[uvindex1].uv.to_3d()
            uv2 = object2.data.uv_layers.active.data[uvindex2].uv.to_3d()
            uv3 = object2.data.uv_layers.active.data[uvindex3].uv.to_3d()

            if is_blender_28():
                p = object2.matrix_world @ point[1]
            else:
                p = object2.matrix_world * point[1]

            v1 = Vector(v1)
            v2 = Vector(v2)
            v3 = Vector(v3)
            uv1 = Vector(uv1)
            uv2 = Vector(uv2)
            uv3 = Vector(uv3)
            newuv = barycentric(p, v1, v2, v3, uv1, uv2, uv3)

            if is_blender_28():
                parloc = par.location @ object2.matrix_world
            else:
                parloc = par.location * object2.matrix_world

            dist = (Vector(
                (parloc[0] - p[0], parloc[1] - p[1], parloc[2] - p[2]))).length
            newuv[2] = dist
            newuv = newuv.to_tuple()
            par.angular_velocity = newuv

        if is_blender_28():
            scene.collection.objects.unlink(object2)
        else:
            scene.objects.unlink(object2)
        bpy.data.objects.remove(object2)
        bpy.data.meshes.remove(newmesh)
        bpy.data.meshes.remove(obdata)
        print('         uv baked on:', psys.settings.name)

        return {'FINISHED'}
Esempio n. 3
0
    def execute(self, context):
        global mol_substep
        global mol_old_endframe
        global mol_exportdata
        global mol_report
        global mol_minsize
        global mol_newlink
        global mol_deadlink
        global mol_totallink
        global mol_totaldeadlink
        global mol_simrun
        global mol_timeremain
        global mol_objuvbake
        global mol_psysuvbake

        scene = context.scene
        object = mol_objuvbake

        print('  start bake uv from:', object.name)
        #object2 = object.copy()

        obdata = object.data.copy()
        object2 = bpy.data.objects.new(name="mol_uv_temp", object_data=obdata)
        object2.matrix_world = object.matrix_world

        context.scene.objects.link(object2)
        mod = object2.modifiers.new("tri_for_uv", "TRIANGULATE")
        mod.use_beauty = False
        newmesh = object2.to_mesh(bpy.context.scene, True, "RENDER", True,
                                  False)
        object2.data = newmesh
        context.scene.update()
        """
        oldmesh = object.data
        newmesh = object.data.copy()
        object.data = newmesh
        mod = object.modifiers.new("tri_for_uv","TRIANGULATE")
        mod.use_beauty = False
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)
        """
        psys = mol_psysuvbake
        #print('-------------start------------')
        for par in psys.particles:
            parloc = (par.location * object2.matrix_world) - object2.location
            point = object2.closest_point_on_mesh(parloc)
            #print('closest:',par.location,point[0],point[2])
            vindex1 = object2.data.polygons[point[2]].vertices[0]
            vindex2 = object2.data.polygons[point[2]].vertices[1]
            vindex3 = object2.data.polygons[point[2]].vertices[2]
            v1 = (object2.matrix_world *
                  object2.data.vertices[vindex1].co).to_tuple()
            v2 = (object2.matrix_world *
                  object2.data.vertices[vindex2].co).to_tuple()
            v3 = (object2.matrix_world *
                  object2.data.vertices[vindex3].co).to_tuple()
            uvindex1 = object2.data.polygons[point[2]].loop_start + 0
            uvindex2 = object2.data.polygons[point[2]].loop_start + 1
            uvindex3 = object2.data.polygons[point[2]].loop_start + 2
            uv1 = object2.data.uv_layers.active.data[uvindex1].uv.to_3d()
            uv2 = object2.data.uv_layers.active.data[uvindex2].uv.to_3d()
            uv3 = object2.data.uv_layers.active.data[uvindex3].uv.to_3d()
            #print(vertices1.co,vertices2.co,vertices3.co)
            #print(uv1,uv2,uv3)
            p = object2.matrix_world * point[0]
            v1 = Vector(v1)
            v2 = Vector(v2)
            v3 = Vector(v3)
            uv1 = Vector(uv1)
            uv2 = Vector(uv2)
            uv3 = Vector(uv3)
            #print(a,b,c,uv1,uv2,uv3,p)
            newuv = barycentric(p, v1, v2, v3, uv1, uv2, uv3)
            #print('New UVs:',newuv)
            parloc = par.location * object2.matrix_world
            dist = (Vector(
                (parloc[0] - p[0], parloc[1] - p[1], parloc[2] - p[2]))).length
            newuv[2] = dist
            newuv = newuv.to_tuple()
            par.angular_velocity = newuv
        scene.objects.unlink(object2)
        bpy.data.objects.remove(object2)
        bpy.data.meshes.remove(newmesh)
        print('         uv baked on:', psys.settings.name)

        return {'FINISHED'}
Esempio n. 4
0
    def execute(self, context):
        global mol_substep
        global mol_old_endframe
        global mol_exportdata
        global mol_report
        global mol_minsize
        global mol_newlink
        global mol_deadlink
        global mol_totallink
        global mol_totaldeadlink
        global mol_simrun
        global mol_timeremain
        global mol_objuvbake
        global mol_psysuvbake

        scene = context.scene
        object = mol_objuvbake

        print('  start bake uv from:', object.name)
        #object2 = object.copy()

        obdata = object.data.copy()
        object2 = bpy.data.objects.new(name="mol_uv_temp", object_data=obdata)
        object2.matrix_world = object.matrix_world

        context.scene.objects.link(object2)
        mod = object2.modifiers.new("tri_for_uv", "TRIANGULATE")
        mod.use_beauty = False
        newmesh = object2.to_mesh(bpy.context.scene, True, "RENDER", True, False)
        object2.data = newmesh
        context.scene.update()
        """
        oldmesh = object.data
        newmesh = object.data.copy()
        object.data = newmesh
        mod = object.modifiers.new("tri_for_uv","TRIANGULATE")
        mod.use_beauty = False
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)
        """
        psys = mol_psysuvbake
        # print('-------------start------------')
        for par in psys.particles:
            parloc = (par.location * object2.matrix_world) - object2.location
            point = object2.closest_point_on_mesh(parloc)
            # print('closest:',par.location,point[0],point[2])
            vindex1 = object2.data.polygons[point[2]].vertices[0]
            vindex2 = object2.data.polygons[point[2]].vertices[1]
            vindex3 = object2.data.polygons[point[2]].vertices[2]
            v1 = (object2.matrix_world * object2.data.vertices[vindex1].co).to_tuple()
            v2 = (object2.matrix_world * object2.data.vertices[vindex2].co).to_tuple()
            v3 = (object2.matrix_world * object2.data.vertices[vindex3].co).to_tuple()
            uvindex1 = object2.data.polygons[point[2]].loop_start + 0
            uvindex2 = object2.data.polygons[point[2]].loop_start + 1
            uvindex3 = object2.data.polygons[point[2]].loop_start + 2
            uv1 = object2.data.uv_layers.active.data[uvindex1].uv.to_3d()
            uv2 = object2.data.uv_layers.active.data[uvindex2].uv.to_3d()
            uv3 = object2.data.uv_layers.active.data[uvindex3].uv.to_3d()
            # print(vertices1.co,vertices2.co,vertices3.co)
            # print(uv1,uv2,uv3)
            p = object2.matrix_world * point[0]
            v1 = Vector(v1)
            v2 = Vector(v2)
            v3 = Vector(v3)
            uv1 = Vector(uv1)
            uv2 = Vector(uv2)
            uv3 = Vector(uv3)
            # print(a,b,c,uv1,uv2,uv3,p)
            newuv = barycentric(p, v1, v2, v3, uv1, uv2, uv3)
            #print('New UVs:',newuv)
            parloc = par.location * object2.matrix_world
            dist = (Vector((parloc[0] - p[0], parloc[1] - p[1], parloc[2] - p[2]))).length
            newuv[2] = dist
            newuv = newuv.to_tuple()
            par.angular_velocity = newuv
        scene.objects.unlink(object2)
        bpy.data.objects.remove(object2)
        bpy.data.meshes.remove(newmesh)
        print('         uv baked on:', psys.settings.name)

        return {'FINISHED'}
Esempio n. 5
0
    def execute(self, context):
        SurfList = []
        scene = context.scene
        print("Transfert UV start")
        for item in context.object.IsoSurf:
            if item.active == True:
                if item.obj != '':
                    if item.psys != '':
                        SurfList.append([item.obj, item.psys])
        for obj, psys in SurfList:
            if bpy.data.objects[obj].data.uv_layers.active == None:
                print("   ... local UV transfert from:", obj, psys)
                bpy.data.objects[obj].particle_systems[psys].settings[
                    'IsoLocalUV'] = [0.0] * len(
                        bpy.data.objects[obj].particle_systems[psys].particles
                    ) * 3
                bpy.data.objects[obj].particle_systems[
                    psys].particles.foreach_get(
                        "location", bpy.data.objects[obj].
                        particle_systems[psys].settings['IsoLocalUV'])
            else:

                object = bpy.data.objects[obj]
                print('   ...start bake uv from:', obj, psys)

                obdata = object.data.copy()
                object2 = bpy.data.objects.new(name="mol_uv_temp",
                                               object_data=obdata)
                object2.matrix_world = object.matrix_world

                context.scene.objects.link(object2)
                mod = object2.modifiers.new("tri_for_uv", "TRIANGULATE")
                mod.quad_method = "BEAUTY"
                mod.ngon_method = "BEAUTY"
                newmesh = object2.to_mesh(bpy.context.scene, True, "RENDER",
                                          True, False)
                object2.data = newmesh
                context.scene.update()
                """
                oldmesh = object.data
                newmesh = object.data.copy()
                object.data = newmesh
                mod = object.modifiers.new("tri_for_uv","TRIANGULATE")
                mod.use_beauty = False
                bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)
                """
                # print('-------------start------------')
                attr = []
                for par in object.particle_systems[psys].particles:
                    parloc = (par.location *
                              object2.matrix_world) - object2.location
                    point = object2.closest_point_on_mesh(parloc)
                    # print('closest:',par.location,point[0],point[2])
                    vindex1 = object2.data.polygons[point[3]].vertices[0]
                    vindex2 = object2.data.polygons[point[3]].vertices[1]
                    vindex3 = object2.data.polygons[point[3]].vertices[2]
                    v1 = (object2.matrix_world *
                          object2.data.vertices[vindex1].co).to_tuple()
                    v2 = (object2.matrix_world *
                          object2.data.vertices[vindex2].co).to_tuple()
                    v3 = (object2.matrix_world *
                          object2.data.vertices[vindex3].co).to_tuple()
                    uvindex1 = object2.data.polygons[point[3]].loop_start + 0
                    uvindex2 = object2.data.polygons[point[3]].loop_start + 1
                    uvindex3 = object2.data.polygons[point[3]].loop_start + 2
                    uv1 = object2.data.uv_layers.active.data[
                        uvindex1].uv.to_3d()
                    uv2 = object2.data.uv_layers.active.data[
                        uvindex2].uv.to_3d()
                    uv3 = object2.data.uv_layers.active.data[
                        uvindex3].uv.to_3d()
                    # print(vertices1.co,vertices2.co,vertices3.co)
                    # print(uv1,uv2,uv3)
                    p = object2.matrix_world * point[1]
                    v1 = Vector(v1)
                    v2 = Vector(v2)
                    v3 = Vector(v3)
                    uv1 = Vector(uv1)
                    uv2 = Vector(uv2)
                    uv3 = Vector(uv3)
                    # print(a,b,c,uv1,uv2,uv3,p)
                    newuv = barycentric(p, v1, v2, v3, uv1, uv2, uv3)
                    #print('New UVs:',newuv)
                    parloc = par.location * object2.matrix_world
                    dist = (Vector((parloc[0] - p[0], parloc[1] - p[1],
                                    parloc[2] - p[2]))).length
                    newuv[2] = dist
                    newuv = newuv.to_tuple()
                    attr.append(newuv[0])
                    attr.append(newuv[1])
                    attr.append(newuv[2])
                    #par.angular_velocity = newuv
                object.particle_systems[psys].settings['IsoLocalUV'] = attr
                scene.objects.unlink(object2)
                bpy.data.objects.remove(object2)
                bpy.data.meshes.remove(newmesh)

        print("Transfert UV end")
        return {'FINISHED'}
    def execute(self, context):
        scene = context.scene

        #dont use rotation data here from cache, only for render mode
        psys_orig = context.object.particle_systems.active
        psys_orig.settings.use_rotations = False

        obj = get_object(context, context.object)

        scene.mol_objuvbake = obj.name
        scene.mol_psysuvbake = obj.particle_systems.active.name

        if not obj.data.uv_layers.active:
            return {'FINISHED'}

        print('  start bake uv from:', obj.name)

        obdata = obj.data.copy()
        obj2 = bpy.data.objects.new(name="mol_uv_temp", object_data=obdata)
        obj2.matrix_world = obj.matrix_world

        context.scene.collection.objects.link(obj2)
        mod = obj2.modifiers.new("tri_for_uv", "TRIANGULATE")
        mod.ngon_method = 'BEAUTY'
        mod.quad_method = 'BEAUTY'

        ctx = bpy.context.copy()
        ctx["object"] = obj2
        bpy.ops.object.modifier_apply(ctx, modifier=mod.name)

        context.view_layer.update()

        psys = obj.particle_systems[scene.mol_psysuvbake]
        par_uv = []
        me = obj2.data

        for par in psys.particles:

            parloc = (par.location @ obj2.matrix_world) - obj2.location

            point = obj2.closest_point_on_mesh(parloc)
            vindex1 = me.polygons[point[3]].vertices[0]
            vindex2 = me.polygons[point[3]].vertices[1]
            vindex3 = me.polygons[point[3]].vertices[2]

            v1 = (obj2.matrix_world @ me.vertices[vindex1].co).to_tuple()
            v2 = (obj2.matrix_world @ me.vertices[vindex2].co).to_tuple()
            v3 = (obj2.matrix_world @ me.vertices[vindex3].co).to_tuple()

            uvindex1 = me.polygons[point[3]].loop_start + 0
            uvindex2 = me.polygons[point[3]].loop_start + 1
            uvindex3 = me.polygons[point[3]].loop_start + 2
            uv1 = me.uv_layers.active.data[uvindex1].uv.to_3d()
            uv2 = me.uv_layers.active.data[uvindex2].uv.to_3d()
            uv3 = me.uv_layers.active.data[uvindex3].uv.to_3d()

            p = obj2.matrix_world @ point[1]

            v1 = Vector(v1)
            v2 = Vector(v2)
            v3 = Vector(v3)
            uv1 = Vector(uv1)
            uv2 = Vector(uv2)
            uv3 = Vector(uv3)
            newuv = barycentric(p, v1, v2, v3, uv1, uv2, uv3)

            parloc = par.location @ obj2.matrix_world

            dist = (Vector(
                (parloc[0] - p[0], parloc[1] - p[1], parloc[2] - p[2]))).length

            newuv[2] = dist
            newuv = newuv.to_tuple()
            par.angular_velocity = newuv
            par_uv.append(newuv)

        scene.collection.objects.unlink(obj2)
        bpy.data.objects.remove(obj2)
        bpy.data.meshes.remove(obdata)
        print('         uv baked on:', psys.settings.name)
        context.object["par_uv"] = par_uv

        return {'FINISHED'}