示例#1
0
 def __enter__(self):
     if self.seed is not None:
         self.rand_state = rand_get_state()
         self.np_state = np_get_state()
         self.torch_state = torch_get_state()
         self.torch_cudnn_deterministic = torch.backends.cudnn.deterministic
         rand_seed(self.seed)
         np_seed(self.seed)
         torch_seed(self.seed)
         torch.backends.cudnn.deterministic = True
     return self
    def execute(self, context):
        # scene = context.scene
        ivyProps = context.window_manager.ivy_gen_props

        if not self.updateIvy:
            return {'PASS_THROUGH'}

        # assign the variables, check if it is default
        # Note: update the values if window_manager props defaults are changed
        randomSeed = ivyProps.randomSeed if not self.defaultIvy else 0
        maxTime = ivyProps.maxTime if not self.defaultIvy else 0
        maxIvyLength = ivyProps.maxIvyLength if not self.defaultIvy else 1.0
        ivySize = ivyProps.ivySize if not self.defaultIvy else 0.02
        maxFloatLength = ivyProps.maxFloatLength if not self.defaultIvy else 0.5
        maxAdhesionDistance = ivyProps.maxAdhesionDistance if not self.defaultIvy else 1.0
        primaryWeight = ivyProps.primaryWeight if not self.defaultIvy else 0.5
        randomWeight = ivyProps.randomWeight if not self.defaultIvy else 0.2
        gravityWeight = ivyProps.gravityWeight if not self.defaultIvy else 1.0
        adhesionWeight = ivyProps.adhesionWeight if not self.defaultIvy else 0.1
        branchingProbability = ivyProps.branchingProbability if not self.defaultIvy else 0.05
        leafProbability = ivyProps.leafProbability if not self.defaultIvy else 0.35
        ivyBranchSize = ivyProps.ivyBranchSize if not self.defaultIvy else 0.001
        ivyLeafSize = ivyProps.ivyLeafSize if not self.defaultIvy else 0.02
        growLeaves = ivyProps.growLeaves if not self.defaultIvy else True

        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

        # Get the selected object
        ob = context.active_object
        bvhtree = bvhtree_from_object(ob)

        # Check if the mesh has at least one polygon since some functions
        # are expecting them in the object's data (see T51753)
        check_face = check_mesh_faces(ob)
        if check_face is False:
            self.report({'WARNING'},
                        "Mesh Object doesn't have at least one Face. "
                        "Operation Cancelled")
            return {"CANCELLED"}

        # Compute bounding sphere radius
        # radius = computeBoundingSphere(ob)  # Not needed anymore

        # Get the seeding point
        seedPoint = context.scene.cursor.location

        # Fix the random seed
        rand_seed(randomSeed)

        # Make the new ivy
        IVY = Ivy(primaryWeight=primaryWeight,
                  randomWeight=randomWeight,
                  gravityWeight=gravityWeight,
                  adhesionWeight=adhesionWeight,
                  branchingProbability=branchingProbability,
                  leafProbability=leafProbability,
                  ivySize=ivySize,
                  ivyLeafSize=ivyLeafSize,
                  ivyBranchSize=ivyBranchSize,
                  maxFloatLength=maxFloatLength,
                  maxAdhesionDistance=maxAdhesionDistance)
        # Generate first root and node
        IVY.seed(seedPoint)

        checkTime = False
        maxLength = maxIvyLength  # * radius

        # If we need to check time set the flag
        if maxTime != 0.0:
            checkTime = True

        t = time.time()
        startPercent = 0.0
        checkAliveIter = [
            True,
        ]

        # Grow until 200 roots is reached or backup counter exceeds limit
        while (any(checkAliveIter) and (IVY.maxLength < maxLength)
               and (not checkTime or (time.time() - t < maxTime))):
            # Grow the ivy for this iteration
            IVY.grow(ob, bvhtree)

            # Print the proportion of ivy growth to console
            if (IVY.maxLength / maxLength * 100) > 10 * startPercent // 10:
                print('%0.2f%% of Ivy nodes have grown' %
                      (IVY.maxLength / maxLength * 100))
                startPercent += 10
                if IVY.maxLength / maxLength > 1:
                    print("Halting Growth")

            # Make an iterator to check if all are alive
            checkAliveIter = (r.alive for r in IVY.ivyRoots)

        # Create the curve and leaf geometry
        createIvyGeometry(IVY, growLeaves)
        print("Geometry Generation Complete")

        print("Ivy generated in %0.2f s" % (time.time() - t))

        self.updateIvy = False
        self.defaultIvy = False

        return {'FINISHED'}
示例#3
0
    def execute(self, context):
        if not self.updateIvy:
            return {'PASS_THROUGH'}

        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

        # Get the selected object
        ob = context.active_object

        # Compute bounding sphere radius
        #radius = computeBoundingSphere(ob)  # Not needed anymore

        # Get the seeding point
        seedPoint = context.scene.cursor_location

        # Fix the random seed
        rand_seed(self.randomSeed)

        # Make the new ivy
        IVY = Ivy(**self.as_keywords(ignore=('randomSeed', 'growLeaves',
                                  'maxIvyLength', 'maxTime', 'updateIvy')))

        # Generate first root and node
        IVY.seed(seedPoint)

        checkAlive = True
        checkTime = False
        maxLength = self.maxIvyLength  # * radius

        # If we need to check time set the flag
        if self.maxTime != 0.0:
            checkTime = True

        t = time.time()
        startPercent = 0.0
        checkAliveIter = [True, ]

        # Grow until 200 roots is reached or backup counter exceeds limit
        while (any(checkAliveIter) and
               (IVY.maxLength < maxLength) and
               (not checkTime or (time.time() - t < self.maxTime))):
            # Grow the ivy for this iteration
            IVY.grow(ob)

            # Print the proportion of ivy growth to console
            if (IVY.maxLength / maxLength * 100) > 10 * startPercent // 10:
                print('%0.2f%% of Ivy nodes have grown' %
                                         (IVY.maxLength / maxLength * 100))
                startPercent += 10
                if IVY.maxLength / maxLength > 1:
                    print("Halting Growth")

            # Make an iterator to check if all are alive
            checkAliveIter = (r.alive for r in IVY.ivyRoots)

        # Create the curve and leaf geometry
        createIvyGeometry(IVY, self.growLeaves)
        print("Geometry Generation Complete")

        print("Ivy generated in %0.2f s" % (time.time() - t))

        self.updateIvy = False

        return {'FINISHED'}
示例#4
0
    def execute(self, context):
        # scene = context.scene
        ivyProps = context.window_manager.ivy_gen_props

        if not self.updateIvy:
            return {'PASS_THROUGH'}

        # assign the variables, check if it is default
        # Note: update the values if window_manager props defaults are changed
        randomSeed = ivyProps.randomSeed if not self.defaultIvy else 0
        maxTime = ivyProps.maxTime if not self.defaultIvy else 0
        maxIvyLength = ivyProps.maxIvyLength if not self.defaultIvy else 1.0
        ivySize = ivyProps.ivySize if not self.defaultIvy else 0.02
        maxFloatLength = ivyProps.maxFloatLength if not self.defaultIvy else 0.5
        maxAdhesionDistance = ivyProps.maxAdhesionDistance if not self.defaultIvy else 1.0
        primaryWeight = ivyProps.primaryWeight if not self.defaultIvy else 0.5
        randomWeight = ivyProps.randomWeight if not self.defaultIvy else 0.2
        gravityWeight = ivyProps.gravityWeight if not self.defaultIvy else 1.0
        adhesionWeight = ivyProps.adhesionWeight if not self.defaultIvy else 0.1
        branchingProbability = ivyProps.branchingProbability if not self.defaultIvy else 0.05
        leafProbability = ivyProps.leafProbability if not self.defaultIvy else 0.35
        ivyBranchSize = ivyProps.ivyBranchSize if not self.defaultIvy else 0.001
        ivyLeafSize = ivyProps.ivyLeafSize if not self.defaultIvy else 0.02
        growLeaves = ivyProps.growLeaves if not self.defaultIvy else True

        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

        # Get the selected object
        ob = context.active_object

        # Check if the mesh has at least one polygon since some functions
        # are expecting them in the object's data (see T51753)
        check_face = check_mesh_faces(ob)
        if check_face is False:
            self.report({'WARNING'},
                        "Mesh Object doesn't have at least one Face. "
                        "Operation Cancelled")
            return {"CANCELLED"}

        # Compute bounding sphere radius
        # radius = computeBoundingSphere(ob)  # Not needed anymore

        # Get the seeding point
        seedPoint = context.scene.cursor_location

        # Fix the random seed
        rand_seed(randomSeed)

        # Make the new ivy
        IVY = Ivy(
            primaryWeight=primaryWeight,
            randomWeight=randomWeight,
            gravityWeight=gravityWeight,
            adhesionWeight=adhesionWeight,
            branchingProbability=branchingProbability,
            leafProbability=leafProbability,
            ivySize=ivySize,
            ivyLeafSize=ivyLeafSize,
            ivyBranchSize=ivyBranchSize,
            maxFloatLength=maxFloatLength,
            maxAdhesionDistance=maxAdhesionDistance
            )
        # Generate first root and node
        IVY.seed(seedPoint)

        checkTime = False
        maxLength = maxIvyLength  # * radius

        # If we need to check time set the flag
        if maxTime != 0.0:
            checkTime = True

        t = time.time()
        startPercent = 0.0
        checkAliveIter = [True, ]

        # Grow until 200 roots is reached or backup counter exceeds limit
        while (any(checkAliveIter) and
               (IVY.maxLength < maxLength) and
               (not checkTime or (time.time() - t < maxTime))):
            # Grow the ivy for this iteration
            IVY.grow(ob)

            # Print the proportion of ivy growth to console
            if (IVY.maxLength / maxLength * 100) > 10 * startPercent // 10:
                print('%0.2f%% of Ivy nodes have grown' %
                                         (IVY.maxLength / maxLength * 100))
                startPercent += 10
                if IVY.maxLength / maxLength > 1:
                    print("Halting Growth")

            # Make an iterator to check if all are alive
            checkAliveIter = (r.alive for r in IVY.ivyRoots)

        # Create the curve and leaf geometry
        createIvyGeometry(IVY, growLeaves)
        print("Geometry Generation Complete")

        print("Ivy generated in %0.2f s" % (time.time() - t))

        self.updateIvy = False
        self.defaultIvy = False

        return {'FINISHED'}
    def execute(self, context):
        if not self.updateIvy:
            return {'PASS_THROUGH'}

        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

        # Get the selected object
        ob = context.active_object

        # Compute bounding sphere radius
        #radius = computeBoundingSphere(ob)  # Not needed anymore

        # Get the seeding point
        seedPoint = context.scene.cursor_location

        # Fix the random seed
        rand_seed(self.randomSeed)

        # Make the new ivy
        IVY = Ivy(**self.as_keywords(ignore=('randomSeed', 'growLeaves',
                                             'maxIvyLength', 'maxTime',
                                             'updateIvy')))

        # Generate first root and node
        IVY.seed(seedPoint)

        checkTime = False
        maxLength = self.maxIvyLength  # * radius

        # If we need to check time set the flag
        if self.maxTime != 0.0:
            checkTime = True

        t = time.time()
        startPercent = 0.0
        checkAliveIter = [
            True,
        ]

        # Grow until 200 roots is reached or backup counter exceeds limit
        while (any(checkAliveIter) and (IVY.maxLength < maxLength)
               and (not checkTime or (time.time() - t < self.maxTime))):
            # Grow the ivy for this iteration
            IVY.grow(ob)

            # Print the proportion of ivy growth to console
            if (IVY.maxLength / maxLength * 100) > 10 * startPercent // 10:
                print('%0.2f%% of Ivy nodes have grown' %
                      (IVY.maxLength / maxLength * 100))
                startPercent += 10
                if IVY.maxLength / maxLength > 1:
                    print("Halting Growth")

            # Make an iterator to check if all are alive
            checkAliveIter = (r.alive for r in IVY.ivyRoots)

        # Create the curve and leaf geometry
        createIvyGeometry(IVY, self.growLeaves)
        print("Geometry Generation Complete")

        print("Ivy generated in %0.2f s" % (time.time() - t))

        self.updateIvy = False

        return {'FINISHED'}
示例#6
0
def set_seed(seed):
	rand_seed(seed)
	np.random.seed(seed)
	tf.random.set_seed(seed)
示例#7
0
# Prepare for rand number generator, a test of what random numbers are we getting
from random import randrange
from random import seed as rand_seed

# Seed
rand_seed(99)
for i in range(100):
    # 0 - 4, inclusive, with ticks at 0.01 (same as the slider outcome)
    rand_num = randrange(0, 400, 1) / 100

    print(rand_num)
示例#8
0
def _combine(identifiers,
             images,
             transform=None,
             min_resize=None,
             max_resize=None,
             random_seed=None,
             give_noise=False):
    if random_seed:
        rand_seed(random_seed)

    if transform:
        if min_resize and max_resize:

            def resize_maintain_aspect_ratio(image):
                width, height = image.size
                aspect_ratio = width / height
                has_looped_for = 0
                while has_looped_for < 1000:
                    new_width, new_height = randint(min_resize,
                                                    max_resize), randint(
                                                        min_resize, max_resize)
                    if abs((new_width / new_height) - aspect_ratio) < .2:
                        break
                    has_looped_for += 1
                if has_looped_for == 1000:
                    random_scale = random() + randint(0, 3)
                    random_scale = random_scale if random_scale > .25 else .25
                    new_width, new_height = int(width * random_scale), int(
                        height * random_scale)

                return Resize((new_height, new_width))(image)

            images = map(resize_maintain_aspect_ratio, images)
        images = [transform(image) for image in images]

    identifiers_images = list(zip(identifiers, images))
    backup_identifiers_images = list(zip(identifiers, images))

    combined_image_size = _get_combined_image_size(images)
    if give_noise == 'varying':
        combined_image = _varying_background_noise(combined_image_size, images)
    elif type(give_noise) == tuple:
        background_noise, background_sources = give_noise
        combined_image = _varying_background_noise(
            combined_image_size, images,
            ImageFolder(root=str(background_noise), loader=Image.open),
            ImageFolder(root=str(background_sources), loader=Image.open))
    elif os.path.exists(give_noise) and os.path.isdir(give_noise):
        combined_image = _varying_background_noise(
            combined_image_size, images,
            ImageFolder(root=str(give_noise), loader=Image.open))
    elif give_noise:
        combined_image = Image.fromarray(
            np.random.randint(0,
                              256, (*combined_image_size, 3),
                              dtype=np.uint8))
    else:
        combined_image = Image.new('RGBA', combined_image_size)
    coordinates = {k: None for k, _ in identifiers_images}

    total_loop = 0
    while len(identifiers_images) != 0:
        # Total loop approaching 1000 means current combined image cannot fit all the images; a 'restart' is required.
        # This is to prevent infinite loop.
        if total_loop == 1000:
            identifiers_images = backup_identifiers_images
            combined_image_size = _get_combined_image_size(images)
            if give_noise:
                combined_image = Image.fromarray(
                    np.random.randint(0,
                                      256, (*combined_image_size, 3),
                                      dtype=np.uint8))
            else:
                combined_image = Image.new('RGBA', combined_image_size)
            coordinates = {k: None for k, _ in identifiers_images}
            total_loop = 0

        for i in identifiers_images:
            identifier, image = i
            image_coordinate = _get_image_coordinate(image,
                                                     combined_image_size)

            if _not_overlapping(coordinates, image_coordinate):
                combined_image.paste(image.convert('RGBA'), image_coordinate,
                                     image.convert('RGBA'))
                coordinates[identifier] = image_coordinate
                identifiers_images.remove(i)
        total_loop += 1

    return combined_image.convert('RGB'), coordinates
示例#9
0
def _combine(identifiers,
             images,
             transform=None,
             min_resize=None,
             max_resize=None,
             random_seed=None):
    if random_seed:
        rand_seed(random_seed)

    if transform:
        if min_resize and max_resize:

            def resize_maintain_aspect_ratio(image):
                width, height = image.size
                aspect_ratio = width / height
                has_looped_for = 0
                while has_looped_for < 1000:
                    new_width, new_height = randint(min_resize,
                                                    max_resize), randint(
                                                        min_resize, max_resize)
                    if abs((new_width / new_height) - aspect_ratio) < .2:
                        break
                    has_looped_for += 1
                if has_looped_for == 1000:
                    random_scale = random() + randint(0, 3)
                    random_scale = random_scale if random_scale > .25 else .25
                    new_width, new_height = int(width * random_scale), int(
                        height * random_scale)

                return Resize((new_height, new_width))(image)

            images = map(resize_maintain_aspect_ratio, images)
        images = [transform(image) for image in images]

    identifiers_images = list(zip(identifiers, images))
    backup_identifiers_images = list(zip(identifiers, images))

    combined_image_size = _get_combined_image_size(images)
    combined_image = Image.new('RGBA', combined_image_size)
    coordinates = {k: None for k, _ in identifiers_images}

    total_loop = 0
    while len(identifiers_images) != 0:
        # Total loop approaching 1000 means current combined image cannot fit all the images; a 'restart' is required.
        # This is to prevent infinite loop.
        if total_loop == 1000:
            identifiers_images = backup_identifiers_images
            combined_image_size = _get_combined_image_size(images)
            combined_image = Image.new('RGBA', combined_image_size)
            coordinates = {k: None for k, _ in identifiers_images}
            total_loop = 0

        for i in identifiers_images:
            identifier, image = i
            image_coordinate = _get_image_coordinate(image,
                                                     combined_image_size)

            if _not_overlapping(coordinates, image_coordinate):
                combined_image.paste(image.convert('RGBA'), image_coordinate,
                                     image.convert('RGBA'))
                coordinates[identifier] = image_coordinate
                identifiers_images.remove(i)
        total_loop += 1

    return combined_image.convert('RGB'), coordinates